<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

AthenaRMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/?>
<?php

require_once 'base_object.php';
require_once 'ticket.php';
require_once 'mail.php';
require_once 'comment.php';

class Email_Blast extends Base_Object {
  function Email_Blast () {
    $this->ticket_p = new Ticket;
    $this->mail = new Mail;

    global $dbi;
    $this->db = &$dbi;
  }
  
  function Records_to_Blast ($crm_id='') {
    if (!$crm_id) $crm_id = $this->Current_CRM ();
    if (!$crm_id) return 0;

    $h = $this->db->prepare ('
      select count(*)
        from ticket t
               inner join ticket_crm_map tcm
                 on (t.ticket_id = tcm.ticket_id)
       where tcm.crm_id = ?
         and t.mass_queue = ?
    ');
    $res = $this->db->execute( $h, array($crm_id,'Y') );
    if (DB::isError($res)) {
       $this->error = "records to blast: ".$res->getMessage();
       return;
    }
    $r = $res->fetchRow ();

    return $r[0];
  } 

  function Tickets ($crm_id='') {
    if (!$crm_id) $crm_id = $this->Current_CRM ();
    if (!$crm_id) return 0;

    $sql = "
      SELECT t.ticket_id
        FROM ticket t INNER JOIN 
	     ticket_crm_map tcm ON t.ticket_id = tcm.ticket_id
       WHERE tcm.crm_id = ?
         and t.crm_id = ?
         AND t.mass_queue = ?
    ";

    $res = $this->db->getCol( $sql, 0, array($crm_id,$crm_id,'Y') );

    if (DB::isError($res)) {
       $this->error = $res->getMessage() . "\n" . $res->getUserInfo();
       return;
    }

    if (count($res)) {
      return $res;
    } else {
      return array();
    }
  }

  function &Send_Emails ($subject, $original_message, $status='') {
    $crm_id = $this->Current_CRM ();
    if (!$crm_id) return array ('no crm_id found');

    $errors = array ();

    if (!$status) $status = 'Closed';

    $comment = new Comment ();

    $updater = $this->Person_Logged_In ();

    $h = $this->db->prepare ('
      select t.ticket_id,
             p.first_name,
             p.last_name,
             p.email
        from ticket t
               inner join ticket_crm_map tcm
                 on (t.ticket_id = tcm.ticket_id)
               inner join person p
                 on (t.user_person_id = p.person_id)
       where tcm.crm_id = ?
         and t.mass_queue = ?
    ');
    $res = $this->db->execute ( $h, array($crm_id,'Y') );
    if (DB::isError($res)) {
       $this->error = getMessage();
       return;
    }

    while (list ($tid, $fn, $ln, $email) = $res->fetchRow ()) {

      $ticket = $this->ticket_p->Info_by_Ticket_ID ($tid);
      $ticket->Set ('status', $status);
      $ticket->Set ('mass_queue', 'N');

      if ($fn or $ln) {
        $name = "$fn $ln";
      } else {
        $name = 'User';
      }
      
      $message = "Dear $name,\n\n$original_message";

      if ($e = $this->mail->send (array (
        'to' => $email,
        'subject' => $subject,
        'message' => $message, 
        'ticket_id' => $tid,
      ))) {
        $errors[] = "Missing $e on ticket $tid";

      } else {
        $comment_id = $comment->Add (array (
          'comments' => "Subject: $subject\n$message",
          'person_id' => $updater,
          'ticket_id' => $tid,
          'updated_by' => "email blast - $updater"
        ));

        $ticket->Update ();
      }
    }

    return $errors;
  }
}
?>
