<?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 includes
 */
include_once('config.inc.php');
include_once('classes/core/Site.class.php');
include_once('classes/core/forms/ACLForm.class.php');
include_once('classes/core/forms/EditionForm.class.php');
include_once('interface/egSubWindow.class.php');
/**#@-*/

/**
 * ACL Edition Form
 *
 * Allows granted user to modify existing permissions and
 * to add or remove groups and users
 *
 * @author     vinadelmar <vinadelmar@users.sourceforge.net>
 * @since      2006-04-25
 * @version    1.0.0
 * @package    Core
 * @subpackage Forms
 */
class ACLEditionForm extends EditionForm {

	var $groupsManager;
	var $usersManager;

	/**
	 * Constructor
	 *
	 * @access public
	 * @param object $acl
	 * @param string $mode
	 */
	function ACLEditionForm(&$acl, &$aclManager, &$usersManager, &$groupsManager) {
		parent::EditionForm($acl, $aclManager);
		$this->usersManager = &$usersManager;
		$this->groupsManager = &$groupsManager;
	}

	function populateUIContainer(&$guiContainer, $parentObjAclId) {
		$acl = &$this->object;
		$objectsInAclList = &new egPropertyList("ppl_1", "list", 10, 30, 480, 160);
		$txtList = &new egStatic("st_rights_1", LANG_ACL_USERSGROUPS_STATIC." : (".$this->object->getAclId().")", 10, 5, 200, 0, true);

		$guiContainer->addObject($objectsInAclList);
		$guiContainer->addObject($txtList);

		// need to define some types for javascript in child window (in case ACL is empty)
		$typesDefinitionContainer = &new egContainer("typedef_ctn", 0, 0, 1, 1);
		$checkBoxType = &new egCheckBox("typedef_cb", "", 1, 1, 1, null, "", 1);
		$staticTextType = &new egStatic("typedef_st", "", 1, 1, 1, 0);
		$typesDefinitionContainer->addObject($checkBoxType);
		$typesDefinitionContainer->addObject($staticTextType);
		$guiContainer->addContainer($typesDefinitionContainer);

		if (isset($parentObjAclId) && ($acl->getAclId() != $parentObjAclId)) {
			$txtInherit = &new egStatic("st_acl_inherit", LANG_ACL_INHERIT_STATIC, 30, 200, 250);
			$cbInherit  = &new egCheckBox("cb_acl_inherit", "", 10, 200, 10, null, "inherit", 1);
			$guiContainer->addObject($txtInherit);
			$guiContainer->addObject($cbInherit);
		}

		$addButton = &new egPressbutton("btn_add", LANG_ADD_BUTTON, 325, 200, 80, null, "addAclEntry();");
		$removeButton = &new egPressbutton("btn_remove", LANG_REMOVE_BUTTON, 415, 200, 80, null, "removeAclEntry();");

		$guiContainer->addObject($addButton);
		$guiContainer->addObject($removeButton);

		// add default controls
		$aclContainerTmp = &new egContainer("acl_ctn_null", 0, 230, 500, 260, true);
		$aclContainerSub = &new egContainer("acl_sub_ctn_null", 11, 31, 478, 213, true);

		$frame = &new egFrame("fra_null", "frame", 10, 30, 480, 215);
		$txtAllow = &new egStatic("st_allow_null", LANG_ACL_ALLOW_STATIC, 370, 5, 60, 0, true);
		$txtDeny  = &new egStatic("st_deny_null", LANG_ACL_DENY_STATIC, 430, 5, 60, 0, true);
		$txtObjName  = &new egStatic("st_obname_null", LANG_ACL_PERMISSION_STATIC, 10, 5, 300, 0, true);

		$aclContainerTmp->addObject($frame);
		$aclContainerTmp->addObject($txtObjName);
		$aclContainerTmp->addObject($txtAllow);
		$aclContainerTmp->addObject($txtDeny);

		$txtView    = &new egStatic("st_view_null", LANG_ACL_VIEW_STATIC, 5, 5, 150);
		$txtEdit    = &new egStatic("st_edit_null", LANG_ACL_EDIT_STATIC, 5, 30, 150);
		$txtPublish = &new egStatic("st_pub_null", LANG_ACL_PUBLISH_STATIC, 5, 55, 150);
		$txtRemove  = &new egStatic("st_rem_null", LANG_ACL_REMOVE_STATIC, 5, 80, 150);
		$txtAdmin   = &new egStatic("st_adm_null", LANG_ACL_ADMIN_STATIC, 5, 105, 150);

		$aclContainerSub->addObject($txtView);
		$aclContainerSub->addObject($txtEdit);
		$aclContainerSub->addObject($txtPublish);
		$aclContainerSub->addObject($txtRemove);
		$aclContainerSub->addObject($txtAdmin);
		$aclContainerTmp->addContainer($aclContainerSub);

		$guiContainer->addContainer($aclContainerTmp);


		$i = 0;
		$aclContainers[] = array();
		// list needed to generate javascript : to keep a track of the current entries in the dialog
		$aclEntries = '';

		if ($acl->getNumGroupsAclEntry()) {
			$groupsArray = $this->groupsManager->getGroupsListForDisplay();

			if (!is_null($groupsArray) && !empty($groupsArray)) {
				$groupsEntry = &$acl->getGroupsAclEntryTable();

				foreach ($groupsArray as $groupId => $groupName) {
					if (!isset($groupsEntry[$groupId])) continue;

					if(strlen($aclEntries)) {
						$aclEntries .= ',';
					}
					$aclEntries .= "'g_$groupId'";

					$rights  = $groupsEntry[$groupId]->getValues();

					$view_allow    = ($rights['view'] == RIGHT_ALLOW)?true:false;
					$edit_allow    = ($rights['edit'] == RIGHT_ALLOW)?true:false;
					$publish_allow = ($rights['publish'] == RIGHT_ALLOW)?true:false;
					$remove_allow  = ($rights['remove'] == RIGHT_ALLOW)?true:false;
					$admin_allow   = ($rights['admin'] == RIGHT_ALLOW)?true:false;
					$view_deny     = ($rights['view'] == RIGHT_DENY)?true:false;
					$edit_deny     = ($rights['edit'] == RIGHT_DENY)?true:false;
					$publish_deny  = ($rights['publish'] == RIGHT_DENY)?true:false;
					$remove_deny   = ($rights['remove'] == RIGHT_DENY)?true:false;
					$admin_deny    = ($rights['admin'] == RIGHT_DENY)?true:false;

					$aclContainers[$i] = &new egContainer("acl_ctn_".$i, 0, 230, 500, 260);
					$aclContainersSub = &new egContainer("acl_sub_ctn_".$i, 11, 31, 478, 213, true);
					$frame = &new egFrame("fra_".$i, "frame ".$i, 10, 30, 480, 215);
					$txtObjName  = &new egStatic("st_obname_".$i, LANG_ACL_PERMISSION_STATIC." ".$groupName.LANG_COLON, 10, 5, 350, 0, true);

					$aclContainers[$i]->addObject($frame);
					$aclContainers[$i]->addObject($txtObjName);

					$txtView    = &new egStatic("st_view_".$i, LANG_ACL_VIEW_STATIC, 5, 5, 150);
					$txtEdit    = &new egStatic("st_edit_".$i, LANG_ACL_EDIT_STATIC, 5, 30, 150);
					$txtPublish = &new egStatic("st_pub_".$i, LANG_ACL_PUBLISH_STATIC, 5, 55, 150);
					$txtRemove  = &new egStatic("st_rem_".$i, LANG_ACL_REMOVE_STATIC, 5, 80, 150);
					$txtAdmin   = &new egStatic("st_adm_".$i, LANG_ACL_ADMIN_STATIC, 5, 105, 150);

					$viewRigthContainer = &new egContainer("view_ctn_".$i, 375, 5, 100, 20, true);
					$cbViewAllow = &new egCheckBox("cb_view_a_".$i, "", 0, 0, 10, null, "acl_groups[".$groupId."][view]", RIGHT_ALLOW, $view_allow, false);
					$cbViewDeny  = &new egCheckBox("cb_view_d_".$i, "", 63, 0, 10, null, "acl_groups[".$groupId."][view]", RIGHT_DENY, $view_deny, false);
					$viewRigthContainer->addObject($cbViewAllow);
					$viewRigthContainer->addObject($cbViewDeny);

					$editRigthContainer = &new egContainer("edit_ctn_".$i, 375, 30, 100, 20, true);
					$cbEditAllow = &new egCheckBox("cb_edit_a_".$i, "", 0, 0, 10, null, "acl_groups[".$groupId."][edit]", RIGHT_ALLOW, $edit_allow, false);
					$cbEditDeny  = &new egCheckBox("cb_edit_d_".$i, "", 63, 0, 10, null, "acl_groups[".$groupId."][edit]", RIGHT_DENY, $edit_deny, false);
					$editRigthContainer->addObject($cbEditAllow);
					$editRigthContainer->addObject($cbEditDeny);

					$publishRigthContainer = &new egContainer("publish_ctn_".$i, 375, 55, 100, 20, true);
					$cbPublishAllow = &new egCheckBox("cb_pub_a_".$i, "", 0, 0, 10, null, "acl_groups[".$groupId."][publish]", RIGHT_ALLOW, $publish_allow, false);
					$cbPublishDeny  = &new egCheckBox("cb_pub_d_".$i, "", 63, 0, 10, null, "acl_groups[".$groupId."][publish]", RIGHT_DENY, $publish_deny, false);
					$publishRigthContainer->addObject($cbPublishAllow);
					$publishRigthContainer->addObject($cbPublishDeny);

					$removeRigthContainer = &new egContainer("remove_ctn_".$i, 375, 80, 100, 20, true);
					$cbRemoveAllow  = &new egCheckBox("cb_rem_a_".$i, "", 0, 0, 10, null, "acl_groups[".$groupId."][remove]", RIGHT_ALLOW, $remove_allow, false);
					$cbRemoveDeny   = &new egCheckBox("cb_rem_d_".$i, "", 63, 0, 10, null, "acl_groups[".$groupId."][remove]", RIGHT_DENY, $remove_deny, false);
					$removeRigthContainer->addObject($cbRemoveAllow);
					$removeRigthContainer->addObject($cbRemoveDeny);

					$adminRigthContainer = &new egContainer("admin_ctn_".$i, 375, 105, 100, 20, true);
					$cbAdminAllow   = &new egCheckBox("cb_adm_a_".$i, "", 0, 0, 10, null, "acl_groups[".$groupId."][admin]", RIGHT_ALLOW, $admin_allow, false);
					$cbAdminDeny    = &new egCheckBox("cb_adm_d_".$i, "", 63, 0, 10, null, "acl_groups[".$groupId."][admin]", RIGHT_DENY, $admin_deny, false);
					$adminRigthContainer->addObject($cbAdminAllow);
					$adminRigthContainer->addObject($cbAdminDeny);

					$aclContainersSub->addContainer($viewRigthContainer);
					$aclContainersSub->addContainer($editRigthContainer);
					$aclContainersSub->addContainer($publishRigthContainer);
					$aclContainersSub->addContainer($removeRigthContainer);
					$aclContainersSub->addContainer($adminRigthContainer);

					$aclContainersSub->addObject($txtView);
					$aclContainersSub->addObject($txtEdit);
					$aclContainersSub->addObject($txtPublish);
					$aclContainersSub->addObject($txtRemove);
					$aclContainersSub->addObject($txtAdmin);

					$aclContainers[$i]->addContainer($aclContainersSub);

					$objectsInAclList->addItem(new egPropertyPage("ptp_".$i, LANG_ACL_GROUP_STATIC." ".$groupName, $aclContainers[$i]->getContainerName()));
					$guiContainer->addContainer($aclContainers[$i]);

					++$i;
				}
			}
		}

		if ($acl->getNumUsersAclEntry()) {
			$get_groups = false;
			$usersArray = $this->usersManager->getUsersArrayForDisplay($get_groups, $acl->getUsersIdList());

			if (!is_null($usersArray) && !empty($usersArray)) {
				$usersEntry = &$acl->getUsersAclEntryTable();

				foreach ($usersArray as $userId => $userLogin) {
					if (!isset($usersEntry[$userId])) continue;

					if(strlen($aclEntries)) {
						$aclEntries .= ',';
					}
					$aclEntries .= "'u_$userId'";

					$rights = $usersEntry[$userId]->getValues();

					$view_allow    = ($rights['view'] == RIGHT_ALLOW)?true:false;
					$edit_allow    = ($rights['edit'] == RIGHT_ALLOW)?true:false;
					$publish_allow = ($rights['publish'] == RIGHT_ALLOW)?true:false;
					$remove_allow  = ($rights['remove'] == RIGHT_ALLOW)?true:false;
					$admin_allow   = ($rights['admin'] == RIGHT_ALLOW)?true:false;
					$view_deny     = ($rights['view'] == RIGHT_DENY)?true:false;
					$edit_deny     = ($rights['edit'] == RIGHT_DENY)?true:false;
					$publish_deny  = ($rights['publish'] == RIGHT_DENY)?true:false;
					$remove_deny   = ($rights['remove'] == RIGHT_DENY)?true:false;
					$admin_deny    = ($rights['admin'] == RIGHT_DENY)?true:false;

					$aclContainers[$i] = &new egContainer("acl_ctn_".$i, 0, 230, 500, 260);
					$aclContainersSub = &new egContainer("acl_sub_ctn_".$i, 11, 31, 478, 213, true);
					$frame = &new egFrame("fra_".$i, "frame ".$i, 10, 30, 480, 215);
					$txtObjName  = &new egStatic("st_obname_".$i, LANG_ACL_PERMISSION_STATIC." ".$userLogin.LANG_COLON, 10, 5, 350, 0, true);

					$aclContainers[$i]->addObject($frame);
					$aclContainers[$i]->addObject($txtObjName);

					$txtView    = &new egStatic("st_view_".$i, LANG_ACL_VIEW_STATIC, 5, 5, 150);
					$txtEdit    = &new egStatic("st_edit_".$i, LANG_ACL_EDIT_STATIC, 5, 30, 150);
					$txtPublish = &new egStatic("st_pub_".$i, LANG_ACL_PUBLISH_STATIC, 5, 55, 150);
					$txtRemove  = &new egStatic("st_rem_".$i, LANG_ACL_REMOVE_STATIC, 5, 80, 150);
					$txtAdmin   = &new egStatic("st_adm_".$i, LANG_ACL_ADMIN_STATIC, 5, 105, 150);

					$viewRigthContainer = &new egContainer("view_ctn_".$i, 375, 5, 100, 20, true);
					$cbViewAllow = &new egCheckBox("cb_view_a_".$i, "", 0, 0, 10, null,  "acl_users[".$userId."][view]", RIGHT_ALLOW, $view_allow, false);
					$cbViewDeny  = &new egCheckBox("cb_view_d_".$i, "", 63, 0, 10, null, "acl_users[".$userId."][view]", RIGHT_DENY, $view_deny, false);
					$viewRigthContainer->addObject($cbViewAllow);
					$viewRigthContainer->addObject($cbViewDeny);

					$editRigthContainer = &new egContainer("edit_ctn_".$i, 375, 30, 100, 20, true);
					$cbEditAllow = &new egCheckBox("cb_edit_a_".$i, "", 0, 0, 10, null, "acl_users[".$userId."][edit]", RIGHT_ALLOW, $edit_allow, false);
					$cbEditDeny  = &new egCheckBox("cb_edit_d_".$i, "", 63, 0, 10, null, "acl_users[".$userId."][edit]", RIGHT_DENY, $edit_deny, false);
					$editRigthContainer->addObject($cbEditAllow);
					$editRigthContainer->addObject($cbEditDeny);

					$publishRigthContainer = &new egContainer("publish_ctn_".$i, 375, 55, 100, 20, true);
					$cbPublishAllow = &new egCheckBox("cb_pub_a_".$i, "", 0, 0, 10, null, "acl_users[".$userId."][publish]", RIGHT_ALLOW, $publish_allow, false);
					$cbPublishDeny  = &new egCheckBox("cb_pub_d_".$i, "", 63, 0, 10, null, "acl_users[".$userId."][publish]", RIGHT_DENY, $publish_deny, false);
					$publishRigthContainer->addObject($cbPublishAllow);
					$publishRigthContainer->addObject($cbPublishDeny);

					$removeRigthContainer = &new egContainer("remove_ctn_".$i, 375, 80, 100, 20, true);
					$cbRemoveAllow  = &new egCheckBox("cb_rem_a_".$i, "", 0, 0, 10, null, "acl_users[".$userId."][remove]", RIGHT_ALLOW, $remove_allow, false);
					$cbRemoveDeny   = &new egCheckBox("cb_rem_d_".$i, "", 63, 0, 10, null, "acl_users[".$userId."][remove]", RIGHT_DENY, $remove_deny, false);
					$removeRigthContainer->addObject($cbRemoveAllow);
					$removeRigthContainer->addObject($cbRemoveDeny);

					$adminRigthContainer = &new egContainer("admin_ctn_".$i, 375, 105, 100, 20, true);
					$cbAdminAllow   = &new egCheckBox("cb_adm_a_".$i, "", 0, 0, 10, null, "acl_users[".$userId."][admin]", RIGHT_ALLOW, $admin_allow, false);
					$cbAdminDeny    = &new egCheckBox("cb_adm_d_".$i, "", 63, 0, 10, null, "acl_users[".$userId."][admin]", RIGHT_DENY, $admin_deny, false);
					$adminRigthContainer->addObject($cbAdminAllow);
					$adminRigthContainer->addObject($cbAdminDeny);

					$aclContainersSub->addContainer($viewRigthContainer);
					$aclContainersSub->addContainer($editRigthContainer);
					$aclContainersSub->addContainer($publishRigthContainer);
					$aclContainersSub->addContainer($removeRigthContainer);
					$aclContainersSub->addContainer($adminRigthContainer);

					$aclContainersSub->addObject($txtView);
					$aclContainersSub->addObject($txtEdit);
					$aclContainersSub->addObject($txtPublish);
					$aclContainersSub->addObject($txtRemove);
					$aclContainersSub->addObject($txtAdmin);

					$aclContainers[$i]->addContainer($aclContainersSub);

					$objectsInAclList->addItem(new egPropertyPage("ptp_".$i, LANG_ACL_USER_STATIC." ".$userLogin, $aclContainers[$i]->getContainerName()));
					$guiContainer->addContainer($aclContainers[$i]);

					++$i;
				}
			}
		}

		// hidden frame for controling and populating the acl entries list
		$subWindow = &new egSubWindow("control_frame", "admin/add_acl_entry.php", 0, 0, 0, 0);
		$guiContainer->addObject($subWindow);

		$scriptLine = "
var acl_entries = new Array(".$aclEntries.");

function addAclEntry() {
  propertyList_ppl_1.unselectAll();
	openWindow('admin/add_acl_entry.php?parent_container_name=".$guiContainer->getContainerName()."&acl_entries='+acl_entries,380,190, 0, 0);
}

function removeAclEntry() {
	var index = propertyList_ppl_1.getValue();
	acl_entries[index] = 0;
	propertyList_ppl_1.removeListItem(index);
}
";

	  return $scriptLine;
	}

}
?>