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 handle changes made to a group
23 * @package GalleryCore
24 * @subpackage UserInterface
25 * @author Bharat Mediratta <bharat@menalto.com>
26 * @version $Revision: 17580 $
27 */
28class AdminEditGroupController extends GalleryController {
29
30    /**
31     * @see GalleryController::handleRequest
32     */
33    function handleRequest($form) {
34	$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
35	if ($ret) {
36	    return array($ret, null);
37	}
38
39	$results = $status = $error = array();
40	$groupId = GalleryUtilities::getRequestVariables('groupId');
41	list ($ret, $group) = GalleryCoreApi::loadEntitiesById($groupId, 'GalleryGroup');
42	if ($ret) {
43	    return array($ret, null);
44	}
45
46	if (isset($form['action']['cancel'])) {
47
48	    /* Redirect back to the AdminGroups view */
49	    $redirect['view'] = 'core.SiteAdmin';
50	    $redirect['subView'] = 'core.AdminGroups';
51
52	} else if (isset($form['action']['undo'])) {
53
54	    /* Redirect back to the same view to reset the form */
55	    $redirect['view'] = 'core.SiteAdmin';
56	    $redirect['subView'] = 'core.AdminEditGroup';
57	    $redirect['groupId'] = $groupId;
58
59	} else if (isset($form['action']['save'])) {
60
61	    if (empty($form['groupName'])) {
62		$error[] = 'form[error][groupName][missing]';
63	    } else {
64		list ($ret, $tmpGroup) = GalleryCoreApi::fetchGroupByGroupname($form['groupName']);
65		if ($ret && !($ret->getErrorCode() & ERROR_MISSING_OBJECT)) {
66		    return array($ret, null);
67		}
68
69		if (!empty($tmpGroup) && $tmpGroup->getId() != $groupId) {
70		    $error[] = 'form[error][groupName][exists]';
71		}
72	    }
73
74	    if (empty($error)) {
75		/* Save our changes */
76		list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($groupId);
77		if ($ret) {
78		    return array($ret, null);
79		}
80
81		/* Refresh our object, now that it's locked */
82		list ($ret, $group) = $group->refresh();
83		if ($ret) {
84		    return array($ret, null);
85		}
86
87		/* Enact the change */
88		$group->setGroupName($form['groupName']);
89		$ret = $group->save();
90		if ($ret) {
91		    return array($ret, null);
92		}
93
94		$ret = GalleryCoreApi::releaseLocks($lockId);
95		if ($ret) {
96		    return array($ret, null);
97		}
98
99		/* Request a redirect to the confirmation screen */
100		$redirect['view'] = 'core.SiteAdmin';
101		$redirect['subView'] = 'core.AdminGroups';
102		$status['modifiedGroup'] = $group->getGroupName();
103	    }
104	}
105
106	if (!empty($redirect)) {
107	    $results['redirect'] = $redirect;
108	} else {
109	    $results['delegate']['view'] = 'core.SiteAdmin';
110	    $results['delegate']['subView'] = 'core.AdminEditGroup';
111	}
112	$results['status'] = $status;
113	$results['error'] = $error;
114
115	return array(null, $results);
116    }
117}
118
119/**
120 * This view will show options available to groups
121 */
122class AdminEditGroupView extends GalleryView {
123
124    /**
125     * @see GalleryView::loadTemplate
126     */
127    function loadTemplate(&$template, &$form) {
128	$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
129	if ($ret) {
130	    return array($ret, null);
131	}
132
133	$groupId = GalleryUtilities::getRequestVariables('groupId');
134	list ($ret, $group) = GalleryCoreApi::loadEntitiesById($groupId, 'GalleryGroup');
135	if ($ret) {
136	    return array($ret, null);
137	}
138
139	if ($form['formName'] != 'AdminEditGroup') {
140	    /* First time around initialize our form */
141	    $form['groupName'] = $group->getGroupName();
142	    $form['formName'] = 'AdminEditGroup';
143	}
144
145	$AdminEditGroup = array();
146	$AdminEditGroup['group'] = (array)$group;
147	$template->setVariable('AdminEditGroup', $AdminEditGroup);
148	$template->setVariable('controller', 'core.AdminEditGroup');
149
150	return array(null, array('body' => 'modules/core/templates/AdminEditGroup.tpl'));
151    }
152}
153?>
154