<?php
/**
 * @class action
 * @brief Manage actions
 * @author Vincent Giersch
 * @date 12/08/2009
 * @version 0.1
*/
class action {

	/**
	 * @brief Check if the action is in progress
	 * @param action_id -> id of action
	 * @param hosting_id-> id of the hosting, optionnaly
	 * @param is_parent_id -> bool(true) if the param 'action_id' is a 'parent_id'
	 * @return 0 : Action not found
	 * @return 1 : Action is in state 'added', it can be deleted
	 * @return 2 : Action is in execution or finished
	*/
	static private function checkBeforeDeleteAction($action_id, $is_parent_id = null)
	{
		$action_id = $_SESSION['database']->clearString($action_id);
		$can_cancel = 1;
		if ( !is_null($is_parent_id) ) {
			$req = "SELECT id, parent_id, status FROM actions WHERE parent_id = '$action_id'";
			$actions = $_SESSION['database']->fetchObject($req);
			foreach ( $actions as $action ) {
				if ( $action->status != 'added' ) $can_cancel = 2;
			}
			return $can_cancel;
		}
		else {
			$req = "SELECT id, parent_id,s tatus FROM actions WHERE id = '$action_id'";
			$action = $_SESSION['database']->fetchObject($req);
			if ( !isset($action[0]) || !is_object($action[0]) ) return 0;
			if ( $action[0]->status == 'added' ) return 1;
			return 2;
		}
	} // End of checkBeforeDeleteAction
	
	/**
	 * @brief Model : Delete action
	 * @param action_id -> id of action
	 * @param is_parent_id -> bool(true) if the param 'action_id' is a 'parent_id'
	 * @return always bool(true)
	*/
	static private function deleteAction($action_id, $is_parent_id = null)
	{
		$action_id = $_SESSION['database']->clearString($action_id);
		if( !is_null($is_parent_id) )
			$_SESSION['database']->execRequest("DELETE FROM actions WHERE parent_id = '$action_id' OR id = '$action_id'");
		else
			$_SESSION['database']->execRequest("DELETE FROM actions WHERE id = '$action_id'");
		return true;	
	} // End of deleteAction
	
	/**
	 * @brief Controller : Cancel an action
	 * @param action_id -> id of action
	 * @param is_parent_id -> bool(true) if the param 'action_id' is a 'parent_id'
	 * @return 0 : Action not found or isn't related to hosting_id (if specified)
	 * @return 1 : Action has been cancelled (Ok)
	 * @return 2 : Action can't be cancelled because is in execution or finished
	*/
	static public function userCancelAction($action_id, $is_parent_id = null)
	{
		$return_check = self::checkBeforeDeleteAction($action_id, $is_parent_id);
		
		if ( $return_check  == 0 || $return_check == 3)	
			return 0;
		elseif ( $return_check  == 2) 
			return 2;
		else {		
			self::deleteAction($action_id, $is_parent_id);
			return 1;
		}
	} // End of userCancelAction
		
	/**
	 * @brief Model : Add action
	 * @param servers_id -> id of service
	 * @param hosting_d -> id of the hosting
	 * @param data -> array, data about the task
	 * @param parent_id -> optionally, the id of the group
	 * @return last action id
	*/
	static private function addAction($service_id, $data, $hosting_id, $parent_id = null)
	{
		$data = $_SESSION['database']->clearString(json_encode($data));
		$service_id = $_SESSION['database']->clearString($service_id);
		$parent_id = ( is_null($parent_id) ) ? 'null' : $parent_id = $_SESSION['database']->clearString($parent_id);
		$req = "INSERT INTO actions
				SET
					parent_id = '$parent_id',
					created_at = CURRENT_TIMESTAMP,
					status = 'added',
					hosting_id = '$hosting_id',
					servers_id = '$service_id',
					data = '$data'";
		$_SESSION['database']->execRequest($req);
		return $_SESSION['database']->getInsertId();
	} // End of addAction
	
	/**
	 * @brief Controller : Add action on the tasklist
	 * @param servers_id -> id of service
	 * @param data -> array, data about the task
	 * @return 
	*/
	static public function userAddAction($service_id, $data, $parent_id = null)
	{
		$hosting_id = $_SESSION['hosting']->information_hosting->id;
		return self::addAction($service_id, $data, $hosting_id, $parent_id);
	} // End of userAddAction	
	
	
	/**
	 * @brief Controller : Add a group of actions on the tasklist
	 * @param data -> array, the group of actions
	 * @return id of the group of actions
	 * @todo Delete this function
	*/
	/*static public function userAddGroupAction($data)
	{
		$parent_id = null;
		$hosting_id = $_SESSION['hosting']->information_hosting->id;
		
		foreach( $data as $key => $value ) {
			if ( is_null($parent_id) ) $parent_id = self::addAction($value['service_id'], $value['data'], $hosting_id);
			else	self::addAction($value['service_id'], $value['data'], $hosting_id, $parent_id);
		}
		return $parent_id;
	} // End of userAddAction */

} // End of class

?>