<?php
/**
 * $Id$
 * klient
 *
 */
class BoxController extends MainController implements ControllerInterface {

	const PATH_BANER = '/upload/home';
	const COUNT_BOX = 4;
	
	const AVATAR_DEST_DIR = '/upload/home';
	const AVATAR_TEMP_DIR = '/upload/temp';
	const MAX_AVATAR_FILE_SIZE = 5; //Rozmiar w mb
	const CROPPER_BIG_PHOTO_MAX_WIDTH = 800;
	const CROPPER_BIG_PHOTO_MAX_HEIGHT = 800;
	//const PHOTO_WIDTH = 288;
	//const PHOTO_HEIGHT = 127;

	const IMAGE_MINI_WIDTH = 170;
	const IMAGE_MINI_HEIGHT = 130;
	//const IMAGE_NORMAL_WIDTH = 627;
	//const IMAGE_NORMAL_HEIGHT = 219;
	/**
	 * 170px × 130px
	 * Domyslna metoda
	 *
	 */
	public function IndexAction($param) {
		//Utils::ArrayDisplay($param);
		$lang = SessionProxy::GetValue('lang');
		$countArray = range(0, 3);
		//Utils::ArrayDisplay($param);
		$dalData = MfHomeSiteDAL::GetDalDataObj();
		//$dalData->setCondition(array('lang' => $param['lang']));
		$dalData->setCondition(array('lang' =>$lang));
		$dalData->setLimit(4);

		$arrayObjHome = MfHomeSiteDAL::GetResult($dalData);

		//Utils::ArrayDisplay($countArray);
		$arrayToEdit = array();
		foreach ($countArray as $key => $element) {
			if (key_exists($key, $arrayObjHome)) {
				$arrayToEdit[] = $arrayObjHome[$key];
			} else {
				$arrayToEdit[] = MfHomeSiteDAL::GetEmptyObj();
			}
		}
		//Utils::ArrayDisplay($arrayToEdit);
		$this->smarty->assign('arrayToEdit', $arrayToEdit);
		$this->smarty->assign('lang', $lang);
	}



	/**
	 * Dodawanie/edycja klient/banera
	 *
	 */
	public function EditAction($param) {
		$countArray = range(0, 3);
		$post = Request::GetAllPost();
		foreach ($countArray as $key => $element) {
			$objHomeElement = MfHomeSiteDAL::GetById($post['elementId'][$key]);
			//$objHomeElement = new MfHomeSite();
			$objHomeElement->SetName($post['elementName'][$key]);
			$objHomeElement->SetSourceUrl($post['elementUrl'][$key]);
			$objHomeElement->SetDescription($post['elementText'][$key]);
			

			if($_FILES['elementImg_'.$key]['tmp_name']) {

				$photoSize = getimagesize($_FILES['elementImg_'.$key]['tmp_name']);
				$photoProp = $photoSize[0] / $photoSize[1];

				$photoWidth = $photoSize[0];
				$photoHeight = $photoSize[1];

				if ($photoWidth > self::CROPPER_BIG_PHOTO_MAX_WIDTH) {
					$photoHeight = self::CROPPER_BIG_PHOTO_MAX_WIDTH / $photoProp;
					$photoWidth = self::CROPPER_BIG_PHOTO_MAX_WIDTH;
				}

				if ($photoHeight > self::CROPPER_BIG_PHOTO_MAX_HEIGHT) {
					$photoWidth = self::CROPPER_BIG_PHOTO_MAX_HEIGHT * $photoProp;
					$photoHeight = self::CROPPER_BIG_PHOTO_MAX_HEIGHT;
				}

				$photoFile = PhotoDAL::SimplePhotoUpload($_FILES['elementImg_'.$key], self::AVATAR_TEMP_DIR . DIRECTORY_SEPARATOR , $photoWidth, $photoHeight, 100);



				$destName = md5('photo' . microtime());
				if(file_exists(PATH_STATIC_CONTENT.'upload/home/'.$destName.'.jpg'))
				unlink(PATH_STATIC_CONTENT.'upload/home/'.$destName.'.jpg');


				$propW = $photoWidth/self::IMAGE_MINI_WIDTH;
				$propH = $photoHeight/self::IMAGE_MINI_HEIGHT;

				if ($propW > $propH) {
					$prop = $propH;
				} else {
					$prop = $propW;
				}
				$photoMini = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/home', 0, 0, self::IMAGE_MINI_WIDTH*$prop, self::IMAGE_MINI_HEIGHT*$prop, self::IMAGE_MINI_WIDTH, self::IMAGE_MINI_HEIGHT);
				$objHomeElement->SetPhoto($photoMini);
			}
			
			
			
			MfHomeSiteDAL::Save($objHomeElement);
			//Utils::ArrayDisplay($objHomeElement);
		}
		
		$this->AddRedirect(Router::GenerateUrl('editBox', array('_value' => 'Box')), 0);
		//Utils::ArrayDisplay($_POST);
		
		//Utils::ArrayDisplay($_FILES);
	}



	/**
	 * Metoda wspolna
	 *
	 */
	public function preDispatch($param) {
		$this->Run($param);
		//$admin = AuthDAL::GetAdmin();
		$this->RunShared('Auth', array());
		$this->smarty->assign('titleAdmin', 'Strona główna');
		$this->smarty->assign('lang', $param['lang']);
//		$struct = array(
//		'User' => array('User' => 'Index'),
//		'Słowniki' => array('Dictionary' => 'Index'),
//		//'Role' => array('Acl' => 'Index'),
//		//'Uprawnienia' => array('Acl' => 'Rules'),
//		'Zmienne serwisu' => array('Setup' => 'Index')
//		
//
//		);

		$this->smarty->assign('structure','');
		
	}
	
	private function renderStruct($struct){
		$return = array();
		
		foreach($struct AS $k => $row){
			$return[] = '<a href="' . Router::GenerateUrl('dictpig',$row).'">'.$k.'</a>';
		}
		
		return implode("<br />",$return);
	}

	public function postDispatch($param) {

	}
}
?>