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 view will show the availiable site-admin options
23 * @package GalleryCore
24 * @subpackage UserInterface
25 * @author Bharat Mediratta <bharat@menalto.com>
26 * @version $Revision: 17769 $
27 */
28class SiteAdminView extends GalleryView {
29
30    /**
31     * @see GalleryView::loadTemplate
32     */
33    function loadTemplate(&$template, &$form) {
34	global $gallery;
35
36	/**
37	 * Make sure we have adequate permissions.
38	 * @todo Remove _permissionCheck() since the check is done in main.php now.
39	 *       (r17170 obsoleted the check here (from r16894). Keep the assert here of course.
40	 */
41	$ret = $this->_permissionCheck(GalleryCoreApi::assertUserIsSiteAdministrator());
42	if ($ret) {
43	    return array($ret[0], $ret[1]);
44	}
45
46	/* Load the module list */
47	list ($ret, $moduleStatus) = GalleryCoreApi::fetchPluginStatus('module');
48	if ($ret) {
49	    return array($ret, null);
50	}
51
52	/* Get a list of all the admin views for each module */
53	$subViewChoices = array();
54	ksort($moduleStatus);
55
56	/*
57	 * We show all site admin links to the Modules page because it will hide any links for
58	 * modules that are not active.
59	 */
60	$subView = GalleryUtilities::getRequestVariables('subView');
61	$showAllSiteAdminViews = ($subView == 'core.AdminPlugins');
62	if ($showAllSiteAdminViews) {
63	    /* Ensure translations available even for uninstalled plugins */
64	    GalleryCoreApi::installTranslationsForLocale();
65	}
66
67	foreach ($moduleStatus as $moduleId => $status) {
68	    if (empty($status['active']) && !$showAllSiteAdminViews) {
69		continue;
70	    }
71
72	    /* Get the selected module's admin view */
73	    if (in_array('getSiteAdminViews', explode('|', $status['callbacks'])) ||
74		$showAllSiteAdminViews) {
75		if ($showAllSiteAdminViews) {
76		    list ($ret, $module) =
77			GalleryCoreApi::loadPlugin('module', $moduleId, true, false);
78		    if ($ret) {
79			if ($ret->getErrorCode() & ERROR_BAD_PARAMETER) {
80			    /* Module is missing or malformed, so ignore it */
81			    continue;
82			}
83			return array($ret, null);
84		    }
85
86		    if ($ret) {
87			return array($ret, null);
88		    }
89		} else {
90		    list ($ret, $module) = GalleryCoreApi::loadPlugin('module', $moduleId);
91		    if ($ret) {
92			if ($ret->getErrorCode() & ERROR_PLUGIN_VERSION_MISMATCH) {
93			    continue;
94			}
95			return array($ret, null);
96		    }
97		}
98
99		list ($ret, $moduleViews) = $module->getSiteAdminViews();
100		if ($ret) {
101		    return array($ret, null);
102		}
103
104		$group = $module->getGroup();
105		/**
106		 * Pre 2.2, we only accepted a 'view', which we made the subView to
107		 * core.SiteAdmin.  In 2.2, we now let them specify extra parameters
108		 * so we create a separate 'view' which contains all the url parameters
109		 * to form a complete SiteAdmin view (except that we force view to
110		 * be core.SiteAdmin).
111		 *
112		 * TODO: change the API to allow the module to return this form directly
113		 * so that we don't have to massage it like this.
114		 */
115		$currentSubView = GalleryUtilities::getRequestVariables('subView');
116		foreach (array_keys($moduleViews) as $i) {
117		    $outer = array('name' => $moduleViews[$i]['name']);
118		    if (isset($moduleViews[$i]['group'])) {
119			$outer['group'] = $moduleViews[$i]['group'];
120			unset($moduleViews[$i]['group']);
121			$outer['groupLabel'] = $moduleViews[$i]['groupLabel'];
122			unset($moduleViews[$i]['groupLabel']);
123		    } else {
124			$outer['group'] = $group['group'];
125			$outer['groupLabel'] = $group['groupLabel'];
126		    }
127
128		    $inner = $moduleViews[$i];
129		    unset($inner['name']);
130		    $inner['subView'] = $inner['view'];
131		    $inner['view'] = 'core.SiteAdmin';
132		    $outer['view'] = $inner;
133
134		    if ($inner['subView'] == $currentSubView) {
135			/* This is probably our selected view */
136			$selected = true;
137			foreach ($inner as $key => $value) {
138			    if ($key == 'view') {
139				continue;
140			    }
141			    if (GalleryUtilities::getRequestVariables($key) != $value) {
142				$selected = false;
143				break;
144			    }
145			}
146			$outer['selected'] = $selected;
147		    }
148		    $moduleViews[$i] = $outer;
149		}
150
151		$subViewChoices = array_merge($subViewChoices, $moduleViews);
152	    }
153	}
154
155	/* Sort and group */
156	$subViewGroups = $groupLabels = array();
157	foreach ($subViewChoices as $adminView) {
158	    $group = $adminView['group'];
159	    $subViewGroups[$group][] = $adminView;
160
161	    if (!empty($adminView['groupLabel'])) {
162		$label = $adminView['groupLabel'];
163		if (!isset($groupLabels[$group][$label])) {
164		    $groupLabels[$group][$label] = 1;
165		} else {
166		    $groupLabels[$group][$label]++;
167		}
168	    }
169	}
170
171	/* Set labels */
172	foreach (array_keys($subViewGroups) as $group) {
173	    $data =& $subViewGroups[$group];
174	    usort($data, array($this, 'nameSort'));
175	    if (isset($groupLabels[$group])) {
176		$tmp = -1;
177		foreach ($groupLabels[$group] as $label => $count) {
178		    if ($count > $tmp) {
179			$tmp = $count;
180			$data[0]['groupLabel'] = $label;
181		    }
182		}
183	    } else {
184		$data[0]['groupLabel'] = ucwords($data[0]['group']);
185	    }
186	}
187	usort($subViewGroups, array($this, 'groupSort'));
188
189	/* If we have a specific sub view, render it now */
190	$subViewName = GalleryUtilities::getRequestVariables('subView');
191	if ($subViewName == 'core.SiteAdmin') {
192	    return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null);
193	} else if (empty($subViewName)) {
194	    $subViewName = 'core.AdminCore';
195	} else if ($subViewName == 'core.AdminModules') {
196	    /*
197	     * In 2.2, we changed core.AdminModules and core.AdminPlugins.  Remap the name
198	     * here to avoid breaking any existing modules.
199	     * TODO: Remove this when GalleryCoreApi is at 8.0+
200	     */
201	    $subViewName = 'core.AdminPlugins';
202	}
203
204	list ($ret, $subView) = GalleryView::loadView($subViewName);
205	if ($ret) {
206	    return array($ret, null);
207	}
208
209	list ($ret, $results) = $subView->loadTemplate($template, $form);
210	if ($ret) {
211	    return array($ret, null);
212	}
213
214	/* Our sub view may have set some hints, like the encoding type */
215	if ($template->hasVariable('SiteAdmin')) {
216	    $SiteAdmin =& $template->getVariableByReference('SiteAdmin');
217	} else {
218	    $SiteAdmin = array();
219	}
220
221	list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'core');
222	if ($ret) {
223	    return array($ret, null);
224	}
225
226	/* Set up my view parameters */
227	$SiteAdmin['subViewGroups'] = $subViewGroups;
228	$SiteAdmin['subViewName'] = $subViewName;
229	$SiteAdmin['viewBodyFile'] = $results['body'];
230	$SiteAdmin['viewL10Domain'] = $subView->getL10Domain();
231
232	$template->setVariable('SiteAdmin', $SiteAdmin);
233	$template->title($module->translate('Gallery Site Administration'));
234	return array(null, array('body' => 'modules/core/templates/SiteAdmin.tpl'));
235    }
236
237    /**
238     * @see GalleryView::getViewDescription
239     */
240    function getViewDescription() {
241	/* Get the description from the current subView */
242	$subViewName = GalleryUtilities::getRequestVariables('subView');
243	if (empty($subViewName)) {
244	    $subViewName = 'core.AdminCore';
245	}
246	list ($ret, $subView) = GalleryView::loadView($subViewName);
247	if ($ret) {
248	    return array($ret, null);
249	}
250
251	list ($ret, $description) = $subView->getViewDescription();
252	if (!empty($description)) {
253	    return array(null, $description);
254	}
255	list ($ret, $core) = GalleryCoreApi::loadPlugin('module', 'core');
256	if ($ret) {
257	    return array($ret, null);
258	}
259
260	/* Fallback if subView doesn't have a name */
261	return array(null, $core->translate('site admin'));
262    }
263
264    /**
265     * @see GalleryView::getViewType
266     */
267    function getViewType() {
268	return VIEW_TYPE_ADMIN;
269    }
270
271    function groupSort($a, $b) {
272	static $groupOrder;
273	if (!isset($groupOrder)) {
274	    /* gallery first, toolkits second, other last */
275	    $groupOrder = array('gallery' => 1, 'toolkits' => 2, '' => 3, 'other' => 4);
276	}
277	$ag = $a[0]['group'];
278	$bg = $b[0]['group'];
279	$ao = isset($groupOrder[$ag]) ? $groupOrder[$ag] : $groupOrder[''];
280	$bo = isset($groupOrder[$bg]) ? $groupOrder[$bg] : $groupOrder[''];
281	if ($ao != $bo) {
282	    return ($ao > $bo) ? 1 : -1;
283	}
284	$ag = isset($a[0]['groupLabel']) ? $a[0]['groupLabel'] : $ag;
285	$bg = isset($b[0]['groupLabel']) ? $b[0]['groupLabel'] : $bg;
286	return ($ag > $bg) ? 1 : -1;
287    }
288
289    function nameSort($a, $b) {
290	$an = $a['name'];
291	$bn = $b['name'];
292	if ($an == $bn) {
293	    return 0;
294	}
295	return ($an > $bn) ? 1 : -1;
296    }
297}
298?>
299