1<?php
2/*
3 * Gallery - a web based photo album viewer and editor
4 * Copyright (C) 2000-2008 Bharat Mediratta
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21/**
22 * Handle the rendering of an album or item.
23 * @package GalleryCore
24 * @subpackage UserInterface
25 * @author Bharat Mediratta <bharat@menalto.com>
26 * @version $Revision: 17580 $
27 */
28class ShowItemController extends GalleryController {
29
30    /**
31     * @see GalleryController::handleRequest
32     */
33    function handleRequest($form) {
34	global $gallery;
35
36	/*
37	 * Note that this always changes user preview mode; if we add management of other variables
38	 * to this controller then we should guard this properly.  (Maybe delete this comment after
39	 * writing the unit test that verifies it)
40	 */
41	$guestPreviewMode = GalleryUtilities::getRequestVariables('guestPreviewMode');
42	if ($guestPreviewMode != null) {
43	    $session =& $gallery->getSession();
44	    $session->put('theme.guestPreviewMode', $guestPreviewMode ? 1 : 0);
45	}
46
47	return array(null, array('return' => 1, 'status' => array(), 'error' => array()));
48    }
49}
50
51/**
52 * Handle the rendering of an album or item.
53 */
54class ShowItemView extends GalleryView {
55
56    /**
57     * @see GalleryView::loadTemplate
58     */
59    function loadTemplate(&$template, &$form) {
60	global $gallery;
61	list ($ret, $item, $wasSpecified) = $this->getItem(true);
62	if ($ret) {
63	    return array($ret, null);
64	}
65
66	if (isset($_GET[GalleryUtilities::prefixFormVariable('path')]) && !$wasSpecified) {
67	    /*
68	     * Bug #1468797
69	     * Detect use of rewritten URL but rewrite module inactive; walk up to find main.php
70	     */
71	    $urlGenerator =& $gallery->getUrlGenerator();
72	    $redirect = dirname($urlGenerator->getCurrentUrlDir()) . '/' . GALLERY_MAIN_PHP;
73	    return array(null, array('redirectUrl' => $redirect));
74	}
75
76	/*
77	 * Don't increment the view count for anything but the first page of the album so that we
78	 * don't count each individual page views as an album view.  This only applies to
79	 * non-persistent sessions because GalleryCoreApi::incrementItemViewCount does the right
80	 * thing for persistent sessions.
81	 */
82	if (GalleryUtilities::isA($item, 'GalleryAlbumItem')) {
83	    $session =& $gallery->getSession();
84	    if (!$session->isPersistent()) {
85		list ($page, $highlightId) =
86		    GalleryUtilities::getRequestVariables('page', 'highlightId');
87		if ($highlightId || ($page && $page != 1)) {
88		    return array(null, null);
89		}
90	    }
91	}
92
93	$ret = GalleryCoreApi::incrementItemViewCount($item->getId());
94	if ($ret) {
95	    return array($ret, null);
96	}
97
98	return array(null, array());
99    }
100
101    /**
102     * @see GalleryView::getItem
103     */
104    function getItem($getOriginalSpecified=false) {
105	static $originalSpecified;
106	list ($ret, $item, $wasSpecified) = parent::getItem();
107	if ($ret) {
108	    return array($ret, null, null);
109	}
110	if (!isset($originalSpecified)) {
111	    /* Save value for first call; $wasSpecified always true later as itemId is set below */
112	    $originalSpecified = $wasSpecified;
113	}
114	if (!$wasSpecified) {
115	    GalleryUtilities::putRequestVariable('itemId', $item->getId());
116	}
117	/* Default wasSpecified=true so without itemId we still use root album theme/params */
118	return array(null, $item, $getOriginalSpecified ? $originalSpecified : true);
119    }
120
121    /**
122     * @see GalleryView::getViewDescription
123     */
124    function getViewDescription() {
125	list ($ret, $item) = $this->getItem();
126	if ($ret) {
127	    return array($ret, null);
128	}
129
130	$typeName = $item->itemTypeName(true);
131	return array(null, $typeName[1]);
132    }
133
134    /**
135     * @see GalleryView::getViewType
136     */
137    function getViewType() {
138	return VIEW_TYPE_SHOW_ITEM;
139    }
140}
141?>
142