<?/*
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 'athena.conf';

class Template extends Base_Object {

  function Template () {
    global $dbi;
    $this->db = &$dbi;
  }

  function Delete ($tid) {
    $h = $this->db->prepare ("
      delete from templates
            where template_id = ?
    ");
    $ok = $this->db->execute ($h, array($tid));
    if (DB::isError($ok)) return 0;
    return 1;
  }

  function Add ($in_ary) {
    foreach ($in_ary as $k => $v) {
      $add[strtoupper($k)] = $v;
    }

    if (!array_key_exists('TEMPLATE_ID', $add))
      $add['TEMPLATE_ID'] = $this->db->nextId ('templates_template_id');

    if (!array_key_exists('CRM_ID', $add))
      $add['CRM_ID'] = $this->Current_CRM ();
    if (!$add['CRM_ID']) return 0;

    $this->Set_Audit_Info ($add);

    $to_date = to_date();
    $h = $this->db->prepare ("
      insert into templates (
        template_id,
        template_text,
        template_name,
        crm_id,
        updated_by,
        created_by,
        updated_date,
        created_date
      ) values (
        ?, ?, ?, ?, ?, ?,
	$to_date,
	$to_date
      )
    ");
    $ok = $this->db->execute ($h, array(
      $add['TEMPLATE_ID'],
      $add['TEMPLATE_TEXT'],
      $add['TEMPLATE_NAME'],
      $add['CRM_ID'],
      $add['UPDATED_BY'],
      $add['CREATED_BY'],
      $add['UPDATED_DATE'],
      $add['CREATED_DATE']
    ));
    if (DB::isError($ok)) {
       $this->error = 'Template::Add(): ' .  $ok->getMessage()
	. "\n" . $ok->getUserInfo();
       return;
    }

    return $add['TEMPLATE_ID'];
  }

  function Get_Info ($tid='', $crm_id='') {
    $this->Set_If_Exists ('template_id', $tid);

    if (!$crm_id) $crm_id = $this->Current_CRM ();
    if (!$crm_id) return 0;

    $h = $this->db->prepare ('
      select template_text,
             template_name,
             template_id,
             crm_id
        from templates
       where template_id = ?
         and crm_id = ?
    ');
    $res = $this->db->execute ($h, array($this->template_id, $crm_id));
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return;
    }
    $r = $res->fetchRow (DB_FETCHMODE_ASSOC);

    foreach ($r as $k => $v) {
      $k = strtolower ($k);
      $this->$k = $v;
    }
  }

  function Update () {

    $this->Set_Audit_Info( $this );

    $to_date = to_date();
    $h = $this->db->prepare ("
      update templates
         set template_name = ?,
             template_text = ?,
             updated_by = ?,
             updated_date = $to_date
       where template_id = ?
         and crm_id = ?
    ");
    $res = $this->db->execute ($h, array(
      $this->template_name,
      $this->template_text,
      $this->updated_by,
      $this->updated_date,
      $this->template_id,
      $this->crm_id
    ));
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return;
    }
  }

  function &List_All ($crm_id='', $assoc=DB_FETCHMODE_ASSOC) {
    if (!$crm_id) $crm_id = $this->Current_CRM ();
    if (!$crm_id) return array();

    $sql = '
      select template_id,
             template_name
        from templates
       where crm_id = ?
    order by upper(template_name)
    ';

    return $this->db->getAll($sql, array($crm_id), $assoc);
  }

  function load ($file, $crm_id='') {
    if (!$file) return;
    if (!$crm_id) $crm_id = $this->Current_CRM ();
    if (!$crm_id) return;

    global $RUSER_REC;

    if ($crm_id == $RUSER_REC['CRM_ID']) {
      $dir = $RUSER_REC['CRM_SHORT_NAME'];
    } else {
      require_once 'crm.php';
      $crm = new CRM ();
      $r = $crm->Select_by_CRM_ID ($crm_id);
      $dir = $r->Get ('crm_short_name');
    }

    global $custom_templates_dir;
    $full_file = "$custom_templates_dir/$dir/$file";

    global $athena_root;
    if (!file_exists($full_file)) $full_file = "$athena_root/templates/$file";

    return $full_file;
  }
}
?>
