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

  /*===================================================
    ShowFormChangeField()
  ===================================================*/

  function ShowFormChangeField() {

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

    // vars url & form
    global $id;

    // vars messages

    // vars template
    global $field, $text, $type, $field_org, $option, $required, $searchable, 
             $permission, $img_width, $img_height, $file_size, $file_extension;

    $field_org = $id;
    $row = $dbConn->FetchOne("select * from idx_custom_field where field_name = '$id'");
    $text = $row['field_text'];
    $type = $row['field_type'];
    $option = $row['field_option'];
    $img_width = $row['field_img_width'];
    $img_height = $row['field_img_height'];
    $file_size = $row['field_file_size'];
    $file_extension = $row['field_file_extension'];
    $required = $row['required'];
    $searchable = $row['searchable'];
    $permission = $row['permission'];

    DisplayTemplate($admin_template_path . "link_custom_field_change_form.html",
      "\$field_org,\$text,\$type,\$option,\$required,\$searchable,\$permission,\$img_width,\$img_height,\$file_size,\$file_extension");
  }


  /*===================================================
    ProcessFormChangeField()
  ===================================================*/

  function ProcessFormChangeField() {

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

    // vars url & form
    global $field, $text, $type, $field_org, $option, $required, $searchable, 
             $permission, $img_width, $img_height, $file_size, $file_extension;

    // vars messages
    global $msg;

    // vars template
    global $message, $back;

    if(empty($field)) { $error_msg = $msg["20361"]; }
    elseif(empty($type)) { $error_msg = $msg["20362"]; }
    elseif(!IsAlphanumericUnderscore($field)) { $error_msg = $msg["20363"]; }

    if(empty($error_msg)) {
      $field = strtolower($field);

      switch($type) {
        case 'TEXTAREA':
        case 'MULTISELECT':
          $field_type = 'text';
          break;
        case 'DATE':
        case 'DATETIME':
          $field_type = $type;
          break;
        default:
          $field_type = 'varchar(255)';
      }

      $query = "alter table idx_link change `$field_org` `$field` $field_type";
      $result = $dbConn->Execute($query);

      $query = "alter table idx_link_temp change `$field_org` `$field` $field_type";
      $result = $dbConn->Execute($query);

      $query = "delete from idx_custom_field where field_name = '$field_org'";
      $result = $dbConn->Execute($query);
      $option = trim($option);
      $query = "insert into idx_custom_field
                (field_name, field_text, field_type, field_option, field_img_width, field_img_height, field_file_size, field_file_extension, required, searchable, permission) values
                ('$field', '$text', '$type', '$option', '$img_width', '$img_height', '$file_size', '$file_extension', '$required', '$searchable', '$permission')";
      $result = $dbConn->Execute($query);

      $message = $msg["20375"];
      $back = "<a href=link_custom_field.php>Back</a>";
    }
    else
      $message = $error_msg;

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


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

  include "../application.php";
  
  RunPreFilter(__FILE__);

  DisplayTemplate($admin_template_path . "header.html");
  if(empty($pflag)) {
    ShowFormChangeField();
  }
  elseif ($pflag == 'change') {
    ProcessFormChangeField();
  }
  DisplayTemplate($admin_template_path . "footer.html");

  RunPostFilter(__FILE__);

?>