<?php
/**
 * Define a sub firm.
 * @author valentin carruesco
 * @category Core
 * @license copyright
 */
class Firm extends Entity{
	public $id,$label,$description,$mail,$phone,$fax,$street,$street2,$city,$zipcode,$siret,$iban;

	protected $fields =
	array(
		'id' => 'key',
		'label' => 'string',
		'description' => 'longstring',
		'mail' => 'string',
		'phone' => 'string',
		'fax' => 'string',
		'street' => 'longstring',
		'street2' => 'longstring',
		'city' => 'string',
		'zipcode' => 'string',
		'siret' => 'string',
		'iban' => 'string'
	);

	public static function logo_path(){
		return 'firm'.SLASH;
	}

	public function address(){
		return $this->street.' '.$this->street2.' '.$this->zipcode.' '.$this->city;
	}

	public function __sleep(){
		return array_merge(array_keys($this->toArray()));
	}

	public function logo($key=null){
	   	$finded = false;
		$image = array();
	   	$paths = array(
	   		'jpg' => FILE_PATH.self::logo_path().$this->id.'.jpg',
	   		'jpeg' => FILE_PATH.self::logo_path().$this->id.'.jpeg',
	   		'png' => FILE_PATH.self::logo_path().$this->id.'.png',
	   	);    	

	   	$image['url'] = $image['path'] = 'img'.SLASH.'default-image.png';
		foreach ($paths as $extension => $path) {
			if (file_exists( __ROOT__.$path) && !$finded) {
				$image['path'] = __ROOT__.$path;
				$image['url'] = 'action.php?action=firm_logo_download&firm='.$this->id.'&extension='.$extension;
				$finded = true;
			}
		}
		return isset($key) && isset($image[$key]) ? $image[$key] : $image['url'];
	}

	public function has_plugin($plugin){
		$states = Plugin::states();
		if(!isset($states[$plugin]) || !in_array($this->id, $states[$plugin])) return false;
		return true;
	}
}
?>