<?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/Site.class.php');
include_once('classes/core/forms/EditionForm.class.php');
include_once('classes/core/types/TypesManager.class.php');
include_once('classes/core/ACL/ACL.class.php');
include_once('classes/core/containers/Container.class.php');
include_once('classes/core/containers/ContainersManager.class.php');
include_once('classes/core/containersLists/ContainersList.class.php');
include_once('classes/core/containersLists/ContainersListsManager.class.php');

include_once('classes/core/groups/GroupsManager.class.php');
include_once('classes/core/users/UsersManager.class.php');

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

include_once('interface/egScript.class.php');
include_once('interface/egManager.class.php');
include_once('interface/egContainer.class.php');
include_once('interface/egMenuItem.class.php');
include_once('interface/egFrame.class.php');
include_once('interface/egToolTip.class.php');
include_once('interface/egRichText.class.php');
include_once('interface/egLink.class.php');
include_once('interface/egStatic.class.php');
include_once('interface/egEdit.class.php');
include_once('interface/egCheckBox.class.php');
include_once('interface/egRadioButton.class.php');
include_once('interface/egPressButton.class.php');
include_once('interface/egDropDownListBox.class.php');
include_once('interface/egDropDownPropertyList.class.php');
include_once('interface/egPropertyList.class.php');
include_once('interface/egPropertySheet.class.php');
include_once('interface/egPropertyPage.class.php');


/**
 * ContainerEditionForm : edition form for Containers
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	   ContainerEditionForm.class, Sun Dec 26 15:40:52 CET 2004
 * @package	   Core
 * @subpackage Forms
 */
class ContainerEditionForm extends EditionForm {
	/**
	 * Class constructor
	 *
	 * @access   public
 	 * @param    string    The mode in which the form is to be displayed.
 	 * @return   void
	 */
	function ContainerEditionForm(&$container, &$containersManager) {
		parent::EditionForm($container, $containersManager);
	}


	function populateUIContainer(&$guiContainer, $currLangId, $action, $error=0) {
		if($action == ASK_DELETE_CONTAINER) {
			if($error) {
				$txtLang = &new egStatic("sta_1", LANG_CONTAINER_DELETION_DENIED, 20, 20, 300, 0);
				$guiContainer->addObject($txtLang);
			}
			else {
				$summary = "";
				$summary .= "<b>".LANG_CONTAINER_IDENTIFIER_STATIC." :</b> ".$this->object->getContainerId();
				$summary .= "<br /><b>".LANG_CONTAINER_NAME_STATIC." :</b> ".$this->object->getContainerName($currLangId);
				$txtLang = &new egStatic("sta_1", LANG_CONTAINER_DELETION_MESSAGE.LANG_COLON."<br /><br />".$summary."<br /><br />".LANG_CONTAINER_DELETION_CONFIRM, 20, 20, 300, 0);
				$guiContainer->addObject($txtLang);
			}
		}
		else {
			$valuesTable = &$this->object->getContainerValuesTable();
			$langsArray = $this->objectManager->getLangsArray();

			$langList = &new egDropDownPropertyList("ddl_lang_1", "Language", 400, 10, 80);
			$txtLang = &new egStatic("sta_1", LANG_LANGUAGE_LISTBOX.' :', 330, 10, 80, 0, true);

      $show_edition_containers = true;

			if(count($langsArray) > 1) {
				$guiContainer->addObject($langList);
				$guiContainer->addObject($txtLang);
        $show_edition_containers = false;
			}


	    $contentsManager = &$this->objectManager->contentsManager;
	    $contentTypesFieldsArray = &$contentsManager->getContentTypesFields();
	    $contentTypesTemplatesArray = &$contentsManager->getContentTypesTemplates();


			$defaultSortField = '';
			$defaultTemplate = '';
			$defaultTypeName = '';


	    // look for the first defined language
			foreach ($langsArray as $langId => $langName) {
	      if(isset($valuesTable[$langId])) {
	        $referenceLangId = $langId;
	        break;
	      }
	    }


	 		$previousTop = 30;


			$idText = &new egStatic("st_value_id", LANG_CONTAINER_IDENTIFIER_STATIC.' :', 5, $previousTop, 80, 0, true);
			$idValue = &new egStatic("obj_item_id", $valuesTable['id'], 90, $previousTop, 80, 0);
	 		$previousTop += 30;


			foreach ($langsArray as $langId => $langName) {
	      $editionContainer = &new egContainer('edt_ctn_'.$langId, 5, $previousTop, 500, 50, $show_edition_containers);
	      $guiContainer->addContainer($editionContainer);
				$langList->addListItem(new egPropertyPage('lti_'.$langId, ucfirst($langName), $editionContainer->getContainerName()));


	      if($langId == $currLangId) {
	        $langList->setSelectedItemByLabel(ucfirst($langName));
	      }


	      // create same fields for this lang than in the reference language

	      if(!isset($valuesTable[$langId])) {
	        if(!isset($referenceLangId)) {
						$valuesTable[$langId]['name'] = '';
	        }
					else {
		        foreach ($valuesTable[$referenceLangId] as $field => $value) {
	  	        $valuesTable[$langId][$field] = '';
	    	    }
					}
	      }


			  $nameText = &new egStatic("st_value_name_".$langId, LANG_CONTAINER_NAME_STATIC.' :', 0, 0, 80, 0, true);
				$nameValue = &new egEdit("obj_item_name_".$langId, $valuesTable[$langId]['name'], 85, 0, 390, 18, $langId.'[name]');


				$checkBox = &new egCheckBox("ckb_name_".$langId, '<i>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</i>', 85, 22, 200, null, $langId.'[name_all_languages]', 1, false);


				$editionContainer->addObject($nameText);
				$editionContainer->addObject($nameValue);


				$editionContainer->addObject($checkBox);
	    }


			$previousTop += 50;


			if($this->object->getContainerType() == CONTAINER_TYPE_PHOTO_GALLERY) {
    		$repositoryText = &new egStatic("st_value_repository", 'Repository :', 5, $previousTop, 80, 0, true);
				$repositoryValue = &new egEdit("obj_item_repository", $valuesTable['repository'], 90, $previousTop, 300, 18, 'repository');
				$repositoryButton = &new egPressbutton("obj_item_repository_button", LANG_BROWSE_BUTTON, 400, $previousTop, 80, null, "openWindow(\'admin/select_directory.php?parent_control_name=obj_item_repository_value&repository_type=".IMAGES_FOLDER."\',425,450,0,0);");

				$guiContainer->addObject($repositoryText);
				$guiContainer->addObject($repositoryValue);
				$guiContainer->addObject($repositoryButton);
		 		$previousTop += 50;
			}


			$typeText = &new egStatic("st_value_type", LANG_CONTAINER_CONTAINS_STATIC.' :', 5, $previousTop, 80, 0, true);


	    if($this->object->getContainerId() == 0){
	      // let's choose the content type
	      $hiddenValuesContainer = &new egContainer("ctn_edt_ctn_hidden_values", 0, 0, 1, 1);


			  $typeValue = &new egDropDownPropertyList("obj_item_type", "Type", 90, $previousTop, 130);


	      $templateText = &new egStatic("st_value_template", LANG_CONTAINER_TEMPLATE_STATIC.' :', 265, $previousTop, 80, 0, true);
	      $guiContainer->addObject($templateText);


				switch($this->object->getContainerType()) {
				  case CONTAINER_TYPE_NORMAL:
	    			if(isset($contentTypesFieldsArray['gallery_photo'])) {
	    			  unset($contentTypesFieldsArray['gallery_photo']);
	    			}
	    			if(isset($contentTypesFieldsArray['mailing_list'])) {
	    			  unset($contentTypesFieldsArray['mailing_list']);
	    			}
	    			if(isset($contentTypesFieldsArray['mailing_message'])) {
	    			  unset($contentTypesFieldsArray['mailing_message']);
	    			}
	    			if(isset($contentTypesFieldsArray['mailing_subscriber'])) {
	    			  unset($contentTypesFieldsArray['mailing_subscriber']);
	    			}
			      break;
					case CONTAINER_TYPE_PHOTO_GALLERY:
						foreach($contentTypesFieldsArray as $contentType => $fieldsArray) {
						  if($contentType != 'gallery_photo') {
						    unset($contentTypesFieldsArray[$contentType]);
						  }
						}
					  break;
				}


				foreach($contentTypesFieldsArray as $contentType => $fieldsArray) {
					if(!strlen($defaultTypeName)) {
						$defaultTypeName = $contentType;
					}


	        $contentTemplateContainer = &new egContainer('edt_template_ctn'.$contentType, 350, $previousTop, 140, 300);
	        $guiContainer->addContainer($contentTemplateContainer);


	      	$templateValue = &new egDropDownListBox("obj_item_template_".$contentType, 0, 0, 130, null);
					$contentTypeDefaultTemplate = '';
	        foreach($contentTypesTemplatesArray[$contentType] as $id => $templateName) {
						if(!strlen($defaultTemplate)) {
							$defaultTemplate = $id;
						}
						if(!strlen($contentTypeDefaultTemplate)) {
							$contentTypeDefaultTemplate = $id;
						}
	          $templateValue->addListItem(new egListItem("lti_templates_".$contentType, $templateName, null, null, "chooseTemplate($id);"));
	        }
	        $templateValue->setSelectedItemByIndex(0);
	        $contentTemplateContainer->addObject($templateValue);
	  			$typeValue->addListItem(new egPropertyPage('lti_types'.$contentType, $contentType, $contentTemplateContainer->getContainerName(), "chooseTypeName(\'$contentType\');chooseTemplate($contentTypeDefaultTemplate);"));
	      }


				$objSortField = &new egEdit("hidden_control_sortField", $defaultSortField, 0, 0, 0, 0, "sort_field");
				$objSortOrder = &new egEdit("hidden_control_sortOrder", SORT_ASCENDING, 0, 0, 0, 0, "sort_order");
				$objSortingType = &new egEdit("hidden_control_sortingType", SORTING_BY_LIST, 0, 0, 0, 0, "sorting_type");
	      $objTemplateId = &new egEdit("hidden_control_template_id", $defaultTemplate, 0, 0, 0, 0, "template_id");
	      $objTypeName = &new egEdit("hidden_control_type_name", $defaultTypeName, 0, 0, 0, 0, "type_name");

	      $hiddenValuesContainer->addObject($objTemplateId);
	      $hiddenValuesContainer->addObject($objTypeName);
				$hiddenValuesContainer->addObject($objSortField);
				$hiddenValuesContainer->addObject($objSortOrder);
				$hiddenValuesContainer->addObject($objSortingType);
				$guiContainer->addContainer($hiddenValuesContainer);
	    }
	    else {
	      // content type already set
	      $typeValue = &new egStatic("st_item_type", $valuesTable['type_name'], 90, $previousTop, 80, 0);

	      $templateText = &new egStatic("st_value_template", LANG_CONTAINER_TEMPLATE_STATIC.' :', 265, $previousTop, 80, 0, true);
	    	$templateValue = &new egDropDownListBox("obj_item_template", 350, $previousTop, 130, null, 'template_id');
	      foreach($contentTypesTemplatesArray[$valuesTable['type_name']] as $id => $templateName) {
	        $templateValue->addListItem(new egListItem("lti_templates_".$id, str_replace('_', ' ', $templateName), null, $id));
	      }
	      $templateValue->setSelectedItemByValue($valuesTable['template_id']);
			  $guiContainer->addObject($templateText);
			  $guiContainer->addObject($templateValue);
	    }

	 		$previousTop += 40;

	    $cloneText = &new egStatic("st_value_clone", LANG_CONTAINER_CLONE_STATIC.' :', 5, $previousTop, 80, 0, true);
	    $cloneValue = &new egEdit("obj_item_clone", $valuesTable['clone_id'], 90, $previousTop, 33, 18, 'clone_id');
	    $cloneComment = &new egStatic("st_value_clone_comment", "<i>".LANG_CONTAINER_CLONE_COMMENT_STATIC."</i>", 90, $previousTop+18, 350, 0);

			$previousTop += 50;

	    if($this->object->getContainerId() != 0){
	      $showText = &new egStatic("st_value_show", LANG_CONTAINER_SHOW_STATIC.' :', 5, $previousTop, 80, 0, true);
	  		$showValue = &new egEdit("obj_item_show", $valuesTable['count_shown'], 90, $previousTop, 33, 18, 'count_shown');
	      $showText2 = &new egStatic("st_value_show2", LANG_CONTAINER_ITEMS_STATIC, 133, $previousTop, 200, 0, true);

	      $showComment = &new egStatic("st_comment_show", "<i>".LANG_CONTAINER_SHOW_COMMENT_STATIC."</i>", 90, $previousTop+18, 350, 0);
	   		$previousTop += 50;

	      $startText = &new egStatic("st_value_start", LANG_CONTAINER_START_STATIC.' :', 5, $previousTop, 80, 0, true);
	  		$startValue = &new egEdit("obj_item_start", $valuesTable['shown_from'], 90, $previousTop, 33, 18, 'shown_from');
	      $startComment = &new egStatic("st_comment_start", "<i>".LANG_CONTAINER_START_COMMENT_STATIC."</i>", 90, $previousTop+18, 350, 0);
	   		$previousTop += 50;

	      $orderText = &new egStatic("st_value_order", LANG_CONTAINER_ORDER_STATIC.' :', 5, $previousTop, 80, 0, true);
	      $orderManual = &new egRadiobutton("obj_item_order_manual", LANG_CONTAINER_MANUALSORT_STATIC, 90, $previousTop, 100, null, "sorting_type", SORTING_BY_LIST, ($valuesTable['sorting_type'] == SORTING_BY_LIST));
	      $orderAuto = &new egRadiobutton("obj_item_order_auto",LANG_CONTAINER_AUTOSORT_STATIC, 220, $previousTop, 100, null, "sorting_type", SORTING_BY_FIELD, ($valuesTable['sorting_type'] == SORTING_BY_FIELD));
	   		$previousTop += 50;

	      $sortByText = &new egStatic("st_value_sort_by", LANG_CONTAINER_SORT_STATIC.' :', 5, $previousTop, 80, 0, true);
	    	$sortByValue = &new egDropDownListBox("obj_item_sort_field", 90, $previousTop, 100, null, "sort_field");
	      foreach($contentTypesFieldsArray[$valuesTable['type_name']] as $name => $typeId) {
	        $sortByValue->addListItem(new egListItem("lti_field_".$typeId, $name, null, $name));
	      }
	      $sortByValue->setSelectedItemByValue($valuesTable['sort_field']);

	      $sortOnText = &new egStatic("st_value_sort_on", LANG_CONTAINER_ON_STATIC, 210, $previousTop, 80, 0, true);

	      $sortOrderContainer = &new egContainer('edt_sort_order_ctn', 250, $previousTop-10, 150, 60, true);
	      $sortOnAsc  = &new egRadiobutton("obj_item_sort_on_asc", LANG_CONTAINER_ASCENDING_STATIC, 0, 0, 100, null, "sort_order", SORT_ASCENDING, ($valuesTable['sort_order'] == SORT_ASCENDING));
	      $sortOnDesc = &new egRadiobutton("obj_item_sort_on_desc", LANG_CONTAINER_DESCENDING_STATIC, 0, 25, 100, null, "sort_order", SORT_DESCENDING, ($valuesTable['sort_order'] == SORT_DESCENDING));
	      $sortOrderContainer->addObject($sortOnAsc);
	      $sortOrderContainer->addObject($sortOnDesc);
	    }

			$guiContainer->addObject($idText);
			$guiContainer->addObject($idValue);

			$guiContainer->addObject($typeText);
			$guiContainer->addObject($typeValue);

			$guiContainer->addObject($cloneText);
			$guiContainer->addObject($cloneValue);
			$guiContainer->addObject($cloneComment);

			$guiContainer->addObject($orderText);
			$guiContainer->addObject($orderManual);
			$guiContainer->addObject($orderAuto);

			$guiContainer->addObject($showText);
      $guiContainer->addObject($showText2);
			$guiContainer->addObject($showValue);
			$guiContainer->addObject($showComment);

			$guiContainer->addObject($startText);
			$guiContainer->addObject($startValue);
			$guiContainer->addObject($startComment);

			$guiContainer->addObject($sortByText);
		  $guiContainer->addObject($sortByValue);

			$guiContainer->addObject($sortOnText);
			$guiContainer->addObject($sortOrderContainer);


			$scriptLine = "
function chooseTemplate(id) {
  edit_hidden_control_template_id.setValue(id);
}

function chooseTypeName(name) {
  edit_hidden_control_type_name.setValue(name);
}
";


			$elems = array('name');

			$scriptLine .= "var elemsArray = new Array();";
			$i = 0;
			foreach ($elems as $elem) {
				$scriptLine .= "elemsArray[$i] = '$elem';";
				++$i;
			}
			$scriptLine .= "var langsArray = new Array();";
			$i = 0;
			foreach ($langsArray as $langId => $langName) {
				$scriptLine .= "langsArray[$i] = $langId;";
				++$i;
			}


			$scriptLine .= "
			function setLangCheckboxes() {
				var identifier = this.id;
				var lastPos = identifier.length-1;
				while(identifier.charAt(lastPos) != '_') {--lastPos;}
				var langId = identifier.substr(lastPos+1);
				var ckbName = identifier.substr(0, lastPos);
				var elemName = ckbName.substr(4, lastPos);
				for(i = 0; i < langsArray.length; i++) {
					var langId1 = langsArray[i];
					if(langId1 != langId) {
						if(document.getElementById(identifier+'_value').checked) {
							egObjectsArray['checkBox_'+ckbName+'_'+langId1].setChecked();
							egObjectsArray['edit_'+'obj_item_' + elemName + '_' + langId1].setValue(egObjectsArray['edit_'+'obj_item_' + elemName + '_' + langId].getValue());
						}
						else {
							egObjectsArray['checkBox_'+ckbName+'_'+langId1].setUnchecked();
						}
					}
				}
			}


			function setSiblingItems() {
				var identifier = this.id;
				var objectlabel = 'obj_item';
				var lastPos = identifier.length-1;
				while(identifier.charAt(lastPos) != '_') {--lastPos;}
				var langId = identifier.substr(lastPos+1);
				var elemName = identifier.substring(objectlabel.length+1, lastPos);
				var inputName = identifier.substr(0, lastPos);
				if(egObjectsArray['checkBox_ckb_'+elemName+'_'+langId].checked) {
					for(i = 0; i < langsArray.length; i++) {
						var langId1 = langsArray[i];
						if(langId1 != langId) {
							egObjectsArray['edit_'+ inputName + '_' + langId1].setValue(egObjectsArray['edit_'+ inputName + '_' + langId].getValue());
						}
					}
				}
			}


			for(i = 0; i < langsArray.length; i++) {
				for(j = 0; j < elemsArray.length; j++) {
					document.getElementById('ckb_'+elemsArray[j]+'_'+langsArray[i]).onclick = setLangCheckboxes;
					egObjectsArray['edit_obj_item_'+elemsArray[j]+'_'+langsArray[i]].onChange = setSiblingItems;
				}
			}
			";


		  return $scriptLine;
		}
	}
}
?>