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 * Update performance settings
23 * @package GalleryCore
24 * @subpackage UserInterface
25 * @author Bharat Mediratta <bharat@menalto.com>
26 * @version $Revision: 17580 $
27 */
28class AdminPerformanceController extends GalleryController {
29
30    /**
31     * @see GalleryController::handleRequest
32     */
33    function handleRequest($form) {
34	global $gallery;
35
36	$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
37	if ($ret) {
38	    return array($ret, null);
39	}
40
41	$results = $status = $error = array();
42	list ($ret, $acceleration) =
43	    GalleryCoreApi::getPluginParameter('module', 'core', 'acceleration');
44	if ($ret) {
45	    return array($ret, null);
46	}
47	if ($acceleration) {
48	    $acceleration = unserialize($acceleration);
49	}
50
51	if (isset($form['action']['save'])) {
52	    foreach (array('user', 'guest') as $class) {
53		if (!isset($form['acceleration'][$class])) {
54		    return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
55						       "Missing '$class' from the form"), null);
56		}
57		$acceleration[$class]['type'] = $form['acceleration'][$class]['type'];
58		switch ($form['acceleration'][$class]['type']) {
59		case 'partial':
60		case 'full':
61		    $acceleration[$class]['expiration'] =
62			(int)$form['acceleration'][$class]['expiration'];
63		    break;
64
65		case 'none':
66		    if (!isset($acceleration[$class]['expiration'])) {
67			$acceleration[$class]['expiration'] = 0;
68		    }
69		    break;
70
71		default:
72		    return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null);
73		}
74	    }
75	    $ret = GalleryCoreApi::setPluginParameter(
76		'module', 'core', 'acceleration', serialize($acceleration));
77	    if ($ret) {
78		return array($ret, null);
79	    }
80
81	    $ret = GalleryCoreApi::setPluginParameter(
82		'module', 'core', 'smarty.compile_check', (int)empty($form['disableCompileCheck']));
83	    if ($ret) {
84		return array($ret, null);
85	    }
86
87	    $status['saved'] = 1;
88	    $redirect = array('view' => 'core.SiteAdmin',
89			      'subView' => 'core.AdminPerformance');
90	} else if (isset($form['action']['clear'])) {
91	    $ret = GalleryCoreApi::removeAllMapEntries('GalleryCacheMap');
92	    if ($ret) {
93		return array($ret, null);
94	    }
95	    $status['cleared'] = 1;
96	    $redirect = array('view' => 'core.SiteAdmin',
97			      'subView' => 'core.AdminPerformance');
98	}
99
100	if (!empty($redirect)) {
101	    $results['redirect'] = $redirect;
102	} else if (empty($results['delegate'])) {
103	    $results['delegate']['view'] = 'core.SiteAdmin';
104	    $results['delegate']['subView'] = 'core.AdminPerformance';
105	}
106
107	$results['status'] = $status;
108	$results['error'] = $error;
109
110	return array(null, $results);
111    }
112}
113
114/**
115 * Update performance settings
116 */
117class AdminPerformanceView extends GalleryView {
118
119    /**
120     * @see GalleryView::loadTemplate
121     */
122    function loadTemplate(&$template, &$form) {
123	$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
124	if ($ret) {
125	    return array($ret, null);
126	}
127
128	list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'core');
129	if ($ret) {
130	    return array($ret, null);
131	}
132
133	/* Load some standard form parameters */
134	if ($form['formName'] != 'AdminPerformance') {
135	    $form['formName'] = 'AdminPerformance';
136
137	    list ($ret, $acceleration) = $module->getParameter('acceleration');
138	    if ($ret) {
139		return array($ret, null);
140	    }
141	    if ($acceleration) {
142		$form['acceleration'] = unserialize($acceleration);
143	    }
144	    foreach (array('user', 'guest') as $class) {
145		if (!isset($form['acceleration'][$class]['type'])) {
146		    $form['acceleration'][$class]['type'] = 'none';
147		}
148		if (!isset($form['acceleration'][$class]['expiration'])) {
149		    $form['acceleration'][$class]['expiration'] = '0';
150		}
151	    }
152
153	    list ($ret, $compileCheck) = $module->getParameter('smarty.compile_check');
154	    if ($ret) {
155		return array($ret, null);
156	    }
157	    $form['disableCompileCheck'] = !$compileCheck;
158	}
159
160	$form['disableCompileCheck'] = !empty($form['disableCompileCheck']);
161
162	/* Set up our type lists */
163	$typeList = array('none' => $module->translate('No acceleration'),
164			  'partial' => $module->translate('Partial acceleration'),
165			  'full' => $module->translate('Full acceleration'));
166
167	/* Set up our time lists */
168	$expirationTimeLists = array();
169	foreach (array(15, 30, 45) as $minutes) {
170	    $expirationTimeList[$minutes * 60] =
171		$module->translate(array('one' => '%d minute',
172					 'many' => '%d minutes',
173					 'count' => $minutes,
174					 'arg1' => $minutes));
175	}
176	foreach (array(1, 6, 12) as $hours) {
177	    $expirationTimeList[$hours * 3600] =
178		$module->translate(array('one' => '%d hour',
179					 'many' => '%d hours',
180					 'count' => $hours,
181					 'arg1' => $hours));
182	}
183	foreach (array(1, 2, 3, 4, 5, 6) as $days) {
184	    $expirationTimeList[$days * 86400] =
185		$module->translate(array('one' => '%d day',
186					 'many' => '%d days',
187					 'count' => $days,
188					 'arg1' => $days));
189	}
190	foreach (array(1, 2, 3) as $weeks) {
191	    $expirationTimeList[$weeks * 7 * 86400] =
192		$module->translate(array('one' => '%d week',
193					 'many' => '%d weeks',
194					 'count' => $weeks,
195					 'arg1' => $weeks));
196	}
197
198	$AdminPerformance = array();
199	$AdminPerformance['expirationTimeList'] = $expirationTimeList;
200	$AdminPerformance['typeList'] = $typeList;
201
202	$template->setVariable('AdminPerformance', $AdminPerformance);
203	$template->setVariable('controller', 'core.AdminPerformance');
204
205	return array(null, array('body' => 'modules/core/templates/AdminPerformance.tpl'));
206    }
207}
208
209?>
210