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

  /*===================================================
    ShowFormEditLink()
  ===================================================*/

  function ShowFormEditLink() {

    // vars global configuration
    global $dbConn, $category_separator, $admin_template_path,
             $theme_path, $pflag, $no_image_file;

    // vars url & form
    global $id, $title, $url, $description, $cat, $contact_name, $email, $bid, $password, $add_cat1, $add_cat2;

    // vars template
    global $error_msg;

    global $vid;

    $vid   = $id;
    $title = $_POST["title"];

    // vars messages
    global $msg;

    // vars template

    $tpl = new Template();

    $links_obj = new clsLink;
    $links_obj->table_name          = "idx_link_temp"; // temp table
    $links_obj->category_table_name = "idx_category";
    $links_obj->custom_field_offset++;

    $category_obj = new clsCategory;
    $category_obj->table_name = "idx_category";
    $category_obj->separator  = $category_separator;

    $fields                   = $links_obj->GetFields();

    $i                        = 0;
    
    $count_fields = @count($fields);
    while ($i < $count_fields) {
      $field = $fields[$i];
      global $$field;
      $i++;
    }

    if ($pflag != "edit") {
      // get values
      $query       = "select * from idx_link_temp where vid = '$id'";
      $result      = $dbConn->Execute($query);
      $category_id = $result->Fields("category_id");
      $cat1        = $result->Fields("cat1");
      $cat2        = $result->Fields("cat2");
    }
    else {
      $category_id = $cat;
      $cat1        = $add_cat1;
      $cat2        = $add_cat2;
    }

    // generate custom field
    $links_obj->autogenerate_form_text            = $msg["30001"];
    $links_obj->autogenerate_form_textarea        = $msg["30002"];
    $links_obj->autogenerate_form_image           = $msg["30004"];
    $links_obj->autogenerate_form_file            = $msg["30006"];
    $links_obj->autogenerate_form_select          = $msg["30007"];
    $links_obj->autogenerate_form_multiselect     = $msg["30008"];
    $links_obj->autogenerate_form_checkbox        = $msg["30009"];
    $links_obj->autogenerate_form_radio           = $msg["30010"];
    $links_obj->autogenerate_form_date            = $msg["30011"];
    $links_obj->autogenerate_form_datetime        = $msg["30012"];
    $links_obj->autogenerate_form_template        = $theme_path . "cp/link_custom_field_form.html";
    $links_obj->template_file                     = $theme_path . "cp/editor_link_validate_edit_form.html";
    $links_obj->header                            = $msg["20061"];

    // fetch
    $i = 0;

    while ($i < $count_fields) {
      if ($pflag != "edit")
        $$fields[$i] = $result->Fields("$fields[$i]");

      $$fields[$i] = htmlspecialchars(stripslashes($$fields[$i]));

      if($links_obj->fields_type[$i]=="IMAGE")
        if($$fields[$i]=="")
          $$fields[$i] = $no_image_file;

      $tpl->assign($fields[$i], $$fields[$i]);
      $i++;
    }

    global $category;
    $category = $category_obj->DisplayCategoryListBox($category_id, 0, 1);
    $tpl->assign('category', $category);
    $tpl->assign('error_msg', $error_msg);

    $add_cat1 = str_replace("<select name=\"cat\">", "<select name=\"add_cat1\">",
                            $category_obj->DisplayCategoryListBox($cat1, 0, 1));
    $add_cat1 = str_replace("<option value=\"\">", "<option value=\"0\">", $add_cat1);
    $add_cat1 = str_replace("Main Category", "", $add_cat1);
    $add_cat2 = str_replace("<select name=\"cat\">", "<select name=\"add_cat2\">",
                            $category_obj->DisplayCategoryListBox($cat2, 0, 1));
    $add_cat2 = str_replace("<option value=\"\">", "<option value=\"0\">", $add_cat2);
    $add_cat2 = str_replace("Main Category", "", $add_cat2);
    $tpl->assign('add_cat1', $add_cat1);
    $tpl->assign('add_cat2', $add_cat2);

    $custom_field_form = $links_obj->GenerateCustomFieldForm();
    if (!empty($custom_field_form))
      $custom_field_form = $links_obj->header . $custom_field_form;
    $tpl->assign('custom_field_form', $custom_field_form);
    $line = $tpl->fetch($links_obj->template_file);

    print $line;
  }


  /*===================================================
    ProcessFormEditLink()
  ===================================================*/

  function ProcessFormEditLink() {

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

    global $allowed_file_extension, $base_path;
    $folder = 'upload_files';

    // vars url & form
    global $id, $title, $url, $description, $cat, $contact_name, $email, $bid, $password, $add_cat1, $add_cat2, $reciprocal_url;

    // vars messages
    global $msg;

    // vars template
    global $error_msg;

    // vars template
    global $message;

    $title = $_POST["title"];

    // verify input

    if (empty($title))
      $error_msg = $msg["20071"];
    elseif (empty($url))
      $error_msg = $msg["20072"];
    elseif (empty($cat))
      $error_msg = $msg["20073"];
    elseif (!empty($email) && !IsEmailAddress($email))
      $error_msg = $msg["20074"];
    elseif (!empty($bid) && !IsMoney($bid))
      $error_msg = $msg["20075"];

    if (empty($error_msg)) {
      $links_obj = new clsLink;
      $links_obj->table_name          = "idx_link_temp"; // temp table
      $links_obj->category_table_name = "idx_category";
      $links_obj->custom_field_offset++;
      $fields                         = $links_obj->GetFields();

      $i                              = 0;

      $count_fields = @count($fields);
      while ($i < $count_fields) {
        $field = $fields[$i];
        global $$field;
        $i++;
      }

      $sql_old = "select * from $links_obj->table_name where vid = '$id'";
      $result_old = $dbConn->Execute($sql_old);

      // build query

      $query = "update $links_obj->table_name set ";
      $i     = 1;

      while ($i < $count_fields) {
        $field = $fields[$i];
        $old[$field] = $result_old->Fields($field);
        global $$field;

        if($links_obj->fields_type[$i]=="IMAGE" || $links_obj->fields_type[$i]=="FILE") {
          if ($links_obj->fields_file_extension[$i]) {
            $allowed_file_extensionx = explode(",", $links_obj->fields_file_extension[$i]);
          }
          else {
            $allowed_file_extensionx = explode(",", $allowed_file_extension);
          }
          if(!empty($_FILES[$field]['name'])) {
            // get file extension
            $temp = explode(".",$_FILES[$field]['name']);
            $last = count($temp)-1;
            $file_ext = $temp[$last];
            $field_name = $field;

            // put file in queue
            $file_obj['src'] = $_FILES[$field]['tmp_name'];
            $file_obj['dst'] = $base_path . $folder. "/". $field ."_ID.". $file_ext;
            $file_queue[$field] = $file_obj;
          }
        }


        if ($field != "link_id" && $field != "hits" && $field != "votes" && $field != "rating" && $field != "hot" && $field != "new"
          && $field != "pick" && $field != "top_rated" && $field != "updated" && $field != "date"
          && $field != "password" && $field != "avg_review" && $field != "reviews" && $field != 'last_updated') {
          if ($field == 'category_id')
            $value = "$cat";
          else
            $value = $$field;

          if ($field == 'cat1')
            $value = "$add_cat1";

          if ($field == 'cat2')
            $value = "$add_cat2";

          if ($field == 'last_updated')
            $query .= "$field = $value, ";
          elseif($links_obj->fields_type[$i]=="IMAGE" || $links_obj->fields_type[$i]=="FILE") {
            $file_del = "{$field}_del";
            global $$file_del;
            if ($$file_del) {
              $query .= "$field = '', ";
              @unlink("{$base_path}$old[$field]");
            }
            else {
              if ($file_queue[$field]) {
                @unlink("{$base_path}$old[$field]");
                $v = $file_queue[$field];
                $k = $field;
                $v['dst'] = str_replace('ID', 'temp'.$id, $v['dst']);
                if (@copy($v['src'],$v['dst'])) {
                  $relatif_path = $folder. "/". $k ."_temp". $id .".". $file_ext;
                  $query .= "$k = '$relatif_path', ";
                }
              }
              else {
                $query .= "$field = '$old[$field]', ";
              }
            }
          }
          else {
            if (!is_array($value)) {
              $query .= "$field = '$value', ";
            }
            else {
              $query .= "$field = '".@implode(',', $value)."', ";
            }
          }
        }

        $i++;
      }

      $query  = substr($query, 0, strlen($query) - 2); // delete last ', '
      $query .= " where vid = '$id'";
      $result = $dbConn->Execute($query);

      DisplayTemplate($theme_path . "cp/editor_link_validate_edit_ok.html");
    }
    else
      ShowFormEditLink();
  }


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

  include "../application.php";

  AddFilter('validate_editor');
  RunPreFilter(__FILE__);

  if (empty($pflag)) {
    ShowFormEditLink();
  }
  elseif ($pflag == 'edit') {
    ProcessFormEditLink();
  }
 
  RunPostFilter(__FILE__);

?>