<?php
/**
 * Customer Class
 *
 */
class customer
{
	var $data;
	/**
	 * Initiates a customer Object from customers-class
	 *
	 * @param Object $customer_object
	 * @return customer
	 */
	function customer( $customer_object ){
		$this->data = $customer_object;
	}

	/**
	 * Get the firstname lastname String
	 *
	 * @return String Fullname
	 */
	function getFullName(){
		return sprintf("%s %s",$this->data->customers_firstname,$this->data->customers_lastname);
	}
	
	/**
	 * get the allocated companies for the customer
	 *
	 * @return array
	 */
	function get_allocated_companies_array(){
		global $conn;
		$array = array();
		$result = $conn->Execute(sprintf("SELECT companies_id FROM %s WHERE customers_id='%s'",TABLE_CUSTOMERS_TO_COMPANIES,$this->data->customers_id));
		while(!$result->EOF){
			$array[] = $result->fields['companies_id'];
			$result->MoveNext();
		}
		return $array;
	}
	
	/**
	 * Save customer to companies relation
	 *
	 * @param Array $data_array The Request Array of companies_id
	 */
	function save_companies_allocation($data_array){
		global $conn;
		$conn->Execute(sprintf("DELETE FROM %s WHERE customers_id='%s'",TABLE_CUSTOMERS_TO_COMPANIES,$this->data->customers_id));
		if(is_array($data_array) and sizeOf($data_array)>0){
			foreach($data_array as $k=>$companies_id){
				$conn->Execute(sprintf("INSERT INTO %s SET companies_id='%s',customers_id='%s'",TABLE_CUSTOMERS_TO_COMPANIES,$companies_id,$this->data->customers_id));
			}
		}
	}
	
	function get_smarty_data_array(){
		$array = (array) $this->data;
		$array['fullname'] = $this->getFullName();
		$array['account'] = xtc_get_customers_status_name($array['customers_status']);
		$array['gender_name'] = yes_get_customers_gender_name($array['customers_gender']);
		$companies = $this->get_allocated_companies_array();
		$companies_array = array();
		for($i=0;$i<sizeOf($companies);$i++){
			$name = yes_get_companies_name($companies[$i]);
			$companies_array[] = array(
				'id'=>$companies[$i],
				'text'=>$name,
				'link_button'=>yes_draw_href_image('images/link_arrow.gif',YES_LINKTEXT,xtc_href_link(FILENAME_COMPANIES,'id='.$companies[$i].'&action=overview'),'companies',$css_class='' ),
				'link'=>yes_draw_href_text( $name,xtc_href_link(FILENAME_COMPANIES,'id='.$companies[$i].'&action=overview'),'companies',$style='',$css_class='') 
			);
		}
		$array['companies'] = $companies_array;
		$array['default_address'] = yes_get_address_data_array($array,'','customer');
		return $array;
	}
}
