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

Last change on this file since 5706 was 5706, checked in by plg, 14 years ago

feature 1583: (tag navigation ergonomy) in the page title, always display the
"remove tag" icon, whatever the number of tags.

File size: 14.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/**
25 * returns the list of categories as a HTML string
26 *
27 * categories string returned contains categories as given in the input
28 * array $cat_informations. $cat_informations array must be an array
29 * of array( id=>?, name=>?, permalink=>?). If url input parameter is null,
30 * returns only the categories name without links.
31 *
32 * @param array cat_informations
33 * @param string url
34 * @param boolean replace_space
35 * @return string
36 */
37function get_cat_display_name($cat_informations,
38                              $url = '',
39                              $replace_space = true)
40{
41  global $conf;
42
43  $output = '<a href="'.get_absolute_root_url().$conf['home_page'].'">'.l10n('Home').'</a>';
44
45  foreach ($cat_informations as $cat)
46  {
47    is_array($cat) or trigger_error(
48        'get_cat_display_name wrong type for category ', E_USER_WARNING
49      );
50
51    $cat['name'] = trigger_event(
52      'render_category_name',
53      $cat['name'],
54      'get_cat_display_name'
55      );
56
57    $output.= $conf['level_separator'];
58
59    if ( !isset($url) )
60    {
61      $output.= $cat['name'];
62    }
63    elseif ($url == '')
64    {
65      $output.= '<a href="'
66            .make_index_url(
67                array(
68                  'category' => $cat,
69                  )
70              )
71            .'">';
72      $output.= $cat['name'].'</a>';
73    }
74    else
75    {
76      $output.= '<a href="'.PHPWG_ROOT_PATH.$url.$cat['id'].'">';
77      $output.= $cat['name'].'</a>';
78    }
79  }
80  if ($replace_space)
81  {
82    return replace_space($output);
83  }
84  else
85  {
86    return $output;
87  }
88}
89
90/**
91 * returns the list of categories as a HTML string, with cache of names
92 *
93 * categories string returned contains categories as given in the input
94 * array $cat_informations. $uppercats is the list of category ids to
95 * display in the right order. If url input parameter is empty, returns only
96 * the categories name without links.
97 *
98 * @param string uppercats
99 * @param string url
100 * @param boolean replace_space
101 * @return string
102 */
103function get_cat_display_name_cache($uppercats,
104                                    $url = '',
105                                    $replace_space = true)
106{
107  global $cache, $conf;
108
109  if (!isset($cache['cat_names']))
110  {
111    $query = '
112SELECT id, name, permalink
113  FROM '.CATEGORIES_TABLE.'
114;';
115    $cache['cat_names'] = hash_from_query($query, 'id');
116  }
117
118  $output = '';
119  $is_first = true;
120  foreach (explode(',', $uppercats) as $category_id)
121  {
122    $cat = $cache['cat_names'][$category_id];
123
124    $cat['name'] = trigger_event(
125      'render_category_name',
126      $cat['name'],
127      'get_cat_display_name_cache'
128      );
129
130    if ($is_first)
131    {
132      $is_first = false;
133    }
134    else
135    {
136      $output.= $conf['level_separator'];
137    }
138
139    if ( !isset($url) )
140    {
141      $output.= $cat['name'];
142    }
143    elseif ($url == '')
144    {
145      $output.= '
146<a href="'
147      .make_index_url(
148          array(
149            'category' => $cat,
150            )
151        )
152      .'">'.$cat['name'].'</a>';
153    }
154    else
155    {
156      $output.= '
157<a href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$cat['name'].'</a>';
158    }
159  }
160  if ($replace_space)
161  {
162    return replace_space($output);
163  }
164  else
165  {
166    return $output;
167  }
168}
169
170/**
171 * returns HTMLized comment contents retrieved from database
172 *
173 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
174 * italic, *word* becomes bolded
175 *
176 * @param string content
177 * @return string
178 */
179function parse_comment_content($content)
180{
181  $pattern = '/(https?:\/\/\S*)/';
182  $replacement = '<a href="$1" rel="nofollow">$1</a>';
183  $content = preg_replace($pattern, $replacement, $content);
184
185  $content = nl2br($content);
186
187  // replace _word_ by an underlined word
188  $pattern = '/\b_(\S*)_\b/';
189  $replacement = '<span style="text-decoration:underline;">$1</span>';
190  $content = preg_replace($pattern, $replacement, $content);
191
192  // replace *word* by a bolded word
193  $pattern = '/\b\*(\S*)\*\b/';
194  $replacement = '<span style="font-weight:bold;">$1</span>';
195  $content = preg_replace($pattern, $replacement, $content);
196
197  // replace /word/ by an italic word
198  $pattern = "/\/(\S*)\/(\s)/";
199  $replacement = '<span style="font-style:italic;">$1$2</span>';
200  $content = preg_replace($pattern, $replacement, $content);
201
202  return $content;
203}
204
205function get_cat_display_name_from_id($cat_id,
206                                      $url = '',
207                                      $replace_space = true)
208{
209  $cat_info = get_cat_info($cat_id);
210  return get_cat_display_name($cat_info['upper_names'], $url, $replace_space);
211}
212
213/**
214 * Returns an HTML list of tags. It can be a multi select field or a list of
215 * checkboxes.
216 *
217 * @param string HTML field name
218 * @param array selected tag ids
219 * @return array
220 */
221function get_html_tag_selection(
222  $tags,
223  $fieldname,
224  $selecteds = array(),
225  $forbidden_categories = null
226  )
227{
228  global $conf;
229
230  if (count ($tags) == 0 )
231  {
232    return '';
233  }
234  $output = '<ul class="tagSelection">';
235  foreach ($tags as $tag)
236  {
237    $output.=
238      '<li>'
239      .'<label>'
240      .'<input type="checkbox" name="'.$fieldname.'[]"'
241      .' value="'.$tag['id'].'"'
242      ;
243
244    if (in_array($tag['id'], $selecteds))
245    {
246      $output.= ' checked="checked"';
247    }
248
249    $output.=
250      '>'
251      .' '. $tag['name']
252      .'</label>'
253      .'</li>'
254      ."\n"
255      ;
256  }
257  $output.= '</ul>';
258
259  return $output;
260}
261
262function name_compare($a, $b)
263{
264  return strcmp(strtolower($a['name']), strtolower($b['name']));
265}
266
267function tag_alpha_compare($a, $b)
268{
269  return strcmp(strtolower($a['url_name']), strtolower($b['url_name']));
270}
271
272/**
273 * exits the current script (either exit or redirect)
274 */
275function access_denied()
276{
277  global $user;
278
279  $login_url =
280      get_root_url().'identification.php?redirect='
281      .urlencode(urlencode($_SERVER['REQUEST_URI']));
282
283  set_status_header(401);
284  if ( isset($user) and !is_a_guest() )
285  {
286    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
287    echo '<div style="text-align:center;">'.l10n('You are not authorized to access the requested page').'<br>';
288    echo '<a href="'.get_root_url().'Identification.php">'.l10n('Identification').'</a>&nbsp;';
289    echo '<a href="'.make_index_url().'">'.l10n('Home').'</a></div>';
290    echo str_repeat( ' ', 512); //IE6 doesn't error output if below a size
291    exit();
292  }
293  else
294  {
295    redirect_html($login_url);
296  }
297}
298
299/**
300 * exits the current script with 403 code
301 * @param string msg a message to display
302 * @param string alternate_url redirect to this url
303 */
304function page_forbidden($msg, $alternate_url=null)
305{
306  set_status_header(403);
307  if ($alternate_url==null)
308    $alternate_url = make_index_url();
309  redirect_html( $alternate_url,
310    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
311<h1 style="text-align:left; font-size:36px;">Forbidden</h1><br>'
312.$msg.'</div>',
313    5 );
314}
315
316/**
317 * exits the current script with 400 code
318 * @param string msg a message to display
319 * @param string alternate_url redirect to this url
320 */
321function bad_request($msg, $alternate_url=null)
322{
323  set_status_header(400);
324  if ($alternate_url==null)
325    $alternate_url = make_index_url();
326  redirect_html( $alternate_url,
327    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
328<h1 style="text-align:left; font-size:36px;">Bad request</h1><br>'
329.$msg.'</div>',
330    5 );
331}
332
333/**
334 * exits the current script with 404 code when a page cannot be found
335 * @param string msg a message to display
336 * @param string alternate_url redirect to this url
337 */
338function page_not_found($msg, $alternate_url=null)
339{
340  set_status_header(404);
341  if ($alternate_url==null)
342    $alternate_url = make_index_url();
343  redirect_html( $alternate_url,
344    '<div style="text-align:left; margin-left:5em;margin-bottom:5em;">
345<h1 style="text-align:left; font-size:36px;">Page not found</h1><br>'
346.$msg.'</div>',
347    5 );
348}
349
350/**
351 * exits the current script with 500 http code
352 * this method can be called at any time (does not use template/language/user etc...)
353 * @param string msg a message to display
354 */
355function fatal_error($msg)
356{
357  $btrace_msg = '';
358  if (function_exists('debug_backtrace'))
359  {
360    $bt = debug_backtrace();
361    for ($i=1; $i<count($bt); $i++)
362    {
363      $class = isset($bt[$i]['class']) ? (@$bt[$i]['class'].'::') : '';
364      $btrace_msg .= "#$i\t".$class.@$bt[$i]['function'].' '.@$bt[$i]['file']."(".@$bt[$i]['line'].")\n";
365    }
366    $btrace_msg = trim($btrace_msg);
367    $msg .= "\n";
368  }
369
370  $display = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
371<h1>Piwigo encountered a non recoverable error</h1>
372<pre style='font-size:larger;background:white;color:red;padding:1em;margin:0;clear:both;display:block;width:auto;height:auto;overflow:auto'>
373<b>$msg</b>
374$btrace_msg
375</pre>\n";
376
377  @set_status_header(500);
378  echo $display.str_repeat( ' ', 300); //IE6 doesn't error output if below a size
379
380  if ( function_exists('ini_set') )
381  {// if possible turn off error display (we display it)
382    ini_set('display_errors', false);
383  }
384  error_reporting( E_ALL );
385  trigger_error( strip_tags($msg).$btrace_msg, E_USER_ERROR );
386  die(0); // just in case
387}
388
389/* returns the title to be displayed above thumbnails on tag page
390 */
391function get_tags_content_title()
392{
393  global $page;
394  $title = count($page['tags']) > 1 ? l10n('Tag') : l10n('Tag');
395  $title.= ' ';
396
397  for ($i=0; $i<count($page['tags']); $i++)
398  {
399    $title.= $i>0 ? ' + ' : '';
400
401    $title.=
402      '<a href="'
403      .make_index_url(
404        array(
405          'tags' => array( $page['tags'][$i] )
406          )
407        )
408      .'" title="'
409      .l10n('See images linked to this tag only')
410      .'">'
411      .$page['tags'][$i]['name']
412      .'</a>';
413
414    $remove_url = null;
415    if (count($page['tags']) == 1)
416    {
417      $remove_url = get_root_url().'tags.php';
418    }
419    else
420    {
421      $other_tags = $page['tags'];
422      unset($other_tags[$i]);
423      $remove_url = make_index_url(
424        array(
425          'tags' => $other_tags
426          )
427        );
428    }
429   
430    $title.=
431      '<a href="'.$remove_url.'" style="border:none;" title="'
432      .l10n('remove this tag from the list')
433      .'"><img src="'
434        .get_root_url().get_themeconf('icon_dir').'/remove_s.png'
435      .'" alt="x" style="vertical-align:bottom;" class="button">'
436      .'</a>';
437  }
438  return $title;
439}
440
441/**
442  Sets the http status header (200,401,...)
443 */
444function set_status_header($code, $text='')
445{
446  if (empty($text))
447  {
448    switch ($code)
449    {
450      case 200: $text='OK';break;
451      case 301: $text='Moved permanently';break;
452      case 302: $text='Moved temporarily';break;
453      case 304: $text='Not modified';break;
454      case 400: $text='Bad request';break;
455      case 401: $text='Authorization required';break;
456      case 403: $text='Forbidden';break;
457      case 404: $text='Not found';break;
458      case 500: $text='Server error';break;
459      case 501: $text='Not implemented';break;
460      case 503: $text='Service unavailable';break;
461    }
462  }
463  $protocol = $_SERVER["SERVER_PROTOCOL"];
464  if ( ('HTTP/1.1' != $protocol) && ('HTTP/1.0' != $protocol) )
465    $protocol = 'HTTP/1.0';
466
467  if ( version_compare( phpversion(), '4.3.0', '>=' ) )
468  {
469    header( "$protocol $code $text", true, $code );
470  }
471  else
472  {
473    header( "$protocol $code $text" );
474  }
475  trigger_action('set_status_header', $code, $text);
476}
477
478/** returns the category comment for rendering in html textual mode (subcatify)
479 * this is an event handler. don't call directly
480 */
481function render_category_literal_description($desc)
482{
483  return strip_tags($desc, '<span><p><a><br><b><i><small><big><strong><em>');
484}
485
486/** returns the argument_ids array with new sequenced keys based on related
487 * names. Sequence is not case sensitive.
488 * Warning: By definition, this function breaks original keys
489 */
490function order_by_name($element_ids,$name)
491{
492  $ordered_element_ids = array();
493  foreach ($element_ids as $k_id => $element_id)
494  {
495    $key = strtolower($name[$element_id]) .'-'. $name[$element_id] .'-'. $k_id;
496    $ordered_element_ids[$key] = $element_id;
497  }
498  ksort($ordered_element_ids);
499  return $ordered_element_ids;
500}
501
502/*event handler for menu*/
503function register_default_menubar_blocks( $menu_ref_arr )
504{
505  $menu = & $menu_ref_arr[0];
506  if ($menu->get_id() != 'menubar')
507    return;
508  $menu->register_block( new RegisteredBlock( 'mbLinks', 'Links', 'piwigo'));
509  $menu->register_block( new RegisteredBlock( 'mbCategories', 'Categories', 'piwigo'));
510  $menu->register_block( new RegisteredBlock( 'mbTags', 'Related tags', 'piwigo'));
511  $menu->register_block( new RegisteredBlock( 'mbSpecials', 'Specials', 'piwigo'));
512  $menu->register_block( new RegisteredBlock( 'mbMenu', 'Menu', 'piwigo'));
513  $menu->register_block( new RegisteredBlock( 'mbIdentification', 'Identification', 'piwigo') );
514}
515
516?>
Note: See TracBrowser for help on using the repository browser.