<?php

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

	/**
	 * Extends default ArticlesEventHandler class
	 *
	 */
	class EArticleEventHandler extends ArticlesEventHandler {

		/**
		 * Allows to override standard permission mapping
		 *
		 */
		function mapPermissions()
		{
			parent::mapPermissions();

			$permissions = Array (
				# admin
				'OnCustomEvent1' => Array ('self' => true),

				# front
				'OnCustomEvent2' => Array ('self' => true),
			);

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

		/**
		 * Permission exceptions
		 *
		 * @param kEvent $event
		 */
		function CheckPermission(&$event)
		{
			$skip_permissions_check_events = Array (
				'OnAnotherCustomEvent',
			);

			if ( in_array($event->Name, $skip_permissions_check_events) ) {
				return true;
			}

			return parent::CheckPermission($event);
		}

		/**
		 * Updates structure config
		 *
		 * @param kEvent $event
		 */
		function OnAfterConfigRead(&$event)
		{
			parent::OnAfterConfigRead($event);

			# 1. Regular Fields from /in-news/units/articles/articles_config.php
			$fields = $this->Application->getUnitOption($event->Prefix, 'Fields'); #get regular fields
//			$fields['Status']['default'] = STATUS_ACTIVE; #add/edit configuration (make default = STATUS_ACTIVE)
			$this->Application->setUnitOption($event->Prefix, 'Fields', $fields); #save fields

			# 2. Virtual Fields from /in-news/units/articles/articles_config.php
			$virtual_fields = $this->Application->getUnitOption($event->Prefix, 'VirtualFields'); #get virtual fields
//			$virtual_fields['UserName']['default'] = ''; #add/edit configuration
			$this->Application->setUnitOption($event->Prefix, 'VirtualFields', $virtual_fields); #save virtual fields
		}

		/**
		 * Sets custom query for the list
		 *
		 * @param kEvent $event
		 */
		function SetCustomQuery(&$event)
		{
			parent::SetCustomQuery($event);

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

			# identifying event based on special and setting filter
			if ($event->Special == 'custom-special') {
//				$object->addFilter('primary_filter', '%1$s.Status = ' . STATUS_DISABLED);
			}

			# identifying event based not admin condition and setting filter
			if (!$this->Application->isAdminUser) {
//				$object->addFilter('status_filter', '%1$s.Status = ' . STATUS_ACTIVE);
			}
		}

		/**
		 * Before new item created
		 *
		 * @param kEvent $event
		 */
		function OnBeforeItemCreate(&$event)
		{
			parent::OnBeforeItemCreate($event);


		}

		/**
		 * Before existing item updated
		 *
		 * @param kEvent $event
		 */
		function OnBeforeItemUpdate(&$event)
		{
			parent::OnBeforeItemUpdate($event);


		}

		/**
		 * Before item deleted
		 *
		 * @param kEvent $event
		 */
		function OnBeforeItemDelete(&$event)
		{
			parent::OnBeforeItemDelete($event);


		}

		/**
		 * After item loaded
		 *
		 * @param kEvent $event
		 */
		function OnBeforeItemLoad(&$event)
		{
			parent::OnBeforeItemLoad($event);


		}


		/**
		 * After new item created
		 *
		 * @param kEvent $event
		 */
		function OnAfterItemCreate(&$event)
		{
			parent::OnAfterItemCreate($event);


		}

		/**
		 * After existing item updated
		 *
		 * @param kEvent $event
		 */
		function OnAfterItemUpdate(&$event)
		{
			parent::OnAfterItemUpdate($event);


		}

		/**
		 * After item deleted
		 *
		 * @param kEvent $event
		 */
		function OnAfterItemDelete(&$event)
		{
			parent::OnAfterItemDelete($event);


		}

		/**
		 * After item loaded
		 *
		 * @param kEvent $event
		 */
		function OnAfterItemLoad(&$event)
		{
			parent::OnAfterItemLoad($event);


		}

	}
