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

  /*===================================================
    ShowFormAddLink()
  ===================================================*/

  function ShowFormAddLink() {

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

    // vars url & form

    // vars messages
    global $msg;

    // vars template
    global $category,$custom_field_form, $add_cat1, $add_cat2;

    // generate category

    $category_obj = new clsCategory;
    $category_obj->table_name = "idx_category";
    $category_obj->separator = $category_separator;
    $category = $category_obj->DisplayCategoryListBox(-1,0,1);
    $add_cat1 = str_replace("<select name=\"cat\">","<select name=\"add_cat1\">",$category_obj->DisplayCategoryListBox(-1,0,1));
    $add_cat1 = str_replace("<option value=\"\">","<option value=\"0\">",$add_cat1);
    $add_cat2 = str_replace("<select name=\"cat\">","<select name=\"add_cat2\">",$category_obj->DisplayCategoryListBox(-1,0,1));
    $add_cat2 = str_replace("<option value=\"\">","<option value=\"0\">",$add_cat2);

    // 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["20061"].$custom_field_form;

    DisplayTemplate($admin_template_path . "link_add_form.html",
      "\$category,\$custom_field_form,\$add_cat1,\$add_cat2");
  }


  /*===================================================
    ProcessFormAddLink()
  ===================================================*/

  function ProcessFormAddLink() {

    // vars global configuration
    global $category_separator, $admin_template_path, $email_address, $theme_path, $active_theme;

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

    // vars messages
    global $msg;

    // vars template
    global $message, $back, $error_msg;

    $theme_path = $theme_path . $active_theme;

    // 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";
      $links_obj->category_table_name = "idx_category";
      $links_obj->editor_table_name = "idx_editor";
      $links_obj->user_table_name = "idx_users";

      $category_obj = new clsCategory;
      $category_obj->table_name = "idx_category";
      $category_obj->user_table_name = "idx_users";
      $category_obj->separator = $category_separator;
      $category = $category_obj->GetCategoryPath($cat);

      $add_result = $links_obj->Add(true);

      if ($add_result != -1) {
        // update new flag
        $links_obj->UpdateNewFlag();

        if ($notify_link_owner) {
          // send link owner
          $body = EvalTemplate($theme_path . "/mail/add_ok.mail", $links_obj->email_template_fields);
          $from = $email_address;
          @mail($email, $msg["20130"], $body, "From: $from \nX-Mailer: INDEXU_X-Mailer/1.0");
        }

        $message = $msg["20062"].$msg["30015"];
        $back = "<a href=link_add.php>Back</a>";
      }
      else {
        $message = $error_msg;
      }
    }
    else {
      $message = $error_msg;
    }

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

  }


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

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

  DisplayTemplate($admin_template_path . "header.html");
  if (empty($pflag)) {
    ShowFormAddLink();
  }
  elseif ($pflag == 'add') {
    ProcessFormAddLink();
  }
  DisplayTemplate($admin_template_path . "footer.html");

  RunPostFilter(__FILE__);

?>