<?/*
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 'html.php';

class Source extends HTML {
  function Source () {
    global $dbi;
    $this->db = &$dbi;
  }

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

    if (!$add['SOURCE']) return 0;

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

    if (!$add['CRM_ID']) return 0;

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

    $this->Set_Audit_Info ($add);

    $to_date = to_date();
    $sql = "
      insert into source (
        source,
        crm_id,
        updated_by,
        source_id,
        created_by,
        created_date,
        updated_date
      ) values (
        ?, ?, ?, ?, ?,
	$to_date,
	$to_date
      )
    ";

    $h = $this->db->prepare( $sql );

    $ok = $this->db->execute ($h, array(
      $add['SOURCE'],
      $add['CRM_ID'],
      $add['UPDATED_BY'],
      $add['SOURCE_ID'],
      $add['CREATED_BY'],
      $add['CREATED_DATE'],
      $add['UPDATED_DATE']
    ));
    if (DB::isError($ok)) {
       $this->error = 'Source::Add(): ' . $ok->getMessage()
	. $ok->getUserInfo();
       return;
    }

    return $add['SOURCE_ID'];
  }

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

    $r = $this->db->getOne('
      SELECT COUNT(*) 
	FROM ticket_crm_map 
       WHERE source_id = ? AND
	     crm_id = ?
    ', array($source, $crm_id));

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

    if ($r) return false;

    $h = $this->db->prepare ('
      delete from source
       where source_id = ?
         and crm_id = ?
    ');
    $ok = $this->db->execute ($h, array($source, $crm_id));

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

    return true;
  }


  function is_Valid ($srcid, $crm_id='') {
    if (!$crm_id) $crm_id = $this->Current_CRM ();
    if (!$crm_id) return 0;
  
    $h = $this->db->prepare ("
      select count(*) 
        from source
       where source_id = ?
         and crm_id = ?
    ");
    $res = $this->db->execute( $h, array($srcid,$crm_id) );
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return;
    }
    list ($c) = $res->fetchRow ();

    return $c;
  }

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

    $sql = '
      select source_id,
             source
        from source
       where crm_id = ?
    order by upper(source)
    ';

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

  function Select_by_Source ($ptype, $crm_id='') {
    $problem_type_r = new Source_Record ();
    $problem_type_r->Select_by_Source ($ptype, $crm_id);
    return $problem_type_r;
  }

  function Move ($from_ptype, $to_ptype, $crm_id='') {
    if (!$crm_id) $crm_id = $this->Current_CRM ();
    if (!$crm_id) return 0;

    $add = array();
    $this->Set_Audit_Info( $add );

    $to_date = to_date();
    $upex = $this->db->prepare ("
      update ticket_crm_map
         set source_id = ?,
             updated_by = ?,
             updated_date = $to_date
       where source_id = ?
         and crm_id = ?
    ");
    $ok = $this->db->execute ($upex, array($to_ptype, 
      $add['UPDATED_BY'], $add['UPDATED_DATE'], $from_ptype, $crm_id));

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

    return true;
  }

  function Update () {
    $this->Set_Audit_Info( $this );

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

    return 1;
  }
}

class Source_Record extends Source {
  function Problem_Type_Record () {
    global $dbi;
    $this->db = &$dbi;
  }

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

    $h = $this->db->prepare ('
      select source,
             in_drop_downs,
             crm_id,
             source_id
        from source
       where source_id = ?
    ');
    $res = $this->db->execute( $h, array($ptype,$crm_id) );
    $r = $res->fetchRow (DB_FETCHMODE_ASSOC);

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