source: trunk/include/functions_url.inc.php @ 1677

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

Feature Issue ID 0000601: Filter all public pages with only recent elements

It's a finalized version.
Obsolete code of draft are removed.

You can filter categories and images with recent date period on your screen selection.
In the future, filter could be easy done on other type data (plugin?)

You can flat categories and sub-categories with a recent date period of your choice.

Next, perhaps, a panel to choice recent date for the 2 features.

On draft, there have problem with MySql 5, be careful!

Css problem not resolved:

  • Menu "Categories" is bad centered
  • Icon on dark too on the top
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net |
5// +-----------------------------------------------------------------------+
6// | branch        : BSF (Best So Far)
7// | file          : $RCSfile$
8// | last update   : $Date: 2006-12-21 21:38:20 +0000 (Thu, 21 Dec 2006) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 1677 $
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
27
28/**
29 * returns a prefix for each url link on displayed page
30 * and return an empty string for current path
31 * @return string
32 */
33function get_root_url()
34{
35  global $page;
36  if ( isset($page['root_path']) )
37  {
38    $root_url = $page['root_path'];
39  }
40  else
41  {
42    $root_url = PHPWG_ROOT_PATH;
43  }
44  if ( dirname($root_url)!='.' )
45  {
46    return $root_url;
47  }
48  else
49  {
50    return substr($root_url, 2);
51  }
52}
53
54/**
55 * returns the url of the current host (e.g. http://www.toto.com )
56 * TODO: if required by someone, treat https case
57 */
58function get_host_url()
59{
60  $url = "http://";
61  $url .= $_SERVER['HTTP_HOST'];
62  if ($_SERVER['SERVER_PORT']!=80)
63  {
64    $url .= ':'.$_SERVER['SERVER_PORT'];
65  }
66  return $url;
67}
68
69/**
70 * adds one or more _GET style parameters to an url
71 * example: add_url_params('/x', array('a'=>'b')) returns /x?a=b
72 * add_url_params('/x?cat_id=10', array('a'=>'b')) returns /x?cat_id=10&amp;a=b
73 * @param string url
74 * @param array params
75 * @return string
76 */
77function add_url_params($url, $params)
78{
79  if ( !empty($params) )
80  {
81    assert( is_array($params) );
82    $is_first = true;
83    foreach($params as $param=>$val)
84    {
85      if ($is_first)
86      {
87        $is_first = false;
88        $url .= ( strstr($url, '?')===false ) ? '?' :'&amp;';
89      }
90      else
91      {
92        $url .= '&amp;';
93      }
94      $url .= $param;
95      if (isset($val))
96      {
97        $url .= '='.$val;
98      }
99    }
100  }
101  return $url;
102}
103
104/**
105 * build an index URL for a specific section
106 *
107 * @param array
108 * @return string
109 */
110function make_index_url($params = array())
111{
112  global $conf;
113  $url = get_root_url().'index';
114  if ($conf['php_extension_in_urls'])
115  {
116    $url .= '.php';
117  }
118  if ($conf['question_mark_in_urls'])
119  {
120    $url .= '?';
121  }
122  $url.= make_section_in_url($params);
123  $url = add_well_known_params_in_url($url, $params);
124  return $url;
125}
126
127/**
128 * build an index URL with current page parameters, but with redefinitions
129 * and removes.
130 *
131 * duplicate_index_url(array('category' => 12), array('start')) will create
132 * an index URL on the current section (categories), but on a redefined
133 * category and without the start URL parameter.
134 *
135 * @param array redefined keys
136 * @param array removed keys
137 * @return string
138 */
139function duplicate_index_url($redefined = array(), $removed = array())
140{
141  return make_index_url(
142    params_for_duplication($redefined, $removed)
143    );
144}
145
146/**
147 * returns $page global array with key redefined and key removed
148 *
149 * @param array redefined keys
150 * @param array removed keys
151 * @return array
152 */
153function params_for_duplication($redefined, $removed)
154{
155  global $page;
156
157  if (count($removed) > 0)
158  {
159    $params = array();
160
161    foreach ($page as $page_item_key => $page_item_value)
162    {
163      if (!in_array($page_item_key, $removed))
164      {
165        $params[$page_item_key] = $page_item_value;
166      }
167    }
168  }
169  else
170  {
171    $params = $page;
172  }
173
174  foreach ($redefined as $redefined_param => $redefined_value)
175  {
176    $params[$redefined_param] = $redefined_value;
177  }
178
179  return $params;
180}
181
182/**
183 * create a picture URL with current page parameters, but with redefinitions
184 * and removes. See duplicate_index_url.
185 *
186 * @param array redefined keys
187 * @param array removed keys
188 * @return string
189 */
190function duplicate_picture_url($redefined = array(), $removed = array())
191{
192  return make_picture_url(
193    params_for_duplication($redefined, $removed)
194    );
195}
196
197/**
198 * create a picture URL on a specific section for a specific picture
199 *
200 * @param array
201 * @return string
202 */
203function make_picture_url($params)
204{
205  global $conf;
206  if (!isset($params['image_id']))
207  {
208    die('make_picture_url: image_id is a required parameter');
209  }
210
211  $url = get_root_url().'picture';
212  if ($conf['php_extension_in_urls'])
213  {
214    $url .= '.php';
215  }
216  if ($conf['question_mark_in_urls'])
217  {
218    $url .= '?';
219  }
220  $url.= '/';
221  switch ( $conf['picture_url_style'] )
222  {
223    case 'id-file':
224      $url .= $params['image_id'];
225      if ( isset($params['image_file']) )
226      {
227        $url .= '-'.get_filename_wo_extension($params['image_file']);
228      }
229      break;
230    case 'file':
231      if ( isset($params['image_file']) )
232      {
233        $fname_wo_ext = get_filename_wo_extension($params['image_file']);
234        if (! preg_match('/^\d+(-|$)/', $fname_wo_ext) )
235        {
236          $url .= $fname_wo_ext;
237          break;
238        }
239      }
240    default:
241      $url .= $params['image_id'];
242  }
243  $url .= make_section_in_url($params);
244  $url = add_well_known_params_in_url($url, $params);
245  return $url;
246}
247
248/**
249 *adds to the url the chronology and start parameters
250*/
251function add_well_known_params_in_url($url, $params)
252{
253  if ( isset($params['chronology_field']) )
254  {
255    $url .= '/'. $params['chronology_field'];
256    $url .= '-'. $params['chronology_style'];
257    if ( isset($params['chronology_view']) )
258    {
259      $url .= '-'. $params['chronology_view'];
260    }
261    if ( !empty($params['chronology_date']) )
262    {
263      $url .= '-'. implode('-', $params['chronology_date'] );
264    }
265  }
266
267  if (isset($params['flat_recent_cat']) and $params['flat_recent_cat'] > 0)
268  {
269    $url.= '/flat_recent_cat-'.$params['flat_recent_cat'];
270  }
271
272  if (isset($params['start']) and $params['start'] > 0)
273  {
274    $url.= '/start-'.$params['start'];
275  }
276  return $url;
277}
278
279/**
280 * return the section token of an index or picture URL.
281 *
282 * Depending on section, other parameters are required (see function code
283 * for details)
284 *
285 * @param array
286 * @return string
287 */
288function make_section_in_url($params)
289{
290  global $conf;
291  $section_string = '';
292
293  $section_of = array(
294    'category' => 'categories',
295    'tags'     => 'tags',
296    'list'     => 'list',
297    'search'   => 'search',
298    );
299
300  foreach ($section_of as $param => $section)
301  {
302    if (isset($params[$param]))
303    {
304      $params['section'] = $section;
305    }
306  }
307
308  if (!isset($params['section']))
309  {
310    $params['section'] = 'categories';
311  }
312
313  switch($params['section'])
314  {
315    case 'categories' :
316    {
317      if (!isset($params['category']))
318      {
319        $section_string.= '/categories';
320      }
321      else
322      {
323        $section_string.= '/category/'.$params['category'];
324        if ($conf['category_url_style']=='id-name' and isset($params['cat_name']) )
325        {
326          if ( is_string($params['cat_name']) )
327          {
328            $section_string.= '-'.str2url($params['cat_name']);
329          }
330          elseif ( is_array( $params['cat_name'] ) and
331                isset( $params['cat_name'][$params['category']] ) )
332          {
333            $section_string.= '-'
334                .str2url($params['cat_name'][$params['category']]);
335          }
336        }
337      }
338
339      break;
340    }
341    case 'tags' :
342    {
343      if (!isset($params['tags']) or count($params['tags']) == 0)
344      {
345        die('make_section_in_url: require at least one tag');
346      }
347
348      $section_string.= '/tags';
349
350      foreach ($params['tags'] as $tag)
351      {
352        switch ( $conf['tag_url_style'] )
353        {
354          case 'id':
355            $section_string.= '/'.$tag['id'];
356            break;
357          case 'tag':
358            if (isset($tag['url_name']) and !is_numeric($tag['url_name']) )
359            {
360              $section_string.= '/'.$tag['url_name'];
361              break;
362            }
363          default:
364            $section_string.= '/'.$tag['id'];
365            if (isset($tag['url_name']))
366            {
367              $section_string.= '-'.$tag['url_name'];
368            }
369        }
370      }
371
372      break;
373    }
374    case 'search' :
375    {
376      if (!isset($params['search']))
377      {
378        die('make_section_in_url: require a search identifier');
379      }
380
381      $section_string.= '/search/'.$params['search'];
382
383      break;
384    }
385    case 'list' :
386    {
387      if (!isset($params['list']))
388      {
389        die('make_section_in_url: require a list of items');
390      }
391
392      $section_string.= '/list/'.implode(',', $params['list']);
393
394      break;
395    }
396    default :
397    {
398      $section_string.= '/'.$params['section'];
399    }
400  }
401
402  return $section_string;
403}
404
405/**
406 * Indicate to build url with full path
407 *
408 * @param null
409 * @return null
410 */
411function set_make_full_url()
412{
413  global $page;
414
415  if (!isset($page['save_root_path']))
416  {
417    if (isset($page['root_path']))
418    {
419      $page['save_root_path']['path'] = $page['root_path'];
420    }
421    $page['save_root_path']['count'] = 1;
422    $page['root_path'] = 'http://'.$_SERVER['HTTP_HOST'].cookie_path();
423  }
424  else
425  {
426    $page['save_root_path']['count'] += 1;
427  }
428}
429
430/**
431 * Restore old parameter to build url with full path
432 *
433 * @param null
434 * @return null
435 */
436function unset_make_full_url()
437{
438  global $page;
439
440  if (isset($page['save_root_path']))
441  {
442    if ($page['save_root_path']['count'] == 1)
443    {
444      if (isset($page['save_root_path']['path']))
445      {
446        $page['root_path'] = $page['save_root_path']['path'];
447      }
448      else
449      {
450        unset($page['root_path']);
451      }
452      unset($page['save_root_path']);
453    }
454    else
455    {
456      $page['save_root_path']['count'] -= 1;
457    }
458  }
459}
460
461?>
Note: See TracBrowser for help on using the repository browser.