<?php

/**
 * $Id$
 * Pytania
 *
 */
class SimpleArticle_IndexController extends MainController implements ControllerInterface {

	const AVATAR_DEST_DIR = '/upload/Article';
	const AVATAR_TEMP_DIR = '/upload/temp';
	const NO_PHOTO_IMG_BIG = "image/Admin/cropperNoPhotoBig.gif";
	const NO_PHOTO_IMG_SMALL = "image/Admin/przekatne.gif";
	const MAX_AVATAR_FILE_SIZE = 5; //Rozmiar w mb
	const CROPPER_BIG_PHOTO_MAX_WIDTH = 1000;
	const CROPPER_BIG_PHOTO_MAX_HEIGHT = 1000;
	const PHOTO_WIDTH = 315;
	const PHOTO_HEIGHT = 298;
	const IMAGE_MINI_WIDTH = 280;
	const IMAGE_MINI_HEIGHT = 165;
	const IMAGE_NORMAL_WIDTH = 280;
	const IMAGE_NORMAL_HEIGHT = 165;
	const IMAGE_NORMAL_WIDTH_3 = 100;
	const IMAGE_NORMAL_HEIGHT_3 = 100;
	const CONTENT_PER_PAGE = 50;

	/**
	 * Domyślna metoda
	 *
	 */
	public function IndexAction($param) {
		SessionProxy::ClearValue('_ARRAY_PHOTO_');
		//$this->SetAjaxRender();
		//		$logger = LoggerManager::getLogger(__CLASS__);
		//
		//		if (isset($param['i'])) {
		//			$this->smarty->assign("deleteInfo", "To pytanie jest wykorzystywane!");
		//		}
		//		NARAZIE WYSZUKIWANIA NIE MA
		//		//WYSZUKIWANIE
		//		if (isset($_REQUEST["searchString"])) {
		//			SessionProxy::SetValue('request', $_REQUEST);
		//		} else if (!(isset($param["page"]))) {
		//			if (!(isset($param["sd"])) || !(isset($param["sc"]))) {
		//				SessionProxy::ClearValue('request');
		//			}
		//		}
		//
		//		$request = SessionProxy::GetValue('request');
		//		if (isset($request["clear"])) {
		//			$request = SessionProxy::ClearValue('request');
		//		}
		//
		//		if (isset($request["searchString"])) {
		//			$searchString = trim($request["searchString"]);
		//			$param['strona'] = 0;
		//		} else {
		//			$searchString = "";
		//		}
		//
		//		$this->smarty->assign('searchString', $searchString);
		//		$searchString = trim($request["searchString"]);
		//		var_dump($searchString);

//		$dalData = SimpleArticle_MfArticleDAL::GetDalDataObj();
//		$dalData->setCondition(array('mf_article.id_mf_article' => $id, 'lang' => SessionProxy::GetValue('lang')));
//		$dalData->setJoin(array('SimpleArticle_MfArticleDescription' => ' LEFT JOIN mf_article_description ON mf_article.id_mf_article=mf_article_description.id_mf_article'));
//		$dalData->setCount(true);
//		$limit = Utils::PageConfigure($this->smarty, $param, ceil(SimpleArticle_MfArticleDAL::GetResult($dalData)), self::CONTENT_PER_PAGE);
//		$dalData->setCount(false);
//		$dalData->setLimit($limit);
//		$arrayObjArticle = SimpleArticle_MfArticleDAL::GetResult($dalData);
//
//		//Utils::ArrayDisplay($arrayObjArticle);
//		$this->smarty->assign('arrayObjArticle', $arrayObjArticle);
	}

	/**
	 * Domyślna metoda
	 *
	 */
	public function IndexStructureAction($param) {
		SessionProxy::ClearValue('_ARRAY_PHOTO_');
		$idCategory = SessionProxy::GetValue('idCategory');
		if (!$idCategory) {
			$idCategory = 0;
		}

		if ($idCategory == 2) {
			$idCategory = null;
		}
		$this->smarty->assign('idCategory', $idCategory);
		//Utils::ArrayDisplay($param);
		// Zakomentowne bo teraz sprawdzam tylko czy jest powiązany ze strukturą
		//		$idsContent = MfLinkDAL::GetIdString('mf_article', 'mf_article_category', $idCategory, 1);
		//		$data = array('mf_article.id_mf_article' => array('condition' => 'IN', 'value' => $idsContent));
		if (isset($param['id'])) {
			$data = array('mf_article.id_structure' => $param['id']);
		}

		$data['lang'] = SessionProxy::GetValue('lang');
		//Utils::ArrayDisplay($data);
		$dalData = SimpleArticle_MfArticleDAL::GetDalDataObj();
		$dalData->setCondition($data);
		$dalData->setJoin(array('SimpleArticle_MfArticleDescription' => ' LEFT JOIN mf_article_description ON mf_article.id_mf_article=mf_article_description.id_mf_article'));
		$dalData->setCount(true);
		$limit = Utils::PageConfigure($this->smarty, $param, ceil(SimpleArticle_MfArticleDAL::GetResult($dalData)), self::CONTENT_PER_PAGE);
		$dalData->setCount(false);
		$dalData->setLimit($limit);
		$dalData->setSortBy('date_publication DESC');
		$arrayObjArticle = SimpleArticle_MfArticleDAL::GetResult($dalData);


		if (isset($param['id'])) {
			$this->smarty->assign('idElement', $param['id']);
		}
		//Utils::ArrayDisplay($param);
		$this->smarty->assign('arrayObj', $arrayObjArticle);
		if (isset($param['methodToRun']) && $param['methodToRun'] == 'Add') {
			$this->smarty->assign('add', true);
		}
	}

	public function ValidateForm($param) {
		$validator = new Validator($_POST);

		$validator->IsEmpty('tytul', 'Pole tytuł musi zostać wypełnione.');
		$validator->IsDate('datap', 'Pole data publikacji musi zostać wypełnione.');
		$validator->IsDate('datae', 'Pole data publikacji musi zostać wypełnione.');
		$validator->IsEmpty('zajawka', 'Artykuł musi mieć zajawkę.');
		$validator->IsEmpty('tresc', 'Pole treść nie może zostać niewypełnione.');
	}

	/**
	 * Dodawanie/Edycja artykułu formularz
	 *
	 * @param Array $param
	 */
	public function AddStructureAction($param) {
		$this->smarty->assign('arrayPhoto', array());

		if (Request::GetPost('idModule')) {
			SessionProxy::ClearValue('idContent');
			$objModule = MfModuleDAL::GetById(Request::GetPost('idModule'));
			SessionProxy::SetValue('objModule', $objModule);
		} else {
			$objModule = SessionProxy::GetValue('objModule');
		}
		if (Request::GetPost('idCategory')) {
			$idCategory = Request::GetPost('idCategory');
			SessionProxy::SetValue('idCategory', $idCategory);
		} elseif (SessionProxy::IsSetValue('idCategory')) {
			$idCategory = SessionProxy::GetValue('idCategory');
		}
		$arrayCategory = SimpleArticle_MfArticle::GetCategory();
		$this->smarty->assign('arrayCategory', $arrayCategory);

		if ($objModule->GetMethodName() == 'Index' && !isset($param['text'])) {
			$this->IndexStructureAction($param);
			$this->smarty->assign('add', true);
			$this->partialTemplate = "../StructureList.tpl";
		} else {
			//Utils::ArrayDisplay($param);
			//Utils::ArrayDisplay(SessionProxy::GetValue('router'));
			$this->partialTemplate = "../StructureEdit.tpl";
			if (is_object($objModule)) {
				//$router = SessionProxy::GetValue('router');
				//$unserializedParam = unserialize($router->GetParam());
				$lang = SessionProxy::GetValue('lang');

				//Utils::ArrayDisplay($_POST);
				//if (!isset($objArticle)) {

				if (SessionProxy::IsSetValue('idContent')) {
					$objArticle = SimpleArticle_MfArticleDAL::GetById(SessionProxy::GetValue('idContent'), $lang);
				} else {
					$objArticle = SimpleArticle_MfArticleDAL::GetEmptyObj();
				}
				$this->smarty->assign('obj', $objArticle);

				$arrayImage = $objArticle->getArrayImage();
				$this->smarty->assign('arrayImage', $arrayImage);

				//Utils::ArrayDisplay($objArticle);
				//}
				//			$this->partialTemplate = 'Edit.tpl';
				//			if (is_object($objModule)) {
				//			}

				if (Request::GetPost('SaveArt')) {


					$validator = new Validator(Request::GetAllPost());
					$data = Request::GetAllPost();

					//$validator -> IsEmpty('description','To pole nie może być puste');
					//$validator -> IsEmpty('datepublication','To pole nie może być puste');
					$param['submitForm'] = 'walidacja';
					$out = $validator->GetErrorList();
					//Utils::ArrayDisplay($out);

					$publication = Request::Get('publication');
					$publication ? $publication = 1 : $publication = '0';


					$objArticle->SetDate(Utils::GetNowDate());
					$objArticle->SetDatePublication("0000-00-00 00:00:00");
					$objArticle->SetDateShown("0000-00-00 00:00:00");
					$objArticle->SetWeight(10);
					$objArticle->SetPublication($publication);
					$objArticle->SetType(1);
					$objArticle->SetAddDate(Utils::GetNowDate());
					$objArticle->SetEditDate('0000-00-00 00:00:00');

					//$iid = SimpleArticle_MfArticleDAL::Save($objArticle);
					$objArticleDescription = $objArticle->GetDescriptionObj();
					$objArticleDescription->SetLang($lang);
					if (isset($data['description'])) {
						$objArticleDescription->SetDescription($data['description']);
					}
					$objArticleDescription->SetShortnote($data['shortnote']);
					$objArticleDescription->SetTitle($data['title']);
					$objArticleDescription->SetBrowserTitle($data['title']);
					$objArticleDescription->SetPublication($publication);

					$objArticleDescription->SetIdMfArticle(-1);

					$objArticle->SetArticleDescriptionObj($objArticleDescription);

					if (empty($out)) {



						SessionProxy::SetValue('ModuleValidator', true);

						try {
							$idArticle = SimpleArticle_MfArticleDAL::Save($objArticle);
							$objArticleDescription->SetIdMfArticle($idArticle);
							SimpleArticle_MfArticleDescriptionDAL::Save($objArticleDescription);
							SessionProxy::SetValue('idContent', $idArticle);


							if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
								$idImageGroup = ImageDAL::SaveImage($idArticle, 'Article', $objArticle->getIdImageGroup());
								if ($idImageGroup) {
									$objArticle = SimpleArticle_MfArticleDAL::GetById($idArticle, $lang);
									$objArticle->setIdImageGroup($idImageGroup);
									SimpleArticle_MfArticleDAL::Save($objArticle);
								}
								SessionProxy::ClearValue('_ARRAY_PHOTO_');
							}

							//zapisanie kategorii
							//MfLinkDAL::DeleteAllLink($idArticle,'mf_article', 'mf_article_category');

							$objLink = new MfLink('-1');
							$objLink->SetSourceType('mf_article');
							$objLink->SetIdSource($idArticle);
							$objLink->SetDestinationType('mf_article_category');
							$objLink->SetIdDestination(1);
							$objLink->SetLinkType(1);
							MfLinkDAL::Save($objLink);
						} catch (Exception $e) {
							Utils::ArrayDisplay($e);
						}
						//koniec
					} else {

						if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
							$arrayFiles = SessionProxy::GetValue('_ARRAY_PHOTO_');
							$this->smarty->assign('arrayPhoto', $arrayFiles);
						}


						$this->smarty->assign('objArticle', $objArticle);
						$datePublished = explode(" ", $objArticle->GetDatePublication());

						$this->smarty->assign("datePublished", $datePublished[0]);
						$this->smarty->assign("timePublished", $datePublished[1]);

						SessionProxy::SetValue('ModuleValidator', false);
						foreach ($out as $item) {
							$error[$item['field']] = $item['msg'];
						}
						$this->smarty->assign('error', $error);
					}
				}
			}
		}
	}

	/**
	 * Dodawanie/Edycja artykułu formularz
	 *
	 * @param Array $param
	 */
	public function EditStructureAction($param) {
		$arrayCategory = SimpleArticle_MfArticle::GetCategory();
		$this->smarty->assign('arrayCategory', $arrayCategory);
		$objModule = SessionProxy::GetValue('objModule');
		if (isset($param['idElement'])) {
			$this->smarty->assign('idElement', $param['idElement']);
		}
		$idCategory = SessionProxy::GetValue('idCategory');
		if (!$idCategory) {
			$idCategory = 0;
		}
		//Utils::ArrayDisplay($param);
		if (!is_object($objModule)) {
			$url = Router::GenerateUrl('IndexStructure', array('Structure' => 'Index'));

			$this->AddRedirect($url, 0);
			Utils::Redirect($url);
		}
		if ($objModule->GetMethodName() == 'Index' && !isset($param['text'])) {
			$this->IndexStructureAction($param);
			//$this->smarty->assign('add', true);
			//$this->partialTemplate = "../StructureList.tpl";
		} else {
			//Utils::ArrayDisplay('sdds');
			//$this->partialTemplate = "../StructureEdit.tpl";
			//$this->SetAjaxRender();
			//Utils::ArrayDisplay('art');
			//Utils::ArrayDisplay($_SESSION);
			//Utils::ArrayDisplay($_POST);

			$router = SessionProxy::GetValue('router');
			$unserializedParam = unserialize($router->GetParam());
			$lang = SessionProxy::GetValue('lang');
			$objElement = SessionProxy::GetValue('objElement');
			//Utils::ArrayDisplay($_SESSION);
			//Utils::ArrayDisplay($unserializedParam);
			if (is_object($objModule)) {

				//if (!isset($objArticle)) {
				if ($unserializedParam['id'] > 0) {
					$objArticle = SimpleArticle_MfArticleDAL::GetById($unserializedParam['id'], $lang);
				} else {
					$objArticle = SimpleArticle_MfArticleDAL::GetEmptyObj();
				}

				$arrayImage = $objArticle->getArrayImage();
				$this->smarty->assign('arrayImage', $arrayImage);


				$this->smarty->assign('obj', $objArticle);
				//Utils::ArrayDisplay($objArticle);
				//}
				//			$this->partialTemplate = 'Edit.tpl';
				//			if (is_object($objModule)) {
				//$this->partialTemplate = "../StructureEdit.tpl";
				//			}


				if (Request::GetPost('SaveArt')) {


					$validator = new Validator(Request::GetAllPost());
					$data = Request::GetAllPost();

					$validator->IsEmpty('description', 'To pole nie może być puste');
					//$validator -> IsEmpty('datepublication','To pole nie może być puste');
					$param['submitForm'] = 'walidacja';
					$out = $validator->GetErrorList();
					//Utils::ArrayDisplay($out);

					$publication = Request::Get('publication');
					$publication ? $publication = 1 : $publication = '0';


					$objArticle->SetDate(Utils::GetNowDate());
					$objArticle->SetDatePublication("0000-00-00 00:00:00");
					$objArticle->SetDateShown("0000-00-00 00:00:00");
					$objArticle->SetWeight(10);
					$objArticle->SetPublication($publication);
					$objArticle->SetType(1);
					$objArticle->SetAddDate(Utils::GetNowDate());
					$objArticle->SetEditDate('0000-00-00 00:00:00');

					//$objArticle = new SimpleArticle_MfArticle()
					//$iid = SimpleArticle_MfArticleDAL::Save($objArticle);
					$objArticleDescription = $objArticle->GetDescriptionObj();
					$objArticleDescription->SetLang($lang);
					$objArticleDescription->SetDescription($data['description']);
					$objArticleDescription->SetShortnote($data['shortnote']);
					$objArticleDescription->SetTitle($data['title']);
					$objArticleDescription->SetBrowserTitle($data['title']);
					$objArticleDescription->SetPublication($publication);

					$objArticleDescription->SetIdMfArticle(-1);

					$objArticle->SetArticleDescriptionObj($objArticleDescription);

					if (empty($out)) {

						SessionProxy::SetValue('ModuleValidator', true);


						$idArticle = SimpleArticle_MfArticleDAL::Save($objArticle);
						//Utils::ArrayDisplay('-'.$idArticle);
						$objArticleDescription->SetIdMfArticle($idArticle);
						SimpleArticle_MfArticleDescriptionDAL::Save($objArticleDescription);
						//Utils::ArrayDisplay('art: '.SessionProxy::GetValue('idContent'));
						SessionProxy::SetValue('idContent', $idArticle);


						if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
							$idImageGroup = ImageDAL::SaveImage($idArticle, 'Article');
							if ($idImageGroup) {
								$objArticle = SimpleArticle_MfArticleDAL::GetById($idArticle, $lang);
								$objArticle->setIdImageGroup($idImageGroup);
								SimpleArticle_MfArticleDAL::Save($objArticle);
							}
						}
					} else {

						if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
							$arrayFiles = SessionProxy::GetValue('_ARRAY_PHOTO_');
							$this->smarty->assign('arrayPhoto', $arrayFiles);
						}

						$this->smarty->assign('obj', $objArticle);
						$datePublished = explode(" ", $objArticle->GetDatePublication());

						$this->smarty->assign("datePublished", $datePublished[0]);
						$this->smarty->assign("timePublished", $datePublished[1]);

						SessionProxy::SetValue('ModuleValidator', false);
						foreach ($out as $item) {
							$error[$item['field']] = $item['msg'];
						}
						$this->smarty->assign('error', $error);
					}
				}
			}
		}
	}

	/**
	 * Edycja artykułu
	 * 
	 * @param Array $param
	 */
	public function _EditAction($param) {
		//$this->SetAjaxRender();
		$objArticle = SimpleArticle_MfArticleDAL::GetById($param['id']);

		if (is_object($objArticle)) {
			$this->smarty->assign('objArticle', $objArticle);
		}

		if (isset($param['idElement'])) {
			$this->smarty->assign('idElement', $param['idElement']);
		}

		//$this->AddAction($param);
		$this->partialTemplate = 'Edit.tpl';
	}

	/**
	 * Akcja dodawania 
	 *
	 * @param <type> $param
	 */
	public function AddAction($param) {
		//Utils::ArrayDisplay($_SESSION);
		//Utils::ArrayDisplay($param);

		$objModule = SessionProxy::GetValue('objModule');
		$this->smarty->assign('idModule', $objModule->GetId());


		$arrayCategory = SimpleArticle_MfArticle::GetCategory();
		$this->smarty->assign('arrayCategory', $arrayCategory);
		$this->smarty->assign('arrayPhoto', array());
		$this->smarty->assign("arrayCustomerLink", array());
		$this->AddScript('autocompleter.js');
		$lang = SessionProxy::GetValue('lang');
		$idStructure = SessionProxy::GetValue('idStructure');
		if (!$idStructure) {
			$this->AddRedirect(Router::GenerateUrl('IndexStructure', array('Structure' => 'Index')), 0);
		}
		$this->smarty->assign('idStructure', $idStructure);
		$idCategory = SessionProxy::GetValue('idCategory');
		$this->smarty->assign('idCategory', $idCategory);
		//if (isset($_POST['idArticle']) && $_POST['idArticle'] != -1) {
		//$objArticle = SimpleArticle_MfArticleDAL::GetById($_POST['idArticle']);
		//}

		$objArticle = SimpleArticle_MfArticleDAL::GetEmptyObj();


		if (Request::IsPost()) {
			$out = array();
			$validator = new Validator(Request::GetAllPost());
			$data = Request::GetAllPost();

			$validator->IsEmpty('tytul', 'Pole tytuł musi zostać wypełnione.');
			//$validator->IsDate('dateshown', 'Pole data wyświetlania  musi zostać wypełnione.');
			$validator->IsDate('datepublication', 'Pole data publikacji musi zostać wypełnione.');
			//$validator->IsEmpty('tags', 'Pole tagi musi zostać wypełnione.');
			//$validator->IsEmpty('zajawka', 'Artykuł musi mieć zajawkę.');
			//$arrayValidiation = array('tytul','dateshown','datepublication','shortnoteHidden');
			//$this->smarty->assign('arrayValidiation', $arrayValidiation );
			//$param['submitForm'] = 'walidacja';
			$out = $validator->GetErrorList();
			//Utils::ArrayDisplay($out);
			//Utils::ArrayDisplay($_POST);
			//$lang =  SessionProxy::GetValue('lang');
			//MAKL: DODAŁEM stripcslashes dla testów
			//$_POST['trescFromFCK'] = stripcslashes($_POST['trescFromFCK']);



			$publication = Request::Get('publication');
			$publication ? $publication = 1 : $publication = '0';

			//$objArticle = new SimpleArticle_MfArticle();
			$objArticleDescription = $objArticle->GetDescriptionObj();
			//$objArticle->SetArticleDescriptionObj($objArticleDescription);
			//$photoId = Request::GetCookie('photoFromUploader');
			//				if ($photoId) {
			//					$objArticle->SetIdPicture($photoId);
			//				}

			if (isset($_POST['saveAndPublish']) && $_POST['saveAndPublish']) {
				$objArticleDescription->SetPublication($publication);
			}


			//				var_dump($objArticleDescription->GetId());

			$objArticle->SetDate(date('Y-m-d'));
			$objArticle->SetDatePublication($data['datepublication'] . " " . $data['timepublication'] . ":00:00");
			$objArticle->SetDateShown(Utils::GetNowDate());
			$objArticle->SetWeight(0);
			$objArticle->SetPublication($publication);
			$objArticle->SetType($idCategory);
			$objArticle->SetDateUpdate(Utils::GetNowDate());
			$objArticle->SetAddDate(Utils::GetNowDate());
			$objArticle->SetEditDate(Utils::GetNowDate());
			$objArticle->SetUrl($_POST['url']);
			$objArticle->SetIdStructure($idStructure);
			$objArticle->SetIdCategory(isset($data['idCategoryList']) ? $data['idCategoryList'] : 0);



			$objArticleDescription->SetLang($lang);
			$objArticleDescription->SetDescription($data['tresc']);
			$objArticleDescription->SetShortnote($data['zajawka']);
			$objArticleDescription->SetTitle($data['tytul']);
			$objArticleDescription->SetBrowserTitle($data['tytul']);
			$objArticleDescription->SetPublication($publication);


			//$objArticleDescription->SetPublication($publication);
			if (empty($out)) {




				if ($_FILES['filename']['tmp_name']) {

					$photoSize = getimagesize($_FILES['filename']['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['filename'], self::AVATAR_TEMP_DIR . DIRECTORY_SEPARATOR, $photoWidth, $photoHeight, 100);



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


					$propW = $photoWidth / self::IMAGE_NORMAL_WIDTH;
					$propH = $photoHeight / self::IMAGE_NORMAL_HEIGHT;

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

					if ($idCategory == 3) {
						$photo = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Article', 0, 0, $photoWidth, $photoHeight, self::IMAGE_NORMAL_WIDTH_3, $photoWidth / $prop);
					} else {
						$photo = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Article', 0, 0, self::IMAGE_NORMAL_WIDTH * $prop, self::IMAGE_NORMAL_HEIGHT * $prop, self::IMAGE_NORMAL_WIDTH, self::IMAGE_NORMAL_HEIGHT);
					}


					$objPhoto = new Picture();
					$objPhoto->SetLink($photo);
					$objPhoto->SetWeight(1);
					$objPhoto->SetPublication(1);
					$idPhoto = PictureDAL::Insert($objPhoto);
					$objArticle->SetIdPicture($idPhoto);
					$destName = md5('photo' . microtime());

					if ($objModule->GetId() == 3) {
						$propW = $photoWidth / 635;
						$propH = $photoHeight / 381;
					} else {
						$propW = $photoWidth / self::IMAGE_MINI_WIDTH;
						$propH = $photoHeight / self::IMAGE_MINI_HEIGHT;
					}
					if ($propW > $propH) {
						$prop = $propH;
					} else {
						$prop = $propW;
					}
					//635 x 381 
					if ($objModule->GetId() == 3) {
						$photoMini = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Article', 0, 0, 635 * $prop, 381 * $prop, 635, 381);
					} else {
						$photoMini = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Article', 0, 0, self::IMAGE_MINI_WIDTH * $prop, self::IMAGE_MINI_HEIGHT * $prop, self::IMAGE_MINI_WIDTH, self::IMAGE_MINI_HEIGHT);
					}
					$objPhoto = new Picture();
					$objPhoto->SetLink($photoMini);
					$objPhoto->SetWeight(1);
					$objPhoto->SetPublication(1);
					$idPhoto = PictureDAL::Insert($objPhoto);
					$objArticle->SetIdPictureMini($idPhoto);
				}

				if ($objModule->GetId() == 3) {
					if ($_FILES['fileLogoName']['tmp_name']) {
						include(Config::Get('PATH_CORE') . '/lib/WideImage/WideImage.php');
						$destDir = 'upload/Article/';
						$ext = Utils::FileExtFromName($_FILES['fileLogoName']['name']);
						$uploadFileName = Utils::FileNameFromName($_FILES['fileLogoName']['name']);
						$destNameOrg = 'org_' . $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
						$destName = $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
						$destNameth = "th_" . $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
						$img = WideImage::loadFromUpload('fileLogoName');
						$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;

						$objPhoto = new Picture();
						$objPhoto->SetLink($destNameOrg);
						$objPhoto->SetWeight(1);
						$objPhoto->SetPublication(1);
						$idPhoto = PictureDAL::Insert($objPhoto);
						$objArticle->SetIdPicture($idPhoto);
					}
				}







				$iid = SimpleArticle_MfArticleDAL::Save($objArticle);
				$objArticleDescription->SetIdMfArticle($iid);

				$objLink = new MfLink('-1');
				$objLink->SetSourceType('mf_article');
				$objLink->SetIdSource($iid);
				$objLink->SetDestinationType('mf_article_category');
				$objLink->SetIdDestination($idCategory);
				$objLink->SetLinkType(1);

				//Utils::ArrayDisplay($objLink);

				MfLinkDAL::Save($objLink);

				SimpleArticle_MfArticleDescriptionDAL::Save($objArticleDescription);

				MfLinkDAL::DeleteFromLink($iid, "mf_article", null, "mf_tag");
				if (isset($data['tags'])) {
					TagDAL::SaveTags($data['tags'], $iid, "mf_article", $publication, $lang);
				}

				MfLinkDAL::DeleteFromLink($iid, "mf_article", null, "mf_tag_2");
				if (isset($data['idCustomer']) && $data['idCustomer']) {
					TagDAL::SaveCustomer($data['idCustomer'], $iid, "mf_article", $publication);
				}


				if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
					$idImageGroup = ImageDAL::SaveImage($iid, 'Article');
					if ($idImageGroup) {
						$objArticle = SimpleArticle_MfArticleDAL::GetById($iid, $lang);
						$objArticle->setIdImageGroup($idImageGroup);
						SimpleArticle_MfArticleDAL::Save($objArticle);
					}
				}

				$this->smarty->assign('iid', $iid);
				$this->AddRedirect(Router::GenerateUrl('editStructure', array('id' => $idStructure)), 0);
			} else {
				//$this->content=$this->FormatAjaxOutput($out, $param);
				//Utils::ArrayDisplay($out);
				if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
					$arrayFiles = SessionProxy::GetValue('_ARRAY_PHOTO_');
					$this->smarty->assign('arrayPhoto', $arrayFiles);
				}

				$objArticle->SetArticleDescriptionObj($objArticleDescription);
				$this->smarty->assign('objArticle', $objArticle);
				$datePublished = explode(" ", $objArticle->GetDatePublication());

				$this->smarty->assign("datePublished", $datePublished[0]);
				$this->smarty->assign("timePublished", $datePublished[1]);
				if (isset($data['idCustomer'])) {
					$this->smarty->assign("arrayCustomerLink", $data['idCustomer']);
				}

				$this->smarty->assign('info', 'Pola obowiązkowe muszą zostać wypełnione.');
				$this->smarty->assign('type', 'error');
				foreach ($out as $item) {
					$error[$item['field']] = $item['msg'];
				}
				$this->smarty->assign('error', $error);
			}
		}
		if ($idCategory != 8) {
			$param['tagType'] = 2;
			$this->RunShared('CustomerLink', $param);
		}
	}

	/**
	 * Akcja Edycji 
	 *
	 * @param <type> $param
	 */
	public function EditAction($param) {

		$objModule = SessionProxy::GetValue('objModule');
		$this->smarty->assign('idModule', $objModule->GetId());

		$arrayCategory = SimpleArticle_MfArticle::GetCategory();
		$this->smarty->assign('arrayCategory', $arrayCategory);
		$lang = SessionProxy::GetValue('lang');
		$idStructure = SessionProxy::GetValue('idStructure');
		$this->smarty->assign('idStructure', $idStructure);
		$idCategory = SessionProxy::GetValue('idCategory');
		$this->smarty->assign('idCategory', $idCategory);
		$this->smarty->assign('param', $param);
		//Utils::ArrayDisplay($this->smarty);
		//if (isset($_POST['idArticle']) && $_POST['idArticle'] != -1) {
		//$objArticle = SimpleArticle_MfArticleDAL::GetById($_POST['idArticle']);
		//}
		//Utils::ArrayDisplay($param);
		$objArticle = SimpleArticle_MfArticleDAL::GetById($param['idArticle'], $lang);
		//$this->smarty->assign('tags', $objArticle->getStringTag());
//		$arrayCustomerLink = $objArticle->getCustomerArrayId();
//		//Utils::ArrayDisplay($objArticle);
//		$this->smarty->assign("arrayCustomerLink", $arrayCustomerLink);
		$arrayImage = $objArticle->getArrayImage();
		//Utils::ArrayDisplay($arrayImage);
		$this->smarty->assign('arrayImage', $arrayImage);

		if (Request::GetPost('doSimpleArticleEdit')) {
			$out = array();
			$validator = new Validator(Request::GetAllPost());
			$data = Request::GetAllPost();
			//Utils::ArrayDisplay($data);
			$validator->IsEmpty('tytul', 'Pole tytuł musi zostać wypełnione.');
			//$validator->IsDate('dateshown', 'Pole data wyświetlania  musi zostać wypełnione.');
			if (!isset($param['homeTemplate'])) {
				$validator->IsDate('datepublication', 'Pole data publikacji musi zostać wypełnione.');
			}

			//$validator->IsEmpty('tags', 'Pole tagi musi zostać wypełnione.');
			//$validator->IsEmpty('zajawka', 'Artykuł musi mieć zajawkę.');
			//$arrayValidiation = array('tytul','dateshown','datepublication','shortnoteHidden');
			//$this->smarty->assign('arrayValidiation', $arrayValidiation );
			//$param['submitForm'] = 'walidacja';
			$out = $validator->GetErrorList();
			//Utils::ArrayDisplay($out);
			//Utils::ArrayDisplay($_POST);
			//$lang =  SessionProxy::GetValue('lang');
			//MAKL: DODAŁEM stripcslashes dla testów
			//$_POST['trescFromFCK'] = stripcslashes($_POST['trescFromFCK']);



			$publication = Request::Get('publication');
			$publication ? $publication = 1 : $publication = '0';

			//$objArticle = new SimpleArticle_MfArticle();
			$objArticleDescription = $objArticle->GetDescriptionObj();
			//$objArticle->SetArticleDescriptionObj($objArticleDescription);
			//$photoId = Request::GetCookie('photoFromUploader');
			//				if ($photoId) {
			//					$objArticle->SetIdPicture($photoId);
			//				}

			if (isset($_POST['saveAndPublish']) && $_POST['saveAndPublish']) {
				$objArticleDescription->SetPublication($publication);
			}


			//				var_dump($objArticleDescription->GetId());

			$objArticle->SetId($param['idArticle']);
			$objArticle->SetDate(date('Y-m-d'));
			$objArticle->SetDatePublication($data['datepublication'] . " " . $data['timepublication'] . ":00:00");
			$objArticle->SetDateShown(Utils::GetNowDate());
			$objArticle->SetWeight(0);
			$objArticle->SetPublication($publication);
			$objArticle->SetType($idCategory);
			$objArticle->SetDateUpdate(Utils::GetNowDate());
			$objArticle->SetAddDate(Utils::GetNowDate());
			$objArticle->SetEditDate(Utils::GetNowDate());
			$objArticle->SetIdStructure($idStructure);
			$objArticle->SetUrl($_POST['url']);
			if (isset($data['idCategoryList'])) {
				$objArticle->SetIdCategory($data['idCategoryList']);
			}
			//$objArticle->SetIdCategory($data['idCategoryList']);



			$objArticleDescription->SetLang($lang);
			$objArticleDescription->SetDescription($data['tresc']);
			$objArticleDescription->SetShortnote($data['zajawka']);
			$objArticleDescription->SetTitle($data['tytul']);
			$objArticleDescription->SetBrowserTitle($data['tytul']);
			$objArticleDescription->SetPublication($publication);


			//$objArticleDescription->SetPublication($publication);
			if (empty($out)) {

				if ($_FILES['filename']['tmp_name']) {

					$photoSize = getimagesize($_FILES['filename']['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['filename'], self::AVATAR_TEMP_DIR . DIRECTORY_SEPARATOR, $photoWidth, $photoHeight, 100);



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


					$propW = $photoWidth / self::IMAGE_NORMAL_WIDTH;
					$propH = $photoHeight / self::IMAGE_NORMAL_HEIGHT;

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

					if ($idCategory == 3) {
						$photo = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Article', 0, 0, $photoWidth, $photoHeight, self::IMAGE_NORMAL_WIDTH_3, $photoWidth / $prop);
					} else {
						$photo = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Article', 0, 0, self::IMAGE_NORMAL_WIDTH * $prop, self::IMAGE_NORMAL_HEIGHT * $prop, self::IMAGE_NORMAL_WIDTH, self::IMAGE_NORMAL_HEIGHT);
					}

					//Utils::ArrayDisplay('foto: '.$photo);
					$objPhoto = new Picture();
					$objPhoto->SetLink($photo);
					$objPhoto->SetWeight(1);
					$objPhoto->SetPublication(1);
					$idPhoto = PictureDAL::Insert($objPhoto);
					if ($objModule->GetId() != 3) {
						$objArticle->SetIdPicture($idPhoto);
					}
					$destName = md5('photo' . microtime());

					if ($objModule->GetId() == 3) {
						$propW = $photoWidth / 635;
						$propH = $photoHeight / 381;
					} else {
						$propW = $photoWidth / self::IMAGE_MINI_WIDTH;
						$propH = $photoHeight / self::IMAGE_MINI_HEIGHT;
					}

					if ($propW > $propH) {
						$prop = $propH;
					} else {
						$prop = $propW;
					}
					//635 x 381 
					if ($objModule->GetId() == 3) {
						$photoMini = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Article', 0, 0, 635 * $prop, 381 * $prop, 635, 381);
					} else {
						$photoMini = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Article', 0, 0, self::IMAGE_MINI_WIDTH * $prop, self::IMAGE_MINI_HEIGHT * $prop, self::IMAGE_MINI_WIDTH, self::IMAGE_MINI_HEIGHT);
					}
					//Utils::ArrayDisplay('fotoMini: '.$photoMini);
					$objPhoto = new Picture();
					$objPhoto->SetLink($photoMini);
					$objPhoto->SetWeight(1);
					$objPhoto->SetPublication(1);
					$idPhoto = PictureDAL::Insert($objPhoto);
					$objArticle->SetIdPictureMini($idPhoto);
				}



				if ($objModule->GetId() == 3) {
					if ($_FILES['fileLogoName']['tmp_name']) {
						include(Config::Get('PATH_CORE') . '/lib/WideImage/WideImage.php');
						$destDir = 'upload/Article/';
						$ext = Utils::FileExtFromName($_FILES['fileLogoName']['name']);
						$uploadFileName = Utils::FileNameFromName($_FILES['fileLogoName']['name']);
						$destNameOrg = 'org_' . $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
						$destName = $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
						$destNameth = "th_" . $uploadFileName . '_' . md5('photo' . time()) . '.jpg';
						$img = WideImage::loadFromUpload('fileLogoName');
						$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;

						$objPhoto = new Picture();
						$objPhoto->SetLink($destNameOrg);
						$objPhoto->SetWeight(1);
						$objPhoto->SetPublication(1);
						$idPhoto = PictureDAL::Insert($objPhoto);
						$objArticle->SetIdPicture($idPhoto);
					}
				}
				if ($objModule->GetId() == 3) {
					if (isset($data['deletePhoto2'])) {

						$objArticle->SetIdPicture(0);
					}
					if (isset($data['deletePhoto'])) {
						$objArticle->SetIdPictureMini(0);
					}
				} else {
					if (isset($data['deletePhoto'])) {
						$objArticle->SetIdPictureMini(0);
						$objArticle->SetIdPicture(0);
					}
				}


				$iid = SimpleArticle_MfArticleDAL::Save($objArticle);
				$objArticleDescription->SetIdMfArticle($iid);


				MfLinkDAL::DeleteAllLink($iid, 'mf_article', 'mf_article_category');
				$objLink = new MfLink('-1');
				$objLink->SetSourceType('mf_article');
				$objLink->SetIdSource($iid);
				$objLink->SetDestinationType('mf_article_category');
				$objLink->SetIdDestination($idCategory);
				$objLink->SetLinkType(1);

				//Utils::ArrayDisplay($objArticle);

				MfLinkDAL::Save($objLink);

				SimpleArticle_MfArticleDescriptionDAL::Save($objArticleDescription);

				MfLinkDAL::DeleteFromLink($iid, "mf_article", null, "mf_tag");
				if (isset($data['tags'])) {
					TagDAL::SaveTags($data['tags'], $iid, "mf_article", $publication, $lang);
				}


				$this->smarty->assign('iid', $iid);

				MfLinkDAL::DeleteFromLink($iid, "mf_article", null, "mf_tag_2");
				if (isset($data['idCustomer']) && $data['idCustomer']) {
					TagDAL::SaveCustomer($data['idCustomer'], $iid, "mf_article", $publication);
				}
				//Utils::ArrayDisplay($data);
				if (isset($data['image_description_old'])) {
//					Utils::ArrayDisplay($p_array);
					foreach ($data['image_description_old'] as $idImage => $descriptionImage) {
						//Utils::ArrayDisplay($idImage);
						$objImage = ImageDAL::GetResult(array('id_mf_image' => $idImage));
						//Utils::ArrayDisplay($objImage);
						if (isset($objImage[0]) && is_object($objImage[0])) {
							$objImage[0]->SetDescription($descriptionImage);
							//Utils::ArrayDisplay($objImage[0]);
							ImageDAL::Update($objImage[0]);
						}
					}
				}

				if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
					$idImageGroup = ImageDAL::SaveImage($iid, 'Article', $objArticle->getIdImageGroup());
					if ($idImageGroup) {
						$objArticle = SimpleArticle_MfArticleDAL::GetById($iid, $lang);
						$objArticle->setIdImageGroup($idImageGroup);
						SimpleArticle_MfArticleDAL::Save($objArticle);
					}
				}

				//Utils::ArrayDisplay('fff'.$idStructure);
				if (isset($param['homeTemplate'])) {
					$this->AddRedirect(Router::GenerateUrl('editPrivate', array('SimpleArticle_Index' => 'Edit', 'idArticle' => $iid)), 0);
				} else {
					$this->AddRedirect(Router::GenerateUrl('editStructure', array('id' => $idStructure)), 0);
				}
			} else {
				//$this->content=$this->FormatAjaxOutput($out, $param);
				//Utils::ArrayDisplay($out);

				if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
					$arrayFiles = SessionProxy::GetValue('_ARRAY_PHOTO_');
					$this->smarty->assign('arrayPhoto', $arrayFiles);
				}

				$objArticle->SetArticleDescriptionObj($objArticleDescription);
				$this->smarty->assign('objArticle', $objArticle);
				$datePublished = explode(" ", $objArticle->GetDatePublication());

				$this->smarty->assign("datePublished", $datePublished[0]);
				$this->smarty->assign("timePublished", $datePublished[1]);
				if (isset($data['idCustomer'])) {
					$this->smarty->assign("arrayCustomerLink", $data['idCustomer']);
				}

				$this->smarty->assign('info', 'Pola obowiązkowe muszą zostać wypełnione.');
				$this->smarty->assign('type', 'error');

				foreach ($out as $item) {
					$error[$item['field']] = $item['msg'];
				}
				$this->smarty->assign('error', $error);
			}
		}
		if ($idCategory != 8) {
			$param['tagType'] = 2;
			$this->RunShared('CustomerLink', $param);
		}

		if (isset($param['homeTemplate'])) {
			$this->partialTemplate = 'EditPriv.tpl';
			$this->smarty->assign('titleAdmin', 'Strona główna');
		}
		$this->smarty->assign('objArticle', $objArticle);
	}

	//$this->smarty->assign('out', $out);


	public function DeleteAction($param) {
		$lang = SessionProxy::GetValue('lang');
		//$this->SetAjaxRender();
		$objArticle = SimpleArticle_MfArticleDAL::GetById($param['idArticle'], $lang);

		SimpleArticle_MfArticleDAL::Delete($objArticle);

		MfLinkDAL::DeleteFromLink($param['idArticle'], "mf_article", null, "mf_tag");

		MfLinkDAL::DeleteFromLink($param['idArticle'], "mf_article", null, "mf_tag_2");
		//TagDAL::ReCountAllTags();

		$this->smarty->assign('idElement', $param['idElement']);

		$this->AddRedirect(Router::GenerateUrl('editStructure', array('id' => $param['idElement'])), 0);
	}

	/**
	 * Sprawdzanie unikalno�ci 
	 *
	 * @param array $param
	 */
	public function AjaxCheckUniqueAction($param) {

		header('Content-Type: text/html; charset=iso-8859-2');

		if ($_POST['value']) {
			$value = $_POST['value'];
		} else {
			$value = "";
		}
		if ($_POST['column']) {
			$column = $_POST['column'];
		} else {
			$column = "";
		}

		//Utils::ArrayDisplay($value);
		$unigue = Utils::CheckUnique('wp_news', $column, $value);
		if (!$unigue) {
			$this->smarty->assign("uniqueInfo", "1");
		}
		$this->smarty->assign("column", $column);
		$template = 'clean.tpl';
		Registry::Remove('smartyTemplate');
		Registry::Set('smartyTemplate', $template);
	}

	public function AjaxPhotoCropperAction($param) {
		$this->SetAjaxRender();

		$photoPath = Request::GetPost('photoPath');
		$photoHeight = Request::GetPost('photoHeight');
		$photoWidth = Request::GetPost('photoWidth');

		$this->smarty->assign('photoWidth', $photoWidth);
		$this->smarty->assign('photoHeight', $photoHeight);

		$this->smarty->assign('minPhotoWidth', self::PHOTO_WIDTH);
		$this->smarty->assign('minPhotoHeight', self::PHOTO_HEIGHT);

		$this->smarty->assign('noPhotoImgBig', URL_STATIC_CONTENT . '/' . self::NO_PHOTO_IMG_BIG);
		$this->smarty->assign('noPhotoImgSmall', URL_STATIC_CONTENT . '/' . self::NO_PHOTO_IMG_SMALL);

		$this->smarty->assign('photoPath', Request::GetPost('photoPath'));
		$cutUrl = array('SimpleArticle_Index' => 'AjaxPhotoCropped');
		if (isset($param['id'])) {
			$cutUrl['id'] = $param['id'];
		}
		$this->smarty->assign('cutUrl', $cutUrl);
		$this->smarty->assign('uploadUrl', array('SimpleArticle_Index' => 'AjaxPhotoUpload'));
	}

	public function AjaxPhotoUploadAction($param) {
		$this->SetAjaxRender();

		$photoFile = $_FILES['photo']['tmp_name'];

		$photoSize = getimagesize($photoFile);

		if ($photoSize[0] < 180) {
			$error = 'raz';
		} else if ($photoSize[1] < 80) {
			$error = 'dwa';
		} else if (filesize($photoFile) > self::MAX_AVATAR_FILE_SIZE * 1048576) {
			$error = 'trzy';
		}

		if (!MimeType::IsImage($_FILES['photo'])) {
			$error = 'cztery';
		}

		if (isset($error)) {
			$this->smarty->assign('error', $error);
		} else {


			$photoSize = getimagesize($photoFile);
			$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['photo'], self::AVATAR_TEMP_DIR . DIRECTORY_SEPARATOR, $photoWidth, $photoHeight, 100);

			$this->smarty->assign('page2load', Router::GenerateUrl(array('SimpleArticle_Index' => 'AjaxPhotoCropper')));
			$this->smarty->assign('photoFile', $photoFile);
			$this->smarty->assign('photoWidth', (int) $photoWidth);
			$this->smarty->assign('photoHeight', (int) $photoHeight);
			$this->smarty->assign('onFly', (Request::Check('onFly') ? 'true' : 'false'));
			$this->smarty->assign('cropPrefix', Request::GetPost('cropPrefix'));
		}
	}

	public function AjaxPhotoCroppedAction($param) {
		$this->SetAjaxRender();

		$x = Request::Get('x');
		$y = Request::Get('y');
		$w = Request::Get('w');
		$h = Request::Get('h');

		$photoFile = Request::Get('photoFileName');
		$destName = md5('photo' . time());

		$destDir = self::AVATAR_DEST_DIR;

		$photo = PhotoDAL::SaveTempFile($photoFile, $destName, $destDir, $x, $y, $w, $h, 180, 80);

		$objPhoto = new Picture();
		$objPhoto->SetLink($photo);

		$idPhoto = PictureDAL::Insert($objPhoto);

		if (isset($param['id'])) {
			$articleObj = SimpleArticle_MfArticleDAL::GetById($param['id']);

			$articleObj->SetIdPicture($idPhoto);

			SimpleArticle_MfArticleDAL::Save($articleObj);
		} else {
			Request::SetCookie('photoFromUploader', pathinfo($idPhoto, PATHINFO_FILENAME), time() + 3600);
		}

		$this->smarty->assign('photoPath', 'Article/' . $photo);
	}

	public function SpecialAction($param) {
		$this->SetAjaxRender();

		if (isset($_POST['id'])) {
			SimpleArticle_MfArticleDAL::Special($_POST['id'], $_POST['value']);

			if ($_POST['value'] == '1') {
				$info = 'Tak';
			} else {
				$info = 'Nie';
			}
			$this->smarty->assign('info', $info);
		}
	}

	public function AjaxGetTags($param) {
		$this->SetAjaxRender();
		$arrayTag = array();

		$tagSearch = Request::GetPost('tag');
		$arrayTag = explode(",", $tagSearch);
		$count = count($arrayTag);

		$arrayTag[$count];

		//Utils::ArrayDisplay($arrayTag);

		$tag = TagDAL::GetResult(array('tag' => array('value' => "($categoryIds)", 'condition' => 'NOT IN')));
		//Utils::ArrayDisplay($tag);
	}

	/**
	 * Metoda wspolna
	 *
	 */
	public function preDispatch($param) {

		$this->Run($param);

		//Utils::ArrayDisplay($param);
		//		$this->AddScript("tree.js");
		$this->AddScript("prototype.js");
		$this->AddScript('effects.js');
		$this->AddScript('controls.js');
		$this->AddScript('UploadFile.js');
		$this->AddScript('dropDown.js');
		$this->AddScript('structure.js');
		$this->AddScript('Dosia.js');
		$this->AddScript('Link.js');
		$this->AddScript('drag-drop-folder-tree.js');
		//		//$this->AddScript('Validator.js');
		$this->AddScript('calendar.js');

		$this->RunShared('Auth', array());

		$this->RunShared('Structure', $param);
		$this->smarty->assign('idStucture', SessionProxy::GetValue('idStructure'));
		$this->smarty->assign('titleAdmin', 'Aktualności');
		$this->smarty->assign('timestamp', microtime());
		$this->smarty->assign("uniqueInfo", "");
		$this->smarty->assign("deleteInfo", "");
	}

	/**
	 * 
	 *
	 */
	public function postDispatch($param) {
		
	}

}

?>