<?php
	/**
	 * Mail Module
	 */
	class MailListModule extends ListModule
	{
		/**
		 * Constructor
		 * @param int $id unique id.
		 * @param array $data list of all actions.
		 */
		function MailListModule($id, $data)
		{
			$this->properties = $GLOBALS["properties"]->getMailProperties();

			parent::ListModule($id, $data, array(OBJECT_SAVE, TABLE_SAVE, TABLE_DELETE));
		}

		/**
		 * Executes all the actions in the $data variable.
		 * @return boolean true on success of false on fialure.
		 */
		function execute()
		{
			$GLOBALS['PluginManager']->triggerHook("server.module.maillistmodule.execute.before", array('moduleObject' =>& $this));

			foreach($this->data as $actionType => $action)
			{
				if(isset($actionType)) {
					try {
						$store = $this->getActionStore($action);
						$parententryid = $this->getActionParentEntryID($action);
						$entryid = $this->getActionEntryID($action);

						switch($actionType)
						{
							case "list":
								$this->getDelegateFolderInfo($store);
								$this->messageList($store, $entryid, $action, $actionType);
								break;
							case "search":
								// @FIXME add handling for private items
								$this->search($store, $entryid, $action, $actionType);
								break;
							case "updatesearch":
								$this->updatesearch($store, $entryid, $action);
								break;
							case "stopsearch":
								$this->stopSearch($store, $entryid, $action);
								break;
							// will be called when a mail is converted into task  
							case "createTask":
								$this->CreateTaskFromMail($store, $entryid);
								break;
							case "convert_meeting":
								$this->getMeetingData($store, $entryid, $action);
								break;
							default:
								$this->handleUnknownActionType($actionType);
						}
					} catch (MAPIException $e) {
						$this->processException($e, $actionType);
					}
				}
			}
			$GLOBALS['PluginManager']->triggerHook("server.module.maillistmodule.execute.after", array('moduleObject' =>& $this));
		}

		/**
		 * Function does customization of exception based on module data.
		 * like, here it will generate display message based on actionType
		 * for particular exception.
		 * 
		 * @param object $e Exception object
		 * @param string $actionType the action type, sent by the client
		 * @param string $entryid entryid of the message.
		 * @param array $action the action data, sent by the client
		 */
		function handleException(&$e, $actionType=null, $entryid=null, $action=null)
		{
			if(is_null($e->displayMessage)) {
				switch($actionType)
				{
					case "list":
						if($e->getCode() == MAPI_E_NO_ACCESS)
							$e->setDisplayMessage(_("You have insufficient privileges to see the contents of this folder."));
						else
							$e->setDisplayMessage(_("Could not load the contents of this folder."));
						break;

					case "search":
						if($e->getCode() == MAPI_E_NO_ACCESS)
							$e->setDisplayMessage(_("You have insufficient privileges to perform search operation in this folder."));
						else
							$e->setDisplayMessage(_("Error in search, please try again"));
						break;

					case "updatesearch":
						$e->setDisplayMessage(_("Could not update search results."));
						break;

					case "stopsearch":
						$e->setDisplayMessage(_("Could not stop search operation."));
						break;

					case "convert_meeting":
						if($e->getCode() == MAPI_E_NO_ACCESS)
							$e->setDisplayMessage(_("You have insufficient privileges to convert this message into meeting."));
						else
							$e->setDisplayMessage(_("Could not convert this message into Meeting Request."));
						break;

					case "createTask":
						if($e->getCode() == MAPI_E_NO_ACCESS)
							$e->setDisplayMessage(_("You have insufficient privileges to convert this message into Task."));
						else
							$e->setDisplayMessage(_("Could not convert this message into Task."));
						break;
				}
			}

			parent::handleException($e, $actionType, $entryid, $action);
		}

		/**
		 * Function which get data of selected meeting item to send as response
		 * @param object $store MAPI Message Store Object
		 * @param string $parententryid entryid of the message
		 * @param array $action the action data, sent by the client
		 * @return boolean true on success or false on failure 
		 */
		function getMeetingData($store, $parententryid, $action)
		{
			if($store){
				if(isset($action["messages"]["message"]) && is_array($action["messages"]["message"])){

					if (!isset($action["messages"]["message"][0])){
						$msg = array($action["messages"]["message"]);
					}else{
						$msg = $action["messages"]["message"];
					}

					$data = array();
					$data["item"] = array();
					$data["targetfolder"] = $action["targetfolder"];
					$data["targetfolderentryid"] = $action["parententryid"];

					foreach($msg as $messageItem){
						$items = array();
						$message = mapi_msgstore_openentry($store, hex2bin($messageItem['id']));
						$items = $GLOBALS["operations"]->getMessageProps($store, $message, $this->properties, true);
						array_push($data["item"], $items);
					}

					$this->addActionData("convert_meeting", $data);
					$GLOBALS["bus"]->addData($this->getResponseData());
				}
			}
		}

		function parseSearchRestriction($action)
		{
			if(isset($action["restriction"])) {
				if(isset($action["restriction"]["start"])) {
					// Set start variable
					$this->start = $action["restriction"]["start"];
				}

				if(isset($action["restriction"]["search"])) {
					$this->searchRestriction = Conversion::json2restriction($action["restriction"]["search"]);
				}else{
					$this->searchRestriction = false;
				}
			}
		}

		/**
		 * Function is used to filter out properties which aren't usefull in the listview and we should
		 * remove it when getting listview data, this function gives us performance benefit of sending less data
		 * in listview because sending properties like body, message_transport_headers in list view can increase
		 * the response data a lot.
		 * We don't want to store filtered properties in the module object because module object should always
		 * have list of all properties.
		 * @return {Object} filtered properties that will be used to get data for list view.
		 */
		function filterOutExtraProperties()
		{
			$properties = $this->properties;

			unset($properties['transport_message_headers']);

			return $properties;
		}
	}
?>
