source: trunk/include/section_init.inc.php @ 1130

Last change on this file since 1130 was 1125, checked in by rvelices, 19 years ago

image sort order for tags

very minor code and language simplifications and corrections

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 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-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $Id: section_init.inc.php 1125 2006-04-05 02:01:36Z rvelices $
9// | last update   : $Date: 2006-04-05 02:01:36 +0000 (Wed, 05 Apr 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1125 $
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
28/**
29 * This included page checks section related parameter and provides
30 * following informations:
31 *
32 * - $page['title']
33 *
34 * - $page['items']: ordered list of items to display
35 *
36 * - $page['cat_nb_images']: number of items in the section (should be equal
37 * to count($page['items']))
38 *
39 * - $page['thumbnails_include']: include page managing thumbnails to
40 * display
41 */
42
43// "index.php?/category/12-foo/start-24&action=fill_caddie" or
44// "index.php/category/12-foo/start-24&action=fill_caddie"
45// must return :
46//
47// array(
48//   'section'  => 'categories',
49//   'category' => 12,
50//   'start'    => 24
51//   'action'   => 'fill_caddie'
52//   );
53
54$page['section'] = 'categories';
55
56if ( isset($_SERVER["PATH_INFO"]) )
57{
58  $rewritten = $_SERVER["PATH_INFO"];
59  $rewritten = str_replace('//', '/', $rewritten);
60  $path_count = count( explode('/', $rewritten) );
61  $page['root_path'] = PHPWG_ROOT_PATH.str_repeat('../', $path_count-1);
62}
63else
64{
65  $rewritten = '';
66  foreach (array_keys($_GET) as $keynum => $key)
67  {
68    $rewritten = $key;
69    break;
70  }
71  $page['root_path'] = PHPWG_ROOT_PATH;
72}
73//phpinfo();
74// deleting first "/" if displayed
75$tokens = explode(
76  '/',
77  preg_replace('#^/#', '', $rewritten)
78  );
79// $tokens = array(
80//   0 => category,
81//   1 => 12-foo,
82//   2 => start-24
83//   );
84
85$next_token = 0;
86if (basename($_SERVER['SCRIPT_FILENAME']) == 'picture.php')
87{ // the first token must be the identifier for the picture
88  if ( isset($_GET['image_id'])
89       and isset($_GET['cat']) and is_numeric($_GET['cat']) )
90  {// url compatibility with versions below 1.6
91    $url = make_picture_url( array(
92        'section' => 'categories',
93        'category' => $_GET['cat'],
94        'image_id' => $_GET['image_id']
95      ) );
96    redirect($url);
97  }
98  $token = $tokens[$next_token];
99  $next_token++;
100  if ( is_numeric($token) )
101  {
102    $page['image_id'] = $token;
103  }
104  else
105  {
106    preg_match('/^(\d+-)?(.*)?$/', $token, $matches);
107    if (isset($matches[1]) and is_numeric($matches[1]=rtrim($matches[1],'-')) )
108    {
109      $page['image_id'] = $matches[1];
110      if ( !empty($matches[2]) )
111      {
112        $page['image_file'] = $matches[2];
113      }
114
115    }
116    else
117    {
118      if ( !empty($matches[2]) )
119      {
120        $page['image_file'] = $matches[2];
121      }
122      else
123      {
124        die('Fatal: picture identifier is missing');
125      }
126    }
127  }
128}
129
130if (0 === strpos($tokens[$next_token], 'categor'))
131{
132  $page['section'] = 'categories';
133  $next_token++;
134
135  if (isset($tokens[$next_token])
136      and preg_match('/^(\d+)/', $tokens[$next_token], $matches))
137  {
138    $page['category'] = $matches[1];
139    $next_token++;
140  }
141}
142else if (0 === strpos($tokens[$next_token], 'tag'))
143{
144  $page['section'] = 'tags';
145  $page['tags'] = array();
146
147  $next_token++;
148  $i = $next_token;
149
150  while (isset($tokens[$i]))
151  {
152    preg_match('/^(\d+)(?:-(.*))?/', $tokens[$i], $matches);
153    if (!isset($matches[1]))
154    {
155      if (0 == count($page['tags']))
156      {
157        die('Fatal: at least one tag required');
158      }
159      else
160      {
161        break;
162      }
163    }
164
165    array_push(
166      $page['tags'],
167      array(
168        'id'       => $matches[1],
169        'url_name' => isset($matches[2]) ? $matches[2] : '',
170        )
171      );
172
173    $i++;
174  }
175
176  $next_token = $i;
177}
178else if (0 === strpos($tokens[$next_token], 'fav'))
179{
180  $page['section'] = 'favorites';
181  $next_token++;
182}
183else if ('most_visited' == $tokens[$next_token])
184{
185  $page['section'] = 'most_visited';
186  $next_token++;
187}
188else if ('best_rated' == $tokens[$next_token])
189{
190  $page['section'] = 'best_rated';
191  $next_token++;
192}
193else if ('recent_pics' == $tokens[$next_token])
194{
195  $page['section'] = 'recent_pics';
196  $next_token++;
197}
198else if ('recent_cats' == $tokens[$next_token])
199{
200  $page['section'] = 'recent_cats';
201  $next_token++;
202}
203else if ('search' == $tokens[$next_token])
204{
205  $page['section'] = 'search';
206  $next_token++;
207
208  preg_match('/(\d+)/', $tokens[$next_token], $matches);
209  if (!isset($matches[1]))
210  {
211    die('Fatal: search identifier is missing');
212  }
213  $page['search'] = $matches[1];
214  $next_token++;
215}
216else if ('list' == $tokens[$next_token])
217{
218  $page['section'] = 'list';
219  $next_token++;
220
221  $page['list'] = array();
222  if (!preg_match('/^\d+(,\d+)*$/', $tokens[$next_token]))
223  {
224    die('wrong format on list GET parameter');
225  }
226  foreach (explode(',', $tokens[$next_token]) as $image_id)
227  {
228    array_push($page['list'], $image_id);
229  }
230  $next_token++;
231}
232
233$i = $next_token;
234
235while (isset($tokens[$i]))
236{
237  if (preg_match('/^start-(\d+)/', $tokens[$i], $matches))
238  {
239    $page['start'] = $matches[1];
240  }
241
242  if (preg_match('/^posted|created/', $tokens[$i] ))
243  {
244    $chronology_tokens = explode('-', $tokens[$i] );
245
246    $page['chronology_field'] = $chronology_tokens[0];
247
248    array_shift($chronology_tokens);
249    $page['chronology_style'] = $chronology_tokens[0];
250
251    array_shift($chronology_tokens);
252    if ( count($chronology_tokens)>0 )
253    {
254      if ('list'==$chronology_tokens[0] or
255          'calendar'==$chronology_tokens[0])
256      {
257        $page['chronology_view'] = $chronology_tokens[0];
258        array_shift($chronology_tokens);
259      }
260      $page['chronology_date'] = $chronology_tokens;
261    }
262  }
263
264  $i++;
265}
266
267
268// $page['nb_image_page'] is the number of picture to display on this page
269// By default, it is the same as the $user['nb_image_page']
270$page['nb_image_page'] = $user['nb_image_page'];
271
272if (isset($_COOKIE['pwg_image_order'])
273    and is_numeric($_COOKIE['pwg_image_order'])
274    and $_COOKIE['pwg_image_order'] > 0)
275{
276  $orders = get_category_preferred_image_orders();
277
278  $conf['order_by'] = str_replace(
279    'ORDER BY ',
280    'ORDER BY '.$orders[ $_COOKIE['pwg_image_order'] ][1].',',
281    $conf['order_by']
282    );
283  $page['super_order_by'] = true;
284}
285
286// +-----------------------------------------------------------------------+
287// |                              category                                 |
288// +-----------------------------------------------------------------------+
289if ('categories' == $page['section'])
290{
291  if (isset($page['category']))
292  {
293    $result = get_cat_info($page['category']);
294
295    $page = array_merge(
296      $page,
297      array(
298        'comment'          => $result['comment'],
299        'cat_dir'          => $result['dir'],
300        'cat_name'         => $result['name'],
301        'cat_site_id'      => $result['site_id'],
302        'cat_uploadable'   => $result['uploadable'],
303        'cat_commentable'  => $result['commentable'],
304        'cat_id_uppercat'  => $result['id_uppercat'],
305        'uppercats'        => $result['uppercats'],
306
307        'title' => get_cat_display_name($result['name'], '', false),
308        )
309      );
310
311    if (!isset($page['chronology_field']))
312    {
313      $query = '
314SELECT image_id
315  FROM '.IMAGE_CATEGORY_TABLE.'
316    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
317  WHERE category_id = '.$page['category'].'
318  '.$conf['order_by'].'
319;';
320      $page['items'] = array_from_query($query, 'image_id');
321
322      $page['thumbnails_include'] =
323        $result['nb_images'] > 0
324        ? 'include/category_default.inc.php'
325        : 'include/category_subcats.inc.php';
326    } //otherwise the calendar will requery all subitems
327  }
328  else
329  {
330    $page['title'] = $lang['no_category'];
331    $page['thumbnails_include'] = 'include/category_subcats.inc.php';
332  }
333}
334// special sections
335else
336{
337  if (!empty($user['forbidden_categories']))
338  {
339    $forbidden =
340      ' category_id NOT IN ('.$user['forbidden_categories'].')';
341  }
342  else
343  {
344    $forbidden = ' 1 = 1';
345  }
346// +-----------------------------------------------------------------------+
347// |                            tags section                               |
348// +-----------------------------------------------------------------------+
349  if ($page['section'] == 'tags')
350  {
351    $page['tag_ids'] = array();
352    foreach ($page['tags'] as $tag)
353    {
354      array_push($page['tag_ids'], $tag['id']);
355    }
356
357    $items = get_image_ids_for_tags($page['tag_ids']);
358
359    // permissions depends on category, so to only keep images that are
360    // reachable to the connected user, we need to check category
361    // associations
362    if (!empty($user['forbidden_categories']) and !empty($items) )
363    {
364      $query = '
365SELECT image_id
366  FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id=id
367  WHERE image_id IN ('.implode(',', $items).')
368    AND '.$forbidden.
369    $conf['order_by'].'
370;';
371      $items = array_unique(
372        array_from_query($query, 'image_id')
373        );
374    }
375
376    // tag names
377    $query = '
378SELECT name, url_name, id
379  FROM '.TAGS_TABLE.'
380  WHERE id IN ('.implode(',', $page['tag_ids']).')
381;';
382    $result = pwg_query($query);
383    $tag_infos = array();
384
385    while ($row = mysql_fetch_array($result))
386    {
387      $tag_infos[ $row['id'] ]['name'] = $row['name'];
388      $tag_infos[ $row['id'] ]['url_name'] = $row['url_name'];
389    }
390
391    $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
392    $title.= ' ';
393
394    $tag_num = 1;
395    foreach ($page['tag_ids'] as $tag_id)
396    {
397      $title.=
398        ($tag_num++ > 1 ? ' + ' : '')
399        .'<a href="'
400        .make_index_url(
401          array(
402            'tags' => array(
403              array(
404                'id' => $tag_id,
405                'url_name' => $tag_infos[$tag_id]['url_name'],
406                ),
407              )
408            )
409          )
410        .'">'
411        .$tag_infos[$tag_id]['name']
412        .'</a>';
413    }
414
415    $page = array_merge(
416      $page,
417      array(
418        'title' => $title,
419        'items' => array_values($items),
420        'thumbnails_include' => 'include/category_default.inc.php',
421        )
422      );
423  }
424// +-----------------------------------------------------------------------+
425// |                           search section                              |
426// +-----------------------------------------------------------------------+
427  if ($page['section'] == 'search')
428  {
429    include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
430
431    $search_items = get_search_items($page['search']);
432    if ( !empty($search_items) )
433    {
434      $query = '
435SELECT DISTINCT(id)
436  FROM '.IMAGES_TABLE.'
437    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
438  WHERE id IN ('.implode(',', $search_items).')
439    AND '.$forbidden.'
440  '.$conf['order_by'].'
441;';
442      $page['items'] = array_from_query($query, 'id');
443    }
444    else
445    {
446      $page['items'] = array();
447    }
448
449    $page = array_merge(
450      $page,
451      array(
452        'title' => $lang['search_result'],
453        'thumbnails_include' => 'include/category_default.inc.php',
454        )
455      );
456  }
457// +-----------------------------------------------------------------------+
458// |                           favorite section                            |
459// +-----------------------------------------------------------------------+
460  else if ($page['section'] == 'favorites')
461  {
462    check_user_favorites();
463
464    $query = '
465SELECT image_id
466  FROM '.FAVORITES_TABLE.'
467    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
468  WHERE user_id = '.$user['id'].'
469  '.$conf['order_by'].'
470;';
471
472    $page = array_merge(
473      $page,
474      array(
475        'title' => $lang['favorites'],
476        'items' => array_from_query($query, 'image_id'),
477        'thumbnails_include' => 'include/category_default.inc.php',
478        )
479      );
480  }
481// +-----------------------------------------------------------------------+
482// |                       recent pictures section                         |
483// +-----------------------------------------------------------------------+
484  else if ($page['section'] == 'recent_pics')
485  {
486    $query = '
487SELECT DISTINCT(id)
488  FROM '.IMAGES_TABLE.'
489    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
490  WHERE date_available > \''.
491      date('Y-m-d', time() - 60*60*24*$user['recent_period']).'\'
492    AND '.$forbidden.'
493  '.$conf['order_by'].'
494;';
495
496    $page = array_merge(
497      $page,
498      array(
499        'title' => $lang['recent_pics_cat'],
500        'items' => array_from_query($query, 'id'),
501        'thumbnails_include' => 'include/category_default.inc.php',
502        )
503      );
504  }
505// +-----------------------------------------------------------------------+
506// |                 recently updated categories section                   |
507// +-----------------------------------------------------------------------+
508  else if ($page['section'] == 'recent_cats')
509  {
510    $page = array_merge(
511      $page,
512      array(
513        'title' => $lang['recent_cats_cat'],
514        'thumbnails_include' => 'include/category_recent_cats.inc.php',
515        )
516      );
517  }
518// +-----------------------------------------------------------------------+
519// |                        most visited section                           |
520// +-----------------------------------------------------------------------+
521  else if ($page['section'] == 'most_visited')
522  {
523    $page['super_order_by'] = true;
524    $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
525    $query = '
526SELECT DISTINCT(id)
527  FROM '.IMAGES_TABLE.'
528    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
529  WHERE hit > 0
530    AND '.$forbidden.'
531    '.$conf['order_by'].'
532  LIMIT 0, '.$conf['top_number'].'
533;';
534
535    $page = array_merge(
536      $page,
537      array(
538        'title' => $conf['top_number'].' '.$lang['most_visited_cat'],
539        'items' => array_from_query($query, 'id'),
540        'thumbnails_include' => 'include/category_default.inc.php',
541        )
542      );
543  }
544// +-----------------------------------------------------------------------+
545// |                          best rated section                           |
546// +-----------------------------------------------------------------------+
547  else if ($page['section'] == 'best_rated')
548  {
549    $page['super_order_by'] = true;
550    $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
551
552    $query ='
553SELECT DISTINCT(id)
554  FROM '.IMAGES_TABLE.'
555    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
556  WHERE average_rate IS NOT NULL
557    AND '.$forbidden.'
558    '.$conf['order_by'].'
559  LIMIT 0, '.$conf['top_number'].'
560;';
561    $page = array_merge(
562      $page,
563      array(
564        'title' => $conf['top_number'].' '.$lang['best_rated_cat'],
565        'items' => array_from_query($query, 'id'),
566        'thumbnails_include' => 'include/category_default.inc.php',
567        )
568      );
569  }
570// +-----------------------------------------------------------------------+
571// |                             list section                              |
572// +-----------------------------------------------------------------------+
573  else if ($page['section'] == 'list')
574  {
575    $query ='
576SELECT DISTINCT(id)
577  FROM '.IMAGES_TABLE.'
578    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
579  WHERE image_id IN ('.implode(',', $page['list']).')
580    AND '.$forbidden.'
581  '.$conf['order_by'].'
582;';
583
584    $page = array_merge(
585      $page,
586      array(
587        'title' => $lang['random_cat'],
588        'items' => array_from_query($query, 'id'),
589        'thumbnails_include' => 'include/category_default.inc.php',
590        )
591      );
592  }
593}
594
595// +-----------------------------------------------------------------------+
596// |                             chronology                                |
597// +-----------------------------------------------------------------------+
598
599if (isset($page['chronology_field']))
600{
601  include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' );
602  initialize_calendar();
603}
604
605$page['cat_nb_images'] = isset($page['items']) ? count($page['items']) : 0;
606
607if (basename($_SERVER['SCRIPT_FILENAME']) == 'picture.php'
608    and !isset($page['image_id']) )
609{
610  if ( !empty($page['items']) )
611  {
612    $query = '
613SELECT id,file
614  FROM '.IMAGES_TABLE .'
615  WHERE id IN ('.implode(',',$page['items']).')
616  AND file LIKE "' . $page['image_file'] . '.%" ESCAPE "|"'
617;
618    $result = pwg_query($query);
619    if (mysql_num_rows($result)>0)
620    {
621      list($page['image_id'], $page['image_file']) = mysql_fetch_row($result);
622    }
623  }
624  if ( !isset($page['image_id']) )
625  {
626    $page['image_id'] = -1; // will fail in picture.php
627  }
628}
629?>
Note: See TracBrowser for help on using the repository browser.