summaryrefslogblamecommitdiffstats
path: root/modules-available/sysconfig/addmodule_screensaver.inc.php
blob: c6f447ab89e20aa59d6a865c72bd794163ba5f84 (plain) (tree)





























































































































































































































                                                                                                                                             
<?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', '20', 'string');
			$clock_color = Request::post('clock_color', '#999', 'string');
			$clock_size = Request::post('clock_size', '20', 'string');
			$header_color = Request::post('header_color', 'inherit', 'string');
			$header_size = Request::post('header_size', '20', 'string');

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