aboutsummaryrefslogtreecommitdiffstats
path: root/include/functions_html.inc.php
blob: bee99f92f520b6e1e7d2dea678593cd19e84281f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
// +-----------------------------------------------------------------------+
// | PhpWebGallery - a PHP based picture gallery                           |
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
// | branch        : BSF (Best So Far)
// | file          : $RCSfile$
// | last update   : $Date$
// | last modifier : $Author$
// | revision      : $Revision$
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

function get_icon( $date )
{
  global $user, $conf, $lang;

  if (!preg_match('/\d{4}-\d{2}-\d{2}/', $date))
  {
    return '';
  }

  list( $year,$month,$day ) = explode( '-', $date );
  $unixtime = mktime( 0, 0, 0, $month, $day, $year );
  
  $diff = time() - $unixtime;
  $day_in_seconds = 24*60*60;
  $output = '';
  $title = $lang['recent_image'].'&nbsp;';
  if ( $diff < $user['recent_period'] * $day_in_seconds )
  {
    $icon_url = get_themeconf('icon_dir').'/recent.png';
    $title .= $user['recent_period'];
    $title .=  '&nbsp;'.$lang['days'];
    $size = getimagesize( $icon_url );
    $output = '<img title="'.$title.'" src="'.$icon_url.'" class="icon" style="border:0;';
    $output.= 'height:'.$size[1].'px;width:'.$size[0].'px" alt="(!)" />';
  }
  return $output;
}

function create_navigation_bar($url, $nb_element, $start,
                               $nb_element_page, $link_class)
{
  global $lang, $conf;

  $pages_around = $conf['paginate_pages_around'];
  
  $navbar = '';
  
  // current page detection
  if (!isset($start)
      or !is_numeric($start)
      or (is_numeric($start) and $start < 0))
  {
    $start = 0;
  }
  
  // navigation bar useful only if more than one page to display !
  if ($nb_element > $nb_element_page)
  {
    // current page and last page
    $cur_page = ceil($start / $nb_element_page) + 1;
    $maximum = ceil($nb_element / $nb_element_page);

    // link to first page ?
    if ($cur_page != 1)
    {
      $navbar.= '<a href="';
      $navbar.= $url.'&amp;start=0';
      $navbar.= '" class="'.$link_class.'" rel="start">'.$lang['first_page'];
      $navbar.= '</a>';
    }
    else
    {
      $navbar.= $lang['first_page'];
    }
    $navbar.= ' | ';
    // link on previous page ?
    if ( $start != 0 )
    {
      $previous = $start - $nb_element_page;
      $navbar.= '<a href="';
      $navbar.= $url.'&amp;start='.$previous;
      $navbar.= '" class="'.$link_class.'" rel="prev">'.$lang['previous_page'];
      $navbar.= '</a>';
    }
    else
    {
      $navbar.= $lang['previous_page'];
    }
    $navbar.= ' | ';

    if ($cur_page > $pages_around + 1)
    {
      $navbar.= '&nbsp;<a href="';
      $navbar.= $url.'&amp;start=0';
      $navbar.= '" class="'.$link_class.'">1</a>';
      if ($cur_page > $pages_around + 2)
      {
        $navbar.= ' ...';
      }
    }
    
    // inspired from punbb source code
    for ($i = $cur_page - $pages_around, $stop = $cur_page + $pages_around + 1;
         $i < $stop;
         $i++)
    {
      if ($i < 1 or $i > $maximum)
      {
        continue;
      }
      else if ($i != $cur_page)
      {
        $temp_start = ($i - 1) * $nb_element_page;
        $navbar.= '&nbsp;<a href="';
        $navbar.= $url.'&amp;start='.$temp_start;
        $navbar.= '" class="'.$link_class.'"';
        if ($i == $cur_page - 1)
        {
          $navbar.= ' rel="prev"';
        }
        if ($i == $cur_page + 1)
        {
          $navbar.= ' rel="next"';
        }
        $navbar.='>'.$i.'</a>';
      }
      else
      {
        $navbar.= '&nbsp;<span class="pageNumberSelected">';
        $navbar.= $i.'</span>';
      }
    }

    if ($cur_page < ($maximum - $pages_around))
    {
      $temp_start = ($maximum - 1) * $nb_element_page;
      if ($cur_page < ($maximum - $pages_around - 1))
      {
        $navbar.= ' ...';
      }
      $navbar.= ' <a href="';
      $navbar.= $url.'&amp;start='.$temp_start;
      $navbar.= '" class="'.$link_class.'">'.$maximum.'</a>';
    }
    
    $navbar.= ' | ';
    // link on next page ?
    if ( $nb_element > $nb_element_page
         && $start + $nb_element_page < $nb_element )
    {
      $next = $start + $nb_element_page;
      $navbar.= '<a href="';
      $navbar.= $url.'&amp;start='.$next;
      $navbar.= '" class="'.$link_class.'" rel="next">'.$lang['next_page'].'</a>';
    }
    else
    {
      $navbar.= $lang['next_page'];
    }
    
    $navbar.= ' | ';
    // link to last page ?
    if ($cur_page != $maximum)
    {
      $temp_start = ($maximum - 1) * $nb_element_page;
      $navbar.= '<a href="';
      $navbar.= $url.'&amp;start='.$temp_start;
      $navbar.= '" class="'.$link_class.'">'.$lang['last_page'];
      $navbar.= '</a>';
    }
    else
    {
      $navbar.= $lang['last_page'];
    }
  }
  return $navbar;
}

//
// Pick a language, any language ...
//
function language_select($default, $select_name = "language")
{
  $available_lang = get_languages();

  $lang_select = '<select name="' . $select_name . '">';
  foreach ($available_lang as $code => $displayname)
  {
    $selected = ( strtolower($default) == strtolower($code) ) ? ' selected="selected"' : '';
    $lang_select .= '<option value="' . $code . '"' . $selected . '>' . ucwords($displayname) . '</option>';
  }
  $lang_select .= '</select>';

  return $lang_select;
}

/**
 * returns the list of categories as a HTML string
 *
 * categories string returned contains categories as given in the input
 * array $cat_informations. $cat_informations array must be an association
 * of {category_id => category_name}. If url input parameter is empty,
 * returns only the categories name without links.
 *
 * @param array cat_informations
 * @param string url
 * @param boolean replace_space
 * @return string
 */
function get_cat_display_name($cat_informations,
                              $url = 'category.php?cat=',
                              $replace_space = true)
{
  global $conf;
  
  $output = '';
  $is_first = true;
  foreach ($cat_informations as $id => $name)
  {
    if ($is_first)
    {
      $is_first = false;
    }
    else
    {
      $output.= $conf['level_separator'];
    }

    if ($url == '')
    {
      $output.= $name;
    }
    else
    {
      $output.= '<a class=""';
      $output.= ' href="'.PHPWG_ROOT_PATH.$url.$id.'">';
      $output.= $name.'</a>';
    }
  }
  if ($replace_space)
  {
    return replace_space($output);
  }
  else
  {
    return $output;
  }
}

/**
 * returns the list of categories as a HTML string, with cache of names
 *
 * categories string returned contains categories as given in the input
 * array $cat_informations. $uppercats is the list of category ids to
 * display in the right order. If url input parameter is empty, returns only
 * the categories name without links.
 *
 * @param string uppercats
 * @param string url
 * @param boolean replace_space
 * @return string
 */
function get_cat_display_name_cache($uppercats,
                                    $url = 'category.php?cat=',
                                    $replace_space = true)
{
  global $cat_names, $conf;

  if (!isset($cat_names))
  {
    $query = '
SELECT id,name
  FROM '.CATEGORIES_TABLE.'
;';
    $result = pwg_query($query);
    while ($row = mysql_fetch_array($result))
    {
      $cat_names[$row['id']] = $row['name'];
    }
  }
  
  $output = '';
  $is_first = true;
  foreach (explode(',', $uppercats) as $category_id)
  {
    $name = $cat_names[$category_id];
    
    if ($is_first)
    {
      $is_first = false;
    }
    else
    {
      $output.= $conf['level_separator'];
    }

    if ($url == '')
    {
      $output.= $name;
    }
    else
    {
      $output.= '
<a class=""
   href="'.PHPWG_ROOT_PATH.$url.$category_id.'">'.$name.'</a>';
    }
  }
  if ($replace_space)
  {
    return replace_space($output);
  }
  else
  {
    return $output;
  }
}

/**
 * returns the HTML code for a category item in the menu (for category.php)
 *
 * HTML code generated uses logical list tags ul and each category is an
 * item li. The paramter given is the category informations as an array,
 * used keys are : id, name, nb_images, date_last
 *
 * @param array categories
 * @return string
 */
function get_html_menu_category($categories)
{
  global $page, $lang;

  $ref_level = 0;
  $level = 0;
  $menu = '';
  
  foreach ($categories as $category)
  {
    $level = substr_count($category['global_rank'], '.') + 1;
    if ($level > $ref_level)
    {
      $menu.= "\n<ul>";
    }
    else if ($level == $ref_level)
    {
      $menu.= "\n</li>";
    }
    else if ($level < $ref_level)
    {
      // we may have to close more than one level at the same time...
      $menu.= "\n</li>";
      $menu.= str_repeat("\n</ul></li>",($ref_level-$level));
    }
    $ref_level = $level;

    $menu.= "\n\n".'<li';
    if (isset($page['cat'])
        and is_numeric($page['cat'])
        and $category['id'] == $page['cat'])
    {
      $menu.= ' class="selected"';
    }
    $menu.= '>';
  
    $url = PHPWG_ROOT_PATH.'category.php?cat='.$category['id'];
    $menu.= "\n".'<a href="'.$url.'">'.$category['name'].'</a>';

    if ($category['nb_images'] > 0)
    {
      $menu.= "\n".'<span class="menuInfoCat"';
      $menu.= ' title="'.$category['nb_images'];
      $menu.= ' '.$lang['images_available'].'">';
      $menu.= '['.$category['nb_images'].']';
      $menu.= '</span>';
      $menu.= get_icon($category['date_last']);
    }
  }

  $menu.= str_repeat("\n</li></ul>",($level));
  
  return $menu;
}

/**
 * returns HTMLized comment contents retrieved from database
 *
 * newlines becomes br tags, _word_ becomes underline, /word/ becomes
 * italic, *word* becomes bolded
 *
 * @param string content
 * @return string
 */
function parse_comment_content($content)
{
  $content = nl2br($content);

  $pattern = '/(http?:\/\/\S*)/';
  $replacement = '<a href="$1">$1</a>';
  $content = preg_replace($pattern, $replacement, $content);
  
  // replace _word_ by an underlined word
  $pattern = '/\b_(\S*)_\b/';
  $replacement = '<span style="text-decoration:underline;">$1</span>';
  $content = preg_replace($pattern, $replacement, $content);
  
  // replace *word* by a bolded word
  $pattern = '/\b\*(\S*)\*\b/';
  $replacement = '<span style="font-weight:bold;">$1</span>';
  $content = preg_replace($pattern, $replacement, $content);
  
  // replace /word/ by an italic word
   $pattern = "/\/(\S*)\/(\s)/";
   $replacement = '<span style="font-style:italic;">$1$2</span>';
   $content = preg_replace($pattern, $replacement, $content);

  return $content;
}

function get_cat_display_name_from_id($cat_id,
                                      $url = 'category.php?cat=',
                                      $replace_space = true)
{
  $cat_info = get_cat_info($cat_id);
  return get_cat_display_name($cat_info['name'], $url, $replace_space);
}
?>