<?php
/*********************************************************************************
 * The contents of this file are subject to the SugarCRM Public License Version
 * 1.1.3 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by SugarCRM" logo and
 *    (ii) the SugarCRM copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: SugarCRM Open Source
 * The Initial Developer of the Original Code is SugarCRM, Inc.
 * Portions created by SugarCRM are Copyright (C) 2004 SugarCRM, Inc.;
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 ********************************************************************************/
/*********************************************************************************
 * $Id: Contact.php,v 1.133.2.2 2005/03/07 20:49:52 joey Exp $
 * Description:  TODO: To be written.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 ********************************************************************************/
include_once('config.php');
require_once('include/logging.php');
require_once('include/database/PearDatabase.php');
require_once('data/SugarBean.php');
require_once('include/utils.php');
require_once('modules/Opportunities/Opportunity.php');
require_once('modules/Cases/Case.php');
require_once('modules/Tasks/Task.php');
require_once('modules/Notes/Note.php');
require_once('modules/Leads/Lead.php');
require_once('modules/Meetings/Meeting.php');
require_once('modules/Calls/Call.php');
require_once('modules/Emails/Email.php');
require_once('modules/Bugs/Bug.php');
require_once('modules/Users/User.php');


// Contact is used to store customer information.
class Contact extends SugarBean {
    var $field_name_map;
	// Stored fields
	var $id;
	var $name = '';
	var $lead_source;
	var $date_entered;
	var $date_modified;
	var $modified_user_id;
	var $assigned_user_id;
	var $created_by;
	var $created_by_name;
	var $modified_by_name;



	var $description;
	var $salutation;
	var $first_name;
	var $last_name;
	var $title;
	var $department;
	var $birthdate;
	var $reports_to_id;
	var $do_not_call;
	var $phone_home;
	var $phone_mobile;
	var $phone_work;
	var $phone_other;
	var $phone_fax;
	var $email1;
	var $email_and_name1;
	var $email_and_name2;
	var $email2;
	var $assistant;
	var $assistant_phone;
	var $email_opt_out;
	var $primary_address_street;
	var $primary_address_city;
	var $primary_address_state;
	var $primary_address_postalcode;
	var $primary_address_country;
	var $alt_address_street;
	var $alt_address_city;
	var $alt_address_state;
	var $alt_address_postalcode;
	var $alt_address_country;
	var $portal_name;
	var $portal_app;
	var $portal_active;

	// These are for related fields
	var $bug_id;
	var $account_name;
	var $account_id;
	var $reports_to_name;
	var $opportunity_role;
	var $opportunity_rel_id;
	var $opportunity_id;
	var $case_role;
	var $case_rel_id;
	var $case_id;
	var $task_id;
	var $note_id;
	var $meeting_id;
	var $call_id;
	var $email_id;
	var $assigned_user_name;







	var $table_name = "contacts";
	var $rel_account_table = "accounts_contacts";
	//This is needed for upgrade.  This table definition moved to Opportunity module.
	var $rel_opportunity_table = "opportunities_contacts";




	var $object_name = "Contact";

	var $new_schema = true;

	var $column_fields = Array("id"
		,"date_entered"
		,"date_modified"
		,"modified_user_id"
		,"assigned_user_id"
		, "created_by"



		,"salutation"
		,"first_name"
		,"last_name"
		,"lead_source"
		,"title"
		,"department"
		,"birthdate"
		,"reports_to_id"
		,"do_not_call"
		,"phone_home"
		,"phone_mobile"
		, "portal_name"
		, "portal_app"
		,"portal_active"
		,"phone_work"
		,"phone_other"
		,"phone_fax"
		,"email1"
		,"email2"
		,"assistant"
		,"assistant_phone"
		,"email_opt_out"
		,"primary_address_street"
		,"primary_address_city"
		,"primary_address_state"
		,"primary_address_postalcode"
		,"primary_address_country"
		,"alt_address_street"
		,"alt_address_city"
		,"alt_address_state"
		,"alt_address_postalcode"
		,"alt_address_country"
		,"description"
		);

/*
	DO NOT DEPEND ON THIS THIS DATA STRUCTURE
	IT IS ONLY TEMPORARY AND MAY NOT BE SUPPORTED IN FUTURE RELEASES
*/
		var $field_defs = array(
        array('name'=>'lead_source','vname'=>'LBL_LEAD_SOURCE','type'=>'enum',
				'options'=>'lead_source_dom'),




        array('name'=>'date_entered','vname'=>'LBL_DATE_ENTERED','type'=>'date'),
        array('name'=>'date_modified','vname'=>'LBL_DATE_MODIFIED','type'=>'date'),
        array('name'=>'description','vname'=>'LBL_DESCRIPTION','type'=>'varchar','len'=>'255'),
        array('name'=>'portal_name','vname'=>'LBL_PORTAL_NAME','type'=>'varchar'),
         array('name'=>'portal_app','vname'=>'LBL_PORTAL_APP','type'=>'varchar'),
        array('name'=>'portal_active','vname'=>'LBL_PORTAL_ACTIVE','type'=>'bool'),

        array('name'=>'salutation','vname'=>'LBL_SALUTATION','type'=>'enum',
				'options'=>'salutation_dom', 'massupdate'=>false),
        array('name'=>'full_name','rname'=>'full_name','vname'=>'LBL_NAME','type'=>'varchar','len'=>'255'),
        array('name'=>'first_name','vname'=>'LBL_FIRST_NAME','type'=>'varchar','len'=>'255'),
        array('name'=>'last_name','vname'=>'LBL_LAST_NAME','type'=>'varchar','len'=>'255'),

        array('name'=>'title','vname'=>'LBL_TITLE','type'=>'varchar','len'=>'255'),
        array('name'=>'department','vname'=>'LBL_DEPARTMENT','type'=>'varchar','len'=>'255'),
        array('name'=>'birthdate','vname'=>'LBL_BIRTHDATE','massupdate'=>false, 'type'=>'date'),
        array('name'=>'do_not_call','vname'=>'LBL_DO_NOT_CALL','type'=>'bool'),
        array('name'=>'phone_home','vname'=>'LBL_HOME_PHONE','type'=>'phone'),
        array('name'=>'phone_mobile','vname'=>'LBL_MOBILE_PHONE','type'=>'phone'),
        array('name'=>'phone_work','vname'=>'LBL_OFFICE_PHONE','type'=>'phone'),
        array('name'=>'phone_other','vname'=>'LBL_OTHER_PHONE','type'=>'phone'),
        array('name'=>'phone_fax','vname'=>'LBL_FAX_PHONE','type'=>'phone'),
        array('name'=>'email1','vname'=>'LBL_EMAIL_ADDRESS','type'=>'email'),
        array('name'=>'email2','vname'=>'LBL_OTHER_EMAIL_ADDRESS','type'=>'email'),
        array('name'=>'assistant','vname'=>'LBL_ASSISTANT','type'=>'varchar','len'=>'255'),
        array('name'=>'assistant_phone','vname'=>'LBL_ASSISTANT_PHONE','type'=>'phone'),
        array('name'=>'email_opt_out','vname'=>'LBL_EMAIL_OPT_OUT','type'=>'bool'),
        array('name'=>'primary_address_street','vname'=>'LBL_PRIMARY_ADDRESS_STREET','type'=>'varchar','len'=>'255'),
        array('name'=>'primary_address_city','vname'=>'LBL_PRIMARY_ADDRESS_CITY','type'=>'varchar','len'=>'255'),
        array('name'=>'primary_address_state','vname'=>'LBL_PRIMARY_ADDRESS_STATE','type'=>'varchar','len'=>'255'),
        array('name'=>'primary_address_postalcode','vname'=>'LBL_PRIMARY_ADDRESS_POSTALCODE','type'=>'varchar','len'=>'255'),
        array('name'=>'primary_address_country','vname'=>'LBL_PRIMARY_ADDRESS_COUNTRY','type'=>'varchar','len'=>'255'),
        array('name'=>'alt_address_street','vname'=>'LBL_ALT_ADDRESS_STREET','type'=>'varchar','len'=>'255'),
        array('name'=>'alt_address_city','vname'=>'LBL_ALT_ADDRESS_CITY','type'=>'varchar','len'=>'255'),
        array('name'=>'alt_address_state','vname'=>'LBL_ALT_ADDRESS_STATE','type'=>'varchar','len'=>'255'),
        array('name'=>'alt_address_postalcode','vname'=>'LBL_ALT_ADDRESS_POSTALCODE','type'=>'varchar','len'=>'255'),
		array('name'=>'alt_address_country','vname'=>'LBL_ALT_ADDRESS_COUNTRY','type'=>'varchar','len'=>'255'),
		array('name'=>'account_name','rname'=>'name','id_name'=>'account_id','vname'=>'LBL_ACCOUNT_NAME','type'=>'relate','table'=>'accounts','isnull'=>'true','module'=>'Accounts'),
        array('name'=>'assigned_user_id','rname'=>'user_name','id_name'=>'assigned_user_id','vname'=>'LBL_ASSIGNED_TO','type'=>'assigned_user_name','table'=>'users','isnull'=>'false'),
/*
		array('name'=>'created_by','rname'=>'user_name',
			'id_name'=>'created_by','vname'=>'LBL_CREATED',
			'type'=>'created_by','table'=>'users',
			'isnull'=>'false'),
		array('name'=>'modified_user_id','rname'=>'user_name',
			'id_name'=>'modified_user_id','vname'=>'LBL_MODIFIED',
			'type'=>'modified_by_name','table'=>'users',
			'isnull'=>'false'),
*/
);

	// This is used to retrieve related fields from form posts.
	var $additional_column_fields = Array('bug_id', 'assigned_user_name', 'account_name', 'account_id', 'opportunity_id', 'case_id', 'task_id', 'note_id', 'meeting_id', 'call_id', 'email_id'



	);

	// This is the list of fields that are in the lists.
	var $list_fields = Array('id', 'first_name', 'last_name', 'account_name', 'account_id', 'title', 'email1','email2', 'phone_work', 'assigned_user_name', 'assigned_user_id', "case_role", 'case_rel_id', 'opportunity_role', 'opportunity_rel_id','email_and_name1','email_and_name2'






		);

	// This is the list of fields that are required
	var $required_fields =  array("last_name"=>1);

	function Contact() {
		global $current_user;
		$this->log = LoggerManager::getLogger('contact');
		$this->db = new PearDatabase();
                $custom = new CustomFields();
                $custom->setFieldDefs($this);
		foreach ($this->field_defs as $field)
		{
			$this->field_name_map[$field['name']] = $field;
		}









	}

	function create_tables () {
		$query = 'CREATE TABLE '.$this->table_name.' ( ';
		$query .='id char(36) NOT NULL';
		$query .=', deleted bool NOT NULL default 0';
		$query .=', date_entered datetime NOT NULL';
		$query .=', date_modified datetime NOT NULL';
		$query .=', modified_user_id char(36) NOT NULL';
		$query .=', assigned_user_id char(36)';
		$query .=', created_by char(36)';



		$query .=', salutation char(5)';
		$query .=', first_name char(25)';
		$query .=', last_name char(25)';
		$query .=', lead_source char(100)';
		$query .=', title char(25)';
		$query .=', department char(100)';
		$query .=', reports_to_id char(36)';
		$query .=', birthdate date';
		$query .=', do_not_call char(3) default 0';
		$query .=', phone_home char(25)';
		$query .=', phone_mobile char(25)';
		$query .=', phone_work char(25)';
		$query .=', phone_other char(25)';
		$query .=', phone_fax char(25)';
		$query .=', email1 char(100)';
		$query .=', email2 char(100)';
		$query .=', assistant char(75)';
		$query .=', assistant_phone char(25)';
		$query .=', email_opt_out char(3) default 0';
		$query .=', primary_address_street char(150)';
		$query .=', primary_address_city char(100)';
		$query .=', primary_address_state char(100)';
		$query .=', primary_address_postalcode char(20)';
		$query .=', primary_address_country char(100)';
		$query .=', alt_address_street char(150)';
		$query .=', alt_address_city char(100)';
		$query .=', alt_address_state char(100)';
		$query .=', alt_address_postalcode char(20)';
		$query .=', alt_address_country char(100)';
		$query .=', description text';
		$query .=', portal_name varchar(255)';
		$query .=', portal_active bool NOT NULL DEFAULT 0';
		$query .=', portal_app varchar(255)';
		$query .=', PRIMARY KEY ( ID ) )';



		$this->db->query($query,true,"Error creating table: ");

		//TODO Clint 4/27 - add exception handling logic here if the table can't be created.

		$query = "CREATE TABLE $this->rel_account_table (";
		$query .='id char(36) NOT NULL';
		$query .=', contact_id char(36)';
		$query .=', account_id char(36)';
		$query .=', deleted bool NOT NULL default 0';
		$query .=', PRIMARY KEY ( ID ) )';


		$this->db->query($query,true,"Error creating account/contact relationship table: ");


		// Create the indexes
		$this->create_index("create index idx_cont_last_first on contacts (last_name, first_name, deleted)");
		$this->create_index("create index idx_acc_cont_acc on accounts_contacts (account_id)");
		$this->create_index("create index idx_acc_cont_cont on accounts_contacts (contact_id)");
		$this->create_index("create index idx_contacts_del_last on contacts ( deleted , last_name ) ");
		$this->create_index("create index idx_cont_del_reports on contacts ( deleted , reports_to_id , last_name )") ;





	}

	function drop_tables () {
		$query = 'DROP TABLE IF EXISTS '.$this->table_name;



		$this->db->query($query);

		$query = 'DROP TABLE IF EXISTS '.$this->rel_account_table;



		$this->db->query($query);

		$query = 'DROP TABLE IF EXISTS '.$this->rel_opportunity_table;



		$this->db->query($query);


		//TODO Clint 4/27 - add exception handling logic here if the table can't be dropped.

	}

	function get_summary_text()
	{
		return "$this->first_name $this->last_name";
	}

	/** Returns a list of the associated contacts who are direct reports
	 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
	 * All Rights Reserved..
	 * Contributor(s): ______________________________________..
	*/
	function get_direct_reports()
	{
		// First, get the list of IDs.
		$query = "SELECT c1.id from contacts as c1, contacts as c2 where c2.id=c1.reports_to_id AND c2.id='$this->id' AND c1.deleted=0 order by c1.last_name";

		return $this->build_related_list($query, new Contact());
	}

	/** Returns a list of the associated opportunities
	 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
	 * All Rights Reserved..
	 * Contributor(s): ______________________________________..
	*/
	function get_opportunities()
	{
		// First, get the list of IDs.
		$query = "SELECT opportunity_id as id from opportunities_contacts where contact_id='$this->id' AND deleted=0";

		return $this->build_related_list($query, new Opportunity());
	}

	function get_bugs()
	{
		// First, get the list of IDs.
		$query = "SELECT bug_id as id from contacts_bugs where contact_id='$this->id' AND deleted=0";

		return $this->build_related_list($query, new Bug());
	}



	/** Returns a list of the associated opportunities
	 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
	 * All Rights Reserved..
	 * Contributor(s): ______________________________________..
	*/
	function get_leads()
	{
		// First, get the list of IDs.
		$query = "SELECT id  from leads where contact_id='$this->id' AND deleted=0";

		return $this->build_related_list($query, new Lead());
	}

		/** Returns a list of the associated opportunities
	 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
	 * All Rights Reserved..
	 * Contributor(s): ______________________________________..
	*/
	function get_accounts()
	{
		// First, get the list of IDs.
		$query = "SELECT account_id as id from accounts_contacts where contact_id='$this->id' AND deleted=0";

		return $this->build_related_list($query, new Account());
	}


































	/** Returns a list of the associated cases
	 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
	 * All Rights Reserved..
	 * Contributor(s): ______________________________________..
	*/
	function get_cases()
	{
		// First, get the list of IDs.
		$query = "SELECT case_id as id from contacts_cases where contact_id='$this->id' AND deleted=0";

		return $this->build_related_list($query, new aCase());
	}

	/** Returns a list of the associated tasks
	 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
	 * All Rights Reserved..
	 * Contributor(s): ______________________________________..
	*/
	function get_tasks()
	{
		// First, get the list of IDs.
		$query = "SELECT id from tasks where contact_id='$this->id' AND deleted=0";

		return $this->build_related_list($query, new Task());
	}

	/** Returns a list of the associated notes
	 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
	 * All Rights Reserved..
	 * Contributor(s): ______________________________________..
	*/
	function get_notes()
	{
		// First, get the list of IDs.
		$query = "SELECT id from notes where contact_id='$this->id' AND deleted=0";

		return $this->build_related_list($query, new Note());
	}

	/** Returns a list of the associated meetings
	 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
	 * All Rights Reserved..
	 * Contributor(s): ______________________________________..
	*/
	function get_meetings()
	{
		// First, get the list of IDs.
		$query = "SELECT meeting_id as id from meetings_contacts where contact_id='$this->id' AND deleted=0";

		return $this->build_related_list($query, new Meeting());
	}

	/** Returns a list of the associated calls
	 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
	 * All Rights Reserved..
	 * Contributor(s): ______________________________________..
	*/
	function get_calls()
	{
		// First, get the list of IDs.
		$query = "SELECT call_id as id from calls_contacts where contact_id='$this->id' AND deleted=0";

		return $this->build_related_list($query, new Call());
	}

	/** Returns a list of the associated emails
	 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
	 * All Rights Reserved..
	 * Contributor(s): ______________________________________..
	*/
	function get_emails()
	{
		// First, get the list of IDs.
		$query = "SELECT email_id as id from emails_contacts where contact_id='$this->id' AND deleted=0";

		return $this->build_related_list($query, new Email());
	}

	function add_list_count_joins(&$query, $where)
	{
		// accounts.name
		if(eregi("accounts.name", $where))
		{
			// add a join to the accounts table.
			$query .= "
	            LEFT JOIN accounts_contacts
	            ON contacts.id=accounts_contacts.contact_id
	            LEFT JOIN accounts
	            ON accounts_contacts.account_id=accounts.id
			";
		}
		$custom_fields = new CustomFields();
		$query .= $custom_fields->get_list_query_custom_from($this);

	}

	function create_list_query(&$order_by, &$where)
	{
		$custom_fields = new CustomFields();
		$query = "SELECT ";
		$query .= $custom_fields->get_list_query_custom_select($this);
		$query .= "
                accounts.name as account_name,
                accounts.id as account_id,
                users.user_name as assigned_user_name, ";




        $query .= "contacts.assigned_user_id,
				contacts.id,
                contacts.first_name,
                contacts.last_name,
                contacts.phone_work,
                contacts.title,
                contacts.email1,
                contacts.email2
                FROM contacts ";





		$query .=		"LEFT JOIN users
	                    ON contacts.assigned_user_id=users.id
	                    LEFT JOIN accounts_contacts
	                    ON contacts.id=accounts_contacts.contact_id
	                    LEFT JOIN accounts
	                    ON accounts_contacts.account_id=accounts.id ";



		$query .= $custom_fields->get_list_query_custom_from($this);

		$where_auto = "( accounts_contacts.deleted IS NULL OR accounts_contacts.deleted=0 )
                    AND ( accounts.deleted IS NULL OR accounts.deleted=0 )
                    AND contacts.deleted=0 ";

		if($where != "")
			$query .= "where ($where) AND ".$where_auto;
		else
			$query .= "where ".$where_auto;

		if(!empty($order_by))
			$query .= " ORDER BY $order_by";

		return $query;
	}



        function create_export_query(&$order_by, &$where)
        {
                         $query = "SELECT
                                contacts.*,
                                accounts.name as account_name,
                                users.user_name as assigned_user_name ";



						 $query .= "FROM contacts ";




                         $query .= "LEFT JOIN users
	                                ON contacts.assigned_user_id=users.id ";



	                     $query .= "LEFT JOIN accounts_contacts
	                                ON contacts.id=accounts_contacts.contact_id
	                                LEFT JOIN accounts
	                                ON accounts_contacts.account_id=accounts.id ";


		$where_auto = "( accounts_contacts.deleted IS NULL OR accounts_contacts.deleted=0 )
                    AND ( accounts.deleted IS NULL OR accounts.deleted=0 )
                    AND contacts.deleted=0 ";

                if($where != "")
                        $query .= "where ($where) AND ".$where_auto;
                else
                        $query .= "where ".$where_auto;

                if(!empty($order_by))
                        $query .= " ORDER BY $order_by";

                return $query;
        }



	function save_relationship_changes($is_update)
    {

    	$this->clear_account_contact_relationship($this->id);

    	if($this->account_id != "")
    	{
    		$this->set_account_contact_relationship($this->id, $this->account_id);
    	}
    	if($this->opportunity_id != "")
    	{
    		$this->set_opportunity_contact_relationship($this->id, $this->opportunity_id);
    	}
    	if($this->case_id != "")
    	{
    		$this->set_case_contact_relationship($this->id, $this->case_id);
    	}
		if($this->bug_id != "") {
			$this->set_contact_bug_relationship($this->id, $this->bug_id);
		}
    	if($this->task_id != "")
    	{
    		$this->set_task_contact_relationship($this->id, $this->task_id);
    	}
    	if($this->note_id != "")
    	{
    		$this->set_note_contact_relationship($this->id, $this->note_id);
    	}
    	if($this->meeting_id != "")
    	{
    		$this->set_meeting_contact_relationship($this->id, $this->meeting_id);
    	}
    	if($this->call_id != "")
    	{
    		$this->set_call_contact_relationship($this->id, $this->call_id);
    	}
    	if($this->email_id != "")
    	{
    		$this->set_email_contact_relationship($this->id, $this->email_id);
    	}






    }


















	function clear_account_contact_relationship($contact_id)
	{
		$query = "delete from accounts_contacts where contact_id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing account to contact relationship: ");
	}

	function set_contact_bug_relationship($contact_id, $bug_id) {
		$query = "insert into contacts_bugs set id='".create_guid()."', bug_id='$bug_id', contact_id='$contact_id'";
		$this->db->query($query,true,"Error setting contact to bug relationship: ");
	}

	function set_account_contact_relationship($contact_id, $account_id)
	{
		$query = "insert into accounts_contacts set id='".create_guid()."', contact_id='$contact_id', account_id='$account_id'";
		$this->db->query($query,true,"Error setting account to contact relationship: "."<BR>$query");
	}

	function set_opportunity_contact_relationship($contact_id, $opportunity_id)
	{
		global $app_list_strings;
		$default = $app_list_strings['opportunity_relationship_type_default_key'];
		$query = "insert into opportunities_contacts set id='".create_guid()."', opportunity_id='$opportunity_id', contact_id='$contact_id', contact_role='$default'";
		$this->db->query($query,true,"Error setting account to contact relationship: "."<BR>$query");
	}

	function clear_opportunity_contact_relationship($contact_id)
	{
		$query = "delete from opportunities_contacts where contact_id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing opportunity to contact relationship: ");
	}

	function set_case_contact_relationship($contact_id, $case_id)
	{
		global $app_list_strings;
		$default = $app_list_strings['case_relationship_type_default_key'];
		$query = "insert into contacts_cases set id='".create_guid()."', case_id='$case_id', contact_id='$contact_id', contact_role='$default'";
		$this->db->query($query,true,"Error setting case to contact relationship: "."<BR>$query");
	}

	function clear_case_contact_relationship($contact_id)
	{
		$query = "delete from contacts_cases where contact_id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing case to contact relationship: ");
	}

	function set_task_contact_relationship($contact_id, $task_id)
	{
		$query = "UPDATE tasks set contact_id='$contact_id' where id='$task_id'";
		$this->db->query($query,true,"Error setting contact to task relationship: ");
	}

	function clear_task_contact_relationship($contact_id)
	{
		$query = "delete from tasks where contact_id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing task to contact relationship: ");
	}

	function set_note_contact_relationship($contact_id, $note_id)
	{
		$query = "UPDATE notes set contact_id='$contact_id' where id='$note_id'";
		$this->db->query($query,true,"Error setting contact to note relationship: ");
	}

	function clear_note_contact_relationship($contact_id)
	{
		$query = "delete from notes where contact_id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing note to contact relationship: ");
	}

	function set_meeting_contact_relationship($contact_id, $meeting_id)
	{
		$query = "insert into meetings_contacts set id='".create_guid()."', meeting_id='$meeting_id', contact_id='$contact_id'";
		$this->db->query($query,true,"Error setting meeting to contact relationship: "."<BR>$query");
	}

	function clear_meeting_contact_relationship($contact_id)
	{
		$query = "delete from meetings_contacts where contact_id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing meeting to contact relationship: ");
	}

	function set_call_contact_relationship($contact_id, $call_id)
	{
		$query = "insert into calls_contacts set id='".create_guid()."', call_id='$call_id', contact_id='$contact_id'";
		$this->db->query($query,true,"Error setting meeting to contact relationship: "."<BR>$query");
	}

	function clear_call_contact_relationship($contact_id)
	{
		$query = "delete from calls_contacts where contact_id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing call to contact relationship: ");
	}

	function set_email_contact_relationship($contact_id, $email_id)
	{
		$query = "insert into emails_contacts set id='".create_guid()."', email_id='$email_id', contact_id='$contact_id'";
		$this->db->query($query,true,"Error setting email to contact relationship: "."<BR>$query");
	}

	function clear_email_contact_relationship($contact_id)
	{
		$query = "delete from emails_contacts where contact_id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing email to contact relationship: ");
	}

	function clear_contact_all_direct_report_relationship($contact_id)
	{
		$query = "UPDATE contacts set reports_to_id='' where reports_to_id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing contact to direct report relationship: ");
	}

	function clear_contact_direct_report_relationship($contact_id)
	{
		$query = "UPDATE contacts set reports_to_id='' where id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing contact to direct report relationship: ");
	}

	function clear_contact_bug_relationship($contact_id)
	{
		$query = "delete from contacts_bugs where contact_id='$contact_id' and deleted=0";
		$this->db->query($query,true,"Error clearing contact to bug relationship: ");
	}

	function mark_relationships_deleted($id)
	{
		$this->clear_contact_all_direct_report_relationship($id);
		$this->clear_account_contact_relationship($id);
		$this->clear_opportunity_contact_relationship($id);
		$this->clear_case_contact_relationship($id);
		$this->clear_task_contact_relationship($id);
		$this->clear_note_contact_relationship($id);
		$this->clear_call_contact_relationship($id);
		$this->clear_meeting_contact_relationship($id);
		$this->clear_email_contact_relationship($id);
		$this->clear_contact_bug_relationship($id);



	}

	function fill_in_additional_list_fields()
	{
		$this->email_and_name1 = $this->first_name." ". $this->last_name." &lt;".$this->email1."&gt;";
		$this->email_and_name2 = $this->first_name." ". $this->last_name." &lt;".$this->email2."&gt;";
		//$this->fill_in_additional_detail_fields();
	}

	function fill_in_additional_detail_fields()
	{
		// Fill in the assigned_user_name
		$this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);




		$query = "SELECT acc.id, acc.name from accounts as acc, accounts_contacts as a_c where acc.id = a_c.account_id and a_c.contact_id = '$this->id' and a_c.deleted=0";
		$result = $this->db->query($query,true," Error filling in additional detail fields: ");

		// Get the id and the name.
		$row = $this->db->fetchByAssoc($result);

		if($row != null)
		{
			$this->account_name = $row['name'];
			$this->account_id = $row['id'];
		}
		else
		{
			$this->account_name = '';
			$this->account_id = '';
		}
		$query = "SELECT c1.first_name, c1.last_name from contacts as c1, contacts as c2 where c1.id = c2.reports_to_id and c2.id = '$this->id' and c1.deleted=0";
		$result = $this->db->query($query,true," Error filling in additional detail fields: ");

		// Get the id and the name.
		$row = $this->db->fetchByAssoc($result);

		if($row != null)
		{
			$this->reports_to_name = $row['first_name'].' '.$row['last_name'];
		}
		else
		{
			$this->reports_to_name = '';
		}

		$this->created_by_name = get_assigned_user_name($this->created_by);
		$this->modified_by_name = get_assigned_user_name($this->modified_user_id);
	}
	function get_list_view_data(){
		$temp_array = $this->get_list_view_array();
        $temp_array["ENCODED_NAME"]=$this->first_name.' '.$this->last_name;
//    	$temp_array["ENCODED_NAME"]=htmlspecialchars($this->first_name.' '.$this->last_name, ENT_QUOTES);
    	return $temp_array;

	}
	function parse_additional_headers(&$list_form, $xTemplateSection) {

	}

	function list_view_parse_additional_sections(&$list_form, $xTemplateSection) {
		return $list_form;
	}
	/**
		builds a generic search based on the query string using or
		do not include any $this-> because this is called on without having the class instantiated
	*/
	function build_generic_where_clause ($the_query_string)
	{
		$where_clauses = Array();
		$the_query_string = addslashes($the_query_string);

		array_push($where_clauses, "contacts.last_name like '$the_query_string%'");
		array_push($where_clauses, "contacts.first_name like '$the_query_string%'");
		array_push($where_clauses, "contacts.assistant like '$the_query_string%'");
		array_push($where_clauses, "contacts.email1 like '$the_query_string%'");
		array_push($where_clauses, "contacts.email2 like '$the_query_string%'");

		if (is_numeric($the_query_string))
		{
			array_push($where_clauses, "contacts.phone_home like '%$the_query_string%'");
			array_push($where_clauses, "contacts.phone_mobile like '%$the_query_string%'");
			array_push($where_clauses, "contacts.phone_work like '%$the_query_string%'");
			array_push($where_clauses, "contacts.phone_other like '%$the_query_string%'");
			array_push($where_clauses, "contacts.phone_fax like '%$the_query_string%'");
			array_push($where_clauses, "contacts.assistant_phone like '%$the_query_string%'");
		}

		$the_where = "";
		foreach($where_clauses as $clause)
		{
			if($the_where != "") $the_where .= " or ";
			$the_where .= $clause;
		}


		return $the_where;
	}

	function set_notification_body($xtpl, $contact)
	{
		$xtpl->assign("CONTACT_NAME", trim($contact->first_name . " " . $contact->last_name));
		$xtpl->assign("CONTACT_DESCRIPTION", $contact->description);

		return $xtpl;
	}

	function get_contact_id_by_email($email)
	{
		$where_clause = "(email1='$email' OR email2='$email') AND deleted=0";

                $query = "SELECT * FROM $this->table_name WHERE $where_clause";
                $this->log->debug("Retrieve $this->object_name: ".$query);
                $result =& $this->db->requireSingleResult($query, true, "Retrieving record $where_clause:");
                if( empty($result))
                {
                        return null;
                }

                $row = $this->db->fetchByAssoc($result, -1, true);
		return $row['id'];

	}

}



?>
