includes/functions.php
changeset 875 0c3dd4c166c0
parent 857 f3a5a276208c
child 898 c75754f5b1da
equal deleted inserted replaced
874:d67f43d93f30 875:0c3dd4c166c0
  2193   }
  2193   }
  2194   return true;
  2194   return true;
  2195 }
  2195 }
  2196 
  2196 
  2197 /**
  2197 /**
       
  2198  * Generates the HTML of a pagination control.
       
  2199  * @param int Current page
       
  2200  * @param int Number of pages
       
  2201  * @param string sprintf()-style formatting URL for pages
       
  2202  * @param int Multiplier for start offset, defaults to 1
       
  2203  * @param int Add to each $i for addition to result urls, usually either 0 or 1 (depends on whether you want your ?page= to start with 0 ro 1)
       
  2204  * @return string HTML
       
  2205  */
       
  2206 
       
  2207 function generate_paginator($current_page, $num_pages, $result_url, $start_mult = 1, $start_add = 1)
       
  2208 {
       
  2209   global $db, $session, $paths, $template, $plugins; // Common objects
       
  2210   global $lang;
       
  2211   
       
  2212   $out = '';
       
  2213   $i = 0;
       
  2214 
       
  2215   // Build paginator
       
  2216   $pg_css = ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) ?
       
  2217             // IE-specific hack
       
  2218             'display: block; width: 1px;':
       
  2219             // Other browsers
       
  2220             'display: table; margin: 10px 0 0 auto;';
       
  2221   
       
  2222   $begin = '<div class="tblholder" style="'. $pg_css . '">
       
  2223     <table border="0" cellspacing="1" cellpadding="4">
       
  2224       <tr><th>' . $lang->get('paginate_lbl_page') . '</th>';
       
  2225   $block = '<td class="row1" style="text-align: center;">{LINK}</td>';
       
  2226   $end = '</tr></table></div>';
       
  2227   $blk = $template->makeParserText($block);
       
  2228   $inner = '';
       
  2229   $cls = 'row2';
       
  2230   if ( $num_pages < 5 )
       
  2231   {
       
  2232     for ( $i = 0; $i < $num_pages; $i++ )
       
  2233     {
       
  2234       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2235       $offset = strval(($i * $start_mult) + $start_add);
       
  2236       $url = htmlspecialchars(sprintf($result_url, $offset));
       
  2237       $j = $i + 1;
       
  2238       $link = ( $i == $current_page ) ? "<b>$j</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>$j</a>";
       
  2239       $blk->assign_vars(array(
       
  2240         'CLASS'=>$cls,
       
  2241         'LINK'=>$link
       
  2242         ));
       
  2243       $inner .= $blk->run();
       
  2244     }
       
  2245   }
       
  2246   else
       
  2247   {
       
  2248     if ( $current_page + 5 > $num_pages )
       
  2249     {
       
  2250       $list = Array();
       
  2251       $tp = $current_page;
       
  2252       if ( $current_page + 0 == $num_pages ) $tp = $tp - 3;
       
  2253       if ( $current_page + 1 == $num_pages ) $tp = $tp - 2;
       
  2254       if ( $current_page + 2 == $num_pages ) $tp = $tp - 1;
       
  2255       for ( $i = $tp - 1; $i <= $tp + 1; $i++ )
       
  2256       {
       
  2257         $list[] = $i;
       
  2258       }
       
  2259     }
       
  2260     else
       
  2261     {
       
  2262       $list = Array();
       
  2263       $current = $current_page;
       
  2264       $lower = ( $current < 3 ) ? 1 : $current - 1;
       
  2265       for ( $i = 0; $i < 3; $i++ )
       
  2266       {
       
  2267         $list[] = $lower + $i;
       
  2268       }
       
  2269     }
       
  2270     $url = sprintf($result_url, '0');
       
  2271     $link = ( 0 == $current_page ) ? "<b>" . $lang->get('paginate_btn_first') . "</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>&laquo; " . $lang->get('paginate_btn_first') . "</a>";
       
  2272     $blk->assign_vars(array(
       
  2273       'CLASS'=>$cls,
       
  2274       'LINK'=>$link
       
  2275       ));
       
  2276     $inner .= $blk->run();
       
  2277 
       
  2278     foreach ( $list as $i )
       
  2279     {
       
  2280       if ( $i == $num_pages )
       
  2281         break;
       
  2282       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2283       $offset = strval(($i * $start_mult) + $start_add);
       
  2284       $url = sprintf($result_url, $offset);
       
  2285       $j = $i + 1;
       
  2286       $link = ( $i == $current_page ) ? "<b>$j</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>$j</a>";
       
  2287       $blk->assign_vars(array(
       
  2288         'CLASS'=>$cls,
       
  2289         'LINK'=>$link
       
  2290         ));
       
  2291       $inner .= $blk->run();
       
  2292     }
       
  2293 
       
  2294     // "Last" button
       
  2295     $total = (($num_pages - 1) * $start_mult) + $start_add;
       
  2296 
       
  2297     if ( $current_page < $num_pages )
       
  2298     {
       
  2299       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2300       $offset = strval($total);
       
  2301       $url = sprintf($result_url, $offset);
       
  2302       $link = ( $num_pages - 1 == $current_page ) ? "<b>" . $lang->get('paginate_btn_last') . "</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>" . $lang->get('paginate_btn_last') . " &raquo;</a>";
       
  2303       $blk->assign_vars(array(
       
  2304         'CLASS'=>$cls,
       
  2305         'LINK'=>$link
       
  2306         ));
       
  2307       $inner .= $blk->run();
       
  2308     }
       
  2309 
       
  2310   }
       
  2311 
       
  2312   $inner .= '<td class="row2" style="cursor: pointer;" onclick="paginator_goto(this, '.$current_page.', '.$num_pages.', '.$start_mult.', unescape(\'' . rawurlencode($result_url) . '\'));">&darr;</td>';
       
  2313 
       
  2314   $paginator = "\n$begin$inner$end\n";
       
  2315   return $paginator;
       
  2316 }
       
  2317 
       
  2318 /**
  2198  * Paginates (breaks into multiple pages) a MySQL result resource, which is treated as unbuffered.
  2319  * Paginates (breaks into multiple pages) a MySQL result resource, which is treated as unbuffered.
  2199  * @param resource The MySQL result resource. This should preferably be an unbuffered query.
  2320  * @param resource The MySQL result resource. This should preferably be an unbuffered query.
  2200  * @param string A template, with variables being named after the column name
  2321  * @param string A template, with variables being named after the column name
  2201  * @param int The number of total results. This should be determined by a second query.
  2322  * @param int The number of total results. This should be determined by a second query.
  2202  * @param string sprintf-style formatting string for URLs for result pages. First parameter will be start offset.
  2323  * @param string sprintf-style formatting string for URLs for result pages. First parameter will be start offset.
  2209  */
  2330  */
  2210 
  2331 
  2211 function paginate($q, $tpl_text, $num_results, $result_url, $start = 0, $perpage = 10, $callers = Array(), $header = '', $footer = '')
  2332 function paginate($q, $tpl_text, $num_results, $result_url, $start = 0, $perpage = 10, $callers = Array(), $header = '', $footer = '')
  2212 {
  2333 {
  2213   global $db, $session, $paths, $template, $plugins; // Common objects
  2334   global $db, $session, $paths, $template, $plugins; // Common objects
  2214   global $lang;
       
  2215   
  2335   
  2216   $parser = $template->makeParserText($tpl_text);
  2336   $parser = $template->makeParserText($tpl_text);
       
  2337   
  2217   $num_pages = ceil ( $num_results / $perpage );
  2338   $num_pages = ceil ( $num_results / $perpage );
  2218   $out = '';
  2339   $out = '';
       
  2340   $this_page = ceil ( $start / $perpage );
  2219   $i = 0;
  2341   $i = 0;
  2220   $this_page = ceil ( $start / $perpage );
  2342   
  2221 
  2343   $paginator = generate_paginator($this_page, $num_pages, $result_url, $perpage, 0);
  2222   // Build paginator
       
  2223   $pg_css = ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) ?
       
  2224             // IE-specific hack
       
  2225             'display: block; width: 1px;':
       
  2226             // Other browsers
       
  2227             'display: table; margin: 10px 0 0 auto;';
       
  2228   $begin = '<div class="tblholder" style="'. $pg_css . '">
       
  2229     <table border="0" cellspacing="1" cellpadding="4">
       
  2230       <tr><th>' . $lang->get('paginate_lbl_page') . '</th>';
       
  2231   $block = '<td class="row1" style="text-align: center;">{LINK}</td>';
       
  2232   $end = '</tr></table></div>';
       
  2233   $blk = $template->makeParserText($block);
       
  2234   $inner = '';
       
  2235   $cls = 'row2';
       
  2236   if ( $num_pages < 5 )
       
  2237   {
       
  2238     for ( $i = 0; $i < $num_pages; $i++ )
       
  2239     {
       
  2240       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2241       $offset = strval($i * $perpage);
       
  2242       $url = htmlspecialchars(sprintf($result_url, $offset));
       
  2243       $j = $i + 1;
       
  2244       $link = ( $offset == strval($start) ) ? "<b>$j</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>$j</a>";
       
  2245       $blk->assign_vars(array(
       
  2246         'CLASS'=>$cls,
       
  2247         'LINK'=>$link
       
  2248         ));
       
  2249       $inner .= $blk->run();
       
  2250     }
       
  2251   }
       
  2252   else
       
  2253   {
       
  2254     if ( $this_page + 5 > $num_pages )
       
  2255     {
       
  2256       $list = Array();
       
  2257       $tp = $this_page;
       
  2258       if ( $this_page + 0 == $num_pages ) $tp = $tp - 3;
       
  2259       if ( $this_page + 1 == $num_pages ) $tp = $tp - 2;
       
  2260       if ( $this_page + 2 == $num_pages ) $tp = $tp - 1;
       
  2261       for ( $i = $tp - 1; $i <= $tp + 1; $i++ )
       
  2262       {
       
  2263         $list[] = $i;
       
  2264       }
       
  2265     }
       
  2266     else
       
  2267     {
       
  2268       $list = Array();
       
  2269       $current = $this_page;
       
  2270       $lower = ( $current < 3 ) ? 1 : $current - 1;
       
  2271       for ( $i = 0; $i < 3; $i++ )
       
  2272       {
       
  2273         $list[] = $lower + $i;
       
  2274       }
       
  2275     }
       
  2276     $url = sprintf($result_url, '0');
       
  2277     $link = ( 0 == $start ) ? "<b>" . $lang->get('paginate_btn_first') . "</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>&laquo; " . $lang->get('paginate_btn_first') . "</a>";
       
  2278     $blk->assign_vars(array(
       
  2279       'CLASS'=>$cls,
       
  2280       'LINK'=>$link
       
  2281       ));
       
  2282     $inner .= $blk->run();
       
  2283 
       
  2284     // if ( !in_array(1, $list) )
       
  2285     // {
       
  2286     //   $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2287     //   $blk->assign_vars(array('CLASS'=>$cls,'LINK'=>'...'));
       
  2288     //   $inner .= $blk->run();
       
  2289     // }
       
  2290 
       
  2291     foreach ( $list as $i )
       
  2292     {
       
  2293       if ( $i == $num_pages )
       
  2294         break;
       
  2295       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2296       $offset = strval($i * $perpage);
       
  2297       $url = sprintf($result_url, $offset);
       
  2298       $j = $i + 1;
       
  2299       $link = ( $offset == strval($start) ) ? "<b>$j</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>$j</a>";
       
  2300       $blk->assign_vars(array(
       
  2301         'CLASS'=>$cls,
       
  2302         'LINK'=>$link
       
  2303         ));
       
  2304       $inner .= $blk->run();
       
  2305     }
       
  2306 
       
  2307     $total = $num_pages * $perpage - $perpage;
       
  2308 
       
  2309     if ( $this_page < $num_pages )
       
  2310     {
       
  2311       // $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2312       // $blk->assign_vars(array('CLASS'=>$cls,'LINK'=>'...'));
       
  2313       // $inner .= $blk->run();
       
  2314 
       
  2315       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2316       $offset = strval($total);
       
  2317       $url = sprintf($result_url, $offset);
       
  2318       $j = $i + 1;
       
  2319       $link = ( $offset == strval($start) ) ? "<b>" . $lang->get('paginate_btn_last') . "</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>" . $lang->get('paginate_btn_last') . " &raquo;</a>";
       
  2320       $blk->assign_vars(array(
       
  2321         'CLASS'=>$cls,
       
  2322         'LINK'=>$link
       
  2323         ));
       
  2324       $inner .= $blk->run();
       
  2325     }
       
  2326 
       
  2327   }
       
  2328 
       
  2329   $inner .= '<td class="row2" style="cursor: pointer;" onclick="paginator_goto(this, '.$this_page.', '.$num_pages.', '.$perpage.', unescape(\'' . rawurlencode($result_url) . '\'));">&darr;</td>';
       
  2330 
       
  2331   $paginator = "\n$begin$inner$end\n";
       
  2332   $out .= $paginator;
  2344   $out .= $paginator;
  2333 
  2345 
  2334   $cls = 'row2';
  2346   $cls = 'row2';
  2335 
  2347 
  2336   if ( $row = $db->fetchrow($q) )
  2348   if ( $row = $db->fetchrow($q) )
  2392   $num_pages = ceil ( $num_results / $perpage );
  2404   $num_pages = ceil ( $num_results / $perpage );
  2393   $out = '';
  2405   $out = '';
  2394   $i = 0;
  2406   $i = 0;
  2395   $this_page = ceil ( $start / $perpage );
  2407   $this_page = ceil ( $start / $perpage );
  2396 
  2408 
  2397   // Build paginator
  2409   $paginator = generate_paginator($this_page, $num_pages, $result_url, $perpage, 0);
  2398   $begin = '<div class="tblholder" style="display: table; margin: 10px 0 0 auto;">
  2410   $out .= $paginator;
  2399     <table border="0" cellspacing="1" cellpadding="4">
  2411   
  2400       <tr><th>' . $lang->get('paginate_lbl_page') . '</th>';
       
  2401   $block = '<td class="row1" style="text-align: center;">{LINK}</td>';
       
  2402   $end = '</tr></table></div>';
       
  2403   $blk = $template->makeParserText($block);
       
  2404   $inner = '';
       
  2405   $cls = 'row2';
       
  2406   $total = $num_pages * $perpage - $perpage;
       
  2407   /*
       
  2408   if ( $start > 0 )
       
  2409   {
       
  2410     $url = sprintf($result_url, abs($start - $perpage));
       
  2411     $link = "<a href=".'"'."$url".'"'." style='text-decoration: none;'>&laquo; " . $lang->get('paginate_btn_prev') . "</a>";
       
  2412     $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2413     $blk->assign_vars(array(
       
  2414       'CLASS'=>$cls,
       
  2415       'LINK'=>$link
       
  2416       ));
       
  2417     $inner .= $blk->run();
       
  2418   }
       
  2419   */
       
  2420   if ( $num_pages < 5 )
       
  2421   {
       
  2422     for ( $i = 0; $i < $num_pages; $i++ )
       
  2423     {
       
  2424       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2425       $offset = strval($i * $perpage);
       
  2426       $url = htmlspecialchars(sprintf($result_url, $offset));
       
  2427       $j = $i + 1;
       
  2428       $link = ( $offset == strval($start) ) ? "<b>$j</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>$j</a>";
       
  2429       $blk->assign_vars(array(
       
  2430         'CLASS'=>$cls,
       
  2431         'LINK'=>$link
       
  2432         ));
       
  2433       $inner .= $blk->run();
       
  2434     }
       
  2435   }
       
  2436   else
       
  2437   {
       
  2438     if ( $this_page + 5 > $num_pages )
       
  2439     {
       
  2440       $list = Array();
       
  2441       $tp = $this_page;
       
  2442       if ( $this_page + 0 == $num_pages ) $tp = $tp - 3;
       
  2443       if ( $this_page + 1 == $num_pages ) $tp = $tp - 2;
       
  2444       if ( $this_page + 2 == $num_pages ) $tp = $tp - 1;
       
  2445       for ( $i = $tp - 1; $i <= $tp + 1; $i++ )
       
  2446       {
       
  2447         $list[] = $i;
       
  2448       }
       
  2449     }
       
  2450     else
       
  2451     {
       
  2452       $list = Array();
       
  2453       $current = $this_page;
       
  2454       $lower = ( $current < 3 ) ? 1 : $current - 1;
       
  2455       for ( $i = 0; $i < 3; $i++ )
       
  2456       {
       
  2457         $list[] = $lower + $i;
       
  2458       }
       
  2459     }
       
  2460     $url = sprintf($result_url, '0');
       
  2461     $link = ( 0 == $start ) ? "<b>" . $lang->get('paginate_btn_first') . "</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>&laquo; " . $lang->get('paginate_btn_first') . "</a>";
       
  2462     $blk->assign_vars(array(
       
  2463       'CLASS'=>$cls,
       
  2464       'LINK'=>$link
       
  2465       ));
       
  2466     $inner .= $blk->run();
       
  2467 
       
  2468     // if ( !in_array(1, $list) )
       
  2469     // {
       
  2470     //   $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2471     //   $blk->assign_vars(array('CLASS'=>$cls,'LINK'=>'...'));
       
  2472     //   $inner .= $blk->run();
       
  2473     // }
       
  2474 
       
  2475     foreach ( $list as $i )
       
  2476     {
       
  2477       if ( $i == $num_pages )
       
  2478         break;
       
  2479       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2480       $offset = strval($i * $perpage);
       
  2481       $url = sprintf($result_url, $offset);
       
  2482       $j = $i + 1;
       
  2483       $link = ( $offset == strval($start) ) ? "<b>$j</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>$j</a>";
       
  2484       $blk->assign_vars(array(
       
  2485         'CLASS'=>$cls,
       
  2486         'LINK'=>$link
       
  2487         ));
       
  2488       $inner .= $blk->run();
       
  2489     }
       
  2490 
       
  2491     if ( $this_page < $num_pages )
       
  2492     {
       
  2493       // $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2494       // $blk->assign_vars(array('CLASS'=>$cls,'LINK'=>'...'));
       
  2495       // $inner .= $blk->run();
       
  2496 
       
  2497       $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2498       $offset = strval($total);
       
  2499       $url = sprintf($result_url, $offset);
       
  2500       $j = $i + 1;
       
  2501       $link = ( $offset == strval($start) ) ? "<b>" . $lang->get('paginate_btn_last') . "</b>" : "<a href=".'"'."$url".'"'." style='text-decoration: none;'>" . $lang->get('paginate_btn_last') . " &raquo;</a>";
       
  2502       $blk->assign_vars(array(
       
  2503         'CLASS'=>$cls,
       
  2504         'LINK'=>$link
       
  2505         ));
       
  2506       $inner .= $blk->run();
       
  2507     }
       
  2508 
       
  2509   }
       
  2510 
       
  2511   /*
       
  2512   if ( $start < $total )
       
  2513   {
       
  2514     $link_offset = abs($start + $perpage);
       
  2515     $url = htmlspecialchars(sprintf($result_url, strval($link_offset)));
       
  2516     $link = "<a href=".'"'."$url".'"'." style='text-decoration: none;'>" . $lang->get('paginate_btn_next') . " &raquo;</a>";
       
  2517     $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
       
  2518     $blk->assign_vars(array(
       
  2519       'CLASS'=>$cls,
       
  2520       'LINK'=>$link
       
  2521       ));
       
  2522     $inner .= $blk->run();
       
  2523   }
       
  2524   */
       
  2525 
       
  2526   $inner .= '<td class="row2" style="cursor: pointer;" onclick="paginator_goto(this, '.$this_page.', '.$num_pages.', '.$perpage.', unescape(\'' . rawurlencode($result_url) . '\'));">&darr;</td>';
       
  2527 
       
  2528   $paginator = "\n$begin$inner$end\n";
       
  2529   if ( $total > 1 )
  2412   if ( $total > 1 )
  2530   {
  2413   {
  2531     $out .= $paginator;
  2414     $out .= $paginator;
  2532   }
  2415   }
  2533 
  2416