<?

class ImageController extends MainController implements ControllerInterface {

	const IMAGE_NORMAL_WIDTH = 1000;
	const IMAGE_NORMAL_HEIGHT = 800;

	public function IndexAction($param) {
		$zip = new ZipArchive();
		$archiveName = PATH_STATIC_CONTENT . 'test.zip';
		if ($zip->open($archiveName) === true) {
			//Utils::ArrayDisplay('ss');
			for ($i = 0; $i < $zip->numFiles; $i++) {
				$entry = $zip->getNameIndex($i);
				//Utils::ArrayDisplay('zip://' . $archiveName . '#' . $entry);
				//Utils::ArrayDisplay(PATH_STATIC_CONTENT . 'temp/zip/' . $entry);
				copy('zip://' . $archiveName . '#' . $entry, PATH_STATIC_CONTENT . 'temp/zip/' . $entry);
			}
			$zip->close();
		}
	}

	public function UploadAction($param) {
		if (!SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
			SessionProxy::SetValue('_ARRAY_PHOTO_', array());
		}
		$arrayPhoto = SessionProxy::GetValue('_ARRAY_PHOTO_');
		$this->SetAjaxRender();

		include(Config::Get('PATH_CORE') . '/lib/WideImage/WideImage.php');

		$destDir = 'temp/';

		//Utils::ArrayDisplay($_FILES);
		if ($_FILES['uploadfile']['name']) {
			$ext = Utils::FileExtFromName($_FILES['uploadfile']['name']);
			$uploadFileName = Utils::FileNameFromName($_FILES['uploadfile']['name']);
			$destNameOrg = 'org_' . $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
			$destName = $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
			$destNameth = "th_" . $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
			if (strtolower($ext) == 'zip') {
//======Muliti=ZIP=File====================================================

				move_uploaded_file($_FILES['uploadfile']['tmp_name'], PATH_STATIC_CONTENT . 'temp/zip/' . $_FILES['uploadfile']['name']);
				$zip = new ZipArchive();
				$archiveName = PATH_STATIC_CONTENT . 'temp/zip/' . $_FILES['uploadfile']['name'];
				if ($zip->open($archiveName) === true) {
					//Utils::ArrayDisplay('ss');
					for ($i = 0; $i < $zip->numFiles; $i++) {
						$entry = $zip->getNameIndex($i);
//						Kopiowanie do temp/zip;
						$uploadFileName = Utils::FileNameFromName($entry);
						$destNameOrg = 'org_' . $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
						$destName = $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
						$destNameth = "th_" . $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
						//Utils::ArrayDisplay($entry);
						copy('zip://' . $archiveName . '#' . $entry, PATH_STATIC_CONTENT . 'temp/zip/' . $entry);

//						wczytywanie do obróbki
						$img = WideImage::loadFromFile(PATH_STATIC_CONTENT . 'temp/zip/' . $entry);

						$img->saveToFile(PATH_STATIC_CONTENT . $destDir . $destNameOrg, 90);
						$photoSize = getimagesize(PATH_STATIC_CONTENT . $destDir . $destNameOrg);

						if ($photoSize[0] < $photoSize[1]) {
							$cropH = true;
						} else {
							$cropH = false;
						}
						$img = WideImage::loadFromFile(PATH_STATIC_CONTENT . $destDir . $destNameOrg);
						$img->resizeDown('1000', '800')->saveToFile(PATH_STATIC_CONTENT . $destDir . $destName, 90);
//
						if ($cropH) {
// zmieniam zmniejszam obrazem do szerokości a potem go obcinam już tylko po wysokości
							$img->resizeDown('137', null)->crop('center', 'center', 137, 77)->saveToFile(PATH_STATIC_CONTENT . $destDir . $destNameth, 90);
						} else {
//Poziom OK!
// zmieniam zmniejszam obrazem do wysokości a potem go obcinam już tylko po szerokości
							$img->resizeDown(null, '137')->crop('center', 'center', 137, 77)->saveToFile(PATH_STATIC_CONTENT . $destDir . $destNameth, 90);
						}


						$file = URL_STATIC_CONTENT . $destDir . $destNameth;
						$array = array($destName => array('normal' => $destName, 'th' => $destNameth, 'full' => $destNameOrg));
						$arrayPhoto = array_merge($arrayPhoto, $array);
						SessionProxy::SetValue('_ARRAY_PHOTO_', $arrayPhoto);
						$this->smarty->assign('photo', PATH_STATIC_CONTENT . $destDir . $destName);
						unlink(PATH_STATIC_CONTENT . 'temp/zip/' . $entry);
					}
					$zip->close();
				}
				unlink(PATH_STATIC_CONTENT . 'temp/zip/' . $_FILES['uploadfile']['name']);
			} else {
//======Simple=File====================================================

				$img = WideImage::loadFromUpload('uploadfile');
				$img->resizeDown('1920', null)->saveToFile(PATH_STATIC_CONTENT . $destDir . $destNameOrg, 90);
//				$img->saveToFile(PATH_STATIC_CONTENT . $destDir . $destNameOrg, 90);

				$photoSize = getimagesize(PATH_STATIC_CONTENT . $destDir . $destNameOrg);

//Utils::ArrayDisplay($photoSize);

				if ($photoSize[0] < $photoSize[1]) {
					$cropH = true;
				} else {
					$cropH = false;
				}
				$img = WideImage::loadFromFile(PATH_STATIC_CONTENT . $destDir . $destNameOrg);
				$img->resizeDown('800', '600')->saveToFile(PATH_STATIC_CONTENT . $destDir . $destName, 90);
//
				if ($cropH) {
// zmieniam zmniejszam obrazem do szerokości a potem go obcinam już tylko po wysokości
					$img->resizeDown('137', null)->crop('center', 'center', 137, 77)->saveToFile(PATH_STATIC_CONTENT . $destDir . $destNameth, 90);
				} else {
//Poziom OK!
// zmieniam zmniejszam obrazem do wysokości a potem go obcinam już tylko po szerokości
					$img->resizeDown(null, '137')->crop('center', 'center', 137, 77)->saveToFile(PATH_STATIC_CONTENT . $destDir . $destNameth, 90);
				}


				$file = URL_STATIC_CONTENT . $destDir . $destNameth;
				$array = array($destName => array('normal' => $destName, 'th' => $destNameth, 'full' => $destNameOrg));
				$arrayPhoto = array_merge($arrayPhoto, $array);
				SessionProxy::SetValue('_ARRAY_PHOTO_', $arrayPhoto);
				$this->smarty->assign('photo', PATH_STATIC_CONTENT . $destDir . $destName);
			}
		}
		$this->smarty->assign('arrayPhoto', $arrayPhoto);
	}

	public function UploadImageAction($param) {
		if (!SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
			SessionProxy::SetValue('_ARRAY_PHOTO_', array());
		}
		$arrayPhoto = SessionProxy::GetValue('_ARRAY_PHOTO_');
//$this->SetAjaxRender();
//		$uploaddir = PATH_STATIC_CONTENT.'/upload/test/'; 
//		$file = $uploaddir . basename($_FILES['uploadfile']['name']); 
//Utils::ArrayDisplay($file);
//		if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
//		  echo "success"; 
//		} else {
//			echo "error";
//		}
//				
		$destName = md5('photo' . time());
		$destName = md5('photo' . time());
		$destNameth = "th_" . $destName . ".jpg";
		$destName .= ".jpg";

//$destNamea = "test.jpg";

		$destDir = 'temp/';

//Utils::ArrayDisplay($_FILES);
		if (isset($_FILES['uploadfile']) && $_FILES['uploadfile']['name']) {
//$photoSize = getimagesize(PATH_STATIC_CONTENT.$destDir.$destName);
//Utils::ArrayDisplay(PATH_STATIC_CONTENT);
//Utils::ArrayDisplay(PATH_STATIC_CONTENT);
			$photo = PhotoDAL::SimplePhotoUpload($_FILES['uploadfile'], $destDir);
//Utils::ArrayDisplay(PATH_STATIC_CONTENT.$photo);
			$img_oryginal = new mImage(PATH_STATIC_CONTENT . $photo);
//$img_oryginal->scaleProp(600, 600, 'ffffff', false, 'c', 0);
			$img_oryginal->saveToFile(PATH_STATIC_CONTENT . $destDir . $destName, 'jpg', 90, true);


			$normalW = 242;
			$normalH = 340;

//				$photoSize = getimagesize(PATH_STATIC_CONTENT.$destDir.$destName);
//				
//				//Utils::ArrayDisplay($photoSize);
//				
////				if ($photoSize[0] < $photoSize[1]) {
////					$cropH =  true;
////				} else {
////					$cropH =  false;
////				}
// 				
//				$propW = $photoSize[0]/$normalW;
//				$propH = $photoSize[1]/$normalH; 
//				
//			//	Utils::ArrayDisplay($propW.' - '.$propH);	
//
//				if ($propW > $propH) {
//					$prop = $propH;
//				} else {
//					$prop = $propW;
//				}
//
//				if ($photoSize[1] < $normalH*$prop) {
//					$photoSizeH = $normalH;
//				} else {
//					$photoSizeH = $photoSize[1];
//				}
//				
//
//					$photoSizeH = $normalW*$prop;
//					$photoSizeW =$normalH*$prop;
//					
//				
//Utils::ArrayDisplay($photoSizeH.'-'.$photoSizeW);
//$img_oryginal->crop1(0, 0, $photoSizeH, $photoSizeW);
//$img_oryginal->scaleProp($normalW, $normalH, 0, 'ffffff', false, 'c', 0);
//$img_oryginal->
//$img_oryginal->saveToFile(PATH_STATIC_CONTENT.$destDir.$destName, 'jpg', 90, true);
//move_uploaded_file($_FILES['uploadfile']['tmp_name'], PATH_STATIC_CONTENT.$destDir.$_FILES['uploadfile']['name']);
			$miniW = 242;
			$miniH = 340;
//Utils::ArrayDisplay(PATH_STATIC_CONTENT.$photo);

			$photoSize = getimagesize(PATH_STATIC_CONTENT . $photo);

//$photoSize = getimagesize(PATH_STATIC_CONTENT.$destDir.$_FILES['uploadfile']['name']);
//				if ($photoSize[0] < $photoSize[1]) {
//					$cropH =  true;
//				} else {
//					$cropH =  false;
//				}

			$propW = $photoSize[0] / $miniW;
			$propH = $photoSize[1] / $miniH;

//	Utils::ArrayDisplay($propW.' - '.$propH);	

			if ($propW > $propH) {
				$prop = $propH;
			} else {
				$prop = $propW;
			}

			if ($photoSize[1] < $miniH * $prop) {
				$photoSizeH = $miniH;
			} else {
				$photoSizeH = $photoSize[1];
			}


			$photoSizeH = $miniW * $prop;
			$photoSizeW = $miniH * $prop;
			$y = ($photoSize[1] - $photoSizeH) / 2;
//Utils::ArrayDisplay($y . '=' .'('.$photoSize[1].' - '.$photoSizeH.')  / 2');
			if ($y < 0) {
				$y = 0;
			}
			$y = 0;
//	Utils::ArrayDisplay($y);
			$img_oryginal->crop1(0, $y, $photoSizeH, $photoSizeW);
			$img_oryginal->scaleProp(242, 340, 0, 'ffffff', false, 'c', 0);
//$img_oryginal->
			$img_oryginal->saveToFile(PATH_STATIC_CONTENT . $destDir . $destNameth, 'jpg', 90, true);
//Utils::ArrayDisplay(PATH_STATIC_CONTENT.$destDir.$destNameth);
			if (is_file(PATH_STATIC_CONTENT . $photo)) {
				unlink(PATH_STATIC_CONTENT . $photo);
			}

			$file = URL_STATIC_CONTENT . $destDir . $destNameth;
//$fileName = $destName;
			$array = array($destName => array('normal' => $destName, 'th' => $destNameth));
			$arrayPhoto = array_merge($arrayPhoto, $array);
			SessionProxy::SetValue('_ARRAY_PHOTO_', $arrayPhoto);
			$this->smarty->assign('photo', PATH_STATIC_CONTENT . $destDir . $destName);
//Utils::ArrayDisplay($arrayPhoto);
			$idImageGroup = ImageDAL::SaveImage(111, 'Banner', 5);


			if (is_file(PATH_STATIC_CONTENT . "upload/Banner/111/" . $destName)) {
				unlink(PATH_STATIC_CONTENT . "upload/Banner/111/" . $destName);
			}
			SessionProxy::ClearValue('_ARRAY_PHOTO_');
		}

		$arrayPhoto = ImageDAL::GetResult(array('id_image_group' => 5, 'size' => 1), null, 'position ASC');
//Utils::ArrayDisplay($arrayPhoto);
		$this->smarty->assign('arrayPhoto', $arrayPhoto);
	}

	public function FormAction($param) {
		$this->SetAjaxRender();
		$this->addScript('jQuery/jquery-1.4.2.js', 'file', 'top', 1);
		$this->addScript('imageUpload.js', 'file', 'top', 3);
		$this->addScript('ajaxupload.3.5.js', 'file', 'top', 2);
	}

	public function DeleteImageAction($param) {
		$this->SetAjaxRender();
		if ($_POST['photo']) {
			$obj = ImageDAL::GetResult(array('id_mf_image' => $_POST['photo']));

			if (isset($obj[0])) {
				ImageDAL::Delete($obj[0]);
			}
		}

		$this->smarty->assign('photo', 'image_' . $_POST['photo']);
	}

	public function DeleteImageBannerAction($param) {
//$this->SetAjaxRender();
		if ($param['photo']) {
			$obj = ImageDAL::GetResult(array('id_mf_image' => $param['photo']));

			if (isset($obj[0])) {
				ImageDAL::Delete($obj[0]);
			}
		}
		$this->AddRedirect(Router::GenerateUrl('ReloadImage', array('Image' => 'UploadImage')), 0);
//$this->smarty->assign('photo', 'image_'.$_POST['photo']);
	}

	public function SortImageAction($param) {
		$this->SetNoRender();
//Utils::ArrayDisplay($_POST);
		if ($_POST['sort']) {
			$arraySort = $_POST['sort'];
			foreach ($arraySort as $id => $sort) {
//$obj = ImageDAL::GetResult(array('id_mf_image' => $id));
				ImageDAL::UpdateSort($id, $sort);
			}
		}
		$this->AddRedirect(Router::GenerateUrl('ReloadImage', array('Image' => 'UploadImage')), 0);
	}

	public static function ImportProductAction() {
		
	}

	/**
	 * preDispatch
	 * @param array $param
	 * @return null
	 */
	public function preDispatch($param) {

//$this->AddScript("jQuery/jquery-1.4.2.js");
//$this->AddScript("ajaxupload.3.5.js");
//$this->AddScript("UploadImage.js");

		$this->RunShared('Auth', array());
		$this->Run($param);
		$this->smarty->assign('titleAdmin', 'Slideshow');
		$this->smarty->assign('structure', '');
	}

	/**
	 * postDispatch
	 * @param array $param
	 * @return null
	 */
	public function postDispatch($param) {
		
	}

}
