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

Last change on this file since 1119 was 1119, checked in by plg, 18 years ago

improvement: tags replace keywords. Better data model, less
limitations. Each image can be associated to as many tag as needed. Tags can
contain non ASCII characters. Oriented navigation with tags by association.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 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-04-02 22:26:19 +0000 (Sun, 02 Apr 2006) $
9// | last modifier : $Author: plg $
10// | revision      : $Revision: 1119 $
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 * @return string
31 */
32function get_root_url()
33{
34  global $page;
35  if ( isset($page['root_path']) )
36  {
37    return $page['root_path'];
38  }
39  return PHPWG_ROOT_PATH;
40}
41
42/**
43 * adds one or more _GET style parameters to an url
44 * example: add_url_params('/x', array('a'=>'b')) returns /x?a=b
45 * add_url_params('/x?cat_id=10', array('a'=>'b')) returns /x?cat_id=10&amp;a=b
46 * @param string url
47 * @param array params
48 * @return string
49 */
50function add_url_params($url, $params)
51{
52  if ( !empty($params) )
53  {
54    assert( is_array($params) );
55    $is_first = true;
56    foreach($params as $param=>$val)
57    {
58      if ($is_first)
59      {
60        $is_first = false;
61        $url .= ( strstr($url, '?')===false ) ? '?' :'&amp;';
62      }
63      else
64      {
65        $url .= '&amp;';
66      }
67      $url .= $param;
68      if (isset($val))
69      {
70        $url .= '='.$val;
71      }
72    }
73  }
74  return $url;
75}
76
77/**
78 * build an index URL for a specific section
79 *
80 * @param array
81 * @return string
82 */
83function make_index_URL($params = array())
84{
85  global $conf;
86  $url = get_root_url().'index';
87  if ($conf['php_extension_in_urls'])
88  {
89    $url .= '.php';
90  }
91  if ($conf['question_mark_in_urls'])
92  {
93    $url .= '?';
94  }
95  $url.= make_section_in_URL($params);
96  $url = add_well_known_params_in_url($url, $params);
97  return $url;
98}
99
100/**
101 * build an index URL with current page parameters, but with redefinitions
102 * and removes.
103 *
104 * duplicate_index_URL(array('category' => 12), array('start')) will create
105 * an index URL on the current section (categories), but on a redefined
106 * category and without the start URL parameter.
107 *
108 * @param array redefined keys
109 * @param array removed keys
110 * @return string
111 */
112function duplicate_index_URL($redefined = array(), $removed = array())
113{
114  return make_index_URL(
115    params_for_duplication($redefined, $removed)
116    );
117}
118
119/**
120 * returns $page global array with key redefined and key removed
121 *
122 * @param array redefined keys
123 * @param array removed keys
124 * @return array
125 */
126function params_for_duplication($redefined, $removed)
127{
128  global $page;
129
130  if (count($removed) > 0)
131  {
132    $params = array();
133
134    foreach ($page as $page_item_key => $page_item_value)
135    {
136      if (!in_array($page_item_key, $removed))
137      {
138        $params[$page_item_key] = $page_item_value;
139      }
140    }
141  }
142  else
143  {
144    $params = $page;
145  }
146
147  foreach ($redefined as $redefined_param => $redefined_value)
148  {
149    $params[$redefined_param] = $redefined_value;
150  }
151
152  return $params;
153}
154
155/**
156 * create a picture URL with current page parameters, but with redefinitions
157 * and removes. See duplicate_index_URL.
158 *
159 * @param array redefined keys
160 * @param array removed keys
161 * @return string
162 */
163function duplicate_picture_URL($redefined = array(), $removed = array())
164{
165  return make_picture_URL(
166    params_for_duplication($redefined, $removed)
167    );
168}
169
170/**
171 * create a picture URL on a specific section for a specific picture
172 *
173 * @param array
174 * @return string
175 */
176function make_picture_URL($params)
177{
178  global $conf;
179  if (!isset($params['image_id']))
180  {
181    die('make_picture_URL: image_id is a required parameter');
182  }
183
184  $url = get_root_url().'picture';
185  if ($conf['php_extension_in_urls'])
186  {
187    $url .= '.php';
188  }
189  if ($conf['question_mark_in_urls'])
190  {
191    $url .= '?';
192  }
193  $url.= '/';
194  switch ( $conf['picture_url_style'] )
195  {
196    case 'id-file':
197      $url .= $params['image_id'];
198      if ( isset($params['image_file']) )
199      {
200        $url .= '-'.get_filename_wo_extension($params['image_file']);
201      }
202      break;
203    case 'file':
204      if ( isset($params['image_file'])
205           and !is_numeric($params['image_file']) )
206      {
207        $url .= get_filename_wo_extension($params['image_file']);
208      }
209      else
210      {
211        $url .= $params['image_id'];
212      }
213      break;
214    default:
215      $url .= $params['image_id'];
216  }
217  $url .= make_section_in_URL($params);
218  $url = add_well_known_params_in_url($url, $params);
219  return $url;
220}
221
222/**
223 *adds to the url the chronology and start parameters
224*/
225function add_well_known_params_in_url($url, $params)
226{
227  if ( isset($params['chronology_field']) )
228  {
229    $url .= '/'. $params['chronology_field'];
230    $url .= '-'. $params['chronology_style'];
231    if ( isset($params['chronology_view']) )
232    {
233      $url .= '-'. $params['chronology_view'];
234    }
235    if ( !empty($params['chronology_date']) )
236    {
237      $url .= '-'. implode('-', $params['chronology_date'] );
238    }
239  }
240
241  if (isset($params['start']) and $params['start'] > 0)
242  {
243    $url.= '/start-'.$params['start'];
244  }
245  return $url;
246}
247
248/**
249 * return the section token of an index or picture URL.
250 *
251 * Depending on section, other parameters are required (see function code
252 * for details)
253 *
254 * @param array
255 * @return string
256 */
257function make_section_in_URL($params)
258{
259  $section_string = '';
260
261  $section_of = array(
262    'category' => 'categories',
263    'tags'     => 'tags',
264    'list'     => 'list',
265    'search'   => 'search',
266    );
267
268  foreach ($section_of as $param => $section)
269  {
270    if (isset($params[$param]))
271    {
272      $params['section'] = $section;
273    }
274  }
275
276  if (!isset($params['section']))
277  {
278    $params['section'] = 'categories';
279  }
280
281  switch($params['section'])
282  {
283    case 'categories' :
284    {
285      if (!isset($params['category']))
286      {
287        $section_string.= '/categories';
288      }
289      else
290      {
291        $section_string.= '/category/'.$params['category'];
292      }
293
294      break;
295    }
296    case 'tags' :
297    {
298      if (!isset($params['tags']) or count($params['tags']) == 0)
299      {
300        die('make_section_in_URL: require at least one tag');
301      }
302
303      $section_string.= '/tags';
304
305      foreach ($params['tags'] as $tag)
306      {
307        $section_string.= '/'.$tag['id'];
308       
309        if (isset($tag['url_name']))
310        {
311          $section_string.= '-'.$tag['url_name'];
312        }
313      }
314
315      break;
316    }
317    case 'search' :
318    {
319      if (!isset($params['search']))
320      {
321        die('make_section_in_URL: require a search identifier');
322      }
323
324      $section_string.= '/search/'.$params['search'];
325
326      break;
327    }
328    case 'list' :
329    {
330      if (!isset($params['list']))
331      {
332        die('make_section_in_URL: require a list of items');
333      }
334
335      $section_string.= '/list/'.implode(',', $params['list']);
336
337      break;
338    }
339    default :
340    {
341      $section_string.= '/'.$params['section'];
342    }
343  }
344
345  return $section_string;
346}
347?>
Note: See TracBrowser for help on using the repository browser.