<?php
/*
 * This file is part of the Easy-CMS project.
 * Easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * @package    Core
 * @subpackage Forms
 */
 
/**
 * @author     vinadelmar <vinadelmar@users.sourceforge.net>
 * @since      2006-02-20
 * @version    0.1.0d
 * @package    Core
 * @subpackage Forms
 */
class ContainersListForm extends ObjectForm {

	var $containersListId;
	var $containerId;
	var $pageId;

	/**
	 * Constructor
	 *
	 * @param object $container
	 */
	function ContainersListForm(&$site) {
		parent::ObjectForm($site);
		$this->containersListId	= $this->site->getSubmittedVar('containers_list_id', 0);
		$this->pageId						= $this->site->getSubmittedVar('page_id', 0);
		$this->containerId			= $this->site->getSubmittedVar('container_id', 0);
		$mode										= $this->site->getSubmittedVar('mode');
		$action									= $this->site->getSubmittedVar('action');

//todo : check if all needed vars are found : otherwise it should generate errors

		$this->objectManager = &$this->site->getContainersListsManager();
		if(!$action) {
			$this->action = $mode;
		}
		else {
			$this->action = $action;
		}

		if ($this->containersListId == 0) {
			if($this->pageId > 0) {
				$pagesManager = &$this->site->getPagesManager();
				$page = &$containersListsManager->getPage($this->pageId, false, false, false);
				$aclId = $page->getPageAclId();
				$containersList = &$this->objectManager->getNewContainersList($aclId);
				$this->objectManager->setContainersList($containersList);
			}
		}
		else {
			$containersList = &$this->objectManager->getContainersList($this->containersListId, false);
		}
		$this->object = &$containersList;
	}


	function processForm() {
		// check user rights on current object
		$aclManager = &$this->site->getACLManager();
		$user = &$this->site->getCurrentUser();
		$aclId = $this->object->getAclId();
		$acl = &$aclManager->getAcl($aclId);

		switch($this->action) {
			case ASK_MODIF_CONTAINERS_LIST:
				break;
			case MODIF_CONTAINERS_LIST:
				if($acl->userHasEditRight($user)) {
					$this->processFormModif();
				}
				break;
			case MOVE_CONTAINER_UP:
			case MOVE_CONTAINER_DOWN:
				if($acl->userHasEditRight($user)) {
					$this->processFormMove();
				}
				break;
			case DELETE_CONTAINERS_LIST:
				if($acl->userHasRemoveRight($user)) {
					$this->processFormDelete();
				}
				break;
		}
	}

	function processFormModif() {
		$this->object->setContainersListValues($_POST);
		$this->objectManager->setContainersList($this->object);

		if (!($containersListId = $this->objectManager->storeContainersList($this->object->getContainersListId()))) {
			return false;
		}

		if ($this->pageId) {
			$pagesManager = &$this->site->getPagesManager();
			$pagesManager->setPageForCacheRenewalAndStore($this->pageId);
		}
		
		return true;
	}

	function processFormMove() {
		if ($this->containersListId && $this->containerId) {
			$dbManipulator = &$this->objectManager->getDBManipulator();
			// get the position index of the content
			$result = $dbManipulator->getRecord(CONTAINERS_LISTS_CONTAINERS_TABLE, $this->containersListId, '`container_id`='.$this->containerId, 'containers_list_id');
			if($row = $dbManipulator->fetchArray($result)){
				$index = $row['index'];
				if($this->action == MOVE_CONTAINER_UP) {
						if( $index > 1) {
						$result = $dbManipulator->sendQuery("SELECT max(`index`) as `index` FROM `".CONTAINERS_LISTS_CONTAINERS_TABLE."`, `".CONTAINERS_TABLE."` WHERE `container_id` = `id` AND `deleted` = 0 AND `containers_list_id` = ".$this->containersListId." and `index` < $index");
						if(!($row = $dbManipulator->fetchArray($result))){
							return false;
						}
						$previousIndex = $row['index'];

						$result = $dbManipulator->getRecord(CONTAINERS_LISTS_CONTAINERS_TABLE, $this->containersListId, '`index`='.$previousIndex, 'containers_list_id');
						if($row = $dbManipulator->fetchArray($result)){
							// there is a previous index
							$previousContainerId = $row['container_id'];
							$fieldsArray = array();
        			$fieldsArray['index'] = $index;
							$dbManipulator->setRecord(CONTAINERS_LISTS_CONTAINERS_TABLE, $this->containersListId, $fieldsArray, '`container_id`='.$previousContainerId, 'containers_list_id');
        			$fieldsArray['index'] = $previousIndex;
							$dbManipulator->setRecord(CONTAINERS_LISTS_CONTAINERS_TABLE, $this->containersListId, $fieldsArray, '`container_id`='.$this->containerId, 'containers_list_id');
						}
					}
				}
				else if ($this->action == MOVE_CONTAINER_DOWN) {
					$result = $dbManipulator->sendQuery("SELECT min(`index`) as `index` FROM `".CONTAINERS_LISTS_CONTAINERS_TABLE."`, `".CONTAINERS_TABLE."` WHERE `container_id` = `id` AND `deleted` = 0 AND `containers_list_id` = ".$this->containersListId." and `index` > $index");
					if(!($row = $dbManipulator->fetchArray($result))){
						return false;
					}
					$nextIndex = $row['index'];

					$result = $dbManipulator->getRecord(CONTAINERS_LISTS_CONTAINERS_TABLE, $this->containersListId, '`index`='.$nextIndex, 'containers_list_id');
					if($row = $dbManipulator->fetchArray($result)){
						// there is a next index
						$nextContainerId = $row['container_id'];
						$fieldsArray = array();
      			$fieldsArray['index'] = $index;
						$dbManipulator->setRecord(CONTAINERS_LISTS_CONTAINERS_TABLE, $this->containersListId, $fieldsArray, '`container_id`='.$nextContainerId, 'containers_list_id');
      			$fieldsArray['index'] = $nextIndex;
						$dbManipulator->setRecord(CONTAINERS_LISTS_CONTAINERS_TABLE, $this->containersListId, $fieldsArray, '`container_id`='.$this->containerId, 'containers_list_id');
					}
				}
			}
		}
		$url = $this->site->getCurrentURL(false);
    // force edit_mode
    $url = str_replace('mode=view','mode=edit', $url);
  	$this->site->redirectTo($url);
		exit();
	}

	/**
	 * @return boolean
	 */
	function processFormDelete() {

	}

	function spreadAcl($aclId) {
		return $this->objectManager->updateContainersListACL($this->object->getContainersListId(), $aclId);
	}

}
?>