<?php

class Contact extends Entity{
	public $id,$label,$phone,$birth,$hour,$author,$address,$vehicle,$story,$login,$password,$icon,$mycheckbox1;
	protected $TABLE_NAME = 'plugin_contact';
	public $fields =
	array(
		'id' => 'key',
		'label' => 'string',
		'phone' => 'string',
		'birth' => 'date',
		'hour' => 'date',
		'author' => 'string',
		'address' => 'string',
		'vehicle' => 'int',
		'story' => 'longstring',
		'login' => 'string',
		'password' => 'string',
		'icon'=>'string',
		'mycheckbox1'=>'boolean'
	);
	
	function picture(){
		return $this->get_image();
	}

	function get_image($key=null){
		$finded = false;
		$image = array();
		$paths = array(
			'jpg' => FILE_PATH.'contact'.SLASH.$this->id.'.jpg',
			'png' => FILE_PATH.'contact'.SLASH.$this->id.'.png',
			'jpeg' => FILE_PATH.'contact'.SLASH.$this->id.'.jpeg',
		);

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

	function documents(){
		$documents = array();
		foreach(glob(__ROOT__.FILE_PATH.'contact'.SLASH.'documents'.SLASH.$this->id.SLASH.'*.*') as $file){
			if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $file = utf8_encode($file);
			$documents[] = array(
				'path' => 'contact'.SLASH.'documents'.SLASH.$this->id.SLASH.basename($file),
				'url' => 'action.php?action=contact_download_document&path='.$this->id.SLASH.rawurlencode(basename($file)),
				'name' => basename($file),
				'icon' => getExtIcon(getExt($file))
			);
		}
		return $documents;
	}
}
?>