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

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