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

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

merge -r1038:1039 from branches/branch-1_5 into trunk (bug 269 fixed)

improvement: use a cache on get_icon function since it's often used with the
same argument on a page generation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 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-02-13 22:15:56 +0000 (Mon, 13 Feb 2006) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 1040 $
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($url, $nb_element, $start,
79                               $nb_element_page, $link_class)
80{
81  global $lang, $conf;
82
83  $pages_around = $conf['paginate_pages_around'];
84 
85  $navbar = '';
86 
87  // current page detection
88  if (!isset($start)
89      or !is_numeric($start)
90      or (is_numeric($start) and $start < 0))
91  {
92    $start = 0;
93  }
94 
95  // navigation bar useful only if more than one page to display !
96  if ($nb_element > $nb_element_page)
97  {
98    // current page and last page
99    $cur_page = ceil($start / $nb_element_page) + 1;
100    $maximum = ceil($nb_element / $nb_element_page);
101
102    // link to first page ?
103    if ($cur_page != 1)
104    {
105      $navbar.= '<a href="';
106      $navbar.= $url;
107      $navbar.= '" class="'.$link_class.'" rel="start">'.$lang['first_page'];
108      $navbar.= '</a>';
109    }
110    else
111    {
112      $navbar.= $lang['first_page'];
113    }
114    $navbar.= ' | ';
115    // link on previous page ?
116    if ( $start != 0 )
117    {
118      $previous = $start - $nb_element_page;
119      $navbar.= '<a href="';
120      $navbar.= $url;
121      if ($previous>0)
122      {
123        $navbar.= '&amp;start='.$previous;
124      }
125      $navbar.= '" class="'.$link_class.'" rel="prev">'.$lang['previous_page'];
126      $navbar.= '</a>';
127    }
128    else
129    {
130      $navbar.= $lang['previous_page'];
131    }
132    $navbar.= ' |';
133
134    if ($cur_page > $pages_around + 1)
135    {
136      $navbar.= '&nbsp;<a href="';
137      $navbar.= $url;
138      $navbar.= '" class="'.$link_class.'">1</a>';
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        $navbar.= '&nbsp;<a href="';
158        $navbar.= $url;
159        if ($temp_start>0)
160        {
161          $navbar.= '&amp;start='.$temp_start;
162        }
163        $navbar.= '" class="'.$link_class.'"';
164        $navbar.='>'.$i.'</a>';
165      }
166      else
167      {
168        $navbar.= '&nbsp;<span class="pageNumberSelected">';
169        $navbar.= $i.'</span>';
170      }
171    }
172
173    if ($cur_page < ($maximum - $pages_around))
174    {
175      $temp_start = ($maximum - 1) * $nb_element_page;
176      if ($cur_page < ($maximum - $pages_around - 1))
177      {
178        $navbar.= ' ...';
179      }
180      $navbar.= ' <a href="';
181      $navbar.= $url.'&amp;start='.$temp_start;
182      $navbar.= '" class="'.$link_class.'">'.$maximum.'</a>';
183    }
184   
185    $navbar.= ' | ';
186    // link on next page ?
187    if ( $nb_element > $nb_element_page
188         && $start + $nb_element_page < $nb_element )
189    {
190      $next = $start + $nb_element_page;
191      $navbar.= '<a href="';
192      $navbar.= $url.'&amp;start='.$next;
193      $navbar.= '" class="'.$link_class.'" rel="next">'.$lang['next_page'].'</a>';
194    }
195    else
196    {
197      $navbar.= $lang['next_page'];
198    }
199   
200    $navbar.= ' | ';
201    // link to last page ?
202    if ($cur_page != $maximum)
203    {
204      $temp_start = ($maximum - 1) * $nb_element_page;
205      $navbar.= '<a href="';
206      $navbar.= $url.'&amp;start='.$temp_start;
207      $navbar.= '" class="'.$link_class.'" rel="last">'.$lang['last_page'];
208      $navbar.= '</a>';
209    }
210    else
211    {
212      $navbar.= $lang['last_page'];
213    }
214  }
215  return $navbar;
216}
217
218//
219// Pick a language, any language ...
220//
221function language_select($default, $select_name = "language")
222{
223  $available_lang = get_languages();
224
225  $lang_select = '<select name="' . $select_name . '">';
226  foreach ($available_lang as $code => $displayname)
227  {
228    $selected = ( strtolower($default) == strtolower($code) ) ? ' selected="selected"' : '';
229    $lang_select .= '<option value="' . $code . '"' . $selected . '>' . ucwords($displayname) . '</option>';
230  }
231  $lang_select .= '</select>';
232
233  return $lang_select;
234}
235
236/**
237 * returns the list of categories as a HTML string
238 *
239 * categories string returned contains categories as given in the input
240 * array $cat_informations. $cat_informations array must be an association
241 * of {category_id => category_name}. If url input parameter is empty,
242 * returns only the categories name without links.
243 *
244 * @param array cat_informations
245 * @param string url
246 * @param boolean replace_space
247 * @return string
248 */
249function get_cat_display_name($cat_informations,
250                              $url = 'category.php?cat=',
251                              $replace_space = true)
252{
253  global $conf;
254 
255  $output = '';
256  $is_first = true;
257  foreach ($cat_informations as $id => $name)
258  {
259    if ($is_first)
260    {
261      $is_first = false;
262    }
263    else
264    {
265      $output.= $conf['level_separator'];
266    }
267
268    if ($url == '')
269    {
270      $output.= $name;
271    }
272    else
273    {
274      $output.= '<a class=""';
275      $output.= ' href="'.PHPWG_ROOT_PATH.$url.$id.'">';
276      $output.= $name.'</a>';
277    }
278  }
279  if ($replace_space)
280  {
281    return replace_space($output);
282  }
283  else
284  {
285    return $output;
286  }
287}
288
289/**
290 * returns the list of categories as a HTML string, with cache of names
291 *
292 * categories string returned contains categories as given in the input
293 * array $cat_informations. $uppercats is the list of category ids to
294 * display in the right order. If url input parameter is empty, returns only
295 * the categories name without links.
296 *
297 * @param string uppercats
298 * @param string url
299 * @param boolean replace_space
300 * @return string
301 */
302function get_cat_display_name_cache($uppercats,
303                                    $url = 'category.php?cat=',
304                                    $replace_space = true)
305{
306  global $cat_names, $conf;
307
308  if (!isset($cat_names))
309  {
310    $query = '
311SELECT id,name
312  FROM '.CATEGORIES_TABLE.'
313;';
314    $result = pwg_query($query);
315    while ($row = mysql_fetch_array($result))
316    {
317      $cat_names[$row['id']] = $row['name'];
318    }
319  }
320 
321  $output = '';
322  $is_first = true;
323  foreach (explode(',', $uppercats) as $category_id)
324  {
325    $name = $cat_names[$category_id];
326   
327    if ($is_first)
328    {
329      $is_first = false;
330    }
331    else
332    {
333      $output.= $conf['level_separator'];
334    }
335
336    if ($url == '')
337    {
338      $output.= $name;
339    }
340    else
341    {
342      $output.= '
343<a class=""
344   href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$name.'</a>';
345    }
346  }
347  if ($replace_space)
348  {
349    return replace_space($output);
350  }
351  else
352  {
353    return $output;
354  }
355}
356
357/**
358 * returns the HTML code for a category item in the menu (for category.php)
359 *
360 * HTML code generated uses logical list tags ul and each category is an
361 * item li. The paramter given is the category informations as an array,
362 * used keys are : id, name, nb_images, date_last
363 *
364 * @param array categories
365 * @return string
366 */
367function get_html_menu_category($categories)
368{
369  global $page, $lang;
370
371  $ref_level = 0;
372  $level = 0;
373  $menu = '';
374
375  // $page_cat value remains 0 for special sections
376  $page_cat = 0;
377  if (isset($page['cat']) and is_numeric($page['cat']) )
378  {
379    $page_cat = $page['cat'];
380  }
381  foreach ($categories as $category)
382  {
383    $level = substr_count($category['global_rank'], '.') + 1;
384    if ($level > $ref_level)
385    {
386      $menu.= "\n<ul>";
387    }
388    else if ($level == $ref_level)
389    {
390      $menu.= "\n</li>";
391    }
392    else if ($level < $ref_level)
393    {
394      // we may have to close more than one level at the same time...
395      $menu.= "\n</li>";
396      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
397    }
398    $ref_level = $level;
399
400    $menu.= "\n\n".'<li';
401    if ($category['id'] == $page_cat)
402    {
403      $menu.= ' class="selected"';
404    }
405    $menu.= '>';
406 
407    $url = PHPWG_ROOT_PATH.'category.php?cat='.$category['id'];
408    $menu.= "\n".'<a href="'.$url.'"';
409    if ($page_cat != 0
410        and $category['id'] == $page['cat_id_uppercat'])
411    {
412      $menu.= ' rel="up"';
413    }
414    $menu.= '>'.$category['name'].'</a>';
415
416    if ($category['nb_images'] > 0)
417    {
418      $menu.= "\n".'<span class="menuInfoCat"';
419      $menu.= ' title="'.$category['nb_images'];
420      $menu.= ' '.$lang['images_available'].'">';
421      $menu.= '['.$category['nb_images'].']';
422      $menu.= '</span>';
423      $menu.= get_icon($category['date_last']);
424    }
425  }
426
427  $menu.= str_repeat("\n</li></ul>",($level));
428 
429  return $menu;
430}
431
432/**
433 * returns HTMLized comment contents retrieved from database
434 *
435 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
436 * italic, *word* becomes bolded
437 *
438 * @param string content
439 * @return string
440 */
441function parse_comment_content($content)
442{
443  $pattern = '/(https?:\/\/\S*)/';
444  $replacement = '<a href="$1" rel="nofollow">$1</a>';
445  $content = preg_replace($pattern, $replacement, $content);
446
447  $content = nl2br($content);
448 
449  // replace _word_ by an underlined word
450  $pattern = '/\b_(\S*)_\b/';
451  $replacement = '<span style="text-decoration:underline;">$1</span>';
452  $content = preg_replace($pattern, $replacement, $content);
453 
454  // replace *word* by a bolded word
455  $pattern = '/\b\*(\S*)\*\b/';
456  $replacement = '<span style="font-weight:bold;">$1</span>';
457  $content = preg_replace($pattern, $replacement, $content);
458 
459  // replace /word/ by an italic word
460  $pattern = "/\/(\S*)\/(\s)/";
461  $replacement = '<span style="font-style:italic;">$1$2</span>';
462  $content = preg_replace($pattern, $replacement, $content);
463
464  $content = '<div>'.$content.'</div>';
465  return $content;
466}
467
468function get_cat_display_name_from_id($cat_id,
469                                      $url = 'category.php?cat=',
470                                      $replace_space = true)
471{
472  $cat_info = get_cat_info($cat_id);
473  return get_cat_display_name($cat_info['name'], $url, $replace_space);
474}
475?>
Note: See TracBrowser for help on using the repository browser.