<?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
 */

include_once('classes/core/forms/ObjectForm.class.php');

class PageForm extends ObjectForm {

	var $pageId;
	var $parentId;

	function PageForm(&$site) {
		parent::ObjectForm($site);
		$this->pageId		= $this->site->getSubmittedVar('page_id', 0);
		$this->parentId	= $this->site->getSubmittedVar('parent_id', 0);
		$this->action 	= $this->site->getSubmittedVar('mode');

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

		if (!($lid = $site->getSubmittedVar('lid', 0))) {
			if (!($lid = $site->getPersistentVar('lid', 0))) {
				$lid = $this->site->getDefaultLangId();
			}
		}

		$this->objectManager = &$this->site->getPagesManager();

		if($this->pageId == 0) {
			$pagesManager = &$this->site->getPagesManager();
			if($this->parentId) {
				$parentPage = &$pagesManager->getPage($this->parentId, false, false, false);
				$aclId = $parentPage->getPageAclId();
			}
			else {
				$aclId = $this->site->getRootAclId();
			}
			$page = &$pagesManager->getNewPage($aclId, $this->parentId);
		}
		else {
			$page = &$this->objectManager->getPage($this->pageId);
			if($this->parentId == 0) {
  	  	$this->parentId = $page->getPageParentId();
			}
		}
		$this->object = &$page;
	}

	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_PAGE:
				break;
			case MODIF_PAGE:
				if($acl->userHasEditRight($user)) {
					$this->processFormModif();
				}
				break;
			case DELETE_PAGE:
				if($acl->userHasRemoveRight($user)) {
					$this->processFormDelete();
				}
				break;
		}
	}

	function processFormModif() {
		$this->object->setPageValues($_POST);
		$this->object->setPageCache(0);
		$this->objectManager->setPage($this->object);

		if (!$this->objectManager->storePage($this->object->getPageId())) {
			return false;
		}
		return true;
	}

	function processFormDelete() {
		return $this->objectManager->removePage($this->pageId);
	}

	function spreadAcl($aclId) {
		return $this->objectManager->updatePageACL($this->object->getPageId(), $aclId);
	}
}
?>