summaryrefslogtreecommitdiffstats
path: root/modules-available/sysconfig/addmodule_screensaver.inc.php
blob: 2971195954bf23e8b777b92c00a5717233cbe38a (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
<?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 ($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' => '',
				'messages' => array(
					'General' => array(
						'shutdown' => '',
						'shutdown-locked' => '',
						'idle-kill' => '',
						'idle-kill-locked' => '',
						'no-timeout' => '',
						'no-timeout-locked' => '',
					)
				),
				'texts' => array(
					'text-shutdown' => '',
					'text-shutdown-locked' => '',
					'text-idle-kill' => '',
					'text-idle-kill-locked' => '',
					'text-no-timeout' => '',
					'text-no-timeout-locked' => '',
				),
			);
		}

		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);
		}

		Session::set('data', $this->session_data);
		Session::save();
	}

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

		$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'];

		if ($next === 'idle-kill') {
			$data['next'] = 'no-timeout';
			$data['step'] = 'Screensaver_Text';
		} elseif ($next === 'no-timeout') {
			$data['next'] = 'shutdown';
			$data['step'] = 'Screensaver_Text';
		} elseif ($next === 'shutdown') {
			$data['step'] = 'Screensaver_Finish';
		}

		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');

		/* Process the last step */
		Screensaver_Helper::processScreensaverText($session_data, Request::post('id', '', 'string'));

		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']);

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

		// Yay
		if ($this->edit !== false)
			Message::addSuccess('module-edited');
		else
			Message::addSuccess('module-added');
		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', '#443', 'string');
			$bg_color_2 = Request::post('bg_color_2', '#000', 'string');
			$label_color = Request::post('label_color', '#f64', 'string');
			$label_size = Request::post('label_size', 10, 'int') . 'pt';
			$clock_color = Request::post('clock_color', '#999', 'string');
			$clock_size = Request::post('clock_size', 20, 'int') . 'pt';
			$header_color = Request::post('header_color', 'inherit', 'string');
			$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}";
		}
	}

	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');
		}
	}
}