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

Last change on this file since 643 was 643, checked in by plg, 19 years ago
  • refresh paginated navigation bar : displays First and Last, displays Previous and Next even if non applicable (but no link), displays only page umbers around the current page (if a category contains hundreds of elements, no more long list of pages)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 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-2004 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-12-13 22:54:44 +0000 (Mon, 13 Dec 2004) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 643 $
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 $user, $conf, $lang;
31
32  if (!preg_match('/\d{4}-\d{2}-\d{2}/', $date))
33  {
34    return '';
35  }
36
37  list( $year,$month,$day ) = explode( '-', $date );
38  $unixtime = mktime( 0, 0, 0, $month, $day, $year );
39 
40  $diff = time() - $unixtime;
41  $day_in_seconds = 24*60*60;
42  $output = '';
43  $title = $lang['recent_image'].'&nbsp;';
44  if ( $diff < $user['recent_period'] * $day_in_seconds )
45  {
46    $icon_url = './template/'.$user['template'].'/theme/';
47    $icon_url.= 'recent.gif';
48    $title .= $user['recent_period'];
49    $title .=  '&nbsp;'.$lang['days'];
50    $size = getimagesize( $icon_url );
51    $output = '<img title="'.$title.'" src="'.$icon_url.'" style="border:0;';
52    $output.= 'height:'.$size[1].'px;width:'.$size[0].'px" alt="" />';
53  }
54  return $output;
55}
56
57function create_navigation_bar($url, $nb_element, $start,
58                               $nb_element_page, $link_class)
59{
60  global $lang, $conf;
61
62  $pages_around = $conf['paginate_pages_around'];
63 
64  $navigation_bar = '';
65 
66  // current page detection
67  if (!isset($start)
68      or !is_numeric($start)
69      or (is_numeric($start) and $start < 0))
70  {
71    $start = 0;
72  }
73 
74  // navigation bar useful only if more than one page to display !
75  if ($nb_element > $nb_element_page)
76  {
77    // searching the current page
78    $cur_page = ceil($start / $nb_element_page) + 1;
79    $maximum = ceil($nb_element / $nb_element_page);
80
81    // link to first page ?
82    if ($cur_page != 1)
83    {
84      $navigation_bar.= '<a href="';
85      $navigation_bar.= add_session_id($url.'&amp;start=0');
86      $navigation_bar.= '" class="'.$link_class.'">'.$lang['first_page'];
87      $navigation_bar.= '</a>';
88    }
89    else
90    {
91      $navigation_bar.= $lang['first_page'];
92    }
93    $navigation_bar.= ' | ';
94    // link on previous page ?
95    if ( $start != 0 )
96    {
97      $previous = $start - $nb_element_page;
98      $navigation_bar.= '<a href="';
99      $navigation_bar.= add_session_id( $url.'&amp;start='.$previous );
100      $navigation_bar.= '" class="'.$link_class.'">'.$lang['previous_page'];
101      $navigation_bar.= '</a>';
102    }
103    else
104    {
105      $navigation_bar.= $lang['previous_page'];
106    }
107    $navigation_bar.= ' | ';
108
109    if ($cur_page > $pages_around + 1)
110    {
111      $navigation_bar.= '&nbsp;<a href="';
112      $navigation_bar.= add_session_id($url.'&amp;start=0');
113      $navigation_bar.= '" class="'.$link_class.'">1</a>&nbsp;...';
114    }
115   
116    // inspired from punbb source code
117    for ($i = $cur_page - $pages_around, $stop = $cur_page + $pages_around + 1;
118         $i < $stop;
119         $i++)
120    {
121      if ($i < 1 or $i > $maximum)
122      {
123        continue;
124      }
125      else if ($i != $cur_page)
126      {
127        $temp_start = ($i - 1) * $nb_element_page;
128        $navigation_bar.= '&nbsp;<a href="';
129        $navigation_bar.= add_session_id($url.'&amp;start='.$temp_start);
130        $navigation_bar.= '" class="'.$link_class.'">'.$i.'</a>';
131      }
132      else
133      {
134        $navigation_bar.= '&nbsp;<span class="pageNumberSelected">';
135        $navigation_bar.= $i.'</span>';
136      }
137    }
138
139    if ($cur_page < ($maximum - $pages_around))
140    {
141      $temp_start = ($maximum - 1) * $nb_element_page;
142      $navigation_bar.= '&nbsp;...&nbsp;<a href="';
143      $navigation_bar.= add_session_id($url.'&amp;start='.$temp_start);
144      $navigation_bar.= '" class="'.$link_class.'">'.$maximum.'</a>';
145    }
146   
147    $navigation_bar.= ' | ';
148    // link on next page ?
149    if ( $nb_element > $nb_element_page
150         && $start + $nb_element_page < $nb_element )
151    {
152      $next = $start + $nb_element_page;
153      $navigation_bar.= '<a href="';
154      $navigation_bar.= add_session_id( $url.'&amp;start='.$next );
155      $navigation_bar.= '" class="'.$link_class.'">'.$lang['next_page'].'</a>';
156    }
157    else
158    {
159      $navigation_bar.= $lang['next_page'];
160    }
161    // link to last page ?
162    if ($cur_page != $maximum)
163    {
164      $temp_start = ($maximum - 1) * $nb_element_page;
165      $navigation_bar.= ' | ';
166      $navigation_bar.= '<a href="';
167      $navigation_bar.= add_session_id($url.'&amp;start='.$temp_start);
168      $navigation_bar.= '" class="'.$link_class.'">'.$lang['last_page'];
169      $navigation_bar.= '</a>';
170    }
171    else
172    {
173      $navigation_bar.= $lang['last_page'];
174    }
175  }
176  return $navigation_bar;
177}
178
179//
180// Pick a language, any language ...
181//
182function language_select($default, $select_name = "language")
183{
184  $available_lang = get_languages();
185
186  $lang_select = '<select name="' . $select_name . '" onchange="this.form.submit()">';
187  foreach ($available_lang as $code => $displayname)
188  {
189    $selected = ( strtolower($default) == strtolower($code) ) ? ' selected="selected"' : '';
190    $lang_select .= '<option value="' . $code . '"' . $selected . '>' . ucwords($displayname) . '</option>';
191  }
192  $lang_select .= '</select>';
193
194  return $lang_select;
195}
196
197//
198// Pick a template/theme combo,
199//
200function style_select($default_style, $select_name = "style")
201{
202  $templates = get_templates();
203
204  $style_selected = '<select name="' . $select_name . '" >';
205  foreach ($templates as $template)
206  {
207    $selected = ( $template == $default_style ) ? ' selected="selected"' : '';
208    $style_selected.= '<option value="'.$template.'"'.$selected.'>';
209    $style_selected.= $template.'</option>';
210  }
211  $style_selected .= '</select>';
212  return $style_selected;
213}
214
215/**
216 * returns the list of categories as a HTML string
217 *
218 * categories string returned contains categories as given in the input
219 * array $cat_informations. $cat_informations array must be an association
220 * of {category_id => category_name}. If url input parameter is empty,
221 * returns only the categories name without links.
222 *
223 * @param array cat_informations
224 * @param string separator
225 * @param string url
226 * @param boolean replace_space
227 * @return string
228 */
229function get_cat_display_name($cat_informations,
230                              $url = 'category.php?cat=',
231                              $replace_space = true)
232{
233  global $conf;
234 
235  $output = '';
236  $is_first = true;
237  foreach ($cat_informations as $id => $name)
238  {
239    if ($is_first)
240    {
241      $is_first = false;
242    }
243    else
244    {
245      $output.= $conf['level_separator'];
246    }
247
248    if ($url == '')
249    {
250      $output.= $name;
251    }
252    else
253    {
254      $output.= '
255<a class="" href="'.add_session_id(PHPWG_ROOT_PATH.$url.$id).'">'.$name.'</a>';
256    }
257  }
258  if ($replace_space)
259  {
260    return replace_space($output);
261  }
262  else
263  {
264    return $output;
265  }
266}
267
268/**
269 * returns the list of categories as a HTML string, with cache of names
270 *
271 * categories string returned contains categories as given in the input
272 * array $cat_informations. $uppercats is the list of category ids to
273 * display in the right order. If url input parameter is empty, returns only
274 * the categories name without links.
275 *
276 * @param string uppercats
277 * @param string url
278 * @param boolean replace_space
279 * @return string
280 */
281function get_cat_display_name_cache($uppercats,
282                                    $url = 'category.php?cat=',
283                                    $replace_space = true)
284{
285  global $cat_names, $conf;
286
287  if (!isset($cat_names))
288  {
289    $query = '
290SELECT id,name
291  FROM '.CATEGORIES_TABLE.'
292;';
293    $result = pwg_query($query);
294    while ($row = mysql_fetch_array($result))
295    {
296      $cat_names[$row['id']] = $row['name'];
297    }
298  }
299 
300  $output = '';
301  $is_first = true;
302  foreach (explode(',', $uppercats) as $category_id)
303  {
304    $name = $cat_names[$category_id];
305   
306    if ($is_first)
307    {
308      $is_first = false;
309    }
310    else
311    {
312      $output.= $conf['level_separator'];
313    }
314
315    if ($url == '')
316    {
317      $output.= $name;
318    }
319    else
320    {
321      $output.= '
322<a class=""
323   href="'.add_session_id(PHPWG_ROOT_PATH.$url.$category_id).'">'.$name.'</a>';
324    }
325  }
326  if ($replace_space)
327  {
328    return replace_space($output);
329  }
330  else
331  {
332    return $output;
333  }
334}
335
336/**
337 * returns the HTML code for a category item in the menu (for category.php)
338 *
339 * HTML code generated uses logical list tags ul and each category is an
340 * item li. The paramter given is the category informations as an array,
341 * used keys are : id, name, nb_images, date_last
342 *
343 * @param array categories
344 * @return string
345 */
346function get_html_menu_category($categories)
347{
348  global $page, $lang;
349
350  $ref_level = 0;
351  $menu = '
352             <ul class="menu">';
353 
354  foreach ($categories as $category)
355  {
356    $level = substr_count($category['global_rank'], '.');
357    if ($level > $ref_level)
358    {
359      $menu.= '
360             <ul class="menu">';
361    }
362    else if ($level < $ref_level)
363    {
364      $menu.= '
365             </ul>';
366    }
367    $ref_level = $level;
368   
369    $menu.= '
370
371           <li>';
372 
373    $url = add_session_id(PHPWG_ROOT_PATH.'category.php?cat='.$category['id']);
374
375    $class = '';
376    if (isset($page['cat'])
377        and is_numeric($page['cat'])
378        and $category['id'] == $page['cat'])
379    {
380      $class = 'menuCategorySelected';
381    }
382    else
383    {
384      $class = 'menuCategoryNotSelected';
385    }
386
387    $menu.= '
388           <a href="'.$url.'"
389              title="'.$lang['hint_category'].'"
390              class="'.$class.'">
391             '.$category['name'].'
392           </a>';
393
394    if ($category['nb_images'] > 0)
395    {
396      $menu.= '
397             <span class="menuInfoCat"
398                   title="'.$category['nb_images'].'
399                          '.$lang['images_available'].'">
400             ['.$category['nb_images'].']
401             </span>
402             '.get_icon($category['date_last']);
403    }
404
405    $menu.= '</li>';
406  }
407 
408  $menu.= '
409             </ul>';
410 
411  return $menu;
412}
413
414/**
415 * returns HTMLized comment contents retrieved from database
416 *
417 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
418 * italic, *word* becomes bolded
419 *
420 * @param string content
421 * @return string
422 */
423function parse_comment_content($content)
424{
425  $content = nl2br($content);
426 
427  // replace _word_ by an underlined word
428  $pattern = '/_([^\s]*)_/';
429  $replacement = '<span style="text-decoration:underline;">\1</span>';
430  $content = preg_replace($pattern, $replacement, $content);
431 
432  // replace *word* by a bolded word
433  $pattern = '/\*([^\s]*)\*/';
434  $replacement = '<span style="font-weight:bold;">\1</span>';
435  $content = preg_replace($pattern, $replacement, $content);
436 
437  // replace /word/ by an italic word
438  $pattern = '/\/([^\s]*)\//';
439  $replacement = '<span style="font-style:italic;">\1</span>';
440  $content = preg_replace($pattern, $replacement, $content);
441
442  return $content;
443}
444?>
Note: See TracBrowser for help on using the repository browser.