<?php
/**
 * Produkty 
 * 
 *
 */
class ProductController extends MainController implements ControllerInterface {


	const AVATAR_DEST_DIR = '/upload/Product';
	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 = 800;
	const CROPPER_BIG_PHOTO_MAX_HEIGHT = 800;
	const PHOTO_WIDTH = 800;
	const PHOTO_HEIGHT = 600;

	const IMAGE_MINI_WIDTH = 150;
	const IMAGE_MINI_HEIGHT = 195;
	const IMAGE_LIST_WIDTH = 90;
	const IMAGE_LIST_HEIGHT = 110;
	const IMAGE_NORMAL_WIDTH = 360;
	const IMAGE_NORMAL_HEIGHT = 440;


	const CONTENT_PER_PAGE = 30;



	public function IndexAction($param) {
		// TODO filtry


		$dalData = MfProductDAL::GetDalDataObj();
		$dalData->setJoin(array('MfProductDescription' => ' LEFT JOIN mf_product_description ON mf_product.id_mf_product=mf_product_description.id_mf_product'));
		$dalData->setCondition(array('lang' => $param['lang']));
		//Utils::ArrayDisplay('ss'.$idsProduct);
		//$dalData->addCondition('   ', 'mf_product.id_mf_product IN ('.$idsProduct.') ', ' ');
		$arrayObj = MfProductDAL::GetResult($dalData);







		$this->smarty->assign('arrayObj', $arrayObj);
	}

	public function IndexStructureAction($param) {


		//$idCategorySelected = SessionProxy::getValue('idCategorySelected');
		//Utils::ArrayDisplay($idCategorySelected);
		//====Pobieranie=Kategorii===========================================================================

		//$arrayStructureProductCategory = MfProductCategoryDAL::GetStructureProductCategory($param['lang']);
		//Utils::ArrayDisplay($arrayStructureProductCategory);
		$dalData = MfProductCategoryDAL::GetDalDataObj();
		$dalData->setJoin(array('MfProductCategoryDescription' => ' LEFT JOIN mf_product_category_description ON mf_product_category.id_mf_product_category=mf_product_category_description.id_mf_product_category '));
		$dalData->setCondition(array('lang' => $param['lang']));
		$dalData->addCondition('mf_product_category.id_parent', 1);
		$arrayStructureProductCategory = MfProductCategoryDAL::GetResult($dalData);

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

		//---------------------------------------------------------------------------------------------------


		//Utils::ArrayDisplay($param);

		$idStructure = SessionProxy::GetValue('idStructure');
		//$idCategory = SessionProxy::GetValue('idCategory');
		$idCategory = $idStructure;
		$this->smarty->assign('idCategory', $idCategory);
		//$this->smarty->assign('idCategorySelected', $idCategorySelected);
		//Utils::ArrayDisplay($idCategory);
		if (!$idCategory) {
			$idCategory = -1;
		}


		//Utils::ArrayDisplay($idsProduct);
		$dalData = MfProductDAL::GetDalDataObj();
		$dalData->setJoin(array('MfProductDescription' => ' LEFT JOIN mf_product_description ON mf_product.id_mf_product=mf_product_description.id_mf_product'));
		$dalData->setCondition(array('lang' => $param['lang']));
		//Utils::ArrayDisplay('ss'.$idsProduct);

		$page = 1;
		SessionProxy::SetValue('__news_page_no__', $page);

		$offset = ($page - 1) * self::CONTENT_PER_PAGE;
		$dalData->setCount(true);
		$limit = Utils::PageConfigure($this->smarty, $param, ceil(MfProductDAL::GetResult($dalData)), self::CONTENT_PER_PAGE);
		$dalData->setCount(false);
		$dalData->setLimit($limit);
		$arrayObj = MfProductDAL::GetResult($dalData);
		$dalData->setSortBy('amount');
		$arrayObj = MfProductDAL::GetResult($dalData);


		$this->smarty->assign('arrayObj', $arrayObj);
		if (isset($param['methodToRun']) && $param['methodToRun'] == 'Add') {
			$this->smarty->assign('add', true);
		}

	}



	public function AjaxListAction($param) {
		$this->SetAjaxRender();
		$dalData = MfProductDAL::GetDalDataObj();
		$dalData->setJoin(array('MfProductDescription' => ' LEFT JOIN mf_product_description ON mf_product.id_mf_product=mf_product_description.id_mf_product'));
		$dalData->setCondition(array('lang' => $param['lang']));

		$search= trim(Request::GetPost('search'));
		$idCategorySelected = trim(Request::GetPost('idCategorySelected'));
		$publication = Request::GetPost('publication');
		$new = Request::GetPost('newList');
		$sale = Request::GetPost('sale');
		$limited = Request::GetPost('limited');
		$arraySearch = explode(' ', $search);
		if (count($arraySearch) > 0) {
			$where = ' ( ';
			foreach ($arraySearch as $key => $search) {
				$where .= $key == 0 ? '' : ' OR ';
				$where .= ' name LIKE "%'.Utils::AddSlashes($search).'%" ';
			}
			$where .= ' ) ';
			$dalData->addCondition(' ', $where, ' ');
		}
		//$dalData->setLimit(self::CONTENT_PER_PAGE);
		$dalData->setSortBy('amount');

		if ($idCategorySelected > 0 ) {

			$idsProduct = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product',$idCategorySelected, $param['lang']);

			$dalData->addCondition('   ', 'mf_product.id_mf_product IN ('.$idsProduct.') ', ' ');
		}
		if ($publication != 'all') {
			$dalData->addCondition('mf_product.publication', $publication);
		}
		if ($new != 'all') {
			$dalData->addCondition('mf_product.new', $new);
		}
		if ($sale != 'all') {
			$dalData->addCondition('mf_product.sale', $sale);
		}
		if ($limited != 'all') {
			$dalData->addCondition('mf_product.limited', $limited);
		}


		$page = 1;

		if(isset($param['p']) && $param['p'] > 0) {
			$page = $param['p'];

		}

		SessionProxy::SetValue('__news_page_no__', $page);

		$offset = ($page - 1) * self::CONTENT_PER_PAGE;
		$param['ajax'] = 'GetTableContent($(this).attr(\'href\'), $(\'#search\').val(), $(\'#category\').val())';
		$this->smarty->assign('ajax',$param['ajax']);
		$dalData->setCount(true);
		try {
			$limit = Utils::PageConfigure($this->smarty, $param, ceil(MfProductDAL::GetResult($dalData)), self::CONTENT_PER_PAGE);
		} catch (Exception $e) {
			Utils::ArrayDisplay($e);
		}
		$dalData->setCount(false);
		$dalData->setLimit($limit);
		$arrayObj = MfProductDAL::GetResult($dalData, false);
		//Utils::ArrayDisplay($arrayObjSpectacle);
		$this->smarty->assign('arrayObj', $arrayObj);
	}


	public function AddAction($param) {

		$arraySeries = Utils::GetArrayList('shop_series', 'id_shop_series', 'name');
		$this->smarty->assign('arraySeries', $arraySeries);

		//Utils::ArrayDisplay($param);
		$this->smarty->assign('arrayPhoto',array());
		$idStructure = SessionProxy::GetValue('idStructure');
		if (!$idStructure) {
			$this->AddRedirect(Router::GenerateUrl('IndexStructure', array('Structure' => 'Index')), 0);
		}
		$this->smarty->assign('idStructure', $idStructure);
		$idCategory = SessionProxy::GetValue('idCategory');
		$idCategory = $idStructure;
		$this->smarty->assign('idCategory', $idCategory);


		$idsProduct = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product',$idCategory, 'pl');
		$idsProductLang = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product',$idCategory, $param['lang']);

		$arrayProduct = Utils::GetArrayList('mf_product_description', 'id_mf_product', 'name', 'pl', ' AND id_mf_product IN ('.$idsProduct.') AND id_mf_product NOT IN ('.$idsProductLang.')  ', 'name');
		$this->smarty->assign('arrayProduct', $arrayProduct);

		//Utils::ArrayDisplay($idsProduct);
		//Utils::ArrayDisplay($idsProductLang);

		//$this->smarty->assign('idStructure', SessionProxy::GetValue('idStructure'));
		//$idsAttribute = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product_attribute', $idCategory, 'pl'); // zlinowane parametry z kategorią
		$dalData = MfProductAttributeDAL::GetDalDataObj();
		$dalData->setCondition(array('lang' => $param['lang']));
		//$dalData->addCondition('   ', 'mf_product_attribute.id_mf_product_attribute IN ('.$idsAttribute.') ', ' '); // /|\
		$dalData->setJoin(array('MfProductAttributeDescription' => ' LEFT JOIN mf_product_attribute_description ON mf_product_attribute.id_mf_product_attribute=mf_product_attribute_description.id_mf_product_attribute'));
		//$dalData->setSortBy(' FIELD(mf_product_attribute.id_mf_product_attribute, '.$idsAttribute.')'); // parametry z kategorii
		$dalData->setSortBy(' mf_product_attribute.weight');
		//Utils::ArrayDisplay($dalData);
		$arrayObjAttribute = MfProductAttributeDAL::GetResult($dalData);
		//$objProductAttribute = MfProductAttributeDAL::GetResultByLink('mf_product_category', $idCategory, 'mf_product_attribute');
		//Utils::ArrayDisplay($arrayObjAttribue);
		$this->smarty->assign('arrayObjAttribute', $arrayObjAttribute);

		//Pobieranie specyfikacji
		$dalData = MfProductSpecificationDAL::GetDalDataObj();
		$dalData->setCondition(array('lang' => $param['lang']));
		$dalData->setSortBy(' mf_product_specification.value');
		$arrayObjProductSpecification = MfProductSpecificationDAL::GetResult($dalData);
		//Utils::ArrayDisplay($arrayObjProductSpecification);
		$this->smarty->assign('arraySpecType', MfProductSpecification::GetTypeName());
		$this->smarty->assign('arrayObjProductSpecification', $arrayObjProductSpecification);


		//====Pobieranie=Kategorii===========================================================================

		//$arrayStructureProductCategory = MfProductCategoryDAL::GetStructureProductCategory($param['lang']);
		$dalData = MfProductCategoryDAL::GetDalDataObj();
		$dalData->setJoin(array('MfProductCategoryDescription' => ' LEFT JOIN mf_product_category_description ON mf_product_category.id_mf_product_category=mf_product_category_description.id_mf_product_category '));
		$dalData->setCondition(array('lang' => $param['lang']));
		$dalData->addCondition('mf_product_category.id_parent', 1);
		$arrayStructureProductCategory = MfProductCategoryDAL::GetResult($dalData);
		//Utils::ArrayDisplay($arrayStructureProductCategory);
		$this->smarty->assign('arrayStructureProductCategory', $arrayStructureProductCategory);

		//---------------------------------------------------------------------------------------------------




		$objProduct = MfProductDAL::GetEmptyObj();

		if (Request::GetPost('doProductAdd')) {
			//Utils::ArrayDisplay(Request::GetAllPost());

			$out = array();
			$data = Request::GetAllPost();

			$data['price'] = str_replace(',','.',$data['price']);
			//$data['price_sale'] = str_replace(',','.',$data['price_sale']);

			$validator  = new Validator($data);

			if ($data['price']) {
				$validator->IsNumber('price', 'Pole cena musi być liczbą.');
			}
			//			if ($data['price_sale']) {
			//				$validator->IsNumber('price_sale', 'Pole cena musi być liczbą.');
			//			}


			$validator->IsEmpty('name', 'Pole tytuł musi zostać wypełnione.');
			//$validator->IsDate('datepublication', 'Pole data publikacji musi zostać wypełnione.');

			$out = $validator->GetErrorList();

			$publication = Request::Get('publication');
			$publication ? $publication = 1 : $publication = '0';
			$sale = Request::Get('sale');
			$sale ? $sale = 1 : $sale = '0';
			$new = Request::Get('new');
			$new ? $new = 1 : $new = '0';
			$available = Request::Get('available');
			//$available ? $available = 1 : $available = '0';

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

			//$objProduct = new MfProduct();
			$datePublic = !$data['datepublication'] ? Utils::GetNowDate() : $data['datepublication'.":00:00"];

			$objProduct->setDate($datePublic);
			$objProduct->setPublication($publication);
			$objProduct->setSale($sale);
			$objProduct->setNew($new);
			$objProduct->SetType($data['type']);
			$objProduct->SetWeight($data['weight']);
			$objProduct->setAvailable($available);
			$objProduct->setAmount($data['amount']);
			$objProduct->setPrice($data['price']);
			//$objProduct->setPriceSale($data['price_sale']);
			$objProduct->setSeries($data['id_series']);
			$objProduct->SetBulb($data['bulb']);
			$objProduct->SetDimension($data['dimension']);
			$objProduct->SetCode($data['code']);

			$objProduct->SetSpecial($special);

			$objProduct->setIdMfProductCategory($data['idProductCategory']);
			$objProductDesc = new MfProductDescription();
			$objProductDesc->setLang($param['lang']);
			$objProductDesc->setName($data['name']);
			$objProductDesc->setShortnote($data['zajawka']);
			$objProductDesc->setDescription($data['tresc']);

			//$arrayObjAttributeWithValue = array();
			foreach ($arrayObjAttribute as $attrKey => $objAttribut ) {
				$objAttribut->setValue($data['attr_value'][$objAttribut->getId()]);
				$arrayObjAttribute[$attrKey] == $objAttribut;
			}
			//Utils::ArrayDisplay($data);
			//die();

			if(empty($out)) {
				try {







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

						$destName = Utils::TextToUrl($_FILES['filename_pdf']['name']);
						$destName = $_FILES['filename_pdf']['name'];
						if(file_exists(PATH_STATIC_CONTENT.'upload/Product/'.$destName)) {
							$destName = $destName;
						}

						move_uploaded_file($_FILES['filename_pdf']['tmp_name'], PATH_STATIC_CONTENT.'upload/Product/'.$destName);
						umask(0000);
						@chmod(PATH_STATIC_CONTENT.'upload/Product/'.$destName, 0777);
						$objFile = MfFileDAL::GetEmptyObj();
						$objFileDescription = $objFile->GetDescriptionObj();

						$objFile->SetDate(Utils::GetNowDate());
						//$objFile->SetDatePublication($data['datepublication']." ".$data['timepublication'].":00:00");
						$objFile->SetDatePublication(Utils::GetNowDate());
						$objFile->SetPublication(1);
						$objFile->SetDateUpdate(Utils::GetNowDate());
						$objFile->SetAddDate(Utils::GetNowDate());
						$objFile->SetEditDate(Utils::GetNowDate());

						$objFileDescription->SetLang('pl');
						$objFileDescription->SetPublication(1);
						$objFile->SetName($destName);
						$idFile = MfFileDAL::Save($objFile);
						$objFileDescription->SetIdMfFile($idFile);

						MfFileDescriptionDAL::Save($objFileDescription);

						$objProductDesc->SetIdFile($idFile);
					}

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


						$destNameOrg = md5('photo' . microtime()).".jpg";
						include(Config::Get('PATH_CORE').'/lib/WideImage/WideImage.php');
						$img = WideImage::loadFromUpload('filename');
						$img->resizeDown('1200','1200')->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destNameOrg, 90);

						$objPhoto = new Picture();
						$objPhoto->SetLink($destNameOrg);
						$objPhoto->SetWeight(1);
						$objPhoto->SetPublication(1);
						$idPhoto = PictureDAL::Insert($objPhoto);
						$objProductDesc->SetIdPictureMax($idPhoto);

						$img = WideImage::loadFromFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destNameOrg);
						//$img = new WideImage();
						$destName = md5('photo' . microtime()).".jpg";
						$img->resizeDown('568','815')->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destName, 90);
						$objPhoto = new Picture();
						$objPhoto->SetLink($destName);
						$objPhoto->SetWeight(1);
						$objPhoto->SetPublication(1);
						$idPhoto = PictureDAL::Insert($objPhoto);
						$objProductDesc->SetIdPicture($idPhoto);
						//$img->resizeDown('90','110')->crop('center', 'center', 90, 110)->saveToFile(PATH_STATIC_CONTENT.$destDir.$destNameth, 90);
						$destNameth = md5('photo' . microtime()).".jpg";
						$img->resizeDown('150','195')->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destNameth, 90);
						$objPhoto = new Picture();
						$objPhoto->SetLink($destNameth);
						$objPhoto->SetWeight(1);
						$objPhoto->SetPublication(1);
						$idPhoto = PictureDAL::Insert($objPhoto);
						$objProductDesc->SetIdPictureMini($idPhoto);

						$destNameth = md5('photo' . microtime()).".jpg";
						$img->resizeDown('90','110')->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destNameth, 90);
						$objPhoto = new Picture();
						$objPhoto->SetLink($destNameth);
						$objPhoto->SetWeight(1);
						$objPhoto->SetPublication(1);
						$idPhoto = PictureDAL::Insert($objPhoto);
						$objProductDesc->SetIdPictureList($idPhoto);
					}
				}
				catch (Exception $e) {

				}


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

				$objImageGroup = new ImageGroup();

				$idImageGroup = ImageGroupDAL::Insert($objImageGroup);
				$objProductDesc->setIdImageGroup($idImageGroup);

				$idProduct = MfProductDAL::Save($objProduct);
				$objProductDesc->setIdMfProduct($idProduct);



				if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
					$idImageGroup = ImageDAL::SaveImage($idProduct, 'Product', $idImageGroup);
					//					if ($idImageGroup) {
					//						$objProductDesc->setIdImageGroup($idImageGroup);
					//					}
				}


				MfProductDescriptionDAL::Save($objProductDesc);

				foreach ($arrayObjAttribute as $attrKey => $objAttribut ) {
					$objAttribureValue = new MfProductAttributeValue();
					$objAttribureValue->setIdMfProduct($idProduct);
					$objAttribureValue->setIdMfProductAttribute($objAttribut->GetId());
					$objAttribureValue->setValue($objAttribut->GetValue());
					$objAttribureValue->setLang($param['lang']);
					MfProductAttributeValueDAL::Save($objAttribureValue);
				}

				//				$objMfProductLink = new MfProductLink();
				//				$objMfProductLink->setDestinationType('mf_product');
				//				$objMfProductLink->setSourceType('mf_product_category');
				//				$objMfProductLink->setIdDestination($idProduct);
				//				$objMfProductLink->setIdSource($idCategory);
				//				$objMfProductLink->setLang($param['lang']);
				//				$objMfProductLink->setPublication(1);
				//
				//				MfProductLinkDAL::Save($objMfProductLink);
				if (isset($data['spec_value'])) {

					foreach ($data['spec_value'] as $idProductSpec => $specVal) {
						$objMfProductLink = new MfProductLink();
						$objMfProductLink->setDestinationType('mf_product');
						$objMfProductLink->setSourceType('mf_product_specyfication');
						$objMfProductLink->setIdDestination($idProduct);
						$objMfProductLink->setIdSource($idProductSpec);
						$objMfProductLink->setLang($param['lang']);
						$objMfProductLink->setPublication(1);

						MfProductLinkDAL::Save($objMfProductLink);
					}

				}

				$objMfProductLink = new MfProductLink();
				$objMfProductLink->setDestinationType('mf_product');
				$objMfProductLink->setSourceType('mf_product_category');
				$objMfProductLink->setIdDestination($idProduct);
				$objMfProductLink->setIdSource($data['idProductCategory']);
				$objMfProductLink->setLang($param['lang']);
				$objMfProductLink->setPublication(1);

				MfProductLinkDAL::Save($objMfProductLink);
				foreach ($data['prod_category_value'] as $idProductCategory => $CatVal) {

					//				Jeśli trzeba linkować również rodzica z automatu

					$objProductCategory = MfProductCategoryDAL::GetById($idProductCategory, $param['lang']);

					if ($objProductCategory->GetIdParent() > 0 && !key_exists($objProductCategory->GetIdParent(), $data['prod_category_value'])) {
						$objMfProductLink = new MfProductLink();
						$objMfProductLink->setDestinationType('mf_product');
						$objMfProductLink->setSourceType('mf_product_category');
						$objMfProductLink->setIdDestination($idProduct);
						$objMfProductLink->setIdSource($objProductCategory->GetIdParent());
						$objMfProductLink->setLang($param['lang']);
						$objMfProductLink->setPublication(1);
						MfProductLinkDAL::Save($objMfProductLink);
					}


					$objMfProductLink = new MfProductLink();
					$objMfProductLink->setDestinationType('mf_product');
					$objMfProductLink->setSourceType('mf_product_category');
					$objMfProductLink->setIdDestination($idProduct);
					$objMfProductLink->setIdSource($idProductCategory);
					$objMfProductLink->setLang($param['lang']);
					$objMfProductLink->setPublication(1);

					MfProductLinkDAL::Save($objMfProductLink);
				}

				$this->AddRedirect(Router::GenerateUrl('editStructure', array('id' => $idStructure)), 0);
			} else {

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

				$objProduct->SetDescriptionObj($objProductDesc);
				$this->smarty->assign('objProduct',$objProduct);
				$datePublished = explode(" ",$objProduct->GetDate());

				$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('arrayObjAttribute', $arrayObjAttribute);
				$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);


			}



		}
	}


	public function EditAction($param) {

		$arraySeries = Utils::GetArrayList('shop_series', 'id_shop_series', 'name');
		$this->smarty->assign('arraySeries', $arraySeries);
		//$param['lang'] = 'en';
		//Utils::ArrayDisplay($param);

		//====Struktura======================================================
		$idStructure = SessionProxy::GetValue('idStructure');
		if (!$idStructure) {
			$this->AddRedirect(Router::GenerateUrl('IndexStructure', array('Structure' => 'Index')), 0);
		}

		$this->smarty->assign('idStructure', $idStructure);
		$idCategory = SessionProxy::GetValue('idCategory');
		$idCategory = $idStructure;
		$this->smarty->assign('idCategory', $idCategory);
		//Utils::ArrayDisplay($idStructure);
		//-------------------------------------------------------------------

		//====Attribute======================================================

		$idsAttribute = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product_attribute', $idCategory, 'pl');
		$dalData = MfProductAttributeDAL::GetDalDataObj();
		$dalData->setCondition(array('lang' => $param['lang']));
		//$dalData->addCondition('   ', 'mf_product_attribute.id_mf_product_attribute IN ('.$idsAttribute.') ', ' ');
		$dalData->setJoin(array('MfProductAttributeDescription' => ' LEFT JOIN mf_product_attribute_description ON mf_product_attribute.id_mf_product_attribute=mf_product_attribute_description.id_mf_product_attribute'));
		$dalData->setSortBy(' FIELD(mf_product_attribute.id_mf_product_attribute, '.$idsAttribute.')');
		$dalData->setSortBy('mf_product_attribute.weight');
		$arrayObjAttribute = MfProductAttributeDAL::GetResult($dalData);
		//--------------------------------------------------------------------

		//====Attribute=Value=================================================
		$dalDataValue = MfProductAttributeValueDAL::GetDalDataObj();
		$dalDataValue->setCondition(array('lang' => $param['lang'], 'id_mf_product' => $param['idProduct']));
		//$dalDataValue->addCondition('   ', 'id_mf_product_attribute IN ('.$idsAttribute.') ', ' ');
		$arrayObjAttributeValue = MfProductAttributeValueDAL::GetResult($dalDataValue);
		//Utils::ArrayDisplay($dalDataValue);
		//Utils::ArrayDisplay($arrayObjAttributeValue);
		$arrayObjAttributeValueTmp = array();
		foreach ($arrayObjAttributeValue as $attrValKey => $objAttributValue ) {
			$arrayObjAttributeValueTmp[$objAttributValue->getIdMfProductAttribute()] = $objAttributValue;
		}
		$arrayObjAttributeValue = $arrayObjAttributeValueTmp;
		foreach ($arrayObjAttribute as $attrKey => $objAttribut ) {
			if (key_exists($objAttribut->getId(), $arrayObjAttributeValue)) {
				$arrayObjAttribute[$attrKey]->setValue($arrayObjAttributeValue[$objAttribut->getId()]->getValue());
			}
		}
		//---------------------------------------------------------------------

		//====Pobieranie specyfikacji========================================================================
		$dalData = MfProductSpecificationDAL::GetDalDataObj();
		$dalData->setCondition(array('lang' => $param['lang']));
		$dalData->setSortBy(' mf_product_specification.value');
		$arrayObjProductSpecification = MfProductSpecificationDAL::GetResult($dalData);
		//Utils::ArrayDisplay($arrayObjProductSpecification);
		$this->smarty->assign('arraySpecType', MfProductSpecification::GetTypeName());
		$this->smarty->assign('arrayObjProductSpecification', $arrayObjProductSpecification);
		//   Pobieranie wartości linked

		$arrayProdSpecIdLinked = MfProductLinkDAL::GetArrayId('mf_product_specyfication', 'mf_product', $param['idProduct'], 'pl');
		$arr = 	array_flip($arrayProdSpecIdLinked);
		//Utils::ArrayDisplay($arr);
		$arrayObjProductSpecificationNew = array();
		foreach ($arrayObjProductSpecification as $objProductSpecification) {
			if (key_exists($objProductSpecification->getId(), $arr)) {
				$objProductSpecification->setChecked(1);
			}
			$arrayObjProductSpecificationNew[] = $objProductSpecification;
		}
		$this->smarty->assign('arrayObjProductSpecification', $arrayObjProductSpecificationNew);


		//---------------------------------------------------------------------------------------------------

		//====Pobieranie=Kategorii===========================================================================
		//		$arrayProdCategoryIdLinked = MfProductLinkDAL::GetArrayId('mf_product_category', 'mf_product', $param['idProduct'], 'pl');
		//		$arrCat = 	array_flip($arrayProdCategoryIdLinked);
		//		//Utils::ArrayDisplay($arr);
		//
		//		$arrayStructureProductCategory = MfProductCategoryDAL::GetStructureProductCategory($param['lang'], $arrCat);

		$dalData = MfProductCategoryDAL::GetDalDataObj();
		$dalData->setJoin(array('MfProductCategoryDescription' => ' LEFT JOIN mf_product_category_description ON mf_product_category.id_mf_product_category=mf_product_category_description.id_mf_product_category '));
		$dalData->setCondition(array('lang' => $param['lang']));
		$dalData->addCondition('mf_product_category.id_parent', 1);
		$arrayStructureProductCategory = MfProductCategoryDAL::GetResult($dalData);
		//Utils::ArrayDisplay($arrayStructureProductCategory);
		$this->smarty->assign('arrayStructureProductCategory', $arrayStructureProductCategory);

		//---------------------------------------------------------------------------------------------------


		//=====Product=========================================================

		$dalData = MfProductAttributeDAL::GetDalDataObj();
		$dalData->setCondition(array('lang' => $param['lang']));
		$dalData->addCondition('   ', 'mf_product_attribute.id_mf_product_attribute IN ('.$idsAttribute.') ', ' ');
		$dalData->setJoin(array('MfProductAttributeDescription' => ' LEFT JOIN mf_product_attribute_description ON mf_product_attribute.id_mf_product_attribute=mf_product_attribute_description.id_mf_product_attribute'));

		//$param['lang'] = 'en';
		$objProduct = MfProductDAL::GetById($param['idProduct'], $param['lang'], false);
		//Utils::ArrayDisplay($objProduct);
		//---------------------------------------------------------------------


		$arrayImage = $objProduct->getArrayImage();
		//Utils::ArrayDisplay($arrayImage);
		$this->smarty->assign('arrayImage', $arrayImage);
		//Utils::ArrayDisplay($arrayObjAttribue);
		$this->smarty->assign('arrayObjAttribute', $arrayObjAttribute);
		$this->smarty->assign('idProduct', $param['idProduct']);
		$objProduct->setPrice(str_replace('.',',',$objProduct->getPrice()));
		$objProduct->setPriceSale(str_replace('.',',',$objProduct->getPriceSale()));
		$this->smarty->assign('objProduct', $objProduct);


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

			$out = array();

			$data = Request::GetAllPost();

			$data['price'] = str_replace(',','.',$data['price']);
			//$data['price_sale'] = str_replace(',','.',$data['price_sale']);

			$validator  = new Validator($data);

			$validator->IsEmpty('name', 'Pole tytuł musi zostać wypełnione.');
			$validator->IsNumber('price', 'Pole cena musi być liczbą.');
			//$validator->IsNumber('price_sale', 'Pole cena musi być liczbą.');
			$validator->IsDate('datepublication', 'Pole data publikacji musi zostać wypełnione.');

			$out = $validator->GetErrorList();

			$publication = Request::Get('publication');
			$publication ? $publication = 1 : $publication = '0';
			$sale = Request::Get('sale');
			$sale ? $sale = 1 : $sale = '0';
			$new = Request::Get('new');
			$new ? $new = 1 : $new = '0';
			$special = Request::Get('special');
			$special ? $special = 1 : $special = '0';
			$available = Request::Get('available');
			//$available ? $available = 1 : $available = '0';

			$objProduct->setDate($data['datepublication']." ".$data['timepublication'].":00:00");
			$objProduct->setPublication($publication);
			$objProduct->setSale($sale);
			$objProduct->setNew($new);
			$objProduct->setAvailable($available);
			$objProduct->setAmount($data['amount']);
			$objProduct->setPrice($data['price']);
			//$objProduct->setPriceSale($data['price_sale']);
			$objProduct->setSeries($data['id_series']);
			$objProduct->SetBulb($data['bulb']);
			$objProduct->SetDimension($data['dimension']);
			$objProduct->SetCode($data['code']);
			$objProduct->SetWeight($data['weight']);
			$objProduct->SetType($data['type']);
			$objProduct->SetSpecial($special);
			$objProduct->setIdMfProductCategory($data['idProductCategory']);
			$objProductDesc = $objProduct->GetDescriptionObj();
			$objProductDesc->setLang($param['lang']);
			$objProductDesc->setName($data['name']);
			$objProductDesc->setShortnote($data['zajawka']);
			$objProductDesc->setDescription($data['tresc']);

			foreach ($arrayObjAttribute as $attrKey => $objAttribut ) {
				$objAttribut->setValue($data['attr_value'][$objAttribut->getId()]);
				$arrayObjAttribute[$attrKey] == $objAttribut;
			}
			//Utils::ArrayDisplay($objProduct);

			if(empty($out)) {
				try {


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

						$destName = Utils::TextToUrl($_FILES['filename_pdf']['name']);
						$destName = $_FILES['filename_pdf']['name'];
						if(file_exists(PATH_STATIC_CONTENT.'upload/Product/'.$destName)) {
							$destName = $destName;
						}

						move_uploaded_file($_FILES['filename_pdf']['tmp_name'], PATH_STATIC_CONTENT.'upload/Product/'.$destName);
						umask(0000);
						@chmod(PATH_STATIC_CONTENT.'upload/Product/'.$destName, 0777);
						$objFile = MfFileDAL::GetEmptyObj();
						$objFileDescription = $objFile->GetDescriptionObj();

						$objFile->SetDate(Utils::GetNowDate());
						//$objFile->SetDatePublication($data['datepublication']." ".$data['timepublication'].":00:00");
						$objFile->SetDatePublication(Utils::GetNowDate());
						$objFile->SetPublication(1);
						$objFile->SetDateUpdate(Utils::GetNowDate());
						$objFile->SetAddDate(Utils::GetNowDate());
						$objFile->SetEditDate(Utils::GetNowDate());

						$objFileDescription->SetLang('pl');
						$objFileDescription->SetPublication(1);
						$objFile->SetName($destName);
						$idFile = MfFileDAL::Save($objFile);
						$objProductDesc->SetIdFile($idFile);
						$objFileDescription->SetIdMfFile($idFile);

						MfFileDescriptionDAL::Save($objFileDescription);
					}


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



						$destNameOrg = md5('photo' . microtime()).".jpg";
						include(Config::Get('PATH_CORE').'/lib/WideImage/WideImage.php');
						$img = WideImage::loadFromUpload('filename');
						$img->resizeDown('1200','1200')->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destNameOrg, 90);

						$objPhoto = new Picture();
						$objPhoto->SetLink($destNameOrg);
						$objPhoto->SetWeight(1);
						$objPhoto->SetPublication(1);
						$idPhoto = PictureDAL::Insert($objPhoto);
						$objProductDesc->SetIdPictureMax($idPhoto);

						$img = WideImage::loadFromFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destNameOrg);
						//$img = new WideImage();
						$destName = md5('photo' . microtime()).".jpg";
						$img->resizeDown('568','815')->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destName, 90);
						$objPhoto = new Picture();
						$objPhoto->SetLink($destName);
						$objPhoto->SetWeight(1);
						$objPhoto->SetPublication(1);
						$idPhoto = PictureDAL::Insert($objPhoto);
						$objProductDesc->SetIdPicture($idPhoto);
						//$img->resizeDown('90','110')->crop('center', 'center', 90, 110)->saveToFile(PATH_STATIC_CONTENT.$destDir.$destNameth, 90);
						$destNameth = md5('photo' . microtime()).".jpg";
						$img->resizeDown('150','195')->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destNameth, 90);
						$objPhoto = new Picture();
						$objPhoto->SetLink($destNameth);
						$objPhoto->SetWeight(1);
						$objPhoto->SetPublication(1);
						$idPhoto = PictureDAL::Insert($objPhoto);
						$objProductDesc->SetIdPictureMini($idPhoto);

						$destNameth = md5('photo' . microtime()).".jpg";
						$img->resizeDown('90','110')->crop('center', 'center', '100%', 110)->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destNameth, 90);
						$objPhoto = new Picture();
						$objPhoto->SetLink($destNameth);
						$objPhoto->SetWeight(1);
						$objPhoto->SetPublication(1);
						$idPhoto = PictureDAL::Insert($objPhoto);
						$objProductDesc->SetIdPictureList($idPhoto);


					}

				} catch (Exception $e) {
					Utils::ArrayDisplay($e);
				}

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





				$idProduct = MfProductDAL::Save($objProduct);
				$objProductDesc->setIdMfProduct($idProduct);

				MfProductDescriptionDAL::UpdatePhoto($idProduct, $objProductDesc->GetIdPicture(), $objProductDesc->GetIdPictureMini(), $objProductDesc->GetIdPictureList());

				if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
					$idImageGroup = ImageDAL::SaveImage($idProduct, 'Product', $objProductDesc->getIdImageGroup());
					if ($idImageGroup) {
						$objProductDesc->setIdImageGroup($idImageGroup);
					}
				}




				MfProductDescriptionDAL::Save($objProductDesc);

				foreach ($arrayObjAttribute as $attrKey => $objAttribut ) {
					$objAttribureValue = new MfProductAttributeValue();
					$objAttribureValue->setIdMfProduct($idProduct);
					$objAttribureValue->setIdMfProductAttribute($objAttribut->GetId());
					$objAttribureValue->setValue($objAttribut->GetValue());
					$objAttribureValue->setLang($param['lang']);
					MfProductAttributeValueDAL::Save($objAttribureValue);
				}

				MfProductLinkDAL::DeleteFromLink(null, 'mf_product_specyfication', $idProduct, 'mf_product');
				if (isset($data['spec_value'])) {
					foreach ($data['spec_value'] as $idProductSpec => $specVal) {
						$objMfProductLink = new MfProductLink();
						$objMfProductLink->setDestinationType('mf_product');
						$objMfProductLink->setSourceType('mf_product_specyfication');
						$objMfProductLink->setIdDestination($idProduct);
						$objMfProductLink->setIdSource($idProductSpec);
						$objMfProductLink->setLang($param['lang']);
						$objMfProductLink->setPublication(1);

						MfProductLinkDAL::Save($objMfProductLink);
					}
				}
				MfProductLinkDAL::DeleteFromLink(null, 'mf_product_category', $idProduct, 'mf_product');

				foreach ($data['prod_category_value'] as $idProductCategory => $CatVal) {

					//				Jeśli trzeba linkować również rodzica z automatu

					$objProductCategory = MfProductCategoryDAL::GetById($idProductCategory, $param['lang']);

					if ($objProductCategory->GetIdParent() > 0 && !key_exists($objProductCategory->GetIdParent(), $data['prod_category_value'])) {
						$objMfProductLink = new MfProductLink();
						$objMfProductLink->setDestinationType('mf_product');
						$objMfProductLink->setSourceType('mf_product_category');
						$objMfProductLink->setIdDestination($idProduct);
						$objMfProductLink->setIdSource($objProductCategory->GetIdParent());
						$objMfProductLink->setLang($param['lang']);
						$objMfProductLink->setPublication(1);
						MfProductLinkDAL::Save($objMfProductLink);
					}


					$objMfProductLink = new MfProductLink();
					$objMfProductLink->setDestinationType('mf_product');
					$objMfProductLink->setSourceType('mf_product_category');
					$objMfProductLink->setIdDestination($idProduct);
					$objMfProductLink->setIdSource($idProductCategory);
					$objMfProductLink->setLang($param['lang']);
					$objMfProductLink->setPublication(1);

					MfProductLinkDAL::Save($objMfProductLink);
				}

				//
				//
				//
				//								$objMfProductLink = new MfProductLink();
				//								$objMfProductLink->setDestinationType('mf_product');
				//								$objMfProductLink->setSourceType('mf_product_category');
				//								$objMfProductLink->setIdDestination($idProduct);
				//								$objMfProductLink->setIdSource($idStructure);
				//								$objMfProductLink->setLang($param['lang']);
				//								$objMfProductLink->setPublication(1);
				//
				//								MfProductLinkDAL::Save($objMfProductLink);


				//Utils::ArrayDisplay($objProduct);

				$this->AddRedirect(Router::GenerateUrl('editStructure', array('id' => $idStructure)), 0);
			} else {


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

				$objProduct->SetDescriptionObj($objProductDesc);
				$this->smarty->assign('objProduct',$objProduct);
				$datePublished = explode(" ",$objProduct->GetDate());

				$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('arrayObjAttribute', $arrayObjAttribute);
				$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);


			}



		}
	}
	//
	//	public function AjaxAddAttributeAction($param) {
	//		$this->SetAjaxRender();
	//
	//		//====Attribute=Linked=====================================================
	//
	//		$idsAttribute = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product_attribute', $idCategory, $param['lang']);
	//		$dalData = MfProductAttributeDAL::GetDalDataObj();
	//		$dalData->setCondition(array('lang' => $param['lang']));
	//		$dalData->addCondition('   ', 'mf_product_attribute.id_mf_product_attribute IN ('.$idsAttribute.') ', ' ');
	//		$dalData->setJoin(array('MfProductAttributeDescription' => ' LEFT JOIN mf_product_attribute_description ON mf_product_attribute.id_mf_product_attribute=mf_product_attribute_description.id_mf_product_attribute'));
	//
	//		$arrayObjAttribute = MfProductAttributeDAL::GetResult($dalData);
	//		//--------------------------------------------------------------------
	//
	//
	//
	//	}





	public function AddLangAction($param) {


		//====Struktura======================================================
		$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);
		//Utils::ArrayDisplay($idStructure);
		//-------------------------------------------------------------------


		$idsProduct = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product',$idCategory, 'pl');
		$idsProductLang = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product',$idCategory, $param['lang']);

		$arrayProduct = Utils::GetArrayList('mf_product_description', 'id_mf_product', 'name', 'pl', ' AND id_mf_product IN ('.$idsProduct.') AND id_mf_product NOT IN ('.$idsProductLang.')  ', 'name');
		$this->smarty->assign('arrayProduct', $arrayProduct);

		//====Attribute======================================================

		$idsAttribute = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product_attribute', $idCategory, 'pl');
		$dalData = MfProductAttributeDAL::GetDalDataObj();
		$dalData->setCondition(array('lang' => $param['lang']));
		$dalData->addCondition('   ', 'mf_product_attribute.id_mf_product_attribute IN ('.$idsAttribute.') ', ' ');
		$dalData->setJoin(array('MfProductAttributeDescription' => ' LEFT JOIN mf_product_attribute_description ON mf_product_attribute.id_mf_product_attribute=mf_product_attribute_description.id_mf_product_attribute'));
		$dalData->setSortBy(' FIELD(mf_product_attribute.id_mf_product_attribute, '.$idsAttribute.')');
		$arrayObjAttribute = MfProductAttributeDAL::GetResult($dalData);
		//--------------------------------------------------------------------

		//====Attribute=Value=================================================
		$dalDataValue = MfProductAttributeValueDAL::GetDalDataObj();
		$dalDataValue->setCondition(array('lang' => 'pl', 'id_mf_product' => $param['idProduct']));
		$dalDataValue->addCondition('   ', 'id_mf_product_attribute IN ('.$idsAttribute.') ', ' ');
		$arrayObjAttributeValue = MfProductAttributeValueDAL::GetResult($dalDataValue);
		//Utils::ArrayDisplay($dalDataValue);
		//Utils::ArrayDisplay($arrayObjAttributeValue);
		$arrayObjAttributeValueTmp = array();
		foreach ($arrayObjAttributeValue as $attrValKey => $objAttributValue ) {
			$arrayObjAttributeValueTmp[$objAttributValue->getIdMfProductAttribute()] = $objAttributValue;
		}
		$arrayObjAttributeValue = $arrayObjAttributeValueTmp;
		foreach ($arrayObjAttribute as $attrKey => $objAttribut ) {
			if (key_exists($objAttribut->getId(), $arrayObjAttributeValue)) {
				$arrayObjAttribute[$attrKey]->setValue($arrayObjAttributeValue[$objAttribut->getId()]->getValue());
			}
		}
		//---------------------------------------------------------------------

		//=====Product=========================================================

		$dalData = MfProductAttributeDAL::GetDalDataObj();
		$dalData->setCondition(array('lang' => $param['lang']));
		$dalData->addCondition('   ', 'mf_product_attribute.id_mf_product_attribute IN ('.$idsAttribute.') ', ' ');
		$dalData->setJoin(array('MfProductAttributeDescription' => ' LEFT JOIN mf_product_attribute_description ON mf_product_attribute.id_mf_product_attribute=mf_product_attribute_description.id_mf_product_attribute'));

		//$param['lang'] = 'en';
		$objProduct = MfProductDAL::GetById($param['idProduct'], 'pl', false);
		//Utils::ArrayDisplay($objProduct);
		//---------------------------------------------------------------------


		$arrayImage = $objProduct->getArrayImage();
		//Utils::ArrayDisplay($arrayImage);
		$this->smarty->assign('arrayImage', $arrayImage);
		//Utils::ArrayDisplay($arrayObjAttribue);
		$this->smarty->assign('arrayObjAttribute', $arrayObjAttribute);
		$this->smarty->assign('idProduct', $param['idProduct']);
		$this->smarty->assign('objProduct', $objProduct);

		if (Request::GetPost('doProductAdd')) {
			//Utils::ArrayDisplay(Request::GetAllPost());

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

			$validator->IsEmpty('name', 'Pole tytuł musi zostać wypełnione.');
			$validator->IsDate('datepublication', 'Pole data publikacji musi zostać wypełnione.');

			$out = $validator->GetErrorList();

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

			//$objProduct = new MfProduct();
			$objProduct->setDate($data['datepublication']." ".$data['timepublication'].":00:00");
			$objProduct->setPublication($publication);
			$objProductDesc = $objProduct->GetDescriptionObj();
			$objProductDesc->setId(-1);
			$objProductDesc->setLang($param['lang']);
			$objProductDesc->setName($data['name']);
			$objProductDesc->setShortnote($data['zajawka']);
			$objProductDesc->setDescription($data['tresc']);

			//$arrayObjAttributeWithValue = array();
			foreach ($arrayObjAttribute as $attrKey => $objAttribut ) {
				$objAttribut->setValue($data['attr_value'][$objAttribut->getId()]);
				$arrayObjAttribute[$attrKey] == $objAttribut;
			}
			//Utils::ArrayDisplay($arrayObjAttribute);

			if(empty($out)) {

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

					$destNameOrg = md5('photo' . microtime());
					include(Config::Get('PATH_CORE').'/lib/WideImage/WideImage.php');
					$img = WideImage::loadFromUpload('filename');
					$img->resizeDown('800','600')->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destNameOrg, 90);

					$objPhoto = new Picture();
					$objPhoto->SetLink($destNameOrg);
					$objPhoto->SetWeight(1);
					$objPhoto->SetPublication(1);
					$idPhoto = PictureDAL::Insert($objPhoto);
					$objProductDesc->SetIdPictureMax($idPhoto);

					$img = WideImage::loadFromFile(PATH_STATIC_CONTENT.$destDir.$destNameOrg);
					//$img = new WideImage();
					$destName = md5('photo' . microtime());
					$img->resizeDown('360','440')->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destName, 90);
					$objPhoto = new Picture();
					$objPhoto->SetLink($destName);
					$objPhoto->SetWeight(1);
					$objPhoto->SetPublication(1);
					$idPhoto = PictureDAL::Insert($objPhoto);
					$objProductDesc->SetIdPicture($idPhoto);
					//$img->resizeDown('90','110')->crop('center', 'center', 90, 110)->saveToFile(PATH_STATIC_CONTENT.$destDir.$destNameth, 90);
					$destNameth = md5('photo' . microtime());
					$img->resizeDown('90','110')->saveToFile(PATH_STATIC_CONTENT.self::AVATAR_DEST_DIR.'/'.$destNameth, 90);
					$objPhoto = new Picture();
					$objPhoto->SetLink($destNameth);
					$objPhoto->SetWeight(1);
					$objPhoto->SetPublication(1);
					$idPhoto = PictureDAL::Insert($objPhoto);
					$objProductDesc->SetIdPictureMini($idPhoto);

					//
					//					$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/Product/'.$destName.'.jpg'))
					//					unlink(PATH_STATIC_CONTENT.'upload/Product/'.$destName.'.jpg');
					//
					//
					//					$propW = $photoWidth/self::IMAGE_NORMAL_WIDTH;
					//					$propH = $photoHeight/self::IMAGE_NORMAL_HEIGHT;
					//
					//					if ($propW > $propH) {
					//						$prop = $propH;
					//					} else {
					//						$prop = $propW;
					//					}
					//
					//					$photo = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Product', 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);
					//					$objProductDesc->SetIdPicture($idPhoto);
					//					$destName = md5('photo' . microtime());
					//
					//					$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/Product', 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);
					//					$objProductDesc->SetIdPictureMini($idPhoto);
				}
				if (isset($data['deletePhoto'])) {
					$objProductDesc->SetIdPictureMini(0);
					$objProductDesc->SetIdPicture(0);
				}

				$objOld = MfProductDAL::GetById($param['idProduct'], $param['lang']);
				if ($objOld->GetId()> 0) {
					$objDescOld = $objOld->getDescriptionObj();
					$dalDataDescOld = MfProductDescriptionDAL::GetDalDataObj();
					$dalDataDescOld->setObj($dalDataDescOld);
					MfProductLinkDAL::DeleteFromLink($idCategory, 'mf_product_category', $param['idProduct'], 'mf_product', $param['lang']);
					MfProductDescriptionDAL::Delete($dalDataDescOld);
				}

				$idProduct = MfProductDAL::Save($objProduct);
				$objProductDesc->setIdMfProduct($idProduct);

				if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
					$idImageGroup = ImageDAL::SaveImage($idProduct, 'Product');
					if ($idImageGroup) {
						$objProductDesc->setIdImageGroup($idImageGroup);
					}
				}


				MfProductDescriptionDAL::Save($objProductDesc);

				foreach ($arrayObjAttribute as $attrKey => $objAttribut ) {
					$objAttribureValue = new MfProductAttributeValue();
					$objAttribureValue->setIdMfProduct($idProduct);
					$objAttribureValue->setIdMfProductAttribute($objAttribut->GetId());
					$objAttribureValue->setValue($objAttribut->GetValue());
					$objAttribureValue->setLang($param['lang']);
					MfProductAttributeValueDAL::Save($objAttribureValue);
				}

				$objMfProductLink = new MfProductLink();
				$objMfProductLink->setDestinationType('mf_product');
				$objMfProductLink->setSourceType('mf_product_category');
				$objMfProductLink->setIdDestination($idProduct);
				$objMfProductLink->setIdSource($idCategory);
				$objMfProductLink->setLang($param['lang']);
				$objMfProductLink->setPublication(1);

				MfProductLinkDAL::Save($objMfProductLink);


				//$this->AddRedirect(Router::GenerateUrl('editStructure', array('id' => $idStructure)), 0);
			} else {

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

				$objProduct->SetDescriptionObj($objProductDesc);
				$this->smarty->assign('objProduct',$objProduct);
				$datePublished = explode(" ",$objProduct->GetDate());

				$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('arrayObjAttribute', $arrayObjAttribute);
				$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);

			}
		}
	}




	public function DeleteAction($param) {

		//Utils::ArrayDisplay($param);
		//====Struktura======================================================
		$idStructure = SessionProxy::GetValue('idStructure');
		if (!$idStructure) {
			$this->AddRedirect(Router::GenerateUrl('IndexStructure', array('Structure' => 'Index')), 0);
		}
		$this->smarty->assign('idStructure', $idStructure);
		$idCategory = SessionProxy::GetValue('idCategory');

		//$isLinked = MfProductLinkDAL::IsLinked($param['id'], 'mf_product_category', null, 'mf_product');
		$delete = true;
		$error = '';
		//		if ($isLinked > 0 ) {
		//			$delete = false;
		//		}

		$arrayLang = (Router::$arrayLang);
		unset($arrayLang[$param['lang']]);
		$langString = implode(',', $arrayLang);

		$isLang = MfProductDAL::CheakDescLang($param['idProduct'], $param['lang']);

		if ($isLang) {
			$delete = false;
		}

		if ($delete) {
			$obj = MfProductDAL::GetById($param['idProduct']);
			$objDesc = $obj->getDescriptionObj();

			$dalDataDesc = MfProductDescriptionDAL::GetDalDataObj();
			$dalDataDesc->setObj($objDesc);

			MfProductLinkDAL::DeleteFromLink($idCategory, 'mf_product_category', $param['idProduct'], 'mf_product');
			MfProductDescriptionDAL::Delete($dalDataDesc);
			$dalData = MfProductDAL::GetDalDataObj();
			$dalData->setObj($obj);
			MfProductDAL::Delete($dalData);
			$this->AddRedirectInfo('Element został usunięty', 'ok', Router::GenerateUrl('editStructure', array('id' => $idStructure)));
		} else {
			$obj = MfProductDAL::GetById($param['idProduct'], $param['lang']);
			$objDesc = $obj->getDescriptionObj();

			$dalDataDesc = MfProductDescriptionDAL::GetDalDataObj();
			$dalDataDesc->setObj($objDesc);

			MfProductLinkDAL::DeleteFromLink($idCategory, 'mf_product_category', $param['idProduct'], 'mf_product', $param['lang']);
			MfProductDescriptionDAL::Delete($dalDataDesc);
			//Utils::ArrayDisplay($dalDataDesc);
			//			$dalData = MfProductDAL::GetDalDataObj();
			//			$dalData->setObj($obj);
			//			MfProductDAL::Delete($dalData);
			$this->AddRedirectInfo('Element w tej wersji jezykiwej został usunięty', 'ok', Router::GenerateUrl('editStructure', array('id' => $idStructure)));
		}
	}

	public function AjaxCeackLangAction($param) {
		$this->SetAjaxRender();
		//$data = Request::GetAllPost();
		$isLang = true;


		if (Request::GetPost('id')) {
			$isLang = MfProductDAL::CheakDescLang($param['idProduct'], $param['lang']);

		}
		$this->smarty->assign('isLang', $isLang);
	}


	public function AjaxDeleteAction($param) {
		$this->SetAjaxRender();
		//Utils::ArrayDisplay($param);
		//====Struktura======================================================
		$idStructure = SessionProxy::GetValue('idStructure');
		if (!$idStructure) {
			$this->AddRedirect(Router::GenerateUrl('IndexStructure', array('Structure' => 'Index')), 0);
		}
		$this->smarty->assign('idStructure', $idStructure);
		$idCategory = SessionProxy::GetValue('idCategory');

		//$isLinked = MfProductLinkDAL::IsLinked($param['id'], 'mf_product_category', null, 'mf_product');
		$delete = true;
		$error = '';
		//		if ($isLinked > 0 ) {
		//			$delete = false;
		//		}

		$arrayLang = (Router::$arrayLang);
		unset($arrayLang[$param['lang']]);
		$langString = implode(',', $arrayLang);

		$isLang = MfProductDAL::CheakDescLang($param['idProduct'], $param['lang']);

		if ($isLang) {
			$delete = false;
		}

		if ($delete) {
			$obj = MfProductDAL::GetById($param['idProduct']);
			$objDesc = $obj->getDescriptionObj();

			$dalDataDesc = MfProductDescriptionDAL::GetDalDataObj();
			$dalDataDesc->setObj($objDesc);

			MfProductLinkDAL::DeleteFromLink($idCategory, 'mf_product_category', $param['idProduct'], 'mf_product', $param['lang']);
			//MfProductDescriptionDAL::Delete($dalDataDesc);
			$dalData = MfProductDAL::GetDalDataObj();
			$dalData->setObj($obj);
			//MfProductDAL::Delete($dalData);
			$this->AddRedirectInfo('Element został usunięty', 'ok', Router::GenerateUrl('editStructure', array('id' => $idStructure)));
		} else {
			if ($isLang) {
				$error .= $error ? '<br/><br/>' : '';
				$error .= 'Produkt posiada wersję jezykową';
			}
			$this->AddRedirectInfo($error, 'error', Router::GenerateUrl('editStructure', array('id' => $idStructure)));
		}
	}

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

		//Utils::ArrayDisplay($_POST);
		if (Request::IsPost()) {

			$objSeries = new ShopSeries();

			$objSeries->setName(Request::GetPost('name'));
			$objSeries->setLang('pl');
			$objSeries->setPublication(1);


			$idSeries = ShopSeriesDAL::Save($objSeries);

			$this->smarty->assign('idSeries', $idSeries);
			$this->smarty->assign('series', $objSeries->getName());

		}


	}

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

		//Utils::ArrayDisplay($_POST);
		if (Request::IsPost()) {


			$dalData = MfProductSpecificationDAL::GetDalDataObj();
			$dalData->setCondition(array('value' => Request::GetPost('name')));

			$arrayRes = MfProductSpecificationDAL::GetResult($dalData);

			if (count($arrayRes) > 0) {
				$state = false;
			} else {
				$state = true;
				$this->smarty->assign('state', $state);

				$productSpec = new MfProductSpecification();

				$productSpec->setValue(Request::GetPost('name'));
				$productSpec->setPublication(1);
				$productSpec->setTyp($param['type']);
				$productSpec->setLang('pl');


				$idSpec = MfProductSpecificationDAL::Save($productSpec);

				$this->smarty->assign('idSpec', $idSpec);
				$this->smarty->assign('spec', $productSpec->getValue());

			}


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


		}


	}


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

		//Utils::ArrayDisplay($_POST);
		if (Request::IsPost()) {

			$dalData = MfProductSpecificationDAL::GetDalDataObj();
			$dalData->setCondition(array('value' => Request::GetPost('name')));

			$arrayRes = MfProductSpecificationDAL::GetResult($dalData);

			if (count($arrayRes) > 0) {
				$state = 'false';
			} else {
				$state = 'true';
			}

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

		}


	}


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

		//Utils::ArrayDisplay($_POST);
		if (Request::IsPost()) {

			$dalData = MfProductDAL::GetDalDataObj();
			//$dalData->setCondition(array('id' => Request::GetPost('id')));

			$obj = MfProductDAL::GetById(Request::GetPost('id'));

			if (Request::GetPost('publication') > 0) {
				$state = '0';
			} else {
				$state = '1';
			}
			$obj->SetPublication($state);
			MfProductDAL::Save($obj);

			$this->smarty->assign('state', $state);
			$this->smarty->assign('id', Request::GetPost('id'));

		}
	}

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

		//Utils::ArrayDisplay($_POST);
		if (Request::IsPost()) {

			$dalData = MfProductDAL::GetDalDataObj();
			//$dalData->setCondition(array('id' => Request::GetPost('id')));

			$obj = MfProductDAL::GetById(Request::GetPost('id'));

			if (Request::GetPost('state') > 0) {
				$state = '0';
			} else {
				$state = '1';
			}
			$obj->SetSale($state);
			MfProductDAL::Save($obj);

			$this->smarty->assign('state', $state);
			$this->smarty->assign('id', Request::GetPost('id'));

		}
	}

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

		//Utils::ArrayDisplay($_POST);
		if (Request::IsPost()) {

			$dalData = MfProductDAL::GetDalDataObj();
			//$dalData->setCondition(array('id' => Request::GetPost('id')));

			$obj = MfProductDAL::GetById(Request::GetPost('id'));

			if (Request::GetPost('state') > 0) {
				$state = '0';
			} else {
				$state = '1';
			}
			$obj->SetNew($state);
			MfProductDAL::Save($obj);

			$this->smarty->assign('state', $state);
			$this->smarty->assign('id', Request::GetPost('id'));

		}
	}

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

		//Utils::ArrayDisplay($_POST);
		if (Request::IsPost()) {

			$dalData = MfProductDAL::GetDalDataObj();
			//$dalData->setCondition(array('id' => Request::GetPost('id')));

			$obj = MfProductDAL::GetById(Request::GetPost('id'));

			if (Request::GetPost('state') > 0) {
				$state = '0';
			} else {
				$state = '1';
			}
			$obj->SetLimited($state);
			MfProductDAL::Save($obj);

			//Utils::ArrayDisplay($state);

			$this->smarty->assign('state', $state);
			$this->smarty->assign('id', Request::GetPost('id'));

		}
	}

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

		//Utils::ArrayDisplay($_POST);
		if (Request::IsPost()) {

			$dalData = MfProductDAL::GetDalDataObj();
			//$dalData->setCondition(array('id' => Request::GetPost('id')));

			$obj = MfProductDAL::GetById(Request::GetPost('id'));
			$obj->SetWeight(Request::GetPost('value'));
			MfProductDAL::Save($obj);

			//Utils::ArrayDisplay($state);

			$this->smarty->assign('value', Request::GetPost('value'));
			$this->smarty->assign('id', Request::GetPost('id'));

		}
	}

	public function AjaxDeleteCheckedAction($param) {
		$this->SetNoRender();

		$arrayIds = Request::GetPost('array');

		if (count($arrayIds) > 0) {
			foreach ($arrayIds as $ids) {


				$obj = MfProductDAL::GetById($ids);
				$objDesc = $obj->getDescriptionObj();

				$dalDataDesc = MfProductDescriptionDAL::GetDalDataObj();
				$dalDataDesc->setObj($objDesc);

				MfProductLinkDAL::DeleteFromLink(null, 'mf_product_specyfication', $ids, 'mf_product');
				MfProductLinkDAL::DeleteFromLink(null, 'mf_product_category', $ids, 'mf_product');
				MfProductDescriptionDAL::Delete($dalDataDesc);
				$dalData = MfProductDAL::GetDalDataObj();
				$dalData->setObj($obj);
				MfProductDAL::Delete($dalData);

			}
		}
		$this->smarty->assign('arrayIds', $arrayIds);
		//Utils::ArrayDisplay($_POST);

	}

	public function ImportAction($param) {

		$arraySeries = Utils::GetArrayList('shop_series', 'id_shop_series', 'name');
		$this->smarty->assign('arraySeries', $arraySeries);

		//Utils::ArrayDisplay($param);
		$this->smarty->assign('arrayPhoto',array());
		$idStructure = SessionProxy::GetValue('idStructure');
		if (!$idStructure) {
			$this->AddRedirect(Router::GenerateUrl('IndexStructure', array('Structure' => 'Index')), 0);
		}
		$this->smarty->assign('idStructure', $idStructure);
		$idCategory = SessionProxy::GetValue('idCategory');
		$idCategory = $idStructure;
		$this->smarty->assign('idCategory', $idCategory);


		$idsProduct = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product',$idCategory, 'pl');
		$idsProductLang = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product',$idCategory, $param['lang']);

		$arrayProduct = Utils::GetArrayList('mf_product_description', 'id_mf_product', 'name', 'pl', ' AND id_mf_product IN ('.$idsProduct.') AND id_mf_product NOT IN ('.$idsProductLang.')  ', 'name');
		$this->smarty->assign('arrayProduct', $arrayProduct);

		//Utils::ArrayDisplay($idsProduct);
		//Utils::ArrayDisplay($idsProductLang);

		//$this->smarty->assign('idStructure', SessionProxy::GetValue('idStructure'));
		//$idsAttribute = MfProductLinkDAL::GetIdStringDestinaion('mf_product_category', 'mf_product_attribute', $idCategory, 'pl'); // zlinowane parametry z kategorią
		$dalData = MfProductAttributeDAL::GetDalDataObj();
		$dalData->setCondition(array('lang' => $param['lang']));
		//$dalData->addCondition('   ', 'mf_product_attribute.id_mf_product_attribute IN ('.$idsAttribute.') ', ' '); // /|\
		$dalData->setJoin(array('MfProductAttributeDescription' => ' LEFT JOIN mf_product_attribute_description ON mf_product_attribute.id_mf_product_attribute=mf_product_attribute_description.id_mf_product_attribute'));
		//$dalData->setSortBy(' FIELD(mf_product_attribute.id_mf_product_attribute, '.$idsAttribute.')'); // parametry z kategorii
		$dalData->setSortBy(' mf_product_attribute.weight');
		//Utils::ArrayDisplay($dalData);
		$arrayObjAttribute = MfProductAttributeDAL::GetResult($dalData);
		//$objProductAttribute = MfProductAttributeDAL::GetResultByLink('mf_product_category', $idCategory, 'mf_product_attribute');
		//Utils::ArrayDisplay($arrayObjAttribue);
		$this->smarty->assign('arrayObjAttribute', $arrayObjAttribute);

		//Pobieranie specyfikacji
		$dalData = MfProductSpecificationDAL::GetDalDataObj();
		$dalData->setCondition(array('lang' => $param['lang']));
		$dalData->setSortBy(' mf_product_specification.value');
		$arrayObjProductSpecification = MfProductSpecificationDAL::GetResult($dalData);
		//Utils::ArrayDisplay($arrayObjProductSpecification);
		$this->smarty->assign('arraySpecType', MfProductSpecification::GetTypeName());
		$this->smarty->assign('arrayObjProductSpecification', $arrayObjProductSpecification);


		//====Pobieranie=Kategorii===========================================================================

		$arrayStructureProductCategory = MfProductCategoryDAL::GetStructureProductCategory($param['lang']);
		//Utils::ArrayDisplay($arrayStructureProductCategory);
		$this->smarty->assign('arrayStructureProductCategory', $arrayStructureProductCategory);

		//---------------------------------------------------------------------------------------------------


		$dalDataOld = ProductsDAL::GetDalDataObj();
		$arrayObjProductsOld = ProductsDAL::GetResult($dalDataOld);

		foreach ($arrayObjProductsOld as $old) {

			//DOWYWALENIA
			//$old = new Products();
			$objProduct = MfProductDAL::GetEmptyObj();

			if (1==1) {
				//Utils::ArrayDisplay(Request::GetAllPost());

				$out = array();
				$data = Request::GetAllPost();

				$data['price'] = str_replace(',','.',$data['price']);
				$data['price_sale'] = str_replace(',','.',$data['price_sale']);

				$validator  = new Validator($data);
				//
				//				if ($data['price']) {
				//					$validator->IsNumber('price', 'Pole cena musi być liczbą.');
				//				}
				//				if ($data['price_sale']) {
				//					$validator->IsNumber('price_sale', 'Pole cena musi być liczbą.');
				//				}
				//
				//
				//				$validator->IsEmpty('name', 'Pole tytuł musi zostać wypełnione.');
				//				$validator->IsDate('datepublication', 'Pole data publikacji musi zostać wypełnione.');

				$out = $validator->GetErrorList();

				$publication = 0;
				$sale = $old->getPromocja();
				$new = 0;
				$available = 1;

				$special = '0';

				//$objProduct = new MfProduct();
				$objProduct->setDate(Utils::GetNowDate());
				$objProduct->setPublication($publication);
				$objProduct->setSale($sale);
				$objProduct->setNew($new);
				$objProduct->SetType($old->getTyp());
				$objProduct->SetWeight($old->getPozycja());
				$objProduct->setAvailable($available);
				$objProduct->setAmount($old->getIlosc());
				$objProduct->setPrice($old->getCena1());
				$objProduct->setPriceSale($old->getCena2());
				//$objProduct->setSeries($old->get);
				$objProduct->SetBulb($old->getZarowka());
				$objProduct->SetDimension($old->getWysokosc());
				$objProduct->SetCode($old->getKod());

				$objProduct->SetSpecial($special);

				$objProduct->setIdMfProductCategory($old->getKat());
				$objProductDesc = new MfProductDescription();
				$objProductDesc->setLang($param['lang']);
				$objProductDesc->setName($old->getNazwa());
				$objProductDesc->setShortnote('');
				$objProductDesc->setDescription('');

				//$arrayObjAttributeWithValue = array();
				//				foreach ($arrayObjAttribute as $attrKey => $objAttribut ) {
				//					$objAttribut->setValue($data['attr_value'][$objAttribut->getId()]);
				//					$arrayObjAttribute[$attrKey] == $objAttribut;
				//				}
				//Utils::ArrayDisplay($data);
				//die();

				if(empty($out)) {
					try {

						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);

							copy(PATH_STATIC_CONTENT.''.$photoFile, PATH_STATIC_CONTENT.str_replace('upload/temp','upload/Product',$photoFile));

							$objPhoto = new Picture();
							$objPhoto->SetLink(str_replace('/upload/temp/', '', $photoFile));
							$objPhoto->SetWeight(1);
							$objPhoto->SetPublication(1);
							$idPhoto = PictureDAL::Insert($objPhoto);
							$objProductDesc->SetIdPictureMax($idPhoto);

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


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

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

							$propHW = $photoHeight/$photoWidth;

							if ($propHW > 1) {
								$newPhotoWidth = $photoWidth;
								$newPhotoHeight = $photoHeight/$propHW;
							} else {
								$newPhotoWidth = $photoWidth/$propHW;
								$newPhotoHeight = $photoHeight;
							}

							//						Utils::ArrayDisplay('$propW: '.$propW);
							//						Utils::ArrayDisplay('$propH: '.$propH);
							//						Utils::ArrayDisplay('$prop: '.$prop);
							//						Utils::ArrayDisplay('$photoWidth: '. $photoWidth);
							//						Utils::ArrayDisplay('$photoHeight: '. $photoHeight);
							//						Utils::ArrayDisplay('$propHW: '.$propHW);
							//						Utils::ArrayDisplay('newPhotoWidth: '.$newPhotoWidth);
							//						Utils::ArrayDisplay('newPhotoHeight: '.$newPhotoHeight);

							$photo = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Product', 0, 0, $photoWidth, $photoHeight, $photoWidth, $photoHeight);
							//Utils::ArrayDisplay('foto: '.$photo);
							$objPhoto = new Picture();
							$objPhoto->SetLink($photo);
							$objPhoto->SetWeight(1);
							$objPhoto->SetPublication(1);
							$idPhoto = PictureDAL::Insert($objPhoto);
							$objProductDesc->SetIdPicture($idPhoto);
							$destName = md5('photo' . microtime());

							$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/Product', 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);
							$objProductDesc->SetIdPictureMini($idPhoto);


							$destName = md5('photo' . microtime());
							$propW = $photoWidth/self::IMAGE_LIST_WIDTH;
							$propH = $photoHeight/self::IMAGE_LIST_HEIGHT;

							if ($propW > $propH) {
								$prop = $propH;
							} else {
								$prop = $propW;
							}
							$photoList = PhotoDAL::SaveTempFile($photoFile, $destName, '/upload/Product', 0, 0, self::IMAGE_LIST_WIDTH*$prop, self::IMAGE_LIST_HEIGHT*$prop, self::IMAGE_LIST_WIDTH, self::IMAGE_LIST_HEIGHT);
							//Utils::ArrayDisplay('fotoMini: '.$photoMini);
							$objPhoto = new Picture();
							$objPhoto->SetLink($photoList);
							$objPhoto->SetWeight(1);
							$objPhoto->SetPublication(1);
							$idPhoto = PictureDAL::Insert($objPhoto);
							$objProductDesc->SetIdPictureList($idPhoto);
						}
					}
					catch (Exception $e) {

					}


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

					$objImageGroup = new ImageGroup();

					$idImageGroup = ImageGroupDAL::Insert($objImageGroup);
					$objProductDesc->setIdImageGroup($idImageGroup);

					$idProduct = MfProductDAL::Save($objProduct);
					$objProductDesc->setIdMfProduct($idProduct);



					if (SessionProxy::IsSetValue('_ARRAY_PHOTO_')) {
						$idImageGroup = ImageDAL::SaveImage($idProduct, 'Product', $idImageGroup);
						//					if ($idImageGroup) {
						//						$objProductDesc->setIdImageGroup($idImageGroup);
						//					}
					}


					MfProductDescriptionDAL::Save($objProductDesc);
					//
					//					foreach ($arrayObjAttribute as $attrKey => $objAttribut ) {
					//						$objAttribureValue = new MfProductAttributeValue();
					//						$objAttribureValue->setIdMfProduct($idProduct);
					//						$objAttribureValue->setIdMfProductAttribute($objAttribut->GetId());
					//						$objAttribureValue->setValue($objAttribut->GetValue());
					//						$objAttribureValue->setLang($param['lang']);
					//						MfProductAttributeValueDAL::Save($objAttribureValue);
					//					}

					//				$objMfProductLink = new MfProductLink();
					//				$objMfProductLink->setDestinationType('mf_product');
					//				$objMfProductLink->setSourceType('mf_product_category');
					//				$objMfProductLink->setIdDestination($idProduct);
					//				$objMfProductLink->setIdSource($idCategory);
					//				$objMfProductLink->setLang($param['lang']);
					//				$objMfProductLink->setPublication(1);
					//
					//				MfProductLinkDAL::Save($objMfProductLink);
					//$old->getKolor();
					//
					//
					//					foreach ($data['spec_value'] as $idProductSpec => $specVal) {
					//						$objMfProductLink = new MfProductLink();
					//						$objMfProductLink->setDestinationType('mf_product');
					//						$objMfProductLink->setSourceType('mf_product_specyfication');
					//						$objMfProductLink->setIdDestination($idProduct);
					//						$objMfProductLink->setIdSource($idProductSpec);
					//						$objMfProductLink->setLang($param['lang']);
					//						$objMfProductLink->setPublication(1);
					//
					//						MfProductLinkDAL::Save($objMfProductLink);
					//					}
					//
					//					foreach ($data['prod_category_value'] as $idProductCategory => $CatVal) {
					//
					//						//				Jeśli trzeba linkować również rodzica z automatu
					//
					//						$objProductCategory = MfProductCategoryDAL::GetById($idProductCategory, $param['lang']);
					//
					//						if ($objProductCategory->GetIdParent() > 0 && !key_exists($objProductCategory->GetIdParent(), $data['prod_category_value'])) {
					//							$objMfProductLink = new MfProductLink();
					//							$objMfProductLink->setDestinationType('mf_product');
					//							$objMfProductLink->setSourceType('mf_product_category');
					//							$objMfProductLink->setIdDestination($idProduct);
					//							$objMfProductLink->setIdSource($objProductCategory->GetIdParent());
					//							$objMfProductLink->setLang($param['lang']);
					//							$objMfProductLink->setPublication(1);
					//							MfProductLinkDAL::Save($objMfProductLink);
					//						}
					//
					//
					//						$objMfProductLink = new MfProductLink();
					//						$objMfProductLink->setDestinationType('mf_product');
					//						$objMfProductLink->setSourceType('mf_product_category');
					//						$objMfProductLink->setIdDestination($idProduct);
					//						$objMfProductLink->setIdSource($idProductCategory);
					//						$objMfProductLink->setLang($param['lang']);
					//						$objMfProductLink->setPublication(1);
					//
					//						MfProductLinkDAL::Save($objMfProductLink);
					//					}

					//$this->AddRedirect(Router::GenerateUrl('editStructure', array('id' => $idStructure)), 0);
				} else {

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

					$objProduct->SetDescriptionObj($objProductDesc);
					$this->smarty->assign('objProduct',$objProduct);
					$datePublished = explode(" ",$objProduct->GetDate());

					$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('arrayObjAttribute', $arrayObjAttribute);
					$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);


				}
			}


		}



	}



	public function PreDispatch($param) {

		$this->Run($param);


		//$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('calendar.js');

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

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

	}

	public function PostDispatch($param) {

	}


}

?>