summaryrefslogblamecommitdiffstats
path: root/modules-available/sysconfig/addmodule_screensaver.inc.php
blob: 7b6d0afbf06e874fb14c8e794d88b75a1d3a9e8a (plain) (tree)
1
2
3
4
5
6
7
8







                                                         




                                                     


                                                                                  
                                               








                                                                               
                                                                                   

                                                           





                                                                                                                                    


                                                 
                                                                                                              
                                                                     

                                                                                                                            




                                                                       
                                                          
                                                          




                                                         
                                                  


                                                                                                                                     
                                                                               




                                                                



                                             
                              




                                                           
                                                        
 
                                      
                                                                                

                                                                                             

                 

                                                                                     
                                                          
 
 
                                       


                                                                                                          




                                                         
                                                  
                                                    

                              
                                                                               




                                                                                









                                                                          

                                                                                         




                                                                                                          
                                                   
 
                                             

                                                     
                                                

                                                   
                                                    
                                                 
                                                 
                                                     
                                                 












                                                                                                                                     


                                                           


                                                                
                                           
                                              
                        
                                                                           
                 






                                                                        
                                           
                                                                
                        
                                                                
                 
 
                                                                

                      
                                                            
                                                             
                                          
                                                            


                                                                                   











                                                                                                  




                                                                                            





                                                                                  
                                                                                    

                                                                                  
                                                                                    

                                                                                    
                                                                                      








                                                                                                                                      






                                                                             
















                                                                                                                                  
<?php

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

class Screensaver_Start extends AddModule_Base
{
	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 !== null) {
			$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::translate('saver_QssDefault'),
				'messages' => array(
					'General' => array(
						'shutdown' => Dictionary::translate('saver_MessageDefaultShutdown'),
						'shutdown-locked' => Dictionary::translate('saver_MessageDefaultShutdownLocked'),
						'idle-kill' => Dictionary::translate('saver_MessageDefaultIdleKill'),
						'idle-kill-locked' => Dictionary::translate('saver_MessageDefaultIdleKillLocked'),
						'no-timeout' => Dictionary::translate('saver_MessageDefaultNoTimeout'),
						'no-timeout-locked' => Dictionary::translate('saver_MessageDefaultNoTimeoutLocked'),
					)
				),
				'texts' => array(
					'text-shutdown' => Dictionary::translate('saver_TextDefaultShutdown'),
					'text-shutdown-locked' => '',
					'text-idle-kill' => Dictionary::translate('saver_TextDefaultIdleKill'),
					'text-idle-kill-locked' => Dictionary::translate('saver_TextDefaultIdleKillLocked'),
					'text-no-timeout' => '',
					'text-no-timeout-locked' => '',
				),
			);
		}
		$this->session_data['next'] = 'idle-kill';
		Session::set('data', $this->session_data);
	}

	protected function renderInternal()
	{
		/* Load summernote module if available */
		Module::isAvailable('summernote');
		Render::addDialog(Dictionary::translateFile('config-module', 'screensaver_title'), false, 'screensaver-start', array(
			'step' => 'Screensaver_Text',
			'next' => 'idle-kill',
			'edit' => $this->edit !== null ? $this->edit->id() : 0,
			'id' => 'start',
			'title' => $this->session_data['title'],
			'qss' => $this->session_data['qss'],
		));
	}
}

class Screensaver_Text extends AddModule_Base
{
	private $session_data;

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

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

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


		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 */
		Module::isAvailable('summernote');
		$next = $this->session_data['next'];

		$data = array(
			'edit' => $this->edit !== null ? $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 = implode(array_map('ucwords', explode('-', $next)));

		/* For translate module:
		 * Dictionary::translate('saver_TitleNoTimeout');
		 * Dictionary::translate('saver_DescriptionNoTimeout');
		 * Dictionary::translate('saver_TitleIdleKill');
		 * Dictionary::translate('saver_DescriptionIdleKill');
		 * Dictionary::translate('saver_TitleShutdown');
		 * Dictionary::translate('saver_DescriptionShutdown');
		 */
		$data['title'] = Dictionary::translate('saver_Title' . $tag);
		$data['description'] = Dictionary::translate('saver_Description' . $tag);
		$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');
		}

		/* Only create an instance, if it's a new one */
		if ($this->edit !== null) {
			$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 !== null) {
			$module->update($session_data['title']);
		} else {
			$module->insert($session_data['title']);
		}

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

		// Yay
		if ($task !== false && $this->edit !== null)
			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');
		}
		$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');
		}
	}
}