<?php 
##############################################################
##In-portal													##
##############################################################
##					      In-portal							##
##					Intechnic Corporation					##
##			   All Rights Reserved, 1998-2002				##
##															##	
##	No portion of this code may be copied, reproduced or	##	
##	   otherwise redistributed without proper written		##
##	  consent of Intechnic Corporation.  Violation will		##
##	   result in revocation of the license and support		##
##	 privileges along maximum prosecution allowed by law.	##
##############################################################
if(!strlen($pathtoroot))
{
  $path=dirname(realpath($_SERVER['SCRIPT_FILENAME']));
  if(strlen($path))
  {
    /* determine the OS type for path parsing */
    $pos = strpos($path,":");
    if ($pos === false)
    {
      $gOS_TYPE="unix";
      $pathchar = "/";
    }
    else
    {
      $gOS_TYPE="win";
      $pathchar="\\";
    }
    $p = $path.$pathchar;
    /*Start looking for the root flag file */
    while(!strlen($pathtoroot) && strlen($p))
    {
      $sub = substr($p,strlen($pathchar)*-1);
      if($sub==$pathchar)
      {
        $filename = $p."root.flg";
      }
      else
        $filename = $p.$pathchar."root.flg";
      if(file_exists($filename))
      {
        $pathtoroot = $p;
      }
      else
      {
        $parent = realpath($p.$pathchar."..".$pathchar);
	if($parent!=$p)
	{
	  $p = $parent;
	}
	else
	  $p = "";
      }
    }
    if(!strlen($pathtoroot))
      $pathtoroot = ".".$pathchar;
  }
  else
  {
    $pathtoroot = ".".$pathchar;
  }
}

$sub = substr($pathtoroot,strlen($pathchar)*-1);
if($sub!=$pathchar)
{
  $pathtoroot = $pathtoroot.$pathchar;
}
//echo $pathtoroot;

require_once($pathtoroot."kernel/startup.php");
//admin only util
$rootURL="http://".ThisDomain().$objConfig->Get("Site_Path");
$admin = $objConfig->Get("AdminDirectory");
if(!strlen($admin))
    $admin = "admin";
$localURL=$rootURL."kernel/";
$adminURL = $rootURL.$admin;
$imagesURL = $adminURL."/images"; 

require_once ($pathtoroot.$admin."/include/elements.php"); 
require_once ($pathtoroot."kernel/admin/include/navmenu.php"); 
require_once($pathtoroot.$admin."/toolbar.php");
require_once($pathtoroot.$admin."/listview/listview.php");

require_once($pathtoroot."kernel/include/tag-class.php");
$objTagList = new clsTagList();
$objTagList->LoadGlobalTags();

$section = "in-portal:template_editor";

$ThemeId = GetVar('ThemeId');
$FileId = GetVar('FileId');

if(!$_POST['Action'] || !is_object($f) ) $f = new clsThemeFile($FileId);

if($FileId)
{  
	$theme = new clsTheme( $f->Get("ThemeId") ); // create theme from file
    $f->LoadFileContents(false);
    $name = $f->Get("FileName");
    $Action="m_template_edit";
}
else
{   
	if($ThemeId) // if theme is set
   	{
		$theme = new clsTheme($ThemeId);
       	$f->Set("ThemeId",$ThemeId);
       	$f->Set("FilePath",$theme->Get("name"));
    	$name = "New Template";
       	$Action = "m_template_add";
   	}
   	
}


if( is_object($f) && isset($_POST['Action']) )
{ 
	// aka SetFieldsFromHash
	//print_pre($_POST);
	//echo "SetFields From POST<br>";
	$f->Set( Array('FileName','Description','Contents'),
			 Array(basename($_POST['name']), $_POST['Description'], $_POST['contents']) );
}

$objTemplateCheck = new clsTemplateChecker($theme->ThemeDirectory()."/");
if(count($f->Contents)>0)
{
	$fullname = $f->Get("FilePath")."/".$name;
	$TemplateType = $objTemplateCheck->GetTemplateType($fullname);
	
}
else 
  $TemplateType="new";
  
if(is_object($theme))
{
	$theme->GetFileList("","FilePath,FileName");
	$AllThemeFiles = $theme->Files->Items;
}

if($ThemeId)
{
  $m_var_list_update["theme"]=$ThemeId;
  $cat = 0;
  $template = "index";
  if(is_object($f))
  {  
    $p = $f->Get("FilePath");
    if(strlen($p))
    {
    	if(substr($p,0,1)=="/")
    	  $p = substr($p,1);
    	$p .= "/";
    	$mod = $objModules->GetModuleByPath($p);
    	if(is_object($mod))
    	{
    		$cat = $mod->Get("RootCat");
    		$template=$mod->Get("TemplatePath")."index";
    	}
    }  
  }
  $m_var_list_update["t"] = $template;
  $m_var_list_update["cat"] = $cat;
  $PreviewUrl ="http://".ThisDomain().$objConfig->Get("Site_Path")."index.php?env=".BuildEnv();  
}

unset($objEditItems);

$envar = "env=".BuildEnv();
$formaction = $_SERVER["PHP_SELF"]."?".$envar;
$sec = $objSections->GetSection($section);

$objListToolBar = new clsToolBar();

$objListToolBar->Set("section",$section);
$objListToolBar->Set("load_menu_func","");
$objListToolBar->Set("CheckClass","ThemeChecks");
  
$objListToolBar->Add("select", "la_ToolTip_Save","#","swap('select','toolbar/tool_select_f2.gif');",
                    "swap('select', 'toolbar/tool_select.gif');",
                     "SelectSubmit();",
                     "tool_select.gif");

$objListToolBar->Add("cancel", "la_ToolTip_Close","#","swap('cancel','toolbar/tool_stop_f2.gif');",
                    "swap('cancel', 'toolbar/tool_stop.gif');","window.close();","tool_stop.gif");

$objListToolBar->Add("divider");

$objListToolBar->Add("template_preview","la_ToolTip_Preview","#", "swap('template_preview','toolbar/tool_preview_template_f2.gif');",
                    "swap('template_preview', 'toolbar/tool_preview_template.gif');",
                     "ShowPreview('$PreviewUrl');","tool_preview_template.gif");

//$objListToolBar->AddToInitScript("fwLoadMenus();");

$title = prompt_language("la_Text_Editing")." ".prompt_language("la_Text_Theme")." '".$theme->Get("Name")."' - ".prompt_language("la_Text_Template");
$title .= " '".$name."'";

$path = $f->FullPath();
if(!is_writable($path))
  $title .= " (".admin_language("la_text_ReadOnly").")";
int_header($objListToolBar,NULL,$title);
?>
<TABLE cellSpacing="0" cellPadding="2" width="100%" border="0" class="tableborder">
<form name="template" ID="template" action="<?php echo $_SERVER["PHP_SELF"]."?".$envar;?>" method=POST>

<?php if( $f->HasError() ) { ?>
<TR <?php int_table_color(); ?> style="color: red;">
  <TD>ERROR:</TD>
  <TD><?php echo $f->ErrorMsg(); ?></TD>
  <TD></TD>
</TR>
<?php } ?>

<?php int_subsection_title(prompt_language("la_tab_General")); ?>

<TR <?php int_table_color(); ?> >
  <TD><?php echo prompt_language("la_prompt_FileId"); ?></TD>
  <TD><?php echo $f->Get("FileId"); ?></TD>
  <TD></TD>
</TR>

<TR <?php int_table_color(); ?> >
  <TD><SPAN id="prompt_name" CLASS="text"><?php echo prompt_language("la_prompt_FileName"); ?></SPAN></TD>
  <TD><input type="text" NAME="name" <?php if($Action == 'm_template_edit') echo 'Readonly'; ?> VALUE="<?php echo $f->Get("FileName"); ?>"> <?php if($Action == 'm_template_edit') echo '&nbsp;(read only)'; ?></TD>
  <TD></TD>
</TR>

<TR <?php int_table_color(); ?> >
  <TD><SPAN id="prompt_description" CLASS="text"><?php echo prompt_language("la_prompt_Description"); ?></SPAN></TD>
  <TD><input type="text" NAME="Description" VALUE="<?php echo $f->Get("Description"); ?>"></TD>
  <TD></TD>  
</TR>                                                       
  <!--
  <tr <?php int_table_color(); ?>>
   <td valign="top" class="text"><?php echo prompt_language("la_prompt_EnableCache"); ?></td>
    <td>
      <input type="checkbox" name="enable_cache" class="text" value="1" <?php if($f->Get("EnableCache") == 1) echo "checked"; ?>>
      <input type=text NAME="cache_timeout" VALUE="<?php echo $f->Get("CacheTimeout"); ?>">
    </td>
    <td class="text">&nbsp;</td>
  </tr>
	-->
	
<?php int_subsection_title(prompt_language("la_Text_Template")); ?>
<tr <?php int_table_color(); ?> >
	<td colspan="3">
	    Item Tags:
	    <SELECT id="itemtags" name="itemtags" onChange="TagSelectChange(this,'tagattribs',true);"></select>
	  	&nbsp;
	  	<a href="javascript:InsertTag('itemtags');">Insert Tag</a>
	  	&nbsp;
	  	Global Tags:
	  	<SELECT id="taglib" name="taglib" onChange="TagSelectChange(this,'tagattribs',true);"></select>
	  	&nbsp;
	  	<a href="javascript:InsertTag('taglib');">Insert Tag</a>
	</td>	
</tr>
<TR <?php int_table_color(); ?> >
  <td colspan="3" align="center">
   		<textarea NAME="contents" id="contents" rows="20" cols="95" nowrap><?php echo $f->Get('Contents'); ?></textarea>
   </td>
</TR>
<tr <?php int_table_color(); ?> >
	<td colspan="3">
		<table border="0" cellpadding="0" cellspacing="0" width="100%">
		  <tr>
			<td width="30%" align="left" id="taghelp">&nbsp;</td>
			<td width="70%" align="left" id="tagexample">&nbsp;</td>
			<!--<td width="33%" align="left" id="attrhelp">&nbsp;</td>-->
			<!--<td id="tagattribs" valign="top">&nbsp;</td>-->
		 </tr>
		</table>
	</td>
</tr>
<TR <?php int_table_color(); ?> >
    <td colspan="3">
      <input type="hidden" NAME="Action" VALUE="<?php echo $Action; ?>">
      <INPUT TYPE="hidden" NAME="ThemeId" VALUE="<?php echo $f->Get("ThemeId"); ?>">
      <INPUT TYPE="hidden" NAME="FileId" VALUE="<?php echo $f->Get("FileId"); ?>">
      <INPUT TYPE="hidden" NAME="FilePath" VALUE="<?php echo $f->Get("FilePath"); ?>">
      <input type="hidden" name="ThemeEditStatus" VALUE="0">
    </td>
</tr>
</FORM>
</TABLE>
  <SCRIPT language="JavaScript">
      init('template','contents');
      LoadTags('taglib','global',false);  
      LoadTags('itemtags','global',true);
  </SCRIPT>

<?php int_footer(); ?>