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-like view will handle the deletion of a single item
23 * Note (Dec. 2006): this functionality has been rewritten using AJAX
24 * @package GalleryCore
25 * @subpackage UserInterface
26 * @author Bharat Mediratta <bharat@menalto.com>
27 * @version $Revision: 17580 $
28 */
29class ItemDeleteSingleController extends GalleryController {
30
31    /**
32     * @see GalleryController::handleRequest
33     */
34    function handleRequest($form) {
35	global $gallery;
36	$urlGenerator =& $gallery->getUrlGenerator();
37
38	list ($pageItemId, $page) =
39	    GalleryUtilities::getRequestVariables('pageId', 'page');
40
41	list ($ret, $item) = $this->getItem();
42	if ($ret) {
43	    return array($ret, null);
44	}
45	$itemId = $item->getId();
46
47	/* The framework shouldn't let us get this far if we don't have delete permission */
48	$ret = GalleryCoreApi::assertHasItemPermission($itemId, 'core.delete');
49	if ($ret) {
50	    return array($ret, null);
51	}
52
53	/* Get the parent *before* we delete the item */
54	$parentId = $item->getParentId();
55
56	/* Get the root album id, so we don't try to delete it */
57	list ($ret, $rootId) =
58	    GalleryCoreApi::getPluginParameter('module', 'core', 'id.rootAlbum');
59	if ($ret) {
60	    return array($ret, null);
61	}
62
63	/* The view shouldn't let us try to delete the root album, either */
64	if ($itemId == $rootId) {
65	    return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
66					 "Can't delete the root album"), null);
67	}
68
69	$ret = GalleryCoreApi::deleteEntityById($itemId, 'GalleryItem');
70	if ($ret) {
71	    return array($ret, null);
72	}
73
74	list ($ret, $unused) = GalleryCoreApi::guaranteeAlbumHasThumbnail($parentId);
75	if ($ret) {
76	    return array($ret, null);
77	}
78
79	/**
80	 * @todo: once there is a common status message, use it to provide feedback on success
81	 */
82	$params = array('view' => 'core.ShowItem',
83	      'itemId' => ($itemId == $pageItemId ? $parentId : $pageItemId));
84	if (!empty($page)) {
85	    $params['page'] = $page;
86	}
87	$result['redirect'] = $params;
88	$result['status'] = array();
89	$result['error'] = array();
90	return array(null, $result);
91    }
92}
93?>
94