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

  /*===================================================
    ShowFormSendMessage()
  ===================================================*/

  function ShowFormSendMessage() {

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

    // vars url & form
    global $id;

    // vars messages
    global $msg;

    // vars template

    $ml_obj = new clsMailingList;
    $ml_obj->table_name = "idx_mailing_list";
    $ml_obj->message_table_name = "idx_message";
    $ml_obj->template_file = $admin_template_path . "message_send_form.html";

    $html = $ml_obj->SendMessage("confirm_form",$id);

    print $html;
  }


  /*===================================================
    ProcessFormSendMessage()
  ===================================================*/

  function ProcessFormSendMessage() {

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

    // vars url & form
    global $subject, $body, $id, $send_to, $test_only, $select_field,
             $filter_operator, $filter_value;

    // vars messages
    global $msg;

    // vars template
    global $message;

    // verify input

    $ml_obj = new clsMailingList;
    $ml_obj->table_name = "idx_mailing_list";
    $ml_obj->message_table_name = "idx_message";
    $ml_obj->email_from = $email_address;

    $links_obj = new clsLink;
    $links_obj->query = "select * from idx_link where category_id = '$cat' order by bid desc";
    $links_obj->table_name = "idx_link";
    $links_obj->category_table_name = "idx_category";
    $links_obj->user_table_name = "idx_users";
    $links_obj->editor_table_name = "idx_editor";

    $users_obj = new clsUsers;
    $users_obj->table_name = "idx_users";

    if ($select_field != "N/A" && $filter_operator) {
      $query_filter = "and $select_field $filter_operator '$filter_value'";
    }

    // get array of email addr
    if ($send_to == 'mailing_list_subscriber') $emails = $ml_obj->GetAllSubscriber($query_filter);
    elseif($send_to == 'webmaster_all') $emails = $links_obj->GetWebmasterEmail('all', TRUE, $query_filter);
    elseif($send_to == 'webmaster_basic') $emails = $links_obj->GetWebmasterEmail('basic', TRUE, $query_filter);
    elseif($send_to == 'webmaster_premium') $emails = $links_obj->GetWebmasterEmail('premium', TRUE, $query_filter);
    elseif($send_to == 'webmaster_sponsored') $emails = $links_obj->GetWebmasterEmail('sponsored', TRUE, $query_filter);
    elseif($send_to == 'webmaster_suspended') $emails = $links_obj->GetWebmasterEmail('suspended', TRUE, $query_filter);
    elseif($send_to == 'registered_user') $emails = $users_obj->GetEmailAddress('', TRUE, $query_filter);
    elseif($send_to == 'editor') $emails = $links_obj->GetEditorEmail('all', TRUE, $query_filter);
    elseif($send_to == 'all') {
      $a1 = $ml_obj->GetAllSubscriber();
      $a2 = $links_obj->GetWebmasterEmail("all");
      $a3 = $links_obj->GetEditorEmail("all");
      $emails = array_unique(array_merge($a1, $a2, $a3));
    }

    $recipient_list = $ml_obj->SendMessage("process_form", $id, $emails, $send_to, $test_only);

    if ($recipient_list) {
      $message = $msg["20341"];
      $message .= "<br><br>" . $recipient_list;
    }
    else {
      $message = $msg["20342"];
    }

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


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

  include "../application.php";

  RunPreFilter(__FILE__);

  DisplayTemplate($admin_template_path . "header.html");
  if (empty($pflag)) {
    ShowFormSendMessage();
  }
  elseif ($pflag == 'send') {
    ProcessFormSendMessage();
  }
  DisplayTemplate($admin_template_path . "footer.html");

  RunPostFilter(__FILE__);

?>