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

Last change on this file since 1606 was 1606, checked in by rvelices, 17 years ago

feature 479: when more than 3 tags selected, be able to remove one tag
from the multiple tags

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.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// | last update   : $Date: 2006-11-14 02:53:24 +0000 (Tue, 14 Nov 2006) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1606 $
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)
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'][$date]))
37  {
38    return $page['get_icon_cache'][$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'][$date] = '';
45    return $page['get_icon_cache'][$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'][$date] = '';
55    return $page['get_icon_cache'][$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').'/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'][$date] = $output;
74
75  return $page['get_icon_cache'][$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, date_last
396 *
397 * @param array categories
398 * @return string
399 */
400function get_html_menu_category($categories)
401{
402  global $page, $lang;
403
404  $ref_level = 0;
405  $level = 0;
406  $menu = '';
407
408  // $page_cat value remains 0 for special sections
409  $page_cat = 0;
410  if (isset($page['category']))
411  {
412    $page_cat = $page['category'];
413  }
414
415  foreach ($categories as $category)
416  {
417    $level = substr_count($category['global_rank'], '.') + 1;
418    if ($level > $ref_level)
419    {
420      $menu.= "\n<ul>";
421    }
422    else if ($level == $ref_level)
423    {
424      $menu.= "\n</li>";
425    }
426    else if ($level < $ref_level)
427    {
428      // we may have to close more than one level at the same time...
429      $menu.= "\n</li>";
430      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
431    }
432    $ref_level = $level;
433
434    $menu.= "\n\n".'<li';
435    if ($category['id'] == $page_cat)
436    {
437      $menu.= ' class="selected"';
438    }
439    $menu.= '>';
440
441    $url = make_index_url(
442            array(
443              'category'=>$category['id'],
444              'cat_name'=>$category['name']
445              )
446            );
447
448    $menu.= "\n".'<a href="'.$url.'"';
449    if ($page_cat != 0
450        and $category['id'] == $page['cat_id_uppercat'])
451    {
452      $menu.= ' rel="up"';
453    }
454    $menu.= '>'.$category['name'].'</a>';
455
456    if ($category['nb_images'] > 0)
457    {
458      $menu.= "\n".'<span class="menuInfoCat"';
459      $menu.= ' title="'.$category['nb_images'];
460      $menu.= ' '.$lang['images_available'].'">';
461      $menu.= '['.$category['nb_images'].']';
462      $menu.= '</span>';
463      $menu.= get_icon($category['date_last']);
464    }
465  }
466
467  $menu.= str_repeat("\n</li></ul>",($level));
468
469  return $menu;
470}
471
472/**
473 * returns HTMLized comment contents retrieved from database
474 *
475 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
476 * italic, *word* becomes bolded
477 *
478 * @param string content
479 * @return string
480 */
481function parse_comment_content($content)
482{
483  $pattern = '/(https?:\/\/\S*)/';
484  $replacement = '<a href="$1" rel="nofollow">$1</a>';
485  $content = preg_replace($pattern, $replacement, $content);
486
487  $content = nl2br($content);
488
489  // replace _word_ by an underlined word
490  $pattern = '/\b_(\S*)_\b/';
491  $replacement = '<span style="text-decoration:underline;">$1</span>';
492  $content = preg_replace($pattern, $replacement, $content);
493
494  // replace *word* by a bolded word
495  $pattern = '/\b\*(\S*)\*\b/';
496  $replacement = '<span style="font-weight:bold;">$1</span>';
497  $content = preg_replace($pattern, $replacement, $content);
498
499  // replace /word/ by an italic word
500  $pattern = "/\/(\S*)\/(\s)/";
501  $replacement = '<span style="font-style:italic;">$1$2</span>';
502  $content = preg_replace($pattern, $replacement, $content);
503
504  $content = '<div>'.$content.'</div>';
505  return $content;
506}
507
508function get_cat_display_name_from_id($cat_id,
509                                      $url = '',
510                                      $replace_space = true)
511{
512  $cat_info = get_cat_info($cat_id);
513  return get_cat_display_name($cat_info['name'], $url, $replace_space);
514}
515
516/**
517 * Returns an HTML list of tags. It can be a multi select field or a list of
518 * checkboxes.
519 *
520 * @param string HTML field name
521 * @param array selected tag ids
522 * @return array
523 */
524function get_html_tag_selection(
525  $tags,
526  $fieldname,
527  $selecteds = array(),
528  $forbidden_categories = null
529  )
530{
531  global $conf;
532
533  if (count ($tags) == 0 )
534  {
535    return '';
536  }
537  $output = '<ul class="tagSelection">';
538  foreach ($tags as $tag)
539  {
540    $output.=
541      '<li>'
542      .'<label>'
543      .'<input type="checkbox" name="'.$fieldname.'[]"'
544      .' value="'.$tag['tag_id'].'"'
545      ;
546
547    if (in_array($tag['tag_id'], $selecteds))
548    {
549      $output.= ' checked="checked"';
550    }
551
552    $output.=
553      ' />'
554      .' '. $tag['name']
555      .'</label>'
556      .'</li>'
557      ."\n"
558      ;
559  }
560  $output.= '</ul>';
561
562  return $output;
563}
564
565function name_compare($a, $b)
566{
567  return strcmp(strtolower($a['name']), strtolower($b['name']));
568}
569
570/**
571 * exits the current script (either exit or redirect)
572 */
573function access_denied()
574{
575  global $user, $lang;
576
577  $login_url =
578      get_root_url().'identification.php?redirect='
579      .urlencode(urlencode($_SERVER['REQUEST_URI']));
580
581  if ( isset($user['is_the_guest']) and !$user['is_the_guest'] )
582  {
583    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
584    echo '<a href="'.get_root_url().'identification.php">'.$lang['identification'].'</a>&nbsp;';
585    echo '<a href="'.make_index_url().'">'.$lang['home'].'</a></div>';
586    exit();
587  }
588  else
589  {
590    header('HTTP/1.1 401 Authorization required');
591    header('Status: 401 Authorization required');
592    redirect($login_url);
593  }
594}
595
596/**
597 * exits the current script with 404 code when a page cannot be found
598 * @param string msg a message to display
599 * @param string alternate_url redirect to this url
600 */
601function page_not_found($msg, $alternate_url=null)
602{
603  header('HTTP/1.1 404 Not found');
604  header('Status: 404 Not found');
605  if ($alternate_url==null)
606    $alternate_url = make_index_url();
607  redirect( $alternate_url,
608    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
609<h1 style="text-align:left; font-size:36px;">Page not found</h1><br/>'
610.$msg.'</div>',
611    5 );
612}
613
614/* returns the title to be displayed above thumbnails on tag page
615 */
616function get_tags_content_title()
617{
618  global $page;
619  $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
620  $title.= ' ';
621
622  for ($i=0; $i<count($page['tags']); $i++)
623  {
624    $title.= $i>0 ? ' + ' : '';
625
626    $title.=
627      '<a href="'
628      .make_index_url(
629        array(
630          'tags' => array( $page['tags'][$i] )
631          )
632        )
633      .'" title="'
634      .l10n('See pictures linked to this tag only')
635      .'">'
636      .$page['tags'][$i]['name']
637      .'</a>';
638
639    if ( count($page['tags'])>2 )
640    {
641      $other_tags = $page['tags'];
642      unset ( $other_tags[$i] );
643      $title.=
644        '<a href="'
645        .make_index_url(
646          array(
647            'tags' => $other_tags
648            )
649          )
650        .'" style="border:none;" title="'
651        .l10n('remove this tag')
652        .'"><img src="'
653        .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
654        .'" alt="x" style="vertical-align:bottom;" class="button"/>'
655        .'</a>';
656    }
657
658  }
659  return $title;
660}
661?>
Note: See TracBrowser for help on using the repository browser.