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

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

  function List_All ($pid, $extended=0) {
    $binds = array($pid);

    if ($extended) {
      $sql = "
      select cc.crm_id,
             cc.crm_name,
             cc.web_domain,
             cc.web_path,
             pcta.pcrm_map_id as is_admin,
             pcm.pcrm_map_id,
             cc.email_domain
        from person_crm_map pcm
               inner join company_crm cc ON (pcm.crm_id = cc.crm_id)
               left outer join person_crm_type_assign pcta
               on (pcm.pcrm_map_id = pcta.pcrm_map_id and
                   pcta.person_type = 'Support Admin')
       where pcm.person_id = ?
    group by cc.crm_id,
             cc.crm_name,
             cc.web_domain,
             cc.web_path,
             pcm.pcrm_map_id,
             cc.email_domain,
             pcta.pcrm_map_id
    order by upper(cc.crm_name)
      ";
    } else {
      $sql = "
      select
    distinct cc.crm_id,
             cc.crm_name,
             cc.web_domain,
	     cc.web_path
        from person_crm_map pcm
               inner join company_crm cc
                 on (pcrm.crm_id = cc.crm_id)
               left outer join person_crm_type_assign pt
                 on pcm.pcrm_map_id = pt.pcrm_map_id
       where pcm.person_id = ?
    order by cc.crm_name
";
    }

    $list = $this->db->getAll($sql, $binds, DB_FETCHMODE_ASSOC);

    if (DB::isError($list)) {
        $this->error = 'CRM_View::List_All(): ' . 
	    $list->getMessage() . $list->getUserInfo();
	return;
    }

    $p = new Person();
    if ($p->Is_Super_Admin($pid)) {
      for ($i = 0; $i < count($list); $i++) $list[$i]['is_admin'] = true;
    }

    return $list;
  }

  function Valid_CRM_ID ($crm_id, $person_id) {
    if (!$crm_id or !$person_id) return 0;

    $sql = '
    select pcm.crm_id
      from person_crm_map pcm
             left join person_crm_type_assign pt
               on (pcm.pcrm_map_id = pt.pcrm_map_id)
     where pcm.crm_id = ?
       and pcm.person_id = ?
    ';

    $h = $this->db->prepare ($sql);
    $res = $this->db->execute( $h, array($crm_id,$person_id) );
    if (DB::isError($res)) {
       $this->error = $res->getMessage();
      return 0;
    }
    
    $r = $res->fetchRow ();

    if ($r[0] && ($r[0] == $crm_id)) return 1;

    return 0;
  }
}
?>
