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

  /*===================================================
    ShowFormFindReplace()
  ===================================================*/

  function ShowFormFindReplace() {

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

    // vars url & form

    // vars messages

    // vars template
    global $message, $select_table, $hash;

    $query         = "show tables";
    $result_table  = $dbConn->Execute($query);
    $table_list[] = "";
    while ($row_table = $result_table->FetchRow()) {
      $table_list[$row_table[key($row_table)]] = $row_table[key($row_table)];
    }
    $select_table = ShowDropDown("select_table", implode(",", $table_list), implode(",", $table_list), "");
    $select_table = str_replace('<select name=select_table>', '<select name=select_table onchange="GetFields(this)">', $select_table);

    include "../key.php";
    $hash = '';

    DisplayTemplate($admin_template_path . "db_find_replace.html", "\$select_table,\$hash");
  }


  /*===================================================
    ProcessFormFindReplace()
  ===================================================*/

  function ProcessFormFindReplace() {

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

    // vars url & form
    global $select_table, $select_field, $find_str, $replace_str;

    // vars messages
    global $msg;

    // vars template
    global $message;

    $tpl = new Template();

    $query = "update $select_table
              set $select_field = replace($select_field, '$find_str', '$replace_str')";
    $result = $dbConn->Execute($query);

    if ($result) {
      $message = $msg["20373"];
      $message .= $msg["20377"];
      $tpl->assign('affected_rows', $dbConn->AffectedRows());
    }
    else {
      $tpl->assign('mysql_error', $dbConn->ErrorMsg());
      $message = $msg["20374"];
    }
    $message = $tpl->fetch('var:' . $message);

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


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

  include "../application.php";

  RunPreFilter(__FILE__);

  DisplayTemplate($admin_template_path . "header.html");
  if (empty($pflag)) {
    ShowFormFindReplace();
  }
  elseif ($pflag == 'findreplace') {
    ProcessFormFindReplace();
  }
  DisplayTemplate($admin_template_path . "footer.html");

  RunPostFilter(__FILE__);

?>