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

// define the path context
chdir('..');
define('EASY_GUI_APP_DIR', '../');


include_once('config.inc.php');
include_once('classes/core/Site.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');


$site = &Site::getInstance(); // constructor checks the $_GET, $_POST and $_SESSION arrays

//security check : user must be ROOT to edit types fields
$user = $site->getCurrentUser();
if(ENABLE_ADMIN_AREA == 0 || is_null($user) || $user->getUserId() != USER_ID_ROOT) {
	die("You do not have permissions to create type fields: access denied.");
	exit();
}

$mode 		  = $site->getSubmittedVar('mode', ASK_CREATE_FIELD);
$type 	 	  = $site->getSubmittedVar('type');
$field 	 	  = $site->getSubmittedVar('field');
$field_type	= $site->getSubmittedVar('field_type', EASY_CMS_TYPE_STRING);

$message = '';

$typesManager = &$site->getTypesManager();

if($mode == CREATE_FIELD && !is_null($field) && !$typesManager->isValidFieldName($field)) {
  $mode = ASK_CREATE_FIELD;
  $message .= LANG_FIELD_RENAME_INVALID;
}

if(isset($type)) {
  $manager = &egManager::getInstance();
  $main = &new egContainer("main_ctn", 0, 0, 0, 0);
	$manager->addContainer($main);
	
  switch($mode) {
    case ASK_CREATE_FIELD:
      if(strlen($message) == 0) {
        $message = LANG_FIELD_CREATION_MESSAGE;
      }
  
  		$txtValue = &new egStatic("txt_value_1", $message.LANG_COLON, 10, 15, 350, 0, false);
  		$nameValue = &new egStatic("name_value_1", LANG_FIELD_NAME_STATIC.LANG_COLON, 10, 50, 80, 0, true);
  		$nameItem = &new egEdit("name_item_1", $field, 70, 50, 150, 18, 'field');

  		$typeValue = &new egStatic("type_value_1", LANG_FIELD_TYPE_STATIC.LANG_COLON, 10, 80, 80, 0, true);
      $typeItem = &new egDropDownListBox("type_item_1", 70, 80, 150, 80, 'field_type');

      $types_array = $typesManager->getCMSTypesArray();

      foreach($types_array as $cms_field_type) {
        $typeItem->addListItem(new egListItem("lti_types_".$cms_field_type, $cms_field_type, null, $cms_field_type));
      }    
  
      $typeItem->setSelectedItemByValue($field_type);

  		$okButton = &new egPressbutton("btn_ok", LANG_OK_BUTTON, 130, 120, 80, null, "clickOKButton();");
  		$cancelButton = &new egPressbutton("btn_cancel", LANG_CANCEL_BUTTON, 215, 120, 80, null, "clickCancelButton();");
  
  		$hiddenValuesContainer = &new egContainer("ctn_hidden_values", 0, 0, 1, 1);
  
  		$objMode = &new egEdit("hidden_control_mode", CREATE_FIELD, 0, 0, 0, 0, 'mode');
      $objType = &new egEdit("hidden_control_type", $type, 0, 0, 0, 0, 'type');

  		$hiddenValuesContainer->addObject($objMode);
  		$hiddenValuesContainer->addObject($objType);
        
  		$manager->addContainer($hiddenValuesContainer);
  
  		$main->addObject($okButton);
  		$main->addObject($cancelButton);
  
  		$main->addObject($txtValue);
  		$main->addObject($nameValue);
  		$main->addObject($nameItem);

  		$main->addObject($typeValue);
  		$main->addObject($typeItem);
  		
  		$scripts = &new egScript();
  		$line = "
      function clickOKButton() {
      	document.forms[0].submit();
      }
      function clickCancelButton() {
      	window.close();
      }
      ";
  		$scripts->addLine($line);
  		$manager->addScripts($scripts);
      break;
    case CREATE_FIELD:
      if(($typeObj = &$typesManager->getType($type)) != null) {
        if($typesManager->isValidCMSType($field_type)){
          $typeObj->addField($field, $field_type);
          $typesManager->storeType($type);
        }
      }
   		$scripts = new egScript();
  		$line = "window.opener.location.reload();window.close();";
  		$scripts->addLine($line);
  		$manager->addScripts($scripts);
      break;
  }
  print($manager->getHTML());
}
?>