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

class Config extends Base_Object {

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

  ## old config info from attributes on the company_crm table
  function Get ($var, $crm_id='') {

    $col = ereg_replace (' ', '_', strtolower ($var));

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

    if (!$crm_id or !$col) return;

    $company_crm = new Company_CRM();

    $cc_rows = $company_crm->GetData($crm_id);
    if (empty($cc_rows)) {
	return '';
    } else {
	return $cc_rows[0][$col];
    }
  }

  function Value ($var, $crm_id='') {
    if (!$crm_id) $crm_id = $this->Current_CRM ();
    if (!$crm_id or !$var) return;

    $h = $this->db->prepare ("
      select cc.config_value
        from crm_config cc,
             config_keys ck
       where cc.crm_id = ?
         and ck.config_name = ?
         and ck.config_id = cc.config_id
    ");
    $res = $this->db->execute($h, array($crm_id, $var));
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
       return;
    }
    $r = $res->fetchRow();
    return $r[0];
  }

  function &Valid_Email_Domains () {

    $sql = "
      select email_domain,
             crm_id,
	     crm_short_name
        from company_crm
       where email_domain is not null
         and not (email_domain = '')
    ";

    if (DB::isError($ret = $this->db->getAll($sql))) {
	$this->error = 'Config::Valid_Email_Domains(): ' . $ret->getMessage()
		. "\n" . $ret->getUserInfo();	
	return;
    }

    return $ret;
  }

  function CRM_URL($crm_id = '') {
      $url = 'http://'
	   . $this->Get('web_domain', $crm_id) 
	   . $this->Get('web_path', $crm_id);
      if (!preg_match('/\/$/', $url)) $url .= '/';
      return $url;
  }

  function Users_URL($enc, $crm_id = '') {
    return $this->CRM_URL($crm_id)
	 . preg_replace('#^/|/$#', '', $this->Get('users_url', $crm_id))
	 . "/$enc";
  }
}
?>
