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

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

feature 169: each category can have its own image order

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