<?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/system/Dir.class.php');
include_once('classes/system/File.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/egPropertyList.class.php');
include_once('interface/egPropertySheet.class.php');
include_once('interface/egPropertyPage.class.php');
include_once('interface/egTreeNode.class.php');
include_once('interface/egTreeList.class.php');
include_once('interface/egSubWindow.class.php');


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

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


$content_type = $site->getSubmittedVar('content_type', null);


$manager = &egManager::getInstance();
$main = &new egContainer("main_ctn", 0, 0, 0, 0);


if(!is_null($content_type)){

  $array_types_icons = array(EASY_CMS_TYPE_INTEGER=>'icon_type_integer.png', EASY_CMS_TYPE_FLOAT=>'icon_type_float.png', EASY_CMS_TYPE_BOOL=>'icon_type_bool.png', EASY_CMS_TYPE_STRING=>'icon_type_string.png', EASY_CMS_TYPE_SHORTTEXT=>'icon_type_shorttext.png', EASY_CMS_TYPE_TEXT=>'icon_type_text.png', EASY_CMS_TYPE_COLOR=>'icon_type_color.png', EASY_CMS_TYPE_LINK=>'icon_type_link.png', EASY_CMS_TYPE_DATE=>'icon_type_date.png', EASY_CMS_TYPE_TIME=>'icon_type_time.png', EASY_CMS_TYPE_DOCUMENT=>'icon_type_document.png', EASY_CMS_TYPE_IMAGE=>'icon_type_image.png', EASY_CMS_TYPE_REFERENCE=>'icon_type_reference.png');

  $listContainer = &new egContainer("list_ctn", 2, 2, 449, 120, true);

  $contentsManager = &$site->getContentsManager();
  $contentTypesFieldsArray = &$contentsManager->getContentTypesFields();

  $vPos = 2;
  $i = 0;

	foreach($contentTypesFieldsArray as $type_name => $fieldsArray) {
    if($content_type == $type_name) {
      foreach($fieldsArray as $field => $cms_type) {
        $action = "setSelectedField(\'$content_type\', \'$field\', \'$cms_type\');";
        $link = &new egLink('field_'.$i, $field, 10, $vPos, 200, 18, $action, 'interface/icons/'.$array_types_icons[$cms_type]);
        $stType = &new egStatic('type_'.$i, $cms_type, 220, $vPos, 200, 18, 0, false);
        $listContainer->addObject($link);
        $listContainer->addObject($stType);
      	$vPos += 20;
        ++$i;
      }
    }
  }

	$fielddNameStatic = &new egStatic("field_name_static", LANG_FIELD_MENU_SELECTED." : ", 0, 130, 120, 0, true);
	$fieldNameValue  = &new egStatic("field_name_value", "", 130, 130, 350, 0, false);

	$createLink  = &new egLink("lnk_create", LANG_FIELD_MENU_NEW, 460, 5, 150, 18, "launchCreateFieldWindow();", "interface/icons/icon_add_template.png");
	$editLink    = &new egLink("lnk_edit", LANG_FIELD_MENU_EDIT, 460, 25, 150, 18, "launchFieldEditionWindow();", "interface/icons/edit.png");
	$deleteLink  = &new egLink("lnk_delete", LANG_FIELD_MENU_REMOVE, 460, 45, 150, 18, "launchFieldDeletionWindow();", "interface/icons/remove.png");


	$main->addObject($fielddNameStatic);
	$main->addObject($fieldNameValue);

	$main->addObject($createLink);
	$main->addObject($editLink);
	$main->addObject($deleteLink);

  $scripts = new egScript();
  $line = "
var selectedType = '$content_type';
var selectedField = '';
var selectedCMSType = '';
function clickOKButton() {
  if(selectedFile != '') {
    if(confirm('Are you sure you want to save the modifications and overwrite the template :\\n'+ selectedFile + '?')) {
  	 document.getElementById('sbw_1').contentWindow.saveContent();
    }
  }
}
function launchFieldEditionWindow() {
	var vars = 'type=' + selectedType + '&field=' + selectedField + '&field_type=' + selectedCMSType;
  openWindow('modify_type_field.php?'+'mode=ask_modif_field&'+vars,330, 200, 0, 0);
}
function launchFieldDeletionWindow() {
	var vars = 'type=' + selectedType + '&field=' + selectedField;
  openWindow('delete_type_field.php?'+'mode=ask_delete_field&'+vars,330, 150, 0, 0);
}
function launchCreateFieldWindow() {
	var vars = 'type=' + selectedType;
  openWindow('create_type_field.php?'+'mode=ask_create_field&'+vars,330, 200, 0, 0);
}
function setSelectedField(type, field, cms_type) {
	selectedType = type;
	selectedField = field;
  selectedCMSType = cms_type;
	document.getElementById('field_name_value').innerHTML = field;
}
";

  $scripts->addLine($line);
  $manager->addScripts($scripts);

  $main->addContainer($listContainer);
}
else {

  $treeListWidth = 450;
  $treeListTop = 10;

  $listContainer = &new egContainer("list_ctn", 12, $treeListTop+2, $treeListWidth-1, 120, true);
  $frame = &new egFrame("fra_1", "", 10, $treeListTop, $treeListWidth, 120);

  $vPos = 2;
  $i = 0;
  $contentsManager = &$site->getContentsManager();
  $contentTypesFieldsArray = &$contentsManager->getContentTypesFields();

  $array_protected_types = array('gallery_photo', 'mailing_list', 'mailing_subscriber', 'mailing_message');

	foreach($contentTypesFieldsArray as $contentType => $fieldsArray) {
    if(!in_array($contentType, $array_protected_types)) {
      $action = "setSelectedType(\'".$contentType."\');";
      $link = &new egLink('content_type_'.$i, $contentType, 10, $vPos, 200, 18, $action, "interface/icons/icon_template.png");
      $listContainer->addObject($link);
    	$vPos += 20;
      ++$i;
    }
  }
	


	$fileNameStatic = &new egStatic("type_name_static", LANG_TYPE_MENU_SELECTED." : ", 10, 140, 120, 0, true);
	$fileNameValue  = &new egStatic("type_name_value", "", 140, 140, 350, 0, false);

	$createLink = &new egLink("lnk_create", LANG_TYPE_MENU_NEW, 470, 15, 200, 18, "launchCreateTypeWindow();", "interface/icons/icon_add_template.png");
	$renameLink = &new egLink("lnk_rename", LANG_TYPE_MENU_RENAME, 470, 35, 200, 18, "launchTypeRenamingWindow();", "interface/icons/edit.png");
	$deleteLink = &new egLink("lnk_delete", LANG_TYPE_MENU_REMOVE, 470, 55, 200, 18, "launchTypeDeletionWindow();", "interface/icons/remove.png");

	$subWindow = &new egSubWindow("sbw_1", "edit_types.php?content_type=none", 11, 181, 660, 145);
  $subFrame = &new egFrame("fra_2", "", 10, 180, 450, 120);


	$main->addObject($fileNameStatic);
	$main->addObject($fileNameValue);

	$main->addObject($createLink);
	$main->addObject($renameLink);
	$main->addObject($deleteLink);

  $main->addObject($frame);
  $main->addContainer($listContainer);

	$main->addObject($subWindow);
	$main->addObject($subFrame);


  $scripts = new egScript();
  $line = "
var selectedType = '';
function launchTypeRenamingWindow() {
	var vars = 'type='+ selectedType;
  openWindow('rename_type.php?'+'mode=ask_rename_type&'+vars,330, 150, 0, 0);
}
function launchTypeDeletionWindow() {
	var vars = 'type='+ selectedType;
  openWindow('delete_type.php?'+'mode=ask_delete_type&'+vars,330, 150, 0, 0);
}
function launchCreateTypeWindow() {
  openWindow('create_type.php?'+'mode=ask_create_type',330, 150, 0, 0);
}
function setSelectedType(type) {
	selectedType = type;
	document.getElementById('type_name_value').innerHTML = type;

	if(document.getElementById('sbw_1') != null) {
    document.getElementById('sbw_1').src = '".$_SERVER['PHP_SELF']."?content_type=' + type;
	}
}
";

  $scripts->addLine($line);
  $manager->addScripts($scripts);
}

$manager->addContainer($main);
print($manager->getHTML());

?>