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

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

0000662: Recent elements don't use sometimes time and not truncated datetime
&
Standardize system date used

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