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 * This controller will make an item the highlight for its parent
23 * @package GalleryCore
24 * @subpackage UserInterface
25 * @author Ernesto Baschny <ernst@baschny.de>
26 * @version $Revision: 17580 $
27 */
28class ItemMakeHighlightController extends GalleryController {
29
30    /**
31     * @see GalleryController::handleRequest
32     */
33    function handleRequest($form) {
34	$status = array();
35	$error = array();
36
37	list ($ret, $item) = $this->getItem();
38	if ($ret) {
39	    return array($ret, null);
40	}
41	$itemId = $item->getId();
42
43	if (isset($form['action']['makeHighlight'])) {
44	    if (empty($form['parentId'])) {
45		return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null);
46	    }
47	    $parentId = (int)$form['parentId'];
48
49	    list ($ret, $canView) = GalleryCoreApi::hasItemPermission($parentId, 'core.view');
50	    if ($ret) {
51		return array($ret, null);
52	    }
53	    if (!$canView) {
54		/* Avoid information disclosure, act as if the item didn't exist. */
55		return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT), null);
56	    }
57
58	    /* Make sure we have permission to edit this item */
59	    $ret = GalleryCoreApi::assertHasItemPermission($parentId, 'core.edit');
60	    if ($ret) {
61		return array($ret, null);
62	    }
63
64	    /* XXX: What should we do if this fails? */
65	    list ($ret, $success) =
66		GalleryCoreApi::setThumbnailFromItem($parentId, $itemId);
67	    if ($ret) {
68		return array($ret, null);
69	    }
70	} /* else $form['action']['cancel'] */
71
72	$results['return'] = true;
73	$results['status'] = $status;
74	$results['error'] = $error;
75
76	return array(null, $results);
77    }
78}
79
80/**
81 * This view will prompt for which ancestor album to set the highlight
82 */
83class ItemMakeHighlightView extends GalleryView {
84
85    /**
86     * @see GalleryView::loadTemplate
87     */
88    function loadTemplate(&$template, &$form) {
89	list ($ret, $item) = $this->getItem();
90	if ($ret) {
91	    return array($ret, null);
92	}
93	$itemId = $item->getId();
94	$parentId = $item->getParentId();
95
96	/* Make sure we have permission do edit the parent of this item */
97	$ret = GalleryCoreApi::assertHasItemPermission($parentId, 'core.edit');
98	if ($ret) {
99	    return array($ret, null);
100	}
101
102	if ($form['formName'] != 'ItemMakeHighlight') {
103	    $form['formName'] = 'ItemMakeHighlight';
104	}
105
106	list ($ret, $parents) =
107	    GalleryCoreApi::fetchParents($item, array('core.edit', 'core.view'), true);
108	if ($ret) {
109	    return array($ret, null);
110	}
111
112	$parentList = array();
113	foreach (array_reverse($parents) as $parent) {
114	    $parentList[] = (array)$parent;
115	}
116
117	$template->setVariable('ItemMakeHighlight', array('parentList' => $parentList));
118	$template->setVariable('controller', 'core.ItemMakeHighlight');
119	return array(null, array('body' => 'modules/core/templates/ItemMakeHighlight.tpl'));
120    }
121
122    /**
123     * @see GalleryView::getViewDescription
124     */
125    function getViewDescription() {
126	list ($ret, $core) = GalleryCoreApi::loadPlugin('module', 'core');
127	if ($ret) {
128	    return array($ret, null);
129	}
130
131	return array(null, $core->translate('make highlight'));
132    }
133}
134?>
135