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





	public function IndexAction($param) {
		//Utils::ArrayDisplay($param);
		$dalData = ShopSeriesDAL::GetDalDataObj();
		//$dalData->setCondition(array('lang' => $param['lang']));
		$arryObjSeries = ShopSeriesDAL::GetResult($dalData);

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


	public function AddAction($param) {



		// Pobiernie rodziców kategorii
		$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'], 'mf_product_category.id_parent' => 0));
		$arrayObjProductCategory = MfProductCategoryDAL::GetResult($dalData);

		//$param['runSharedVariable'] = 'linkedAttribute';
		//$this->RunModuleController( 'ProductCategoryController', 'LinkedAttribute', $param, true);
		//
		//		$param['runSharedVariable'] = 'unlinkedAttribute';
		//		$this->RunModuleController( 'ProductCategoryController', 'UnlinkedAttribute', $param, true);

		//Utils::ArrayDisplay($arrayObjProductCategory);
		$this->smarty->assign('arrayObjProductCategory', $arrayObjProductCategory);
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

		//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);
		$this->smarty->assign('arrayObjAttributeLinked', array());
		$objCategoryProduct = MfProductCategoryDAL::GetEmptyObj();

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

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

			$validator->IsEmpty('name', 'Pole nazwa musi zostać wypełnione.');

			$out = $validator->GetErrorList();

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

			//$objProduct = new MfProduct();
			//$objCategoryProduct->setDate($data['datepublication']." ".$data['timepublication'].":00:00");

			$objCategoryProduct->setPublication($publication);
			$objCategoryProduct->setIdParent($data['idParent']);
			$objCategoryProduct->setWeight($data['weight']);
			$objCategoryProductDesc = new MfProductCategoryDescription();
			$objCategoryProductDesc->setDatePublication("0000-00-00 00:00:00");
			$objCategoryProductDesc->setLang('pl');
			$objCategoryProductDesc->setName($data['name']);
			$objCategoryProductDesc->setPublication($publication);

			//$arrayObjAttributeWithValue = array();

			//Utils::ArrayDisplay($arrayObjAttribute);

			if(empty($out)) {


				$idCategoryProduct = MfProductCategoryDAL::Save($objCategoryProduct);
				$objCategoryProductDesc->setIdMfProductCategory($idCategoryProduct);
				$objCategoryProductDescEn = $objCategoryProductDesc;
				//$objCategoryProductDescEn->setLang('en');
				$objCategoryProductDescRu = $objCategoryProductDesc;
				//$objCategoryProductDescRu->setLang('ru');


				MfProductCategoryDescriptionDAL::Save($objCategoryProductDesc);
				///Utils::ArrayDisplay($objCategoryProductDesc);
				$objCategoryProductDescEn->setLang('en');
				$objCategoryProductDescEn->setId(-1);
				MfProductCategoryDescriptionDAL::Save($objCategoryProductDescEn);
				//Utils::ArrayDisplay($objCategoryProductDesc);
				$objCategoryProductDescRu->setLang('ru');
				$objCategoryProductDescRu->setId(-1);
				MfProductCategoryDescriptionDAL::Save($objCategoryProductDescRu);
				//Utils::ArrayDisplay($objCategoryProductDesc);
				if (isset($data['attr'])) {

					foreach ($data['attr'] as $attrKey => $attrId ) {
						$objMfProductLink = new MfProductLink();
						$objMfProductLink->setDestinationType('mf_product_attribute');
						$objMfProductLink->setSourceType('mf_product_category');
						$objMfProductLink->setIdDestination($attrId);
						$objMfProductLink->setIdSource($idCategoryProduct);
						$objMfProductLink->setLang('pl');
						$objMfProductLink->setPublication(1);
						MfProductLinkDAL::Save($objMfProductLink);
					}

				}
				$this->AddRedirectInfo('Zapisano', 'ok', Router::GenerateUrl('editCategory', array('ProductCategory' => 'index')));
			} else {

				if (isset($data['attr'])) {
					$idAttrLinked = implode(', ',$data['attr']);
				} else {
					$idAttrLinked = '-1';
				}
				$dalData = MfProductAttributeDAL::GetDalDataObj();
				$dalData->setCondition(array('lang' => $param['lang']));
				$dalData->addCondition('   ', 'mf_product_attribute.id_mf_product_attribute IN ('.$idAttrLinked.') ', ' ');
				$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 '));

				//Utils::ArrayDisplay($dalData);
				$arrayObjAttributeLinked = MfProductAttributeDAL::GetResult($dalData);
				//$objProductAttribute = MfProductAttributeDAL::GetResultByLink('mf_product_category', $idCategory, 'mf_product_attribute');
				//Utils::ArrayDisplay($arrayObjAttributeLinked);

				$dalData = MfProductAttributeDAL::GetDalDataObj();
				$dalData->setCondition(array('lang' => $param['lang']));
				$dalData->addCondition('   ', 'mf_product_attribute.id_mf_product_attribute NOT IN ('.$idAttrLinked.') ', ' ');
				$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 '));

				//Utils::ArrayDisplay($dalData);
				$arrayObjAttribute = MfProductAttributeDAL::GetResult($dalData);

				$this->smarty->assign('arrayObjAttribute', $arrayObjAttribute);
				$this->smarty->assign('arrayObjAttributeLinked', $arrayObjAttributeLinked);


				$objCategoryProduct->SetDescriptionObj($objCategoryProductDesc);
				$this->smarty->assign('objCategoryProduct',$objCategoryProduct);


				$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) {



		$objSeries = ShopSeriesDAL::GetById($param['id'], $param['lang']);
		$this->smarty->assign('obj', $objSeries);

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

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

			$validator->IsEmpty('name', 'Pole nazwa musi zostać wypełnione.');

			$out = $validator->GetErrorList();

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


			$objSeries->setLang($param['lang']);
			$objSeries->setName($data['name']);
			$objSeries->setPublication($publication);

			if(empty($out)) {

				//Utils::ArrayDisplay($data);
				$idseries = ShopSeriesDAL::Save($objSeries);

				$this->AddRedirectInfo('Zapisano', 'ok', Router::GenerateUrl('editSeries', array('ProductSeries' => 'index')));
			} else {


				$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 AjaxAddLinkAttrAction($param) {
		//$array
	}



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

		if ($delete) {

			$objSeries = ShopSeriesDAL::GetById($param['id']);
			$dalData = ShopSeriesDAL::GetDalDataObj();
			$dalData->setObj($objSeries);
			ShopSeriesDAL::Delete($dalData);
			$this->AddRedirectInfo('Element został usunięty', 'ok', Router::GenerateUrl('editSeries', array('ProductSeries' => 'Index')));
		} else {
			if ($isLinked) {
				$error .= 'seria jest powiązana z produktem.';
			}

			$this->AddRedirectInfo($error, 'error', Router::GenerateUrl('editCategory', array('ProductSeries' => 'Index')));
		}
	}


	/**
     * Wspolna metoda
     *
     */
	public function preDispatch($param) {


		$this->RunShared('Auth', $param);
		$this->Run($param);
		$this->AddScript('structure.js');
		$admin = AuthDAL::GetAdmin();
		$this->user = $admin;

		$this->smarty->assign('titleAdmin', 'Administracja');

		$struct = array(
		//'User' => array('User' => 'Index'),
		'Słowniki' => array('Dictionary' => 'Index'),
		'Zmienne serwisu' => array('Setup' => 'Index'),
		'Kategorie produktów' => array('ProductCategory' => 'Index'),
		'Serie produktów' => array('ProductSeries' => 'Index'),
		'Materiał produktów' => array('ProductSpec' => 'Index', 'type' => 2),
		'Kolor produktów' => array('ProductSpec' => 'Index', 'type' => 1),
		'Żarówki produktów' => array('ProductSpec' => 'Index', 'type' => 3),
		//'Parametry produktów' => array('ProductAttribute' => 'Index')


		);

		$this->smarty->assign('structure',$this->renderStruct($struct));

	}

	private function renderStruct($struct){
		$return = '';

		foreach($struct AS $k => $row){
			$return .= '<li><a href="' . Router::GenerateUrl('dictpig',$row).'">'.$k.'</a></li>';
		}

		$html = '<ul>';
		$html .= $return;
		$html .= '</ul>';


		return $html;
	}

	public function PostDispatch($param) {

	}


}

?>