<?php
require_once(DIR_WS_CLASSES.'ships.php');
class company
{
	var $data;
	function company( $company_object ){
		$this->data = $company_object;
	}

	/**
	 * get the allocated customers for the company
	 *
	 * @return array
	 */
	function get_allocated_customers_array(){
		global $conn;
		$array = array();
		$result = $conn->Execute(sprintf("SELECT customers_id FROM %s WHERE companies_id='%s'",TABLE_CUSTOMERS_TO_COMPANIES,$this->data->companies_id));
		while(!$result->EOF){
			$array[] = $result->fields['customers_id'];
			$result->MoveNext();
		}
		return $array;
	}

	/**
	 * gibt ein array aller zugewiesenen schiffe zurueck
	 *
	 * @return array
	 */
	function get_allocated_ships_array(){
		global $conn;
		$array = array();
		$ships = new ships;
		$result = $conn->Execute(sprintf("SELECT DISTINCT(ships_id) FROM %s WHERE companies_id='%s'",TABLE_SHIPS_TO_COMPANIES,$this->data->companies_id));
		while(!$result->EOF){
			$ship = $ships->get_ship($result->fields['ships_id']);
			$array[] = $ship->get_smarty_data_array();
			$result->MoveNext();
		}
		return $array;
	}
	
	/**
	 * return array with table data
	 *
	 * @return array
	 */
	function get_smarty_data_array(){
		$array = (array) $this->data;
		$customers = $this->get_allocated_customers_array();
		$customers_array = array();
		for($i=0;$i<sizeOf($customers);$i++){
			$name = yes_get_customers_name($customers[$i]);
			$customers_array[] = array(
			'id'=>$customers[$i],
			'text'=>$name,
			'link_button'=>yes_draw_href_image('images/link_arrow.gif',YES_LINKTEXT,xtc_href_link(FILENAME_CUSTOMERS,'id='.$customers[$i].'&action=overview'),'customers',$css_class='' ),
			'link'=>yes_draw_href_text( $name,xtc_href_link(FILENAME_CUSTOMERS,'id='.$customers[$i].'&action=overview'),'customers',$style='',$css_class='') 
			);
		}
		$array['customers'] = $customers_array;
		
		$ships = $this->get_allocated_ships_array();
		$array['ships'] = $ships;
		
		$array['country_name'] = xtc_get_country_name($array['countries_id']);
		$array['type'] = yes_get_company_types_name($array['company_type']);

		if(xtc_not_null($array['default_airports_id'])){
			$array['default_airport'] = yes_get_location_name($array['default_airports_id']);
		}

		$array['company_address'] = yes_get_address_data_array($array,'','company',$id='companies_id');
		$array['edit_link'] = array('text'=>IMAGE_EDIT,'link'=>xtc_href_link(FILENAME_COMPANIES_EDIT,'id='.$this->data->companies_id),'access'=>'companies_edit');
		$array['add_customer_link'] = array('text'=>BUTTON_ADD_CUSTOMER,'link'=>xtc_href_link(FILENAME_CUSTOMERS_EDIT,'companies_id='.$this->data->companies_id),'access'=>'customers_edit');
		$array['add_ship_link'] = array('text'=>BUTTON_ADD_SHIP,'link'=>xtc_href_link(FILENAME_SHIPS_EDIT,'companies_id='.$this->data->companies_id),'access'=>'ships_edit');
		$array['action_array'] = array(
			'edit'=>$array['edit_link'],
			'add_customer'=>$array['add_customer_link'],
			'add_ship'=>$array['add_ship_link']
		);
		
		return $array;
	}
}
