<?php
/**
* @version	$Id: skin_eh.php 14241 2011-03-16 20:24:35Z 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 {

		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
		 */
		function getPassedID(&$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 = 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
		 */
		function OnBeforeClone(&$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
		 */
		function OnAfterItemUpdate(&$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
		 */
		function OnCompileStylesheet(&$event)
		{
			$object =& $event->getObject( Array('skip_autoload' => true) );
			$object->SwitchToLive();

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

			if (!$ids) {
				return false;
			}

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

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

	}