<?php
/**
* @version	$Id: skin_eh.php 15145 2012-03-04 09:04:08Z alex $
* @package	In-Portal
* @copyright	Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license      GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/

	defined('FULL_PATH') or die('restricted access!');

	class SkinEventHandler extends kDBEventHandler {

		/**
		 * Allows to override standard permission mapping
		 *
		 * @return void
		 * @access protected
		 * @see kEventHandler::$permMapping
		 */
		protected function mapPermissions()
		{
			parent::mapPermissions();

			$permissions = Array (
				'OnItemBuild' => Array ('self' => true),
			);

			$this->permMapping = array_merge($this->permMapping, $permissions);
		}

		/**
		 * With "primary" special loads primary skin
		 *
		 * @param kEvent $event
		 * @return int
		 * @access public
		 */
		public function getPassedID(kEvent $event)
		{
			if ( $event->Special == 'primary' ) {
				return Array ('IsPrimary' => 1);
			}

			return parent::getPassedID($event);
		}

		/**
		 * Allows to set selected theme as primary
		 *
		 * @param kEvent $event
		 */
		function OnSetPrimary($event)
		{
			if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
				$event->status = kEvent::erFAIL;
				return;
			}

			$ids = $this->StoreSelectedIDs($event);
			if ($ids) {
				$id = array_shift($ids);
				$this->setPrimary($id);
			}

			$this->clearSelectedIDs($event);
		}

		function setPrimary($id)
		{
			$id_field = $this->Application->getUnitOption($this->Prefix, 'IDField');
			$table_name = $this->Application->getUnitOption($this->Prefix, 'TableName');

			$sql = 'UPDATE '.$table_name.'
					SET IsPrimary = 0';
			$this->Conn->Query($sql);


			$sql = 'UPDATE '.$table_name.'
					SET IsPrimary = 1
					WHERE '.$id_field.' = '.$id;
			$this->Conn->Query($sql);
		}

		/**
		 * Don't make cloned skin primary
		 *
		 * @param kEvent $event
		 * @return void
		 * @access protected
		 */
		protected function OnBeforeClone(kEvent $event)
		{
			parent::OnBeforeClone($event);

			$object = $event->getObject();
			/* @var $object kDBItem */

			$object->SetDBField('IsPrimary', 0);
		}

		/**
		 * Re-compile skin, after it's changed (live table only)
		 *
		 * @param kEvent $event
		 * @return void
		 * @access protected
		 */
		protected function OnAfterItemUpdate(kEvent $event)
		{
			parent::OnAfterItemUpdate($event);

			$object = $event->getObject();
			/* @var $object kDBItem */

			if ( !$object->IsTempTable() ) {
				$skin_helper = $this->Application->recallObject('SkinHelper');
				/* @var $skin_helper SkinHelper */

				$skin_helper->compile($object);
			}
		}

		/**
		 * [HOOK] Compile stylesheet file based on theme definitions
		 *
		 * @param kEvent $event
		 * @return void
		 * @access protected
		 */
		protected function OnCompileStylesheet($event)
		{
			$object = $event->getObject( Array ('skip_autoload' => true) );
			/* @var $object kDBItem */

			$object->SwitchToLive();

			$ids = $event->MasterEvent->getEventParam('ids');
			if ( !is_array($ids) ) {
				$ids = explode(',', $ids);
			}

			if ( !$ids ) {
				return ;
			}

			$skin_helper = $this->Application->recallObject('SkinHelper');
			/* @var $skin_helper SkinHelper */

			foreach ($ids as $id) {
				$object->Load($id);
				$skin_helper->compile($object);
			}
		}
	}