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

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

improvement: $pagewhere string replaced by $pageitems.
$pagewhere was an SQL clause used to retrieve pictures in #images
table. $pageitems is the list of picture ids of the current section.

improvement: function initialize_category replaced by dedicated included PHP
script include/section_init.inc.php. Code was refactored to improve
readibility and maintenability. $pagenavigation_bar is now build in
category.php instead of initialize_category function. Function check_cat_id
was also replaced by a piece of code in the new file. The file to include to
display thumbnails from category.php is now set in section_init.inc.php
instead of calculated in category.php.

bug fix: the test for rel="up" link for standard HTML navigation links in
category menu was not working with non numeric categories, such as
"favorites".

improvement: function check_login_authorization removed because useless but
in profile.php.

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