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

  /*===================================================
    ShowFormEnhanceLink()
  ===================================================*/

  function ShowFormEnhanceLink() {

    // vars global configuration
    global $dbConn, $admin_template_path, $paid_link_day_period;

    // vars url & form

    // vars messages
    global $msg, $id, $title, $listing_type, $expire, $type, $listing_period;


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

    // get current listing type

    $query     = "select * from idx_paid_listing where link_id = '$id'";
    $result    = $dbConn->Execute($query);
    $premium   = $result->Fields("premium");
    $sponsored = $result->Fields("sponsored");
    $expire    = $result->Fields("expire");
    $expire    = date("D, j M Y", @strtotime($expire));

    if(empty($expire) || @strtotime($expire)<time()) {
      $listing_type = "
            <select name=type>
              <option selected value=basic>Basic Listing</option>
              <option value=premium>Premium Listing</option>
              <option value=sponsored>Sponsored Listing</option>
            </select>";

      $expire = "n/a";
    }

    if ($premium) {
      $listing_type = "
            <select name=type>
              <option value=basic>Basic Listing</option>
              <option selected value=premium>Premium Listing</option>
              <option value=sponsored>Sponsored Listing</option>
            </select>";
    }

    if ($sponsored) {
      $listing_type = "
            <select name=type>
              <option value=basic>Basic Listing</option>
              <option value=premium>Premium Listing</option>
              <option selected value=sponsored>Sponsored Listing</option>
            </select>";
    }

    // period
    $period         = @explode(',', $paid_link_day_period);
    $listing_period = "<select name=period>";
    while (list($k, $v) = @each($period)) {
      $listing_period .= "<option value=$v>$v days</option>";
    }
    $listing_period .= "</select>";

    DisplayTemplate($admin_template_path . "link_premium_sponsored_form.html",
      "\$id,\$listing_type,\$expire,\$title,\$listing_period");
  }


  /*===================================================
    ProcessFormEnhanceLink()
  ===================================================*/

  function ProcessFormEnhanceLink() {

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

    // vars url & form
    global $id, $type, $extend_time, $period;

    // vars messages
    global $msg, $message;

    if ($type=='basic') {
      $query  = "delete from idx_paid_listing where link_id = '$id'";
      $result = $dbConn->Execute($query);
    }
    else {

      if ($type == 'premium') {
        $premium   = 1;
        $sponsored = 0;
      }
      else {
        $premium   = 0;
        $sponsored = 1;
      }

      $days_extend = 3600 * 24 * $period;

      // check if exist

      $query  = "select link_id, premium, sponsored, expire
                 from idx_paid_listing where link_id = '$id'";

      $result = $dbConn->Execute($query);

      if ($result->RecordCount() == 0) {
        // create new record
        $expire = date('Y-m-d H:i:s', strtotime("+$days_extend seconds"));
        $query  = "insert into idx_paid_listing
                   (link_id, premium, sponsored, expire, paid)
                   values ('$id', '$premium', '$sponsored', '$expire', 1)";
        $result = $dbConn->Execute($query);
      }
      else {
        // get expire time
        $expire         = $result->Fields("expire");
        $curr_premium   = $result->Fields("premium");
        $curr_sponsored = $result->Fields("sponsored");

        if ($curr_premium != $premium || $curr_sponsored != $sponsored) {
          $expire = date('Y-m-d H:i:s');
        }

        if (time() < @strtotime($expire) && $extend_time) {
          $expire = date('Y-m-d H:i:s', strtotime("$expire +$days_extend seconds"));
        }
        else {
          $expire = date('Y-m-d H:i:s', strtotime("+$days_extend seconds"));
        }

        // update existing record
        $query  = "update idx_paid_listing
                   set expire = '$expire', premium = $premium, sponsored = $sponsored
                   where link_id = '$id'";
        $result = $dbConn->Execute($query);
      }
    }

    $message = $msg['20511'];

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


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

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

  DisplayTemplate($admin_template_path . "header.html");
  if (empty($pflag)) {
    ShowFormEnhanceLink();
  }
  elseif ($pflag == 'enhance') {
    ProcessFormEnhanceLink();
  }
  DisplayTemplate($admin_template_path . "footer.html");

  RunPostFilter(__FILE__);

?>