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

  /*===================================================
    ShowFormSearchLink()
  ===================================================*/

  function ShowFormSearchLink() {

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

    // vars url & form

    // vars messages
    global $msg;

    // vars template
    global $category,$custom_field_form;

    // generate category

    $category_obj = new clsCategory;
    $category_obj->table_name = "idx_category";
    $category_obj->separator = $category_separator;
    $category = $category_obj->DisplayCategoryListBox(-1,false,1);

    // generate custom field
    $links_obj                                    = new clsLink;
    $links_obj->table_name                        = "idx_link";
    $links_obj->category_table_name               = "idx_category";
    $links_obj->autogenerate_form_text            = $msg["30001"];
    $links_obj->autogenerate_form_textarea        = $msg["30002"];
    $links_obj->autogenerate_form_image_add       = $msg["30005"];
    $links_obj->autogenerate_form_file_add        = $msg["30005"];
    $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 = $admin_template_path . "link_custom_field_form.html";
    $custom_field_form = $links_obj->GenerateCustomFieldForm(true);

    if(!empty($custom_field_form))
      $custom_field_form = $msg["20111"].$custom_field_form;

    DisplayTemplate($admin_template_path . "link_search_form.html",
      "\$category,\$custom_field_form");
  }


  /*===================================================
    ProcessFormSearchLink()
  ===================================================*/

  function ProcessFormSearchLink() {

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

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

    // vars messages
    global $msg;

    // vars template
    global $num_rows,$pagination,$link,$param,$back,$pg_which;

    // get field
    $links_obj = new clsLink;
    $links_obj->table_name = "idx_link";
    $links_obj->category_table_name = "idx_category";
    $fields = $links_obj->GetFields();


    // ----- querying -----

    $query = "select * from idx_link where 0=0 ";
    $i = 0;
    $count_fields = @count($fields);
    while ($i < $count_fields) {
      $field = $fields[$i];

      global $$field;

      if($field=='link_id' && !empty($$field))
        $query .= "and ($field = '{$$field}')";

      elseif($field=='category_id' && !empty($cat))
        $query .= "and ($field = '$cat' or cat1 = '$cat' or cat2 = '$cat')";

      elseif (!empty($$field)) {
        if ($i >= $links_obj->custom_field_offset && ($links_obj->fields_type[$i] == 'IMAGE' || $links_obj->fields_type[$i] == 'FILE')) {
          $i++;
          continue;
        }
        if (!is_array($$field)) {
          $query .= "and $field like '%" . $$field . "%' ";
        }
        else {
          $custom_fields = $$field;
          $query .= "and (0!=0 ";
          while (list($k, $v) = @each($custom_fields)) {
            $query .= "or $field like '%" . $v . "%' ";
          }
          $query .= ")";
        }
      }

      $i++;
    }
    //$query .= " order by bid desc";

    $links_obj->query = $query;
    $links_obj->template_file = $admin_template_path . "link_view_rows.html";
    $links_obj->date_format = $msg["20081"];

    $links_obj->paging = true;
    $links_obj->pg_size = 15;
    $links_obj->href = "link_search.php";


    // get more param for custom fields

    $i = $links_obj->custom_field_offset;
    $count_fields = @count($fields);
    while ($i < $count_fields) {
      $field = $fields[$i];

      global $$field;

      if ($links_obj->fields_type[$i] == 'IMAGE' || $links_obj->fields_type[$i] == 'FILE') {
        $i++;
        continue;
      }

      if (!is_array($$field)) {
        $custom_field_param .= "&" . $field . "=" . $$field;
      }
      else {
        $custom_fields = $$field;
        while (list($k, $v) = @each($custom_fields)) {
          $custom_field_param .= "&" . $field . "[]=" . $v;
        }
      }
      $i++;
    }

    $par = "&pflag=search&title=$title&url=$url&description=$description&cat=$cat&contact_name=$contact_name&email=$email".$custom_field_param;

    $links_obj->more_param = $par;

    $links_obj->new_link_number=1;
    $link = $links_obj->Display();
    $pagination = $links_obj->pagination;
    $num_rows = $links_obj->new_link_number-1;
    $param = $links_obj->more_param . "&pg_which=$pg_which";

    $back = "link_search.php?".$par."&pg_which=$pg_which";

    DisplayTemplate($admin_template_path . "link_search_result.html",
      "\$num_rows,\$pagination,\$link,\$param,\$back");
  }


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

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

  DisplayTemplate($admin_template_path . "header.html");
  if (empty($pflag)) {
    ShowFormSearchLink();
  }
  elseif ($pflag == 'search') {
    ProcessFormSearchLink();
  }
  DisplayTemplate($admin_template_path . "footer.html");

  RunPostFilter(__FILE__);

?>