<?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)
 *
 */
 
  /*===================================================
    SelectClaim()
  ===================================================*/
  
  function SelectClaim() {
    global $theme_path;

    DisplayTemplate($theme_path . "cp/claim_select.html");
  }
  

  /*===================================================
    SearchLinkID()
  ===================================================*/
  
  function SearchLinkID() {

    // vars global configuration
    global $theme_path;

    // vars url & form
    global $query;

    // vars messages
    global $msg;

    // vars template
    global $link, $num_links, $pagination, $error_msg, $keyword;

    $tpl       = new Template();
    $keyword   = $query;
    $query     = "select * from idx_link where title like '%$query%'";

    $links_obj = new clsLink;
    $links_obj->query               = $query;
    $links_obj->table_name          = "idx_link";
    $links_obj->category_table_name = "idx_category";
    $links_obj->template_file       = $theme_path . "cp/claim_search_rows.html";
    $links_obj->date_format         = $msg["10151"];
    $links_obj->paging              = true;
    $links_obj->pg_size             = 25;
    $links_obj->href                = "claim.php";
    $links_obj->more_param          = "&pflag=search&query=$keyword";
    $link                           = $links_obj->Display();
    $pagination                     = $links_obj->pagination;
    $num_links                      = $links_obj->record_count_of_display;

    if ($num_links == 0) {
      $tpl->assign('keyword', $keyword);
      $error_msg = $tpl->fetch('var:' . $msg["10152"]);
      DisplayTemplate($theme_path . "search_error.html", "\$error_msg,\$keyword");
    }
    else
      DisplayTemplate($theme_path . "cp/claim_search.html", "\$link,\$num_links,\$pagination,\$keyword");
    }


  /*===================================================
    ClaimForm()
  ===================================================*/
  
  function ClaimForm() {
    global $dbConn, $theme_path, $link_id, $id, $username, $title, $error_msg;
    
    if (empty($link_id))
      $link_id = $id;
      
    if (empty($username))
      $username = $_COOKIE['COOKIE_USERNAME'];

    $query  = "select title from idx_link where link_id = '$link_id'";
    $result = $dbConn->Execute($query);
    $title  = $result->Fields("title");

    DisplayTemplate($theme_path . "cp/claim_form.html", "\$username,\$link_id,\$title,\$error_msg");
  }
  
  
  /*===================================================
    ProcessClaim()
  ===================================================*/

  function ProcessClaim() {
    global $dbConn, $msg, $theme_path, $link_id, $id, $username, $password, $title, $error_msg;

    // verify input

    if (empty($password))
      $error_msg = $msg['10343'];
    elseif (empty($username))
      $error_msg = $msg['10341'];
    elseif (strlen($username) < 3)
      $error_msg = $msg['10342'];
    elseif (!IsAlphanumeric($username))
      $error_msg = $msg['10344'];

    if (empty($error_msg)) {

      // check password
      $query  = "select password from idx_link where link_id = '$link_id'";
      $result = $dbConn->Execute($query);
      $pwd  = $result->Fields("password");

      // check username
      $query  = "select username from idx_users where username = '$username'";
      $result = $dbConn->Execute($query);
      $un  = $result->Fields("username");
      
      $auth = new auth();
      if ($pwd != $auth->Convert($password))
        $error_msg = $msg['10345'];
      elseif (empty($un))
        $error_msg = $msg['10346'];

      if (empty($error_msg)) {
        $query  = "insert into idx_link_user (link_id, username)
                   values ('$link_id','$username')";
        $result = $dbConn->Execute($query);

        DisplayTemplate($theme_path . "cp/claim_ok.html", "\$link_id,\$title");
      }
      else {
        ClaimForm();
      }
    }
    else {
      ClaimForm();
    }
  }


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

  include "../application.php";

  RunPreFilter(__FILE__);

  if (empty($pflag)) {
    SelectClaim();
  }
  elseif ($pflag == 'search') {
    SearchLinkID();
  }
  elseif ($pflag == 'form') {
    ClaimForm();
  }
  elseif ($pflag == 'claim') {
    ProcessClaim();
  }

  RunPostFilter(__FILE__)

?>
