summaryrefslogtreecommitdiffstats
path: root/modules-available/sysconfig/addmodule_screensaver.inc.php
blob: 889a94e541ffb4570948f37bc65234b0bc0dd84e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php

/*
 * Wizard for configuring the xscreensaver (client side).
 */

class Screensaver_Start extends AddModule_Base
{
	private $hasSummernote = false;
	private $session_data;

	protected function preprocessInternal()
	{
		/* Load or initialise session data */
		if (Request::get('back', 'false', 'string') !== 'false')
			/* If coming via the back button, load the session data */
			$this->session_data = Session::get('data');
		elseif ($this->edit !== false) {
			$this->session_data = array(
				'title' => $this->edit->title(),
				'qss' => $this->edit->getData('qss'),
				'messages' => $this->edit->getData('messages'),
				'texts' => $this->edit->getData('texts'),
			);
		} else {
			$this->session_data = array(
				'title' => '',
				'qss' => Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenQssDefault', true),
				'messages' => array(
					'General' => array(
						'shutdown' => Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenMessageDefaultShutdown', true),
						'shutdown-locked' => Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenMessageDefaultShutdownLocked', true),
						'idle-kill' => Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenMessageDefaultIdleKill', true),
						'idle-kill-locked' => Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenMessageDefaultIdleKillLocked', true),
						'no-timeout' => Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenMessageDefaultNoTimeout', true),
						'no-timeout-locked' => Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenMessageDefaultNoTimeoutLocked', true),
					)
				),
				'texts' => array(
					'text-shutdown' => Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenTextDefaultShutdown', true),
					'text-shutdown-locked' => '',
					'text-idle-kill' => Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenTextDefaultIdleKill', true),
					'text-idle-kill-locked' => Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenTextDefaultIdleKillLocked', true),
					'text-no-timeout' => '',
					'text-no-timeout-locked' => '',
				),
			);
		}
		$this->session_data['next'] = 'idle-kill';
		Session::set('data', $this->session_data);
		Session::save();
	}

	protected function renderInternal()
	{
		/* Load summernote module if available */
		$this->hasSummernote = Module::isAvailable('summernote');

		Render::addDialog(Dictionary::translateFile('config-module', 'screensaver_title'), false, 'screensaver-start', array(
			'step' => 'Screensaver_Text',
			'next' => 'idle-kill',
			'edit' => $this->edit ? $this->edit->id() : 0,
			'id' => 'start',
			'title' => $this->session_data['title'],
			'qss' => $this->session_data['qss'],
		));
	}
}

class Screensaver_Text extends AddModule_Base
{
	private $hasSummernote = false;
	private $session_data;
	private $id;

	protected function preprocessInternal()
	{
		/* Load session data */
		$this->session_data = Session::get('data');
		$this->id = Request::post('id', '', 'string');

		if ($this->id === 'start') {
			Screensaver_Helper::processQssData($this->session_data);
		} elseif ($this->id !== '') {
			Screensaver_Helper::processScreensaverText($this->session_data, $this->id);
		}

		$next = Request::post('next', $this->session_data['next'], 'string');
		$this->session_data['next'] = $next;
		Session::set('data', $this->session_data);
		Session::save();


		if ($next === 'finish')
			Util::redirect('?do=SysConfig&action=addmodule&step=Screensaver_Finish');
		elseif ($next === 'start')
			Util::redirect('?do=SysConfig&action=addmodule&step=Screensaver_Start&back=true');
	}

	protected function renderInternal()
	{
		/* Load summernote module if available */
		$this->hasSummernote = Module::isAvailable('summernote');
		$next = $this->session_data['next'];

		$data = array(
			'edit' => $this->edit ? $this->edit->id() : 0,
		);

		/* Prepare and translate labels for the frontend */
		$data['id'] = $next;
		/* Convert the id to a language tag (camelCase) styled string */
		$tag = explode('-', $next);
		foreach ($tag as $key => $value) {
			$tag[$key] = ucwords($value);
		}
		$tag = implode($tag);

		$data['title'] = Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenTitle' . $tag, true);
		$data['description'] = Dictionary::translateFileModule('sysconfig', 'template-tags', 'lang_screenDescription' . $tag, true);;
		$data['msg_value'] = $this->session_data['messages']['General'][$next];
		$data['msg_locked_value'] = $this->session_data['messages']['General'][$next . '-locked'];
		$data['text_value'] = $this->session_data['texts']['text-' . $next];
		$data['text_locked_value'] = $this->session_data['texts']['text-' . $next . '-locked'];
		$data['inherit_locked'] = $this->session_data['texts'][$next . '-inherit'];
		$data['step'] = 'Screensaver_Text';

		/* Set next and prev pages */
		if ($next === 'idle-kill') {
			$data['next'] = 'no-timeout';
			$data['prev'] = 'start';
		} elseif ($next === 'no-timeout') {
			$data['next'] = 'shutdown';
			$data['prev'] = 'idle-kill';
		} elseif ($next === 'shutdown') {
			$data['next'] = 'finish';
			$data['prev'] = 'no-timeout';
			$data['lastStep'] = true;
		}

		Render::addDialog(Dictionary::translateFile('config-module', 'screensaver_title'), false, 'screensaver-text', $data);
	}
}

class Screensaver_Finish extends AddModule_Base
{
	protected function preprocessInternal()
	{
		/* Get session data */
		$session_data = Session::get('data');

		if (empty($session_data['title'])) {
			Message::addError('missing-title');
			Util::redirect('?do=SysConfig');
			return;
		}

		/* Only create an instance, if it's a new one */
		if ($this->edit !== false)
			$module = $this->edit;
		else
			$module = ConfigModule::getInstance('Screensaver');

		/* Set all the data to the module instance */
		$module->setData('qss', $session_data['qss']);
		$module->setData('messages', $session_data['messages']);
		$module->setData('texts', $session_data['texts']);

		/* Insert or update database entries */
		if ($this->edit !== false)
			$module->update($session_data['title']);
		else
			$module->insert($session_data['title']);

		$task = $module->generate($this->edit === false);

		// Yay
		if ($task !== false && $this->edit !== false)
			Message::addSuccess('module-edited');
		elseif ($task !== false) {
			Message::addSuccess('module-added');
			AddModule_Base::setStep('AddModule_Assign', $module->id());
			return;
		}
		Util::redirect('?do=SysConfig');
	}
}

class Screensaver_Helper
{
	public static function processQssData(&$session_data) {
		/* Process post data from the Screensaver_Start */
		$session_data['title'] = Request::post('title', $session_data['title'], 'string');
		if (empty($session_data['title'])) {
			Message::addError('missing-title');
			Util::redirect('?do=SysConfig');
			return;
		}
		$session_data['qss'] = Request::post('qss', $session_data['qss'], 'string');
		$helperMode = Request::post('helper_mode', 'false', 'string');
		if ($helperMode !== 'false') {
			// Get all the helper variables and build the qss
			$bg_color_1 = Request::post('bg_color_1', '', 'string');
			self::fixColor($bg_color_1, '#443');
			$bg_color_2 = Request::post('bg_color_2', '', 'string');
			self::fixColor($bg_color_2, '#000');
			$label_color = Request::post('label_color', '', 'string');
			self::fixColor($label_color, '#f64');
			$label_size = Request::post('label_size', 10, 'int') . 'pt';
			$clock_color = Request::post('clock_color', '', 'string');
			self::fixColor($clock_color, '#999');
			$clock_size = Request::post('clock_size', 20, 'int') . 'pt';
			$header_color = Request::post('header_color', '', 'string');
			self::fixColor($header_color, $label_color);
			$header_size = Request::post('header_size', 20, 'int') . 'pt';

			$session_data['qss'] = "#Saver {\n  background: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 " .
				$bg_color_1 . ", stop:1 " . $bg_color_2 . ")\n}\n\n" .
				"QLabel {\n  color: " . $label_color . ";\n  font-size: " . $label_size . ";\n}\n\n" .
				"#lblClock {\n  color: " . $clock_color . ";\n  font-size: " . $clock_size . ";\n}\n\n" .
				"#lblHeader {\n  color: " . $header_color . ";\n  font-size: " . $header_size . ";\n}";
		}
	}

	private static function fixColor(&$color, $fix)
	{
		if (!preg_match('/^#([0-9a-f]{3}|[0-6a-f]{6})$/i', $color)) {
			$color = $fix;
		}
	}

	public static function processScreensaverText(&$session_data, $name) {
		/* Process post data from the Screensaver_Text */
		$session_data['messages']['General'][$name] = Request::post('msg_value', '', 'string');
		$session_data['texts']['text-' . $name] = Request::post('text_value', '', 'string');
		$inherit_locked = Request::post('inherit_locked', 'false', 'string');
		$session_data['texts'][$name . '-inherit'] = $inherit_locked;

		if ($inherit_locked !== 'false') {
			$session_data['messages']['General'][$name . '-locked'] = $session_data['messages']['General'][$name];
			$session_data['texts']['text-' . $name . '-locked'] = $session_data['texts']['text-' . $name];
		} else {
			$session_data['messages']['General'][$name . '-locked'] = Request::post('msg_locked_value', '', 'string');
			$session_data['texts']['text-' . $name . '-locked'] = Request::post('text_locked_value', '', 'string');
		}
	}
}