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

Last change on this file since 960 was 960, checked in by chrisaga, 18 years ago

improve template : split theme from template itself

rest of the job : template (yoga), themes (clear dark), and php to handle them

  • 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-03 17:33:38 +0000 (Sat, 03 Dec 2005) $
10// | last modifier : $Author: chrisaga $
11// | revision      : $Revision: 960 $
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  $menu = '';
342 
343  foreach ($categories as $category)
344  {
345    $level = substr_count($category['global_rank'], '.') + 1;
346    if ($level > $ref_level)
347    {
348      $menu.= "\n<ul>";
349    }
350    else if ($level == $ref_level)
351    {
352      $menu.= "\n</li>";
353    }
354    else if ($level < $ref_level)
355    {
356      // we may have to close more than one level at the same time...
357      $menu.= "\n</li>";
358      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
359    }
360    $ref_level = $level;
361
362    $menu.= "\n\n".'<li';
363    if (isset($page['cat'])
364        and is_numeric($page['cat'])
365        and $category['id'] == $page['cat'])
366    {
367      $menu.= ' class="selected"';
368    }
369    $menu.= '>';
370 
371    $url = add_session_id(PHPWG_ROOT_PATH.'category.php?cat='.$category['id']);
372    $menu.= "\n".'<a href="'.$url.'">'.$category['name'].'</a>';
373
374    if ($category['nb_images'] > 0)
375    {
376      $menu.= "\n".'<span class="menuInfoCat"';
377      $menu.= ' title="'.$category['nb_images'];
378      $menu.= ' '.$lang['images_available'].'">';
379      $menu.= '['.$category['nb_images'].']';
380      $menu.= '</span>';
381      $menu.= get_icon($category['date_last']);
382    }
383  }
384
385  $menu.= str_repeat("\n</li></ul>",($level));
386 
387  return $menu;
388}
389
390/**
391 * returns HTMLized comment contents retrieved from database
392 *
393 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
394 * italic, *word* becomes bolded
395 *
396 * @param string content
397 * @return string
398 */
399function parse_comment_content($content)
400{
401  $content = nl2br($content);
402 
403  // replace _word_ by an underlined word
404  $pattern = '/_([^\s]*)_/';
405  $replacement = '<span style="text-decoration:underline;">\1</span>';
406  $content = preg_replace($pattern, $replacement, $content);
407 
408  // replace *word* by a bolded word
409  $pattern = '/\*([^\s]*)\*/';
410  $replacement = '<span style="font-weight:bold;">\1</span>';
411  $content = preg_replace($pattern, $replacement, $content);
412 
413  // replace /word/ by an italic word
414  $pattern = '/\/([^\s]*)\//';
415  $replacement = '<span style="font-style:italic;">\1</span>';
416  $content = preg_replace($pattern, $replacement, $content);
417
418  return $content;
419}
420
421function get_cat_display_name_from_id($cat_id,
422                                      $url = 'category.php?cat=',
423                                      $replace_space = true)
424{
425  $cat_info = get_cat_info($cat_id);
426  return get_cat_display_name($cat_info['name'], $url, $replace_space);
427}
428?>
Note: See TracBrowser for help on using the repository browser.