source: trunk/include/functions_html.inc.php @ 1084

Last change on this file since 1084 was 1084, checked in by plg, 18 years ago

bug fixed: create_navigation_bar take into account clean URL if requested.

deletion: argument link_class (HTML class of links) in function
create_navigation_bar was removed, useless since branch 1.5.

bug fixed: rate_items are now a configuration parameter (set in config file)

modification: new functions library functions_rate.inc.php to reduce
picture.php length.

bug fixed: categories were never expanded in the menu since clean URLs.

bug fixed: changing pictures sorting order in main page was always
rederecting to root category.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-03-16 22:34:45 +0000 (Thu, 16 Mar 2006) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 1084 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28function get_icon($date)
29{
30  global $page, $user, $conf, $lang;
31
32  if (empty($date))
33  {
34    $date = 'NULL';
35  }
36
37  if (isset($page['get_icon_cache'][$date]))
38  {
39    return $page['get_icon_cache'][$date];
40  }
41
42  if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $date, $matches))
43  {
44    // date can be empty, no icon to display
45    $page['get_icon_cache'][$date] = '';
46    return $page['get_icon_cache'][$date];
47  }
48 
49  list($devnull, $year, $month, $day) = $matches;
50  $unixtime = mktime( 0, 0, 0, $month, $day, $year );
51
52  if ($unixtime === false  // PHP 5.1.0 and above
53      or $unixtime === -1) // PHP prior to 5.1.0
54  {
55    $page['get_icon_cache'][$date] = '';
56    return $page['get_icon_cache'][$date];
57  }
58 
59  $diff = time() - $unixtime;
60  $day_in_seconds = 24*60*60;
61  $output = '';
62  $title = $lang['recent_image'].'&nbsp;';
63  if ( $diff < $user['recent_period'] * $day_in_seconds )
64  {
65    $icon_url = get_themeconf('icon_dir').'/recent.png';
66    $title .= $user['recent_period'];
67    $title .=  '&nbsp;'.$lang['days'];
68    $size = getimagesize( $icon_url );
69    $output = '<img title="'.$title.'" src="'.$icon_url.'" class="icon" style="border:0;';
70    $output.= 'height:'.$size[1].'px;width:'.$size[0].'px" alt="(!)" />';
71  }
72
73  $page['get_icon_cache'][$date] = $output;
74
75  return $page['get_icon_cache'][$date];
76}
77
78function create_navigation_bar(
79  $url, $nb_element, $start, $nb_element_page, $clean_url = false
80  )
81{
82  global $lang, $conf;
83
84  $pages_around = $conf['paginate_pages_around'];
85  $start_str = $clean_url ? '/start-' : '&amp;start=';
86 
87  $navbar = '';
88 
89  // current page detection
90  if (!isset($start)
91      or !is_numeric($start)
92      or (is_numeric($start) and $start < 0))
93  {
94    $start = 0;
95  }
96 
97  // navigation bar useful only if more than one page to display !
98  if ($nb_element > $nb_element_page)
99  {
100    // current page and last page
101    $cur_page = ceil($start / $nb_element_page) + 1;
102    $maximum = ceil($nb_element / $nb_element_page);
103
104    // link to first page ?
105    if ($cur_page != 1)
106    {
107      $navbar.=
108        '<a href="'.$url.'" rel="start">'
109        .$lang['first_page']
110        .'</a>';
111    }
112    else
113    {
114      $navbar.= $lang['first_page'];
115    }
116    $navbar.= ' | ';
117    // link on previous page ?
118    if ($start != 0)
119    {
120      $previous = $start - $nb_element_page;
121     
122      $navbar.=
123        '<a href="'
124        .$url.($previous > 0 ? $start_str.$previous : '')
125        .'" rel="prev">'
126        .$lang['previous_page']
127        .'</a>';
128    }
129    else
130    {
131      $navbar.= $lang['previous_page'];
132    }
133    $navbar.= ' |';
134
135    if ($cur_page > $pages_around + 1)
136    {
137      $navbar.= '&nbsp;<a href="'.$url.'">1</a>';
138     
139      if ($cur_page > $pages_around + 2)
140      {
141        $navbar.= ' ...';
142      }
143    }
144   
145    // inspired from punbb source code
146    for ($i = $cur_page - $pages_around, $stop = $cur_page + $pages_around + 1;
147         $i < $stop;
148         $i++)
149    {
150      if ($i < 1 or $i > $maximum)
151      {
152        continue;
153      }
154      else if ($i != $cur_page)
155      {
156        $temp_start = ($i - 1) * $nb_element_page;
157       
158        $navbar.=
159          '&nbsp;'
160          .'<a href="'.$url
161          .($temp_start > 0 ? $start_str.$temp_start : '')
162          .'">'
163          .$i
164          .'</a>';
165      }
166      else
167      {
168        $navbar.=
169          '&nbsp;'
170          .'<span class="pageNumberSelected">'
171          .$i
172          .'</span>';
173      }
174    }
175
176    if ($cur_page < ($maximum - $pages_around))
177    {
178      $temp_start = ($maximum - 1) * $nb_element_page;
179     
180      if ($cur_page < ($maximum - $pages_around - 1))
181      {
182        $navbar.= ' ...';
183      }
184     
185      $navbar.= ' <a href="'.$url.$start_str.$temp_start.'">'.$maximum.'</a>';
186    }
187   
188    $navbar.= ' | ';
189    // link on next page ?
190    if ($nb_element > $nb_element_page
191        and $start + $nb_element_page < $nb_element)
192    {
193      $next = $start + $nb_element_page;
194     
195      $navbar.=
196        '<a href="'.$url.$start_str.$next.'" rel="next">'
197        .$lang['next_page']
198        .'</a>';
199    }
200    else
201    {
202      $navbar.= $lang['next_page'];
203    }
204   
205    $navbar.= ' | ';
206    // link to last page ?
207    if ($cur_page != $maximum)
208    {
209      $temp_start = ($maximum - 1) * $nb_element_page;
210     
211      $navbar.=
212        '<a href="'.$url.$start_str.$temp_start.'" rel="last">'
213        .$lang['last_page']
214        .'</a>';
215    }
216    else
217    {
218      $navbar.= $lang['last_page'];
219    }
220  }
221  return $navbar;
222}
223
224//
225// Pick a language, any language ...
226//
227function language_select($default, $select_name = "language")
228{
229  $available_lang = get_languages();
230
231  $lang_select = '<select name="' . $select_name . '">';
232  foreach ($available_lang as $code => $displayname)
233  {
234    $selected = ( strtolower($default) == strtolower($code) ) ? ' selected="selected"' : '';
235    $lang_select .= '<option value="' . $code . '"' . $selected . '>' . ucwords($displayname) . '</option>';
236  }
237  $lang_select .= '</select>';
238
239  return $lang_select;
240}
241
242/**
243 * returns the list of categories as a HTML string
244 *
245 * categories string returned contains categories as given in the input
246 * array $cat_informations. $cat_informations array must be an association
247 * of {category_id => category_name}. If url input parameter is empty,
248 * returns only the categories name without links.
249 *
250 * @param array cat_informations
251 * @param string url
252 * @param boolean replace_space
253 * @return string
254 */
255function get_cat_display_name($cat_informations,
256                              $url = 'category.php?/category/',
257                              $replace_space = true)
258{
259  global $conf;
260 
261  $output = '';
262  $is_first = true;
263  foreach ($cat_informations as $id => $name)
264  {
265    if ($is_first)
266    {
267      $is_first = false;
268    }
269    else
270    {
271      $output.= $conf['level_separator'];
272    }
273
274    if ($url == '')
275    {
276      $output.= $name;
277    }
278    else
279    {
280      $output.= '<a class=""';
281      $output.= ' href="'.PHPWG_ROOT_PATH.$url.$id.'">';
282      $output.= $name.'</a>';
283    }
284  }
285  if ($replace_space)
286  {
287    return replace_space($output);
288  }
289  else
290  {
291    return $output;
292  }
293}
294
295/**
296 * returns the list of categories as a HTML string, with cache of names
297 *
298 * categories string returned contains categories as given in the input
299 * array $cat_informations. $uppercats is the list of category ids to
300 * display in the right order. If url input parameter is empty, returns only
301 * the categories name without links.
302 *
303 * @param string uppercats
304 * @param string url
305 * @param boolean replace_space
306 * @return string
307 */
308function get_cat_display_name_cache($uppercats,
309                                    $url = 'category.php?/category/',
310                                    $replace_space = true)
311{
312  global $cat_names, $conf;
313
314  if (!isset($cat_names))
315  {
316    $query = '
317SELECT id,name
318  FROM '.CATEGORIES_TABLE.'
319;';
320    $result = pwg_query($query);
321    while ($row = mysql_fetch_array($result))
322    {
323      $cat_names[$row['id']] = $row['name'];
324    }
325  }
326 
327  $output = '';
328  $is_first = true;
329  foreach (explode(',', $uppercats) as $category_id)
330  {
331    $name = $cat_names[$category_id];
332   
333    if ($is_first)
334    {
335      $is_first = false;
336    }
337    else
338    {
339      $output.= $conf['level_separator'];
340    }
341
342    if ($url == '')
343    {
344      $output.= $name;
345    }
346    else
347    {
348      $output.= '
349<a class=""
350   href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$name.'</a>';
351    }
352  }
353  if ($replace_space)
354  {
355    return replace_space($output);
356  }
357  else
358  {
359    return $output;
360  }
361}
362
363/**
364 * returns the HTML code for a category item in the menu (for the main page)
365 *
366 * HTML code generated uses logical list tags ul and each category is an
367 * item li. The paramter given is the category informations as an array,
368 * used keys are : id, name, nb_images, date_last
369 *
370 * @param array categories
371 * @return string
372 */
373function get_html_menu_category($categories)
374{
375  global $page, $lang;
376
377  $ref_level = 0;
378  $level = 0;
379  $menu = '';
380
381  // $page_cat value remains 0 for special sections
382  $page_cat = 0;
383  if (isset($page['category']))
384  {
385    $page_cat = $page['category'];
386  }
387 
388  foreach ($categories as $category)
389  {
390    $level = substr_count($category['global_rank'], '.') + 1;
391    if ($level > $ref_level)
392    {
393      $menu.= "\n<ul>";
394    }
395    else if ($level == $ref_level)
396    {
397      $menu.= "\n</li>";
398    }
399    else if ($level < $ref_level)
400    {
401      // we may have to close more than one level at the same time...
402      $menu.= "\n</li>";
403      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
404    }
405    $ref_level = $level;
406
407    $menu.= "\n\n".'<li';
408    if ($category['id'] == $page_cat)
409    {
410      $menu.= ' class="selected"';
411    }
412    $menu.= '>';
413 
414    $url = make_index_url(array('category' => $category['id']));
415   
416    $menu.= "\n".'<a href="'.$url.'"';
417    if ($page_cat != 0
418        and $category['id'] == $page['cat_id_uppercat'])
419    {
420      $menu.= ' rel="up"';
421    }
422    $menu.= '>'.$category['name'].'</a>';
423
424    if ($category['nb_images'] > 0)
425    {
426      $menu.= "\n".'<span class="menuInfoCat"';
427      $menu.= ' title="'.$category['nb_images'];
428      $menu.= ' '.$lang['images_available'].'">';
429      $menu.= '['.$category['nb_images'].']';
430      $menu.= '</span>';
431      $menu.= get_icon($category['date_last']);
432    }
433  }
434
435  $menu.= str_repeat("\n</li></ul>",($level));
436 
437  return $menu;
438}
439
440/**
441 * returns HTMLized comment contents retrieved from database
442 *
443 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
444 * italic, *word* becomes bolded
445 *
446 * @param string content
447 * @return string
448 */
449function parse_comment_content($content)
450{
451  $pattern = '/(https?:\/\/\S*)/';
452  $replacement = '<a href="$1" rel="nofollow">$1</a>';
453  $content = preg_replace($pattern, $replacement, $content);
454
455  $content = nl2br($content);
456 
457  // replace _word_ by an underlined word
458  $pattern = '/\b_(\S*)_\b/';
459  $replacement = '<span style="text-decoration:underline;">$1</span>';
460  $content = preg_replace($pattern, $replacement, $content);
461 
462  // replace *word* by a bolded word
463  $pattern = '/\b\*(\S*)\*\b/';
464  $replacement = '<span style="font-weight:bold;">$1</span>';
465  $content = preg_replace($pattern, $replacement, $content);
466 
467  // replace /word/ by an italic word
468  $pattern = "/\/(\S*)\/(\s)/";
469  $replacement = '<span style="font-style:italic;">$1$2</span>';
470  $content = preg_replace($pattern, $replacement, $content);
471
472  $content = '<div>'.$content.'</div>';
473  return $content;
474}
475
476function get_cat_display_name_from_id($cat_id,
477                                      $url = 'category.php?/category/',
478                                      $replace_space = true)
479{
480  $cat_info = get_cat_info($cat_id);
481  return get_cat_display_name($cat_info['name'], $url, $replace_space);
482}
483?>
Note: See TracBrowser for help on using the repository browser.