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

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

    $this->valid_default_columns = array (
      'default_problem_id',
      'default_source_id',
      'email_source_id',
      'default_support_person_id',
      'default_escalated_to_id',
      'email_domain',
      'users_url',
      'email_sig',
    );

    $this->error = "";
  }

  function CRM_ID_By_Host ($host, $type='web') {
    return $this->CRM_ID_By_Host_Path($host);
  }

  function CRM_ID_By_Host_Path ($host='', $path='') {
    if (!$host) $host = $_SERVER['SERVER_NAME'];
    if (!$path) $path = $_SERVER['PHP_SELF'];

    $crms = $this->db->getAll ("
      SELECT crm_id, web_path
        FROM company_crm
       WHERE web_domain = ?
    ", array($host), DB_FETCHMODE_ASSOC);

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

    if (!$crms) {
      $this->error = "CRM::CRM_ID_By_Host_Path(): " .
	"no CRMs found matching host: '$host'";
      return;
    }

    $crm_id = false;

    foreach ($crms as $crm) {
	if (path_under_in($crm['web_path'], $path)) {
	    $crm_id = $crm['crm_id'];
	    break;
	}
    }

    if (!$crm_id) {
      $this->error = "CRM::CRM_ID_By_Host_Path(): No CRM found for "
	. "host: '$host', path: '$path'";
      return;
    }

    return $crm_id;
  }

  function Remove_Access ($from_crm_id, $on_crm_id, $upd_by='') {
    if (!$upd_by) $upd_by = $this->Person_Logged_In ();

    $h = $this->db->prepare ("
      delete from crm_to_crm_access
            where owner_crm_id = ?
              and access_to_crm_id = ?
    ");
    $res = $this->db->execute($h, array($on_crm_id, $from_crm_id));
    if (DB::isError($res)) {
      $this->error = $res->getMessage();
      return 0;
    }

    return 1;
  }

  function Grant_Access ($to_crm_id, $on_crm_id, $upd_by='') {

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

    $to_date = to_date();
    $h = $this->db->prepare ("
      insert into crm_to_crm_access (
        owner_crm_id,
        access_to_crm_id,
        updated_by,
        created_by,
        created_date,
        updated_date
      ) values (
        ?, ?, ?, ?,
	$to_date,
	$to_date
      )
    ");
    $res = $this->db->execute($h, array($on_crm_id, $to_crm_id, 
      $add['UPDATED_BY'], $add['CREATED_BY'], $add['CREATED_DATE'], 
      $add['UPDATED_DATE']));
    if (DB::isError($res)) {
      $this->error = $res->getMessage();
      return 0;
    }

    return 1;
  }

  function Access_To ($crm_id, $assoc=DB_FETCHMODE_DEFAULT) {
    $sql = "
   select cca.owner_crm_id,
          cc1.crm_name as owner_crm_name,
          cca.access_to_crm_id,
          cc2.crm_name as access_to_crm_name
     from crm_to_crm_access cca,
          company_crm cc1,
          company_crm cc2
    where cca.owner_crm_id = cc1.crm_id
      and cca.access_to_crm_id = cc2.crm_id
      and cca.owner_crm_id = ?
 order by upper(cc2.crm_name)
    ";

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

  function Select_by_CRM_ID ($crm_id) {
    $r = new CRM_Record ();

    if (!$crm_id) {
	$this->error = "CRM::Select_by_CRM_ID(): no ID!";
	return;
    }

    if (!$r->Select_by_CRM_ID ($crm_id)) {
	$this->error = "CRM::Select_by_CRM_ID(): " . $r->error;
	return;
    }
    return $r;
  }

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

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

    $this->Set_Audit_Info ($add);

    $to_date = to_date();
    $h = $this->db->prepare ("
      insert into company_crm (
        crm_id,
        crm_name,
	crm_short_name,
        company_id,
        updated_by,
        default_problem_id,
        email_source_id,
        default_source_id,
        default_support_person_id,
        default_escalated_to_id,
        email_domain,
        web_domain,
	web_path,
        users_url,
        email_sig,
        extended_email_sig,
        confirmation_url,
        web_page_id,
        created_by,
        created_date,
        updated_date
      ) values (
        ?, ?, ?, ?, ?, ?,
        ?, ?, ?, ?, ?, ?,
        ?, ?, ?, ?, ?, ?, ?,
	$to_date,
	$to_date
      )
    ");
    $ok = $this->db->execute($h, array(
      $add['CRM_ID'],
      $add['CRM_NAME'],
      $add['CRM_SHORT_NAME'],
      $add['COMPANY_ID'],
      $add['UPDATED_BY'],
      $add['DEFAULT_PROBLEM_ID'],
      $add['EMAIL_SOURCE_ID'],
      $add['DEFAULT_SOURCE_ID'],
      $add['DEFAULT_SUPPORT_PERSON_ID'],
      $add['DEFAULT_ESCALATED_TO_ID'],
      $add['EMAIL_DOMAIN'],
      $add['WEB_DOMAIN'],
      $add['WEB_PATH'],
      $add['USERS_URL'],
      $add['EMAIL_SIG'],
      $add['EXTENDED_EMAIL_SIG'],
      $add['CONFIRMATION_URL'],
      $add['WEB_PAGE_ID'],
      $add['CREATED_BY'],
      $add['CREATED_DATE'],
      $add['UPDATED_DATE']
    ));
    if (DB::isError($ok)) {
       $this->error = 'CRM::Add(): ' . $ok->getMessage() . $ok->getUserInfo();
       return;
    }

    return $add['CRM_ID'];
  }

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

    $to_date = to_date();
    $h = $this->db->prepare ("
      update company_crm
         set crm_name = ?,
	     crm_short_name = ?,
             updated_by = ?,
             updated_date = $to_date,
             default_problem_id = ?,
             default_source_id = ?,
             email_source_id = ?,
             default_support_person_id = ?,
             default_escalated_to_id = ?,
             email_domain = ?,
             web_domain = ?,
	     web_path = ?,
             users_url = ?,
             email_sig = ?,
             backup_email = ?,
             extended_email_sig = ?,
             new_ticket_notify = ?,
             web_page_id = ?,
             confirmation_url = ?
       where crm_id = ?
    ");
    $res = $this->db->execute($h, array(
      $this->crm_name,
      $this->crm_short_name,
      $this->updated_by,
      $this->updated_date,
      $this->default_problem_id,
      $this->default_source_id,
      $this->email_source_id,
      $this->default_support_person_id,
      $this->default_escalated_to_id,
      $this->email_domain,
      $this->web_domain,
      $this->web_path,
      $this->users_url,
      $this->email_sig,
      $this->backup_email,
      $this->extended_email_sig,
      $this->new_ticket_notify,
      $this->web_page_id,
      $this->confirmation_url,
      $this->crm_id
    ));
    if (DB::isError($res)) {
       $this->error = 'CRM::Update(): ' . $res->getMessage() . 
	$res->getUserInfo();
       return;
    }

    return(1);
  }

  function Delete ($crm_id) {
    $h = $this->db->prepare ('
      delete from company_crm
       where crm_id = ?
    ');
    $res = $this->db->execute($h, array($crm_id));
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return 0;
    }
    return 1;
  }

  function List_All ($assoc=DB_FETCHMODE_ASSOC) {
    $sql = '
      select crm_id,
             crm_name,
             company_id
        from company_crm
    order by company_id,
             upper(crm_name)
    ';
    return $this->db->getAll($sql, $assoc);
  }
}

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

  function Select_by_CRM_ID ($crm_id) {
    $order = array(
	       'crm_id',
	       'company_id',
	       'crm_name',
	       'crm_short_name',
	       'default_problem_id',
	       'default_source_id',
	       'email_source_id',
	       'default_support_person_id',
	       'default_escalated_to_id',
	       'email_domain',
	       'web_domain',
	       'web_path',
	       'users_url',
	       'email_sig',
	       'extended_email_sig',
	       'backup_email',
	       'new_ticket_notify',
	       'web_page_id',
	       'confirmation_url',
	     );

  $company_crm = new Company_CRM();
  $cc_rows = $company_crm->GetData($crm_id);

  if (is_null($cc_rows)) {
    $this->error = "CRM_Record::Select_by_CRM_ID(): " . $company_crm->error;
    return;
  }

  if (empty($cc_rows)) return false;

  $r = $cc_rows[0];
  if (!$r) return false;
  foreach ($order as $field) {
    $this->$field = $r[$field];
  }

  return true;
  }
}

?>
