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

Last change on this file since 1092 was 1092, checked in by rvelices, 18 years ago

URL rewriting: capable of fully working with urls without ?

URL rewriting: works with image file instead of image id (change
make_picture_url to generate urls with file name instead of image id)

URL rewriting: completely works with category/best_rated and
picture/best_rated/534 (change 'category.php?' to 'category' in make_index_url
and 'picture.php?' to 'picture' in make_picture_url to see it)

fix: picture category display in upper bar

fix: function rate_picture variables and use of the new user type

fix: caddie icon appears now on category page

fix: admin element_set sql query was using storage_category_id column
(column has moved to #image_categories)

fix: replaced some old $_GET[xxx] with $page[xxx]

fix: pictures have metadata url (use ? parameter - might change later)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 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// | file          : $RCSfile$
9// | last update   : $Date: 2006-03-22 01:01:47 +0000 (Wed, 22 Mar 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1092 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28function get_icon($date)
29{
30  global $page, $user, $conf, $lang;
31
32  if (empty($date))
33  {
34    $date = 'NULL';
35  }
36
37  if (isset($page['get_icon_cache'][$date]))
38  {
39    return $page['get_icon_cache'][$date];
40  }
41
42  if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})/', $date, $matches))
43  {
44    // date can be empty, no icon to display
45    $page['get_icon_cache'][$date] = '';
46    return $page['get_icon_cache'][$date];
47  }
48
49  list($devnull, $year, $month, $day) = $matches;
50  $unixtime = mktime( 0, 0, 0, $month, $day, $year );
51
52  if ($unixtime === false  // PHP 5.1.0 and above
53      or $unixtime === -1) // PHP prior to 5.1.0
54  {
55    $page['get_icon_cache'][$date] = '';
56    return $page['get_icon_cache'][$date];
57  }
58
59  $diff = time() - $unixtime;
60  $day_in_seconds = 24*60*60;
61  $output = '';
62  $title = $lang['recent_image'].'&nbsp;';
63  if ( $diff < $user['recent_period'] * $day_in_seconds )
64  {
65    $icon_url = get_themeconf('icon_dir').'/recent.png';
66    $title .= $user['recent_period'];
67    $title .=  '&nbsp;'.$lang['days'];
68    $size = getimagesize( $icon_url );
69    $icon_url = get_root_url().$icon_url;
70    $output = '<img title="'.$title.'" src="'.$icon_url.'" class="icon" style="border:0;';
71    $output.= 'height:'.$size[1].'px;width:'.$size[0].'px" alt="(!)" />';
72  }
73
74  $page['get_icon_cache'][$date] = $output;
75
76  return $page['get_icon_cache'][$date];
77}
78
79function create_navigation_bar(
80  $url, $nb_element, $start, $nb_element_page, $clean_url = false
81  )
82{
83  global $lang, $conf;
84
85  $pages_around = $conf['paginate_pages_around'];
86  $start_str = $clean_url ? '/start-' : '&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="'.make_index_url( array('category'=>$id) ).'">';
283      $output.= $name.'</a>';
284    }
285    else
286    {
287      $output.= '<a class=""';
288      $output.= ' href="'.PHPWG_ROOT_PATH.$url.$id.'">';
289      $output.= $name.'</a>';
290    }
291  }
292  if ($replace_space)
293  {
294    return replace_space($output);
295  }
296  else
297  {
298    return $output;
299  }
300}
301
302/**
303 * returns the list of categories as a HTML string, with cache of names
304 *
305 * categories string returned contains categories as given in the input
306 * array $cat_informations. $uppercats is the list of category ids to
307 * display in the right order. If url input parameter is empty, returns only
308 * the categories name without links.
309 *
310 * @param string uppercats
311 * @param string url
312 * @param boolean replace_space
313 * @return string
314 */
315function get_cat_display_name_cache($uppercats,
316                                    $url = '',
317                                    $replace_space = true)
318{
319  global $cat_names, $conf;
320
321  if (!isset($cat_names))
322  {
323    $query = '
324SELECT id,name
325  FROM '.CATEGORIES_TABLE.'
326;';
327    $result = pwg_query($query);
328    while ($row = mysql_fetch_array($result))
329    {
330      $cat_names[$row['id']] = $row['name'];
331    }
332  }
333
334  $output = '';
335  $is_first = true;
336  foreach (explode(',', $uppercats) as $category_id)
337  {
338    $name = $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.= $name;
352    }
353    elseif ($url == '')
354    {
355      $output.= '
356<a class=""
357   href="'.make_index_url( array('category'=>$category_id) ).'">'.$name.'</a>';
358    }
359    else
360    {
361      $output.= '
362<a class=""
363   href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$name.'</a>';
364    }
365  }
366  if ($replace_space)
367  {
368    return replace_space($output);
369  }
370  else
371  {
372    return $output;
373  }
374}
375
376/**
377 * returns the HTML code for a category item in the menu (for the main page)
378 *
379 * HTML code generated uses logical list tags ul and each category is an
380 * item li. The paramter given is the category informations as an array,
381 * used keys are : id, name, nb_images, date_last
382 *
383 * @param array categories
384 * @return string
385 */
386function get_html_menu_category($categories)
387{
388  global $page, $lang;
389
390  $ref_level = 0;
391  $level = 0;
392  $menu = '';
393
394  // $page_cat value remains 0 for special sections
395  $page_cat = 0;
396  if (isset($page['category']))
397  {
398    $page_cat = $page['category'];
399  }
400
401  foreach ($categories as $category)
402  {
403    $level = substr_count($category['global_rank'], '.') + 1;
404    if ($level > $ref_level)
405    {
406      $menu.= "\n<ul>";
407    }
408    else if ($level == $ref_level)
409    {
410      $menu.= "\n</li>";
411    }
412    else if ($level < $ref_level)
413    {
414      // we may have to close more than one level at the same time...
415      $menu.= "\n</li>";
416      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
417    }
418    $ref_level = $level;
419
420    $menu.= "\n\n".'<li';
421    if ($category['id'] == $page_cat)
422    {
423      $menu.= ' class="selected"';
424    }
425    $menu.= '>';
426
427    $url = make_index_url(array('category' => $category['id']));
428
429    $menu.= "\n".'<a href="'.$url.'"';
430    if ($page_cat != 0
431        and $category['id'] == $page['cat_id_uppercat'])
432    {
433      $menu.= ' rel="up"';
434    }
435    $menu.= '>'.$category['name'].'</a>';
436
437    if ($category['nb_images'] > 0)
438    {
439      $menu.= "\n".'<span class="menuInfoCat"';
440      $menu.= ' title="'.$category['nb_images'];
441      $menu.= ' '.$lang['images_available'].'">';
442      $menu.= '['.$category['nb_images'].']';
443      $menu.= '</span>';
444      $menu.= get_icon($category['date_last']);
445    }
446  }
447
448  $menu.= str_repeat("\n</li></ul>",($level));
449
450  return $menu;
451}
452
453/**
454 * returns HTMLized comment contents retrieved from database
455 *
456 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
457 * italic, *word* becomes bolded
458 *
459 * @param string content
460 * @return string
461 */
462function parse_comment_content($content)
463{
464  $pattern = '/(https?:\/\/\S*)/';
465  $replacement = '<a href="$1" rel="nofollow">$1</a>';
466  $content = preg_replace($pattern, $replacement, $content);
467
468  $content = nl2br($content);
469
470  // replace _word_ by an underlined word
471  $pattern = '/\b_(\S*)_\b/';
472  $replacement = '<span style="text-decoration:underline;">$1</span>';
473  $content = preg_replace($pattern, $replacement, $content);
474
475  // replace *word* by a bolded word
476  $pattern = '/\b\*(\S*)\*\b/';
477  $replacement = '<span style="font-weight:bold;">$1</span>';
478  $content = preg_replace($pattern, $replacement, $content);
479
480  // replace /word/ by an italic word
481  $pattern = "/\/(\S*)\/(\s)/";
482  $replacement = '<span style="font-style:italic;">$1$2</span>';
483  $content = preg_replace($pattern, $replacement, $content);
484
485  $content = '<div>'.$content.'</div>';
486  return $content;
487}
488
489function get_cat_display_name_from_id($cat_id,
490                                      $url = '',
491                                      $replace_space = true)
492{
493  $cat_info = get_cat_info($cat_id);
494  return get_cat_display_name($cat_info['name'], $url, $replace_space);
495}
496?>
Note: See TracBrowser for help on using the repository browser.