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

/**
 * required classes
 */
include_once('config.inc.php');
include_once('classes/core/Site.class.php');

include_once('classes/core/forms/ObjectForm.class.php');
include_once('classes/core/ACL/ACLManager.class.php');
include_once('classes/core/ACL/ACL.class.php');
include_once('classes/core/ACL/ACLEntry.class.php');
include_once('classes/core/groups/GroupsManager.class.php');
include_once('classes/core/users/UsersManager.class.php');


/**
 * ACL Form
 *
 * @author  vinadelmar <vinadelmar@users.sourceforge.net>
 * @since   2006-02-22
 * @version 1.1.0d
 * @todo    write better class description
 */
class ACLForm extends ObjectForm {

	var $parentObjAclId;
	var $pageId;

	/**
	 * Class Constructor
	 *
	 * @param object $acl instance of ACL
	 */
	function ACLForm(&$site) {
		parent::ObjectForm($site);
		$pageId 					= $this->site->getSubmittedVar('page_id', 0);
		$parentId 				= $this->site->getSubmittedVar('parent_id', 0);
		$containersListId = $this->site->getSubmittedVar('containers_list_id', 0);
		$containerId 			= $this->site->getSubmittedVar('container_id', 0);
		$menuId 					= $this->site->getSubmittedVar('menu_id', 0);
		$contentId   			= $this->site->getSubmittedVar('content_id', 0);
		$contentType 			= $this->site->getSubmittedVar('type_name');
		$display          = $this->site->getSubmittedVar('display');
		$mode							= $this->site->getSubmittedVar('mode');

		$this->objectManager = &$this->site->getACLManager();
		$this->action = $mode;
		$this->pageId = 0;

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

		switch($display) {
			case EDIT_CONTENT:
        if(isset($containerId) && $containerId > 0) {
  				$containersManager = &$this->site->getContainersManager();
          if($container = &$containersManager->getContainer($containerId, false)) {
            if(($cloneId = $container->getContainerCloneId()) > 0) {
              if($cloneContainer = &$containersManager->getContainer($cloneId, false)) {
	   				    $this->parentObjAclId = $cloneContainer->getContainerAclId();
              }
              else {
                // error
						    $this->parentObjAclId = $this->site->getRootAclId();
              }
            }
            else {
	   				  $this->parentObjAclId = $container->getContainerAclId();
            }
					}
					else {
            // error
						$this->parentObjAclId = $this->site->getRootAclId();
					}
        }

				if($contentId == 0) {
					// new content : get the parent object ACL id
					if($container) {
						$aclId = $container->getContainerAclId();
					}
					else {
						$aclId = $this->site->getRootAclId();
					}
				}
				else {
					$contentsManager = &$this->site->getContentsManager();
					$content = &$contentsManager->getContent($contentId, $contentType);
					$aclId = $content->getContentAclId();
				}
				$this->object = &$this->objectManager->getACL($aclId);
				break;
			case EDIT_CONTAINER:
				if($containerId == 0) {
					// new container : get the parent object ACL id
					if($containersListId > 0) {
						$containersListsManager = &$this->site->getContainersListsManager();
						$containersList = &$containersListsManager->getContainersList($containersListId, false);
						$this->parentObjAclId = $containersList->getContainersListAclId();
					 	$aclId = $containersList->getContainersListAclId();
					}
					else if($pageId > 0) {
						$pagesManager = &$this->site->getPagesManager();
						$page = &$pagesManager->getPage($pageId, false);
						$this->parentObjAclId = $page->getPageAclId();
				 		$aclId = $page->getPageAclId();
					}
				}
				else {
					$containersManager = &$this->site->getContainersManager();
					$container = &$containersManager->getContainer($containerId, false);
					//retrieve parentAclId from the container object
					$familyManager = &$this->site->getFamilyManager();
					$this->parentObjAclId = $familyManager->getParentObjAclId($container);
					$aclId = $container->getContainerAclId();
				}
				break;
			case EDIT_MENU:
				if($menuId == 0) {
					// new menu
					$aclId = $this->site->getRootAclId();
				  $this->parentObjAclId = $aclId;
				}
				else {
					$menusManager = &$this->site->getMenusManager();
					$menu = &$menusManager->getMenu($menuId, false);
  				//retrieve parentAclId from the menu object
          $familyManager = &$this->site->getFamilyManager();
          $this->parentObjAclId = $familyManager->getParentObjAclId($menu);
					$aclId = $menu->getMenuAclId();
				}
				break;
			case EDIT_CONTAINERS_LIST:
				if($containersListId == 0) {
					// new containersList : get the parent object ACL id
          if($pageId > 0) {
						$pagesManager = &$this->site->getPagesManager();
						$page = &$pagesManager->getPage($pageId, false);
						$this->parentObjAclId = $page->getPageAclId();
						$aclId = $page->getPageAclId();
					}
				}
				else {

					$containersListsManager = &$this->site->getContainersListsManager();
					$containersList = &$containersListsManager->getContainersList($containersListId, false);
					//retrieve parentAclId from the container object
					$familyManager = &$this->site->getFamilyManager();
					$this->parentObjAclId = $familyManager->getParentObjAclId($containersList);
					$aclId = $containersList->getContainersListAclId();
				}
				break;
			case EDIT_PAGE:
				$pagesManager = &$this->site->getPagesManager();
				$this->pageId = $pageId;

				if($pageId == 0) {
				// new page : get the parent object ACL id
					if($parentId > 0) {
						$parentPage = &$pagesManager->getPage($parentId, false, false, false);
						$this->parentObjAclId = $parentPage->getPageAclId();
						$aclId = $parentPage->getPageAclId();
					}
					else {
						$aclId = $this->site->getRootAclId();
					}
				}
				else {
					$page = &$pagesManager->getPage($pageId, false);
					$aclId = $page->getPageAclId();
					if($parentId == 0) {
	    			$parentId = $page->getPageParentId();
					}
					if($parentId > 0) {
						$parentPage = &$pagesManager->getPage($parentId, false, false, false);
						$this->parentObjAclId = $parentPage->getPageAclId();
					}
				}

				break;
			default :
				// error
        return null;
		}

		$this->object = &$this->objectManager->getACL($aclId);
	}

	function getParentObjAclId() {
		return $this->parentObjAclId;
	}

	/**
	 * @return integer aclId or 0 if error occurs
	 */
	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 MODIF_PAGE:
			case MODIF_CONTAINERS_LIST:
			case MODIF_CONTAINER:
			case MODIF_MENU:
			case MODIF_CONTENT:
				if($acl->userHasAdminRight($user)) {
					return $this->processFormModif();
				}
				break;
			case DELETE_CONTENT:
				return $this->processFormDelete();
				break;
		}
	}
  /**
   *
   * @returns -1 if error; 0 if no changes, new aclId otherwise
   */
	function processFormModif() {
		$postedAcl = &$this->objectManager->getNewACL();

		$restoreInheritance = $this->site->getSubmittedVar('inherit', false);

		$objAclId = $this->object->getAclId();

		if ($restoreInheritance) {
			if (!isset($this->parentObjAclId)) {
				return -1;
			}
			$this->objectManager->deleteACL($objAclId);
			return $this->parentObjAclId;
		}

    if (!isset($_POST['acl_users']) && !isset($_POST['acl_groups'])) {
			return -1;
		}

		//rebuild acl from posted values
    if (isset($_POST['acl_users'])) {
			foreach($_POST['acl_users'] as $id => $aclRecord) {
				$aclRecord['user_id'] = $id;
				$postedAcl->setUserAclEntry($id, $aclRecord);
			}
    }

    if (isset($_POST['acl_groups'])) {
			foreach($_POST['acl_groups'] as $id => $aclRecord) {
				$aclRecord['group_id'] = $id;
				$postedAcl->setGroupAclEntry($id, $aclRecord);
			}
    }

		// no changes were done : stop processing here
		if (($cmp=$postedAcl->compareTo($this->object)) == 0) {
			return 0;
		}

		// change has been made
		// there is a parentObject and current object is not the root page: need a new ACL
		if (($this->pageId != $this->site->getRootPageId()) && isset($this->parentObjAclId) && ($objAclId == $this->parentObjAclId)) {
			if(!($objAclId = $this->objectManager->storeACL(0))) {
				// error : unable to store new ACL
				return -1;
			}
		}
		// there is no parent object : store changes
		else {
			$postedAcl->setAclId($objAclId);
			$this->objectManager->setACL($postedAcl, true);
			if (!$this->objectManager->storeACL($objAclId)) {
				return -1;
			}
			return 0;
		}

		return $objAclId;
	}

	function processFormDelete() {
	}

}
?>