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

Last change on this file since 1861 was 1861, checked in by rvelices, 17 years ago
  • refactoring pagecategory before 1.7 release

pagecategory is not an id anymore, but an associative array of category info
all of pagecat_xxx or pageuppercats merged into one
simplifies calls to make_index_url
give plugins a clean start for page variables for version 1.7

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