<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
 * SugarCRM is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004 - 2009 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program 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, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contactlead SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contactlead@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/
/*********************************************************************************

 * Description:  TODO: To be written.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 ********************************************************************************/

require_once('include/SugarObjects/templates/person/Person.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');
require_once('modules/Campaigns/Campaign.php');
require_once('include/SugarObjects/templates/person/Person.php');
// ContactLead is used to store customer information.
class ContactLead extends Person {
    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 $foreign_contactlead;
	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;
	var $contactleads_users_id;
	// These are for related fields
	var $bug_id;
	var $lead_name;
	var $lead_id;
	var $report_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 $accept_status;
    var $accept_status_id;
    var $accept_status_name;
    var $alt_address_street_2;
    var $alt_address_street_3;
    var $opportunity_role_id;
    var $portal_password;
    var $primary_address_street_2;
    var $primary_address_street_3;
    var $campaign_id;
    var $sync_contactlead;

	var $mailing_tag;






	var $full_name; // l10n localized name
	var $invalid_email;
	var $table_name = "contactleads";
	var $rel_lead_table = "leads_contactleads";
	//This is needed for upgrade.  This table definition moved to Opportunity module.
	var $rel_opportunity_table = "opportunities_contactleads";




	var $object_name = "ContactLead";
	var $module_dir = 'ContactLeads';
	var $emailAddress;
	var $new_schema = true;
	var $importable = true;

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






	);

	var $relationship_fields = Array('lead_id'=> 'leads','bug_id' => 'bugs', 'call_id'=>'calls','case_id'=>'cases','email_id'=>'emails',
								'meeting_id'=>'meetings','note_id'=>'notes','task_id'=>'tasks', 'opportunity_id'=>'opportunities', 'contactleads_users_id' => 'user_sync'



								);








	function ContactLead() {
		parent::Person();
		global $current_user;







	}
	
	function fill_in_relationship_fields(){
		if(!empty($this->relDepth)) {
			if($this->relDepth > 1)return;
		}else $this->relDepth = 0;

        foreach($this->field_defs as $field){
            if(0 == strcmp($field['type'],'relate') && !empty($field['module'])){
                $name = $field['name'];

                if(empty($this->$name)){

                    // set the value of this relate field in this bean ($this->$field['name']) to the value of the 'name' field in the related module for the record identified by the value of $this->$field['id_name']
	                $related_module = $field['module'];
	                $id_name = $field['id_name'];

	                // first, load the link field. This is not done automatically by the bean; usually bean subclasses load any needed link fields in fill_in_additional_fields()
	                // here though we need it to be loaded as it contains the id of the related record
	                if (empty($this->$id_name))
                        $this->fill_in_link_field($id_name);

	                if(!empty($this->$id_name) && $this->$id_name != $this->id){
	                	if(isset($GLOBALS['beanList'][ $related_module])){
	                    	$class = $GLOBALS['beanList'][$related_module];

	                    	if(!empty($this->$id_name) && file_exists($GLOBALS['beanFiles'][$class]) && isset($this->$name)){
	                        	require_once($GLOBALS['beanFiles'][$class]);
								$mod = new $class();
	                        	$mod->relDepth = $this->relDepth + 1;
	                        	$mod->retrieve($this->$id_name);
                                if (!empty($field['rname'])) {
                                    $this->$name = $mod->$field['rname'];
                                } else
	                        	if (isset($mod->name)) {
	                        		$this->$name = $mod->name;
	                        	}
	                           if(isset($field['additionalFields'])){
	                               foreach($field['additionalFields'] as $field=>$to){
	                                   if(isset($mod->$field)){
	                                       $this->$to = $mod->$field;
	                                   }
	                               }
	                           }
	                    	}
	                	}
					}
                }
            }
        }
    }









































	function add_list_count_joins(&$query, $where)
	{
		// leads.name
		if(eregi("leads.name", $where))
		{
			// add a join to the leads table.
			$query .= "
	            LEFT JOIN leads_contactleads
	            ON contactleads.id=leads_contactleads.contactlead_id
	            LEFT JOIN leads
	            ON leads_contactleads.lead_id=leads.id
			";
		}
		$custom_join = $this->custom_fields->getJOIN();
		if($custom_join){
  				$query .= $custom_join['join'];
		}


	}

	function listviewACLHelper(){
		$array_assign = parent::listviewACLHelper();
		$is_owner = false;
		//MFH BUG 18281; JChi #15255
		$is_owner = !empty($this->assigned_user_id) && $this->assigned_user_id == $GLOBALS['current_user']->id;
			if(!ACLController::moduleSupportsACL('Accounts') || ACLController::checkAccess('Accounts', 'view', $is_owner)){
				$array_assign['ACCOUNT'] = 'a';
			}else{
				$array_assign['ACCOUNT'] = 'span';

			}
		return $array_assign;
	}

	function create_list_query($order_by, $where, $show_deleted = 0)
	{
		$custom_join = $this->custom_fields->getJOIN();
		// MFH - BUG #14208 creates alias name for select
		$query = "SELECT ";
		$query .= db_concat($this->table_name,array('first_name','last_name')) . " name, ";
		$query .= "
				$this->table_name.*,
                leads.name as lead_name,
                leads.id as lead_id,
                leads.assigned_user_id lead_id_owner,
                users.user_name as assigned_user_name ";



		if($custom_join){
   				$query .= $custom_join['select'];
 		}
        $query .= "
                FROM contactleads ";





		$query .=		"LEFT JOIN users
	                    ON contactleads.assigned_user_id=users.id
	                    LEFT JOIN leads_contactleads
	                    ON contactleads.id=leads_contactleads.contactlead_id  and leads_contactleads.deleted = 0
	                    LEFT JOIN leads
	                    ON leads_contactleads.lead_id=leads.id AND leads.deleted=0 ";



		$query .= "LEFT JOIN email_addr_bean_rel eabl  ON eabl.bean_id = contactleads.id AND eabl.bean_module = 'ContactLeads' and eabl.primary_address = 1 and eabl.deleted=0 ";
        $query .= "LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id) ";
		if($custom_join){
  				$query .= $custom_join['join'];
		}
		$where_auto = '1=1';
		if($show_deleted == 0){
            	$where_auto = " $this->table_name.deleted=0 ";
            	//$where_auto .= " AND leads.deleted=0  ";
		}else if($show_deleted == 1){
				$where_auto = " $this->table_name.deleted=1 ";
		}


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

		if(!empty($order_by))
		    $query .=  " ORDER BY ". $this->process_order_by($order_by, null);
		return $query;
	}



        function create_export_query(&$order_by, &$where)
        {
        		$custom_join = $this->custom_fields->getJOIN(true, true);
                         $query = "SELECT
                                contactleads.*,email_addresses.email_address email1,
                                leads.name as lead_name,
                                users.user_name as assigned_user_name ";



						if($custom_join){
   							$query .= $custom_join['select'];
 						}
						 $query .= " FROM contactleads ";




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



	                     $query .= "LEFT JOIN leads_contactleads
	                                ON ( contactleads.id=leads_contactleads.contactlead_id and (leads_contactleads.deleted is null or leads_contactleads.deleted = 0))
	                                LEFT JOIN leads
	                                ON leads_contactleads.lead_id=leads.id ";
						
						//join email address table too.
						$query .=  ' LEFT JOIN  email_addr_bean_rel on contactleads.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module=\'ContactLeads\' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 ';
						$query .=  ' LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ' ;
						
						if($custom_join){
  							$query .= $custom_join['join'];
						}

		$where_auto = "( leads.deleted IS NULL OR leads.deleted=0 )
                      AND contactleads.deleted=0 ";

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

                if(!empty($order_by))
                        $query .=  " ORDER BY ". $this->process_order_by($order_by, null);

                return $query;
        }

	function fill_in_additional_list_fields() {	
		parent::fill_in_additional_list_fields();
		// cn: bug 8586 - l10n names for ContactLeads in Email TO: field
		$this->_create_proper_name_field();
		$this->email_and_name1 = "{$this->full_name} &lt;".$this->email1."&gt;";
		$this->email_and_name2 = "{$this->full_name} &lt;".$this->email2."&gt;";
		
		if($this->force_load_details == true) {
			$this->fill_in_additional_detail_fields();
		}
	}

	function fill_in_additional_detail_fields() {
		parent::fill_in_additional_detail_fields();
		global $locale, $app_list_strings, $current_user;
		

		// retrieve the lead information and the information about the person the contactlead reports to.
		$query = "SELECT acc.id, acc.name, con_reports_to.first_name, con_reports_to.last_name
		from contactleads
		left join leads_contactleads a_c on a_c.contactlead_id = '".$this->id."' and a_c.deleted=0
		left join leads acc on a_c.lead_id = acc.id and acc.deleted=0
		left join contactleads con_reports_to on con_reports_to.id = contactleads.reports_to_id
		where contactleads.id = '".$this->id."'";

		$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->lead_name = $row['name'];
			$this->lead_id = $row['id'];
			$this->report_to_name = $row['first_name'].' '.$row['last_name'];
		}
		else
		{
			$this->lead_name = '';
			$this->lead_id = '';
			$this->report_to_name = '';
		}
		$this->load_contactleads_users_relationship();
		/** concating this here because newly created ContactLeads do not have a
		 * 'name' attribute constructed to pass onto related items, such as Tasks
		 * Notes, etc.
		 */
		$this->name = $locale->getLocaleFormattedName($this->first_name, $this->last_name);
        if(!empty($this->contactleads_users_id)) {
		   $this->sync_contactlead = true;
		}

		if(!empty($this->portal_active) && $this->portal_active == 1) {
		   $this->portal_active = true;
		}

        // Set campaign name if there is a campaign id
		if( !empty($this->campaign_id)){
			require_once('modules/Campaigns/Campaign.php');
			$camp = new Campaign();
		    $where = "campaigns.id='{$this->campaign_id}'";
		    $campaign_list = $camp->get_full_list("campaigns.name", $where, true);
		    $this->campaign_name = $campaign_list[0]->name;	
		}
		
		//asterisk phone extension
		if ($_REQUEST['action']=='DetailView') {
		require_once("include/ECM/EcmFormatPhoneNumber/EcmFormatPhoneNumber.php");
		$this->phone_work = EcmFormatPhoneNumber($this->phone_work, "ContactLead", "ContactLeads", $this->id);
		$this->phone_mobile = EcmFormatPhoneNumber($this->phone_mobile, "ContactLead", "ContactLeads", $this->id);
		$this->phone_home = EcmFormatPhoneNumber($this->phone_home, "ContactLead", "ContactLeads", $this->id);
		$this->phone_other = EcmFormatPhoneNumber($this->phone_other, "ContactLead", "ContactLeads", $this->id);
		}
	}

		/**
		loads the contactleads_users relationship to populate a checkbox
		where a user can select if they would like to sync a particular
		contactlead to Outlook
	*/
	function load_contactleads_users_relationship(){
		/*global $current_user;

		$this->load_relationship("user_sync");
		$query_array=$this->user_sync->getQuery(true);

		$query_array['where'] .= " AND users.id = '$current_user->id'";

		$query='';
		foreach ($query_array as $qstring) {
			$query.=' '.$qstring;
		}

		$list = $this->build_related_list($query, new User());
		if(!empty($list)){
			//this should only return one possible value so set it
			$this->contactleads_users_id = $list[0]->id;
		}*/
	}

	function get_list_view_data() {
		global $system_config;
		global $current_user;

		$this->_create_proper_name_field();
		$temp_array = $this->get_list_view_array();
		$temp_array['NAME'] = $this->name;
		$temp_array['ENCODED_NAME'] = $this->name;

		/*
		if(isset($system_config->settings['system_skypeout_on'])
			&& $system_config->settings['system_skypeout_on'] == 1)
		{
			if(!empty($temp_array['PHONE_WORK'])
				&& skype_formatted($temp_array['PHONE_WORK']))
			{
				$temp_array['PHONE_WORK'] = '<a href="callto://'
					. $temp_array['PHONE_WORK']. '">'
					. $temp_array['PHONE_WORK']. '</a>' ;
			}
		}
		*/
		require_once("include/ECM/EcmFormatPhoneNumber/EcmFormatPhoneNumber.php");
		$temp_array['PHONE_WORK'] = EcmFormatPhoneNumber($temp_array['PHONE_WORK'], "ContactLead", "ContactLeads", $this->id);
		$temp_array['PHONE_MOBILE'] = EcmFormatPhoneNumber($temp_array['PHONE_MOBILE'], "ContactLead", "ContactLeads", $this->id);
		$temp_array['PHONE_HOME'] = EcmFormatPhoneNumber($temp_array['PHONE_HOME'], "ContactLead", "ContactLeads", $this->id);
		$temp_array['PHONE_OTHER'] = EcmFormatPhoneNumber($temp_array['PHONE_OTHER'], "ContactLead", "ContactLeads", $this->id);

		$temp_array['EMAIL1'] = $this->emailAddress->getPrimaryAddress($this);
		$this->email1 = $temp_array['EMAIL1'];
		$temp_array['EMAIL1_LINK'] = $current_user->getEmailLink('email1', $this, '', '', 'ListView');
		$temp_array['EMAIL_AND_NAME1'] = "{$this->full_name} &lt;".$temp_array['EMAIL1']."&gt;";
		
		include_once("modules/Accounts/Account.php");
		$addrs=Account::get_email_addresses($this->id,'ContactLeads');
		
		$temp_array["EMAIL"] = $r['addr'];
		$temp_array["ADDRS"] = @implode("||",$addrs);
		$temp_array["CNT_DATA"] = $cnt;
		$temp_array["ACC_DATA"] = $acc;
		if($_REQUEST['action']!="Popup"){
		$temp_array['FIRST_NAME']='<a target="_blank" href="index.php?module=ContactLeads&return_module=Contacts&action=DetailView&record='.$this->id.'">'.$this->first_name.'</a>';
		}
		return $temp_array;
	}

	/**
		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 = $this->db->quote($the_query_string);

		array_push($where_clauses, "contactleads.last_name like '$the_query_string%'");
		array_push($where_clauses, "contactleads.first_name like '$the_query_string%'");
		array_push($where_clauses, "leads.name like '$the_query_string%'");
		array_push($where_clauses, "contactleads.assistant like '$the_query_string%'");
		array_push($where_clauses, "ea.email_address like '$the_query_string%'");

		if (is_numeric($the_query_string))
		{
			array_push($where_clauses, "contactleads.phone_home like '%$the_query_string%'");
			array_push($where_clauses, "contactleads.phone_mobile like '%$the_query_string%'");
			array_push($where_clauses, "contactleads.phone_work like '%$the_query_string%'");
			array_push($where_clauses, "contactleads.phone_other like '%$the_query_string%'");
			array_push($where_clauses, "contactleads.phone_fax like '%$the_query_string%'");
			array_push($where_clauses, "contactleads.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, $contactlead)
	{
		$xtpl->assign("CONTACTLEAD_NAME", trim($contactlead->first_name . " " . $contactlead->last_name));
		$xtpl->assign("CONTACTLEAD_DESCRIPTION", $contactlead->description);

		return $xtpl;
	}

	function get_contactlead_id_by_email($email)
	{
		$email = trim($email);
		if(empty($email)){
			//email is empty, no need to query, return null
			return null;
		}

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

                $query = "SELECT * FROM $this->table_name WHERE $where_clause";
                $GLOBALS['log']->debug("Retrieve $this->object_name: ".$query);
		        //requireSingleResult has beeen deprecated.
                //$result = $this->db->requireSingleResult($query, true, "Retrieving record $where_clause:");
				$result = $this->db->limitQuery($query,0,1,true, "Retrieving record $where_clause:");

                if( empty($result))
                {
                        return null;
                }

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

	}

	function save_relationship_changes($is_update) {

		//if lead_id was replaced unlink the previous lead_id.
		//this rel_fields_before_value is populated by sugarbean during the retrieve call.
		if (!empty($this->lead_id) and !empty($this->rel_fields_before_value['lead_id']) and
				(trim($this->lead_id) != trim($this->rel_fields_before_value['lead_id']))) {
				//unlink the old record.
				$this->load_relationship('leads');
				$this->leads->delete($this->id,$this->rel_fields_before_value['lead_id']);
		}
		parent::save_relationship_changes($is_update);
	}

	function bean_implements($interface){
		switch($interface){
			case 'ACL':return true;
		}
		return false;
	}
	function get_unlinked_email_query($type=array()) {
		require_once('include/utils.php');
		return get_unlinked_email_query($type, $this);
	}
	
    /**
     * used by import to add a list of users
     *
     * Parameter can be one of the following:
     * - string 'all': add this contactlead for all users
     * - comma deliminated lists of teams and/or users
     *
     * @param string $list_of_user
     */
    
    function process_sync_to_outlook(
        $list_of_users
        ) 
    {
        static $focus_user;
        
        // cache this object since we'll be reusing it a bunch
        if ( !($focus_user instanceof User) ) {
            require_once('modules/Users/User.php');
            $focus_user = new User();
        }
        









        
		if ( empty($list_of_users) ) {
            return;
		}
        if ( !isset($this->users) ) {
            $this->load_relationship('user_sync');
        }
                
		if ( strtolower($list_of_users) == 'all' ) {
            // add all non-deleted users
			$sql = "SELECT id FROM users WHERE deleted=0 AND is_group=0 AND portal_only=0";
			$result=$this->db->query($sql);
			while ( $hash = $this->db->fetchByAssoc($result) ) {
                $this->user_sync->add($hash['id']);
			}
		} 
        else {
            $theList = explode(",",$list_of_users);
            foreach ($theList as $eachItem) {
                if ( $focus_user->retrieve_user_id($eachItem)
                        || $focus_user->retrieve($eachItem)) {
                    // it is a user, add user
                    $this->user_sync->add($this->id);
                    return;
                }














			}
		}
	}
	

	function showPositions55(){
		$arr=$this->getPositionList55(true);
	
		global $mod_strings;
		if(count($arr)>0){
			$table='
			<table cellpadding="0" cellspacing="0" border="0" class="list view" width="40%">
			<tr class="oddListRowS1">
				<th><b>Nazwa grupy</b></td>
				<th><b>Aktywna</b></td>
			</tr>
			';
			$i == 0;
			foreach($arr as $a){
				$i++;
	
	
				$table.='
				<tr class="oddListRowS1">
					<td valign="top" class="oddListRowS1" style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;"><a href="index.php?module=EcmMailingCategories&action=DetailView&record='.$a['cat_id'].'">'.$a['product_group'].'</a></td>
	
					<td valign="top" class="oddListRowS1" style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">'.($a['discount']==1 ? 'Tak' : "Nie").'</td>
	
				';
			}
			$table.='</table>';
		}
	
		return $table;
	}
	
	function getPositionList55($array = false) {
		if(isset($this->id) && $this->id != '') {
			$query = "SELECT * FROM ecmmailingcategories_relation WHERE parent_id='".$this->id."' AND deleted='0' AND parent_type='ContactLead'";
	
			$r = $this->db->query($query);
			$return_array = array();
			if($r) {
	
				while($w = $this->db->fetchByAssoc($r)) {
					//get category name && assigned_file
					$n = $GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("SELECT name FROM ecmmailingcategories WHERE id='".$w['ecmmailingcategoriy_id']."'"));
					$w['product_group'] = $n['name'];
					$w['cat_id'] =$w['ecmmailingcategoriy_id'];
					$w['discount'] =$w['available'];
					$return_array [] = $w;
	
				}
				$json = getJSONobj();
	
				return $array ? $return_array : $json->encode($return_array);
			}
		}
	
		return $array ? false : '[]';
	}
	
	//end managing addresses
	function savePositions55($pl) {

		global $current_user;
	
		$exists = array();
		foreach ($pl as $p) {
	
			if (!isset($p['product_group']) || $p['product_group']=='') continue;
	
	
			if (isset($p['id']) && $p['id']!='') {
	
				//update exists
				$q = "
				UPDATE ecmmailingcategories_relation set
					parent_id = '".addslashes($this->id)."',
					ecmmailingcategoriy_id= '".addslashes($p['cat_id'])."',
					available = '".addslashes($p['discount'])."',
				    parent_type = 'ContactLead',
					parent_name = '".addslashes($p['product_group'])."'
					WHERE id = '".$p['id']."'
				";
	
				//$GLOBALS['db']->query("INSERT INTO log VALUES ('".addslashes($q)."')");
				$GLOBALS['db']->query($q);
				$exists[] = $p['id'];
	
			} else {
				//insert new record
				$id = create_guid();
				$t = array(
						$id,
						addslashes($p['cat_id']),
						'0',
						addslashes($p['discount']),
						$this->id,
						addslashes($p['product_group']),
						'ContactLead'
	
				);
				$q = "INSERT INTO ecmmailingcategories_relation VALUES ('".implode("','", $t)."')";
	
				$GLOBALS['db']->query($q);
				$exists[] = $id;
			}
	
		}
	
		//delete old
		$GLOBALS['db']->query("UPDATE ecmmailingcategories_relation SET deleted='1' WHERE parent_id='".$this->id."' AND id NOT IN ('".implode("','",$exists)."')");
	}
	

}



?>
