<?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
 */
 
/**
 * ContentEditionForm : edition form for Contents
 *
 * @package    Core
 * @subpackage Forms
 */

/**
 */
include_once('config.inc.php');
include_once('classes/core/Site.class.php');
include_once('classes/core/forms/EditionForm.class.php');
include_once('classes/core/forms/ContentForm.class.php');

include_once('classes/core/types/TypesManager.class.php');
include_once('classes/core/contents/Content.class.php');
include_once('classes/core/contents/ContentsManager.class.php');
include_once('classes/core/pages/Page.class.php');
include_once('classes/core/pages/PagesManager.class.php');
include_once('classes/core/containers/Container.class.php');
include_once('classes/core/containers/ContainersManager.class.php');

include_once('classes/core/versions/ContentVersionsManager.class.php');

include_once('classes/types/Date.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');

/**
 * ContentEditionForm : edition form for Contents
 *
 * @access     public
 * @author	   Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	   ContentEditionForm.class, Tue Dec 14 22:12:56 CET 2004
 * @package	   Core
 * @subpackage Forms
 */
class ContentEditionForm extends EditionForm {

	/**
 	 * Constructor
	 * 
	 * @param  object $content
 	 * @param  string $mode The mode in which the form is to be displayed.	
 	 * @return void  
	 */
	function ContentEditionForm(&$content, &$contentsManager) {
		parent::EditionForm($content, $contentsManager);
	}

	function populateUIContainer(&$guiContainer, $currLangId, $action, $error=0) {
		if($action == ASK_DELETE_CONTENT) {

			if($error) {
				$txtLang = &new egStatic("sta_1", LANG_CONTENT_DELETION_DENIED, 20, 20, 300, 0);
				$guiContainer->addObject($txtLang);
			}
			else {
				$summary = $this->object->toHTML($currLangId);
				$txtLang = &new egStatic("sta_1", LANG_CONTENT_DELETION_MESSAGE." :<br /><br />".$summary."<br /><br />".LANG_CONTENT_DELETION_CONFIRM, 20, 20, 300, 0);
				$guiContainer->addObject($txtLang);
			}
			return "";
		}
		else {
			$elems = array();

			$valuesTable = &$this->object->getContentValuesTable();
			$langsArray = $this->objectManager->getLangsArray();


			$typeName = $this->object->getContentTypeName();
			if($typeName == 'mailing_list' || $typeName == 'mailing_subscriber') {
			  // keep only the first language for specific types
				foreach ($langsArray as $langId => $langName) {
		      if($langId > 1) {
						unset($langsArray[$langId]);
	      	}
	    	}
			}

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

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

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

			foreach ($langsArray as $langId => $langName) {
				if(count($langsArray) > 1) {
		      $editionContainer = &new egContainer('edt_ctn_'.$langId);
		      $guiContainer->addContainer($editionContainer);
					$langList->addListItem(new egPropertyPage('lti_'.$langId, ucfirst($langName), $editionContainer->getContainerName()));
		      if($langId == $currLangId) {
		        $langList->setSelectedItemByLabel(ucfirst($langName));
		      }
				}
				else {
		      $editionContainer = &new egContainer('edt_ctn_'.$langId, 0, 0, 0, 0, true);
		      $guiContainer->addContainer($editionContainer);
				}

	      // create same fields for this lang than in the reference language
	      if(!isset($valuesTable[$langId])) {
	        if(!isset($referenceLangId)) {
	          continue;
	        }
	        foreach ($valuesTable[$referenceLangId] as $field => $value) {
            $empty = '';
	          $value->setValue($empty);
	          $valuesTable[$langId][$field] = $value;
	        }
	      }
	    	$i = 0;
	  		$previousTop = 45;
				foreach ($valuesTable[$langId] as $field => $value) {

					$txtValue = &new egStatic("st_value_".$i."_".$langId, ucfirst(str_replace('_', ' ', $field)).LANG_COLON, 5, $previousTop, 80, 0, true);
					$type = $value->getType();

					unset($checkBox);

					switch($type) {
						case EASY_CMS_TYPE_INTEGER:
							$objItem = &new egEdit("obj_item_".$field."_".$i."_".$langId, $value->getValue(), 90, $previousTop, 37, 18, $langId.'['.$field.']');
							$previousTop += 25;
							//$elems[] = $field."_".$i;

/*
							$checkBox = &new egCheckBox("ckb_".$field."_".$i."_".$langId, '<b>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</b>', 90, $previousTop, 30, null, $langId.'['.$field.'_all_languages]', 1, false);
							$previousTop += 25;
*/
						  break;
						case EASY_CMS_TYPE_FLOAT:
							$objItem = &new egEdit("obj_item_".$field."_".$i."_".$langId, $value->getValue(), 90, $previousTop, 37, 18, $langId.'['.$field.']');
							$previousTop += 25;
							//$elems[] = $field."_".$i;

/*							$checkBox = &new egCheckBox("ckb_".$field."_".$i."_".$langId, '<b>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</b>', 90, $previousTop, 30, null, $langId.'['.$field.'_all_languages]', 1, false);
							$previousTop += 25;
*/
						  break;
						case EASY_CMS_TYPE_BOOL:
				      $objItem = &new egContainer("obj_item_".$i."_".$langId, 90, $previousTop, 0, 0, true);
				      $objItemYes  = &new egRadiobutton("obj_item_".$field."_".$i."_yes_".$langId, LANG_BOOL_TRUE, 0, 0, 100, null, $langId.'['.$field.']', 1, ($value->getValue() == 1));
				      $objItemNo = &new egRadiobutton("obj_item_".$field."_".$i."_no_".$langId, LANG_BOOL_FALSE, 50, 0, 100, null, $langId.'['.$field.']', 0, ($value->getValue() == 0));
							$objItem->addObject($objItemYes);
							$objItem->addObject($objItemNo);
							$previousTop += 25;
							//$elems[] = $field."_".$i."_yes";
							//$elems[] = $field."_".$i."_no";

/*							$checkBox = &new egCheckBox("ckb_".$field."_".$i."_".$langId, '<b>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</b>', 90, $previousTop, 30, null, $langId.'['.$field.'_all_languages]', 1, false);
							$previousTop += 25;
*/
						  break;
						case EASY_CMS_TYPE_STRING:
							$objItem = &new egEdit("obj_item_".$field."_".$i."_".$langId, $value->getValue(), 90, $previousTop, 390, 18, $langId.'['.$field.']');
							$previousTop += 18;
							$elems[] = "edit_".$field."_".$i;

 							$checkBox = &new egCheckBox("ckb_edit_".$field."_".$i."_".$langId, '<i>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</i>', 90, $previousTop, 200, null, $langId.'['.$field.'_all_languages]', 1, false);
							$previousTop += 25;
						  break;
						case EASY_CMS_TYPE_SHORTTEXT:
							$objItem = &new egEdit("obj_item_".$field."_".$i."_".$langId, $value->getValue(), 90, $previousTop, 390, 100, $langId.'['.$field.']');
							$previousTop += 107;
							$elems[] = "edit_".$field."_".$i;

							$checkBox = &new egCheckBox("ckb_edit_".$field."_".$i."_".$langId, '<i>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</i>', 90, $previousTop, 200, null, $langId.'['.$field.'_all_languages]', 1, false);
							$previousTop += 25;

						  break;
						case EASY_CMS_TYPE_TEXT:
							$objItem = &new egRichText("obj_item_".$field."_".$i."_".$langId, $value->getValue(), 90, $previousTop, 390, 250, $langId.'['.$field.']');
							$previousTop += 251;
							$elems[] = "richText_".$field."_".$i;

							$checkBox = &new egCheckBox("ckb_richText_".$field."_".$i."_".$langId, '<i>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</i>', 90, $previousTop, 200, null, $langId.'['.$field.'_all_languages]', 1, false);
							$previousTop += 25;
						  break;
						case EASY_CMS_TYPE_COLOR:
							$previousTop += 25;
						  break;
						case EASY_CMS_TYPE_LINK:
							$objItem = &new egContainer("obj_item_".$i."_".$langId, 90, $previousTop, 0, 0, true);
							$objItemEdit = &new egEdit("obj_item_edit_".$field."_".$i."_".$langId, $value->getValue(), 0, 2, 305, 18, $langId.'['.$field.']');
							$objItemButton = &new egPressbutton("obj_item_button_".$i."_".$langId, LANG_CHOOSE_BUTTON, 315, 0, 80, null, "openWindow(\'admin/select_page.php?parent_control_name=obj_item_edit_".$field."_".$i."_".$langId."_value\',425,450,0,0);");

							$objItem->addObject($objItemEdit);
							$objItem->addObject($objItemButton);
							$previousTop += 25;
							$elems[] = "edit_edit_".$field."_".$i;
							
							$checkBox = &new egCheckBox("ckb_edit_edit_".$field."_".$i."_".$langId, '<i>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</i>', 90, $previousTop, 200, null, $langId.'['.$field.'_all_languages]', 1, false);
							$previousTop += 25;

						  break;
						case EASY_CMS_TYPE_DATE:
							$objItem = &new egContainer("obj_item_".$i."_".$langId, 90, $previousTop, 0, 0, true);
							$objItemDaysList   = &new egDropDownListBox("obj_item_days_".$field."_".$i."_".$langId, 0, 0, 40, null, $langId.'['.$field.'][day]');
							$objItemMonthsList = &new egDropDownListBox("obj_item_months_".$field."_".$i."_".$langId, 60, 0, 80, null, $langId.'['.$field.'][month]');
							$objItemYearsList  = &new egDropDownListBox("obj_item_years_".$field."_".$i."_".$langId, 160, 0, 50, null, $langId.'['.$field.'][year]');

							$elems[] = "dropDownListBox_"."days_".$field."_".$i;
							$elems[] = "dropDownListBox_"."months_".$field."_".$i;
							$elems[] = "dropDownListBox_"."years_".$field."_".$i;

							$currDay = $value->getDay();
              if($this->object->getContentId() == 0) {
                $currDay = Date::getCurrentDay();
              }
							$objItemDaysList->addListItem(new egListItem("lti_days_".$i."_0_".$langId, "", null, 0));
							for($day = 1; $day <= 31; ++$day) {
								$dayNum = (($day<10)?"0":"").$day;
								$objItemDaysList->addListItem(new egListItem("lti_days_".$i."_".$day."_".$langId, $dayNum, null, $day));
							}
							$objItemDaysList->setSelectedItemByValue($currDay);

							$currMonth = $value->getMonth();
              if($this->object->getContentId() == 0) {
                $currMonth = Date::getCurrentMonth();
              }
							$objItemMonthsList->addListItem(new egListItem("lti_months_".$i."_0_".$langId, "", null, 0));
							for($month = 1; $month <= 12; ++$month) {
								$monthName = ucfirst(Date::getMonthName($month));
								$objItemMonthsList->addListItem(new egListItem("lti_months_".$i."_".$month."_".$langId, $monthName, null, $month));
							}
							$objItemMonthsList->setSelectedItemByValue($currMonth);

							$currYear = $value->getYear();
              if($this->object->getContentId() == 0) {
                $currYear = Date::getCurrentYear();
              }
              if($currYear == 0) {
                $nowYear = Date::getCurrentYear();
								$fromYear = $nowYear - 2;
								$toYear   = $nowYear + 10;
              }
							else {
								$fromYear = $currYear - 2;
								$toYear   = $currYear + 10;
							}
							$objItemYearsList->addListItem(new egListItem("lti_years_".$i."_0_".$langId, "", null, 0));
							for ($year = $fromYear; $year <= $toYear; $year++) {
								$objItemYearsList->addListItem(new egListItem("lti_years_".$i."_".$year."_".$langId, $year, null, $year));
							}
							$objItemYearsList->setSelectedItemByValue($currYear);

							$objItem->addObject($objItemDaysList);
							$objItem->addObject($objItemMonthsList);
							$objItem->addObject($objItemYearsList);
							$previousTop += 21;

							$checkBox = &new egCheckBox("ckb_dropDownListBox_".$field."_".$i."_".$langId, '<i>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</i>', 90, $previousTop, 200, null, $langId.'['.$field.'_all_languages]', 1, false);
							$previousTop += 25;

						  break;
						case EASY_CMS_TYPE_TIME:
							$previousTop += 25;
						  break;
						case EASY_CMS_TYPE_DOCUMENT:
							$objItem = &new egContainer("obj_item_".$i."_".$langId, 90, $previousTop, 0, 0, true);
							$objItemEdit = &new egEdit("obj_item_edit_".$field."_".$i."_".$langId, $value->getValue(), 0, 2, 305, 18, $langId.'['.$field.']');
							$objItemButton = &new egPressbutton("obj_item_button_".$i."_".$langId, LANG_BROWSE_BUTTON, 315, 0, 80, null, "openWindow(\'admin/select_file.php?parent_control_name=obj_item_edit_".$field."_".$i."_".$langId."_value&repository_type=".DOCUMENTS_FOLDER."\',680,430,0,0);");

							$objItem->addObject($objItemEdit);
							$objItem->addObject($objItemButton);
							$previousTop += 25;
							$elems[] = "edit_edit_".$field."_".$i;

							$checkBox = &new egCheckBox("ckb_edit_edit_".$field."_".$i."_".$langId, '<i>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</i>', 90, $previousTop, 200, null, $langId.'['.$field.'_all_languages]', 1, false);
							$previousTop += 25;

						  break;
						case EASY_CMS_TYPE_IMAGE:
							$objItem = &new egContainer("obj_item_".$field."_".$i."_".$langId, 90, $previousTop, 0, 0, true);
							$objItemEdit = &new egEdit("obj_item_edit_".$field."_".$i."_".$langId, $value->getValue(), 0, 2, 305, 18, $langId.'['.$field.']');
							$objItemButton = &new egPressbutton("obj_item_button_".$field."_".$i."_".$langId, LANG_BROWSE_BUTTON, 315, 0, 80, null, "openWindow(\'admin/select_file.php?parent_control_name=obj_item_edit_".$field."_".$i."_".$langId."_value&repository_type=".IMAGES_FOLDER."\',680,550,0,0);");

							$objItem->addObject($objItemEdit);
							$objItem->addObject($objItemButton);
							$previousTop += 25;
							$elems[] = "edit_edit_".$field."_".$i;

							$checkBox = &new egCheckBox("ckb_edit_edit_".$field."_".$i."_".$langId, '<i>'.LANG_CONTENT_SAME_VALUE_FOR_FIELD_IN_ALL_LANG.'</i>', 90, $previousTop, 200, null, $langId.'['.$field.'_all_languages]', 1, false);
							$previousTop += 25;

						  break;
						case EASY_CMS_TYPE_REFERENCE:
							$previousTop += 25;
						  break;
					}
					if(isset($checkBox)) {
						if(count($langsArray) > 1) {
							$editionContainer->addObject($checkBox);
						}
					}
					$editionContainer->addObject($txtValue);
					$editionContainer->addObject($objItem);
					++$i;
	      }
				$emptyValue = &new egStatic("st_value_empty_".$langId, " ", 5, $previousTop, 80, 0, true);
				$editionContainer->addObject($emptyValue);
			}

			$scriptLine = "";

			$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;
			}

			if(count($langsArray) > 1) {
				$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);
					if(ckbName.indexOf('_days_')) {
						ckbName = ckbName.replace(/_days_/, '_');
					}
					if(ckbName.indexOf('_months_')) {
						ckbName = ckbName.replace(/_months_/, '_');
					}
					if(ckbName.indexOf('_years_')) {
						ckbName = ckbName.replace(/_years_/, '_');
					}
					var elemName = ckbName.substr(4, lastPos);
					elemName = elemName.substr(elemName.indexOf('_')+1);
					for(langIndex = 0; langIndex < langsArray.length; langIndex++) {
						var langId1 = langsArray[langIndex];
						if(langId1 != langId) {
							if(document.getElementById(identifier+'_value').checked) {
								var pos1 = identifier.indexOf('_');
								var pos2 = identifier.substr(pos1+1).indexOf('_');
								var typeName = identifier.substr(pos1+1, pos2);
								egObjectsArray['checkBox_'+ckbName+'_'+langId1].setChecked();
								if(identifier.indexOf('dropDownListBox') >= 0) {
									egObjectsArray[typeName+'_'+ 'obj_item_days' + '_' + elemName + '_' + langId1].setValue(egObjectsArray[typeName + '_' + 'obj_item_days' + '_' + elemName + '_' + langId].getValue());
									egObjectsArray[typeName+'_'+ 'obj_item_months' + '_' + elemName + '_' + langId1].setValue(egObjectsArray[typeName + '_' + 'obj_item_months' + '_' + elemName + '_' + langId].getValue());
									egObjectsArray[typeName+'_'+ 'obj_item_years' + '_' + elemName + '_' + langId1].setValue(egObjectsArray[typeName + '_' + 'obj_item_years' + '_' + elemName + '_' + langId].getValue());
								}
								else {
									egObjectsArray[typeName+'_'+ 'obj_item' + '_' + elemName + '_' + langId1].setValue(egObjectsArray[typeName + '_' + 'obj_item' + '_' + elemName + '_' + langId].getValue());
								}
							}
							else {
								egObjectsArray['checkBox_'+ckbName+'_'+langId1].setUnchecked();
							}
						}
					}
				}

				function setSiblingItems() {
					var identifier = this.id;
					var objectlabel = 'obj_item';
					if(identifier.indexOf('obj_item_days') >= 0) {
						objectlabel = 'obj_item_days';
					}
					if(identifier.indexOf('obj_item_months') >= 0) {
						objectlabel = 'obj_item_months';
					}
					if(identifier.indexOf('obj_item_years') >= 0) {
						objectlabel = 'obj_item_years';
					}
					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 typeName = this.objName.substring(0, this.objName.indexOf('_'));
					var inputName = identifier.substr(0, lastPos);
					if(egObjectsArray['checkBox_ckb_'+ typeName + '_' + elemName+'_'+langId].checked) {
						for(langIndex = 0; langIndex < langsArray.length; langIndex++) {
							var langId1 = langsArray[langIndex];
							if(langId1 != langId) {
								egObjectsArray[typeName+'_'+ inputName + '_' + langId1].setValue(egObjectsArray[typeName+'_'+ inputName + '_' + langId].getValue());
							}
						}
					}
				}

				for(i = 0; i < langsArray.length; i++) {
					for(j = 0; j < elemsArray.length; j++) {
						var startPos = elemsArray[j].indexOf('_');
						var elemName = elemsArray[j].substr(startPos+1);
						var typeName = elemsArray[j].substring(0, startPos);
						var ckbElem = elemsArray[j];
						if(ckbElem.indexOf('_days_')) {
							ckbElem = ckbElem.replace(/_days_/, '_');
						}
						if(ckbElem.indexOf('_months_')) {
							ckbElem = ckbElem.replace(/_months_/, '_');
						}
						if(ckbElem.indexOf('_years_')) {
							ckbElem = ckbElem.replace(/_years_/, '_');
						}
						document.getElementById('ckb_'+ckbElem+'_'+langsArray[i]).onclick = setLangCheckboxes;
						egObjectsArray[typeName+'_obj_item_'+elemName+'_'+langsArray[i]].onChange = setSiblingItems;
					}
				}
				";
			}
			return $scriptLine;
		}
	}
}
?>