<?php

/**
 *
 * INDEXU
 * Copyright(C), Nicecoder, 2000-2006, All Rights Reserved.
 *
 * INDEXU(tm) is protected by Indonesia and International copyright laws.
 * Unauthorized use or distribution of INDEXU(tm) is strictly prohibited,
 * violators will be prosecuted. To obtain a license for using INDEXU(tm),
 * please register at Nicecoder home page at http://www.nicecoder.com
 *
 * Author:
 *    Dody Rachmat Wicaksono (dody@nicecoder.com)
 *    M. Zuber (zubby@nicecoder.com)
 *
 */

  /*===================================================
    ShowFormRenameTemplate()
  ===================================================*/

  function ShowFormRenameTemplate() {

    // vars global configuration
    global $admin_template_path, $theme_path;

    // vars url & form
    global $theme, $new_name;

    // vars messages

    // vars template
    global $file_content;

    DisplayTemplate($admin_template_path . "template_rename_form.html", "\$theme,\$new_name");

  }


  /*===================================================
    ProcessFormRenameTemplate()
  ===================================================*/

  function ProcessFormRenameTemplate() {

    // vars global configuration
    global $admin_template_path, $theme_path, $active_theme, $base_path;

    // vars url & form
    global $theme, $new_name;

    // vars messages
    global $msg;

    // vars template
    global $message, $back;
    
    // rename folder
    
    $new_name = str_replace(' ','_',$new_name);
    rename($theme_path . $theme, $theme_path . $new_name);
    
    // if active theme, then change application.php

    if ($theme==$active_theme) {
      $lines = file($base_path . "application.php");

      $i = 0;
      $count_lines = @count($lines);
      while ($i < $count_lines) {
        $s = chop(ltrim($lines[$i]));
        if(substr($s,0,13)=='$active_theme') {
          // change
          $arr       = explode('=',$lines[$i]);
          $arr[1]    = " '{$new_name}';\n";
          $lines[$i] = implode('=',$arr);
        }
        $i++;
      }

      // write again
      $content = implode("",$lines);
      $fp = fopen($base_path . "application.php", 'w+');
      fwrite($fp,$content);
      fclose($fp);
    }
    
    $message = $msg["20427"];
    $back = "<a href=template_manager.php>Back</a>";

    DisplayTemplate($admin_template_path . "message.html", "\$message,\$back");
  }


  /*===================================================
    main
  ===================================================*/

  include "../application.php";

  RunPreFilter(__FILE__);

  DisplayTemplate($admin_template_path . "header.html");
  if (empty($pflag)) {
    ShowFormRenameTemplate();
  }
  elseif ($pflag == 'rename') {
    ProcessFormRenameTemplate();
  }
  DisplayTemplate($admin_template_path . "footer.html");

  RunPostFilter(__FILE__);

?>