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 the creation of a group
23 * @package GalleryCore
24 * @subpackage UserInterface
25 * @author Bharat Mediratta <bharat@menalto.com>
26 * @version $Revision: 17580 $
27 */
28class AdminCreateGroupController 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	if (isset($form['action']['cancel'])) {
41
42	    /* Go back to the AdminGroups view */
43	    $redirect['view'] = 'core.SiteAdmin';
44	    $redirect['subView'] = 'core.AdminGroups';
45
46	} else if (isset($form['action']['create'])) {
47	    /*
48	     * If all the right fields are in place then go ahead and
49	     * create the group.
50	     */
51	    if (!empty($form['groupName'])) {
52		list ($ret, $group) =
53		    GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryGroup');
54		if ($ret) {
55		    return array($ret, null);
56		}
57
58		if (!isset($group)) {
59		    return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT),
60				 null);
61		}
62
63		$ret = $group->create($form['groupName']);
64		if ($ret) {
65		    if (!($ret->getErrorCode() & ERROR_COLLISION)) {
66			return array($ret, null);
67		    }
68
69		    // Set our error status and fall back to the view.
70		    $error[] = 'form[error][groupName][exists]';
71		} else {
72		    $ret = $group->save();
73		    if ($ret) {
74			return array($ret, null);
75		    }
76
77		    /* Request a redirect to the confirmation screen */
78		    $redirect['view'] = 'core.SiteAdmin';
79		    $redirect['subView'] = 'core.AdminGroups';
80		    $status['createdGroup'] = $group->getGroupName();
81		}
82	    } else {
83		// Set our error status and fall back to the view.
84		$error[] = 'form[error][groupName][missing]';
85	    }
86	}
87
88	if (!empty($redirect)) {
89	    $results['redirect'] = $redirect;
90	} else {
91	    $results['delegate']['view'] = 'core.SiteAdmin';
92	    $results['delegate']['subView'] = 'core.AdminCreateGroup';
93	}
94	$results['status'] = $status;
95	$results['error'] = $error;
96
97	return array(null, $results);
98    }
99}
100
101/**
102 * This view will prompt for data to create a new group
103 */
104class AdminCreateGroupView extends GalleryView {
105
106    /**
107     * @see GalleryView::loadTemplate
108     */
109    function loadTemplate(&$template, &$form) {
110	$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
111	if ($ret) {
112	    return array($ret, null);
113	}
114
115	if ($form['formName'] != 'AdminCreateGroup') {
116	    $form['groupName'] = '';
117	    $form['formName'] = 'AdminCreateGroup';
118	}
119
120	$template->setVariable('AdminCreateGroup', array());
121	$template->setVariable('controller', 'core.AdminCreateGroup');
122	return array(null,
123		     array('body' => 'modules/core/templates/AdminCreateGroup.tpl'));
124    }
125}
126?>
127