<?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, $theme_path;

    // vars messages
    global $msg;

    // vars template
    global $category, $custom_field_form;


    // generate custom field
    $links_obj                                    = new clsLink;
    $links_obj->table_name                        = "idx_link";
    $links_obj->category_table_name               = "idx_category";
    $links_obj->editor_table_name                 = "idx_editor";
    $links_obj->autogenerate_form_text            = $msg["00001"];
    $links_obj->autogenerate_form_textarea        = $msg["00002"];
    $links_obj->autogenerate_form_image_add       = $msg["00005"];
    $links_obj->autogenerate_form_file_add        = $msg["00005"];
    $links_obj->autogenerate_form_select          = $msg["00007"];
    $links_obj->autogenerate_form_multiselect     = $msg["00008"];
    $links_obj->autogenerate_form_checkbox        = $msg["00009"];
    $links_obj->autogenerate_form_radio           = $msg["00010"];
    $links_obj->autogenerate_form_date            = $msg["00011"];
    $links_obj->autogenerate_form_datetime        = $msg["00012"];
    $links_obj->autogenerate_form_template        = $theme_path . "custom_field_form_rows.html";
    $custom_field_form                            = $links_obj->GenerateCustomFieldForm(true);

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

    // get editor category
    $categories = $links_obj->GetEditorCategory($_COOKIE['COOKIE_USERNAME']);

    // build query
    $query = "select category_id, parent_id, name from idx_category where 0!=0";

    while (list(, $v) = @each($categories)) {
      $query .= " or category_id = '$v'";
    }

    $category_obj->query = $query;
    $category            = $category_obj->DisplayCategoryListBox(-1, 0, 2);

    DisplayTemplate($theme_path . "cp/editor_link_search_form.html", "\$category,\$custom_field_form");
  }


  /*===================================================
    ProcessFormSearchLink()
  ===================================================*/
  
  function ProcessFormSearchLink() {

    // vars global configuration
    global $category_separator, $theme_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;

    $title     = $_POST["title"];

    // get field
    $links_obj = new clsLink;
    $links_obj->table_name          = "idx_link";
    $links_obj->category_table_name = "idx_category";
    $links_obj->editor_table_name   = "idx_editor";
    $categories                     = $links_obj->GetEditorCategory($_COOKIE['COOKIE_USERNAME']);
    $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 == 'category_id' && !empty($cat)) {
        $query .= "and $field = '$cat' ";
      }
      elseif ($field == 'category_id' && empty($cat)) {
        $query .= "and (0!=0";
        
        while (list(, $v) = @each($categories)) {
          $query .= " or category_id = '$v'";
        }
        @reset($categories);

        $query .= ") ";
      }
      elseif (!empty($$field)) {
        if ($i >= $links_obj->custom_field_offset && (!$links_obj->fields_searchable[$i] || $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 = $theme_path . "cp/editor_link_search_rows.html";
    $links_obj->date_format   = $msg["20081"];
    $links_obj->paging        = true;
    $links_obj->pg_size       = 15;
    $links_obj->href          = "editor_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_searchable[$i] || $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++;
    }

    $links_obj->more_param
                                =
      "&pflag=search&title=$title&url=$url&description=$description&cat=$cat&contact_name=$contact_name&email=$email"
      . $custom_field_param;

    $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";

    DisplayTemplate($theme_path . "cp/editor_link_search_result.html", "\$num_rows,\$pagination,\$link,\$param");
  }


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

  include "../application.php";

  AddFilter('validate_editor');
  RunPreFilter(__FILE__);
  
  if (empty($pflag)) {
    ShowFormSearchLink();
  }
  elseif ($pflag == 'search') {
    ProcessFormSearchLink();
  }

  RunPostFilter(__FILE__);

?>