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

  /*===================================================
    ShowFormDBExport()
  ===================================================*/

  function ShowFormDBExport() {

    // vars global configuration
    global $admin_template_path;

    // vars url & form

    // vars messages

    // vars template
    

    DisplayTemplate($admin_template_path . "db_export_form.html");
  }


  /*===================================================
    ProcessFormDBExport()
  ===================================================*/

  function ProcessFormDBExport() {

    // vars global configuration
    global $dbConn;
    
    // vars url & form
    global $t;

    // vars messages

    // vars template

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-type: text/plain");
      
    if($t=='link') $tb = 'idx_link';
    elseif($t=='category') $tb = 'idx_category';
    elseif($t=='ml') $tb = 'idx_mailing_list';

    header("Content-Disposition: attachment; filename=\"$tb.csv\";" );
    
    if($t=='ml') $query = "select * from $tb where status=1";
    else $query = "select * from $tb";

    $result = $dbConn->Execute($query);
    $num_rows = $result->RecordCount();
    $num_fields = $result->FieldCount();
    
    // get fields information 

    if($result) {
      $i=0;
      while($i<$num_fields) {
        $fld = $result->FetchField($i);
        $fields[] = $fld->name;
        $i++;
      }
    }
    
    if($t=='ml') {
      unset($fields);
      $fields[] = "email";
    }

    while (!$result->EOF) {      
      $output = '';
    
      // get field's value
    
      $j = 0;      
      $count_fields = @count($fields);
      while ($j < $count_fields) {
        $value[$j] = $result->Fields("$fields[$j]");
        $value[$j] = $value[$j];        
        $value[$j] = str_replace("\n",'',$value[$j]);
        $value[$j] = str_replace("\r",'',$value[$j]);
        $output .= $value[$j];
        $output .= "\t";
        $j++;
      }                      
    
      $result->MoveNext();

      $output .= "\n";
      print $output;       
    }
  }


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

  include "../application.php";
  ini_set("max_execution_time", "3600");
  
  if ($pflag == "export") {
    RemoveFilter("gzip_compression");
    RemoveFilter("runtime");
  }
  RunPreFilter(__FILE__);

  if (empty($pflag)) {
    DisplayTemplate($admin_template_path . "header.html");
    ShowFormDBExport();
    DisplayTemplate($admin_template_path . "footer.html");
  }
  elseif ($pflag == 'export') {
    ProcessFormDBExport();
  }

  RunPostFilter(__FILE__);

?>