<?php
/**
 * $Id$
 * klient
 *
 */
class HomeController extends MainController implements ControllerInterface {
	
	const PATH_BANER = '/upload/Banner';
	/**
	 * Domyslna metoda
	 *
	 */
	public function IndexAction($param) {
		
		$lang = SessionProxy::GetValue('lang');

		if (!is_dir(PATH_STATIC_CONTENT.self::PATH_BANER)) {
			mkdir(PATH_STATIC_CONTENT.self::PATH_BANER, 0777);
		}
		if (!is_dir(PATH_STATIC_CONTENT.self::PATH_BANER."/".$lang)) {
			mkdir(PATH_STATIC_CONTENT.self::PATH_BANER."/".$lang, 0777);
		}

		//AKCJE DOMYSLNE
		$logger = LoggerManager::getLogger(__CLASS__);

		if(isset($_POST['saveBanners'])) {
			//Utils::ArrayDisplay($_POST);
			foreach ($_POST['id'] as $id) {
				
				$name = $_POST['name'][$id];

				if (isset($_FILES['baner'.$id]) && $_FILES['baner'.$id]['error'] != 4) {
					//echo "sss   ".isset($_FILES['photo']['name']);
					$fileName = $_FILES['baner'.$id]['name'];
					$ext = explode(".",$fileName);
					$fileName = Utils::ClearString($fileName);

					if (is_file(PATH_STATIC_CONTENT.self::PATH_BANER.$id."/".$_POST['banerSaved'.$id])) {
						unlink(PATH_STATIC_CONTENT.self::PATH_BANER.$id."/".$_POST['banerSaved'.$id]);
					}

				}
				else if (isset($_POST['banerSaved'.$id])) {
					$fileName = $_POST['banerSaved'.$id];
				}

				if ($_POST['id']) {

				}

				//$idCustomer = $_POST['idCustomer'];
			
				$id = HomeDAL::Update($id, $name, $fileName, $lang);

				if (!is_dir(PATH_STATIC_CONTENT.self::PATH_BANER."/".$lang."/".$id)) {
					mkdir(PATH_STATIC_CONTENT.self::PATH_BANER."/".$lang."/".$id, 0777);
				}
				CustomerDAL::SaveCustomerFile($_FILES, 'baner'.$id, $fileName, PATH_STATIC_CONTENT.self::PATH_BANER."/".$lang."/".$id);
			}
			
//			die(Header('Location: customer'.APPLICATION_FILE_TYPE));
		}

		$arrayBanner = HomeDAL::GetAll($lang);
		$this->smarty->assign('arrayBanner', $arrayBanner);
		//Utils::ArrayDisplay($arrayBanner);
		$this -> AddScript('AC_RunActiveContent.js');
		$this->AddScript('swfobject.js');

	}

	/**
	 * Dodawanie/edycja klient/banera
	 *
	 */
	public function EditAction($param) {

		$logger = LoggerManager::getLogger(__CLASS__);
		//$this->AddScript('CopyFiles.js');
		$this->AddScript('prototype.js');
		$this->AddScript('validateFormCustomer.js');
		$this->AddScript('CheckUnique.js');


		if(isset($param['id'])) {
			$id=$param['id'];
		} else{
			$id = null;
		}

		if(isset($_POST['saveCustomer'])) {
			//Utils::ArrayDisplay($_POST);

			$id = $_POST['id'];
			$name = $_POST['name'];
			$url = $_POST['url'];
			$sort = $_POST['sort'];
			$content = $_POST['content'];
			if(!strstr($url, "http://")) {
				$url = "http://".$url;
			}
			if (isset($_FILES['baner']) && $_FILES['baner']['error'] != 4) {
				//echo "sss   ".isset($_FILES['photo']['name']);
				$fileName = $_FILES['baner']['name'];
				$ext = explode(".",$fileName);
				$fileName = Utils::ClearString($fileName);

				if (is_file(PATH_CUSTOMER.$id."/".$_POST['banerSaved'])) {
					unlink(PATH_CUSTOMER.$id."/".$_POST['banerSaved']);
				}

			}
			else if (isset($_POST['banerSaved'])) {
				$fileName = $_POST['banerSaved'];
			}

			if ($_POST['id']) {

			}

			//$idCustomer = $_POST['idCustomer'];
			$objCustomer = new Customer($id, $name, $fileName, $content, $url, $sort, $published);
			$id = CustomerDAL::Save($objCustomer);

			if (!is_dir(PATH_CUSTOMER.$id)) {
				mkdir(PATH_CUSTOMER.$id, 0777);
			}
			CustomerDAL::SaveCustomerFile($_FILES, 'baner', $fileName, PATH_CUSTOMER.$id);
			die(Header('Location: customer'.APPLICATION_FILE_TYPE));
		}
		//AKCJE DOMY�LNE
		$objCustomer = CustomerDAL::GetCustomerById($id);
		//Utils::ArrayDisplay($objQuery);
		$this->smarty->assign('objCustomer', $objCustomer);
		$this->smarty->assign('id', $id);
		//Utils::ArrayDisplay($objCustomer);
		$this->smarty->register_modifier("sslash", "stripslashes");
	}


	/**
	 * Sprawdzanie unikalno�ci nazwy klient
	 *
	 * @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 = "";
		}

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


	/**
	 * usuwanie banera
	 *
	 */
	public function DeleteAction($param) {

		$logger = LoggerManager::getLogger(__CLASS__);

		$idCustomer = SessionProxy::GetValue(EnumSessionValue::IDCUSTOMER);

		if(isset($param['id'])) {
			$id=$param['id'];
		} else{
			$id = null;
		}

		if($id) {
			CustomerDAL::Delete($id);
			//			$filePath = PATH_BILLBOARD.$idCustomer."/".$id.".swf";
			//			if (is_file($filePath)) {
			//				@unlink($filePath);
			//			}
			die(Header('Location: customer'.APPLICATION_FILE_TYPE));
			if (Pat) {

			}
		}
		//AKCJE DOMY�LNE
		//Utils::ArrayDisplay($objQuery);


	}


	public function RedirectAction($param) {
		if(isset($param) && is_array($param) && !empty($param) && isset($param['id']) && !empty($param['id'])) {
			$customer = CustomerDAL::GetCustomerById($param['id']);
			Utils::Redirect($customer -> GetRedirectUrl());
		}

		exit;

	}


	/**
	 * Metoda wspolna
	 *
	 */
	public function preDispatch($param) {
		$this->AddScript("prototype.js");
		$this->AddScript("scriptaculous.js");
		$this->AddScript("GetContent.js");
		$this->AddScript("tree.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');

		//Utils::ArrayDisplay($param);

		$this->Run($param);
		$this->RunShared('Structure', $param);
		$this->smarty->assign('titleAdmin', 'Struktura strony');
		$arrayModuleName = MfModuleDAL::GetArrayModuleName();
		$this->smarty->assign( 'arrayModuleName' ,$arrayModuleName);
		$this->smarty->assign('showIcon', true);
		$this->RunShared('Auth', $param);
		
		//Utils::ArrayDisplay(Utils::ArrayDisplay(SessionProxy::GetValue(EnumSessionValue::USER_OBJECT)));
//		if($this->IsUser()==false) {
//			$this->AddRedirect(URL_MAIN.'/Login.frmx', 0);
//		}
	}	
	
	
	public function postDispatch($param) {
		
	}
}
?>