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

Last change on this file since 1624 was 1624, checked in by rub, 17 years ago

Resolved Issue ID 0000299:

o Add (new) icon of parent category with children categories including new images
o Improved display text for images count
o Improved (a little) mass_* functions

More explications on the forum.
You must call directly upgrade_feep.php (http://127.0.0.1/BSF/upgrade_feed.php for example)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.8 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// | last update   : $Date: 2006-12-01 23:31:19 +0000 (Fri, 01 Dec 2006) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 1624 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27function get_icon($date, $is_child_date = false)
28{
29  global $page, $user, $conf, $lang;
30
31  if (empty($date))
32  {
33    $date = 'NULL';
34  }
35
36  if (isset($page['get_icon_cache'][$is_child_date][$date]))
37  {
38    return $page['get_icon_cache'][$is_child_date][$date];
39  }
40
41  if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $date, $matches))
42  {
43    // date can be empty, no icon to display
44    $page['get_icon_cache'][$is_child_date][$date] = '';
45    return $page['get_icon_cache'][$is_child_date][$date];
46  }
47
48  list($devnull, $year, $month, $day) = $matches;
49  $unixtime = mktime( 0, 0, 0, $month, $day, $year );
50
51  if ($unixtime === false  // PHP 5.1.0 and above
52      or $unixtime === -1) // PHP prior to 5.1.0
53  {
54    $page['get_icon_cache'][$is_child_date][$date] = '';
55    return $page['get_icon_cache'][$is_child_date][$date];
56  }
57
58  $diff = time() - $unixtime;
59  $day_in_seconds = 24*60*60;
60  $output = '';
61  $title = $lang['recent_image'].'&nbsp;';
62  if ( $diff < $user['recent_period'] * $day_in_seconds )
63  {
64    $icon_url = get_themeconf('icon_dir').'/'.($is_child_date ? 'recent_by_child.png' : 'recent.png');
65    $title .= $user['recent_period'];
66    $title .=  '&nbsp;'.$lang['days'];
67    $size = getimagesize( PHPWG_ROOT_PATH.$icon_url );
68    $icon_url = get_root_url().$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'][$is_child_date][$date] = $output;
74
75  return $page['get_icon_cache'][$is_child_date][$date];
76}
77
78function create_navigation_bar(
79  $url, $nb_element, $start, $nb_element_page, $clean_url = false
80  )
81{
82  global $lang, $conf;
83
84  $pages_around = $conf['paginate_pages_around'];
85  $start_str = $clean_url ? '/start-' :
86    ( ( strstr($url, '?')===false ? '?':'&amp;') . 'start=' );
87
88  $navbar = '';
89
90  // current page detection
91  if (!isset($start)
92      or !is_numeric($start)
93      or (is_numeric($start) and $start < 0))
94  {
95    $start = 0;
96  }
97
98  // navigation bar useful only if more than one page to display !
99  if ($nb_element > $nb_element_page)
100  {
101    // current page and last page
102    $cur_page = ceil($start / $nb_element_page) + 1;
103    $maximum = ceil($nb_element / $nb_element_page);
104
105    // link to first page ?
106    if ($cur_page != 1)
107    {
108      $navbar.=
109        '<a href="'.$url.'" rel="start">'
110        .$lang['first_page']
111        .'</a>';
112    }
113    else
114    {
115      $navbar.= $lang['first_page'];
116    }
117    $navbar.= ' | ';
118    // link on previous page ?
119    if ($start != 0)
120    {
121      $previous = $start - $nb_element_page;
122
123      $navbar.=
124        '<a href="'
125        .$url.($previous > 0 ? $start_str.$previous : '')
126        .'" rel="prev">'
127        .$lang['previous_page']
128        .'</a>';
129    }
130    else
131    {
132      $navbar.= $lang['previous_page'];
133    }
134    $navbar.= ' |';
135
136    if ($cur_page > $pages_around + 1)
137    {
138      $navbar.= '&nbsp;<a href="'.$url.'">1</a>';
139
140      if ($cur_page > $pages_around + 2)
141      {
142        $navbar.= ' ...';
143      }
144    }
145
146    // inspired from punbb source code
147    for ($i = $cur_page - $pages_around, $stop = $cur_page + $pages_around + 1;
148         $i < $stop;
149         $i++)
150    {
151      if ($i < 1 or $i > $maximum)
152      {
153        continue;
154      }
155      else if ($i != $cur_page)
156      {
157        $temp_start = ($i - 1) * $nb_element_page;
158
159        $navbar.=
160          '&nbsp;'
161          .'<a href="'.$url
162          .($temp_start > 0 ? $start_str.$temp_start : '')
163          .'">'
164          .$i
165          .'</a>';
166      }
167      else
168      {
169        $navbar.=
170          '&nbsp;'
171          .'<span class="pageNumberSelected">'
172          .$i
173          .'</span>';
174      }
175    }
176
177    if ($cur_page < ($maximum - $pages_around))
178    {
179      $temp_start = ($maximum - 1) * $nb_element_page;
180
181      if ($cur_page < ($maximum - $pages_around - 1))
182      {
183        $navbar.= ' ...';
184      }
185
186      $navbar.= ' <a href="'.$url.$start_str.$temp_start.'">'.$maximum.'</a>';
187    }
188
189    $navbar.= ' | ';
190    // link on next page ?
191    if ($nb_element > $nb_element_page
192        and $start + $nb_element_page < $nb_element)
193    {
194      $next = $start + $nb_element_page;
195
196      $navbar.=
197        '<a href="'.$url.$start_str.$next.'" rel="next">'
198        .$lang['next_page']
199        .'</a>';
200    }
201    else
202    {
203      $navbar.= $lang['next_page'];
204    }
205
206    $navbar.= ' | ';
207    // link to last page ?
208    if ($cur_page != $maximum)
209    {
210      $temp_start = ($maximum - 1) * $nb_element_page;
211
212      $navbar.=
213        '<a href="'.$url.$start_str.$temp_start.'" rel="last">'
214        .$lang['last_page']
215        .'</a>';
216    }
217    else
218    {
219      $navbar.= $lang['last_page'];
220    }
221  }
222  return $navbar;
223}
224
225//
226// Pick a language, any language ...
227//
228function language_select($default, $select_name = "language")
229{
230  $available_lang = get_languages();
231
232  $lang_select = '<select name="' . $select_name . '">';
233  foreach ($available_lang as $code => $displayname)
234  {
235    $selected = ( strtolower($default) == strtolower($code) ) ? ' selected="selected"' : '';
236    $lang_select .= '<option value="' . $code . '"' . $selected . '>' . ucwords($displayname) . '</option>';
237  }
238  $lang_select .= '</select>';
239
240  return $lang_select;
241}
242
243/**
244 * returns the list of categories as a HTML string
245 *
246 * categories string returned contains categories as given in the input
247 * array $cat_informations. $cat_informations array must be an association
248 * of {category_id => category_name}. If url input parameter is null,
249 * returns only the categories name without links.
250 *
251 * @param array cat_informations
252 * @param string url
253 * @param boolean replace_space
254 * @return string
255 */
256function get_cat_display_name($cat_informations,
257                              $url = '',
258                              $replace_space = true)
259{
260  global $conf;
261
262  $output = '';
263  $is_first = true;
264  foreach ($cat_informations as $id => $name)
265  {
266    if ($is_first)
267    {
268      $is_first = false;
269    }
270    else
271    {
272      $output.= $conf['level_separator'];
273    }
274
275    if ( !isset($url) )
276    {
277      $output.= $name;
278    }
279    elseif ($url == '')
280    {
281      $output.= '<a class=""';
282      $output.= ' href="'
283            .make_index_url(
284                array(
285                  'category'=>$id,
286                  'cat_name'=>$name
287                  )
288              )
289            .'">';
290      $output.= $name.'</a>';
291    }
292    else
293    {
294      $output.= '<a class=""';
295      $output.= ' href="'.PHPWG_ROOT_PATH.$url.$id.'">';
296      $output.= $name.'</a>';
297    }
298  }
299  if ($replace_space)
300  {
301    return replace_space($output);
302  }
303  else
304  {
305    return $output;
306  }
307}
308
309/**
310 * returns the list of categories as a HTML string, with cache of names
311 *
312 * categories string returned contains categories as given in the input
313 * array $cat_informations. $uppercats is the list of category ids to
314 * display in the right order. If url input parameter is empty, returns only
315 * the categories name without links.
316 *
317 * @param string uppercats
318 * @param string url
319 * @param boolean replace_space
320 * @return string
321 */
322function get_cat_display_name_cache($uppercats,
323                                    $url = '',
324                                    $replace_space = true)
325{
326  global $cat_names, $conf;
327
328  if (!isset($cat_names))
329  {
330    $query = '
331SELECT id,name
332  FROM '.CATEGORIES_TABLE.'
333;';
334    $result = pwg_query($query);
335    while ($row = mysql_fetch_array($result))
336    {
337      $cat_names[$row['id']] = $row['name'];
338    }
339  }
340
341  $output = '';
342  $is_first = true;
343  foreach (explode(',', $uppercats) as $category_id)
344  {
345    $name = $cat_names[$category_id];
346
347    if ($is_first)
348    {
349      $is_first = false;
350    }
351    else
352    {
353      $output.= $conf['level_separator'];
354    }
355
356    if ( !isset($url) )
357    {
358      $output.= $name;
359    }
360    elseif ($url == '')
361    {
362      $output.= '
363<a class=""
364   href="'
365      .make_index_url(
366          array(
367            'category'=>$category_id,
368            'cat_name'=>$name
369            )
370        )
371      .'">'.$name.'</a>';
372    }
373    else
374    {
375      $output.= '
376<a class=""
377   href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$name.'</a>';
378    }
379  }
380  if ($replace_space)
381  {
382    return replace_space($output);
383  }
384  else
385  {
386    return $output;
387  }
388}
389
390/**
391 * returns the HTML code for a category item in the menu (for the main page)
392 *
393 * HTML code generated uses logical list tags ul and each category is an
394 * item li. The paramter given is the category informations as an array,
395 * used keys are : id, name, nb_images, max_date_last, is_child_date_last,
396 * count_images, count_categories
397 *
398 * @param array categories
399 * @return string
400 */
401function get_html_menu_category($categories)
402{
403  global $page, $lang;
404
405  $ref_level = 0;
406  $level = 0;
407  $menu = '';
408
409  // $page_cat value remains 0 for special sections
410  $page_cat = 0;
411  if (isset($page['category']))
412  {
413    $page_cat = $page['category'];
414  }
415
416  foreach ($categories as $category)
417  {
418    $level = substr_count($category['global_rank'], '.') + 1;
419    if ($level > $ref_level)
420    {
421      $menu.= "\n<ul>";
422    }
423    else if ($level == $ref_level)
424    {
425      $menu.= "\n</li>";
426    }
427    else if ($level < $ref_level)
428    {
429      // we may have to close more than one level at the same time...
430      $menu.= "\n</li>";
431      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
432    }
433    $ref_level = $level;
434
435    $menu.= "\n\n".'<li';
436    if ($category['id'] == $page_cat)
437    {
438      $menu.= ' class="selected"';
439    }
440    $menu.= '>';
441
442    $url = make_index_url(
443            array(
444              'category'=>$category['id'],
445              'cat_name'=>$category['name']
446              )
447            );
448
449    $menu.= "\n".'<a href="'.$url.'"';
450    if ($page_cat != 0
451        and $category['id'] == $page['cat_id_uppercat'])
452    {
453      $menu.= ' rel="up"';
454    }
455    $menu.= '>'.$category['name'].'</a>';
456
457    // Count of category is main
458    // if not picture on categorie, test on sub-categories
459    if (($category['nb_images'] > 0) or ($category['count_images'] > 0))
460    {
461      $menu.= "\n".'<span class="';
462      $menu.= ($category['nb_images'] > 0 ? "menuInfoCat"
463                                          : "menuInfoCatByChild").'"';
464      $menu.= ' title="';
465      $menu.= ' '.get_display_images_count
466                  (
467                    $category['nb_images'],
468                    $category['count_images'],
469                    $category['count_categories'],
470                    false
471                  ).'">';
472      $menu.= '['.($category['nb_images'] > 0 ? $category['nb_images']
473                                              : $category['count_images']).']';
474      $menu.= '</span>';
475    }
476
477    $menu.= get_icon($category['max_date_last'], $category['is_child_date_last']);
478  }
479
480  $menu.= str_repeat("\n</li></ul>",($level));
481
482  return $menu;
483}
484
485/**
486 * returns HTMLized comment contents retrieved from database
487 *
488 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
489 * italic, *word* becomes bolded
490 *
491 * @param string content
492 * @return string
493 */
494function parse_comment_content($content)
495{
496  $pattern = '/(https?:\/\/\S*)/';
497  $replacement = '<a href="$1" rel="nofollow">$1</a>';
498  $content = preg_replace($pattern, $replacement, $content);
499
500  $content = nl2br($content);
501
502  // replace _word_ by an underlined word
503  $pattern = '/\b_(\S*)_\b/';
504  $replacement = '<span style="text-decoration:underline;">$1</span>';
505  $content = preg_replace($pattern, $replacement, $content);
506
507  // replace *word* by a bolded word
508  $pattern = '/\b\*(\S*)\*\b/';
509  $replacement = '<span style="font-weight:bold;">$1</span>';
510  $content = preg_replace($pattern, $replacement, $content);
511
512  // replace /word/ by an italic word
513  $pattern = "/\/(\S*)\/(\s)/";
514  $replacement = '<span style="font-style:italic;">$1$2</span>';
515  $content = preg_replace($pattern, $replacement, $content);
516
517  $content = '<div>'.$content.'</div>';
518  return $content;
519}
520
521function get_cat_display_name_from_id($cat_id,
522                                      $url = '',
523                                      $replace_space = true)
524{
525  $cat_info = get_cat_info($cat_id);
526  return get_cat_display_name($cat_info['name'], $url, $replace_space);
527}
528
529/**
530 * Returns an HTML list of tags. It can be a multi select field or a list of
531 * checkboxes.
532 *
533 * @param string HTML field name
534 * @param array selected tag ids
535 * @return array
536 */
537function get_html_tag_selection(
538  $tags,
539  $fieldname,
540  $selecteds = array(),
541  $forbidden_categories = null
542  )
543{
544  global $conf;
545
546  if (count ($tags) == 0 )
547  {
548    return '';
549  }
550  $output = '<ul class="tagSelection">';
551  foreach ($tags as $tag)
552  {
553    $output.=
554      '<li>'
555      .'<label>'
556      .'<input type="checkbox" name="'.$fieldname.'[]"'
557      .' value="'.$tag['tag_id'].'"'
558      ;
559
560    if (in_array($tag['tag_id'], $selecteds))
561    {
562      $output.= ' checked="checked"';
563    }
564
565    $output.=
566      ' />'
567      .' '. $tag['name']
568      .'</label>'
569      .'</li>'
570      ."\n"
571      ;
572  }
573  $output.= '</ul>';
574
575  return $output;
576}
577
578function name_compare($a, $b)
579{
580  return strcmp(strtolower($a['name']), strtolower($b['name']));
581}
582
583/**
584 * exits the current script (either exit or redirect)
585 */
586function access_denied()
587{
588  global $user, $lang;
589
590  $login_url =
591      get_root_url().'identification.php?redirect='
592      .urlencode(urlencode($_SERVER['REQUEST_URI']));
593
594  if ( isset($user['is_the_guest']) and !$user['is_the_guest'] )
595  {
596    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
597    echo '<a href="'.get_root_url().'identification.php">'.$lang['identification'].'</a>&nbsp;';
598    echo '<a href="'.make_index_url().'">'.$lang['home'].'</a></div>';
599    exit();
600  }
601  else
602  {
603    header('HTTP/1.1 401 Authorization required');
604    header('Status: 401 Authorization required');
605    redirect($login_url);
606  }
607}
608
609/**
610 * exits the current script with 404 code when a page cannot be found
611 * @param string msg a message to display
612 * @param string alternate_url redirect to this url
613 */
614function page_not_found($msg, $alternate_url=null)
615{
616  header('HTTP/1.1 404 Not found');
617  header('Status: 404 Not found');
618  if ($alternate_url==null)
619    $alternate_url = make_index_url();
620  redirect( $alternate_url,
621    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
622<h1 style="text-align:left; font-size:36px;">Page not found</h1><br/>'
623.$msg.'</div>',
624    5 );
625}
626
627/* returns the title to be displayed above thumbnails on tag page
628 */
629function get_tags_content_title()
630{
631  global $page;
632  $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
633  $title.= ' ';
634
635  for ($i=0; $i<count($page['tags']); $i++)
636  {
637    $title.= $i>0 ? ' + ' : '';
638
639    $title.=
640      '<a href="'
641      .make_index_url(
642        array(
643          'tags' => array( $page['tags'][$i] )
644          )
645        )
646      .'" title="'
647      .l10n('See pictures linked to this tag only')
648      .'">'
649      .$page['tags'][$i]['name']
650      .'</a>';
651
652    if ( count($page['tags'])>2 )
653    {
654      $other_tags = $page['tags'];
655      unset ( $other_tags[$i] );
656      $title.=
657        '<a href="'
658        .make_index_url(
659          array(
660            'tags' => $other_tags
661            )
662          )
663        .'" style="border:none;" title="'
664        .l10n('remove this tag')
665        .'"><img src="'
666        .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
667        .'" alt="x" style="vertical-align:bottom;" class="button"/>'
668        .'</a>';
669    }
670
671  }
672  return $title;
673}
674?>
Note: See TracBrowser for help on using the repository browser.