<?php

/**
 * Generated: 2014-03-24 15:01:35
 * @author Rtn_Module_Tool
 */
class Banner_Controller_Admin_AdvertisementController extends Eppe_Crud_Controller_Action {

	protected $grid = 'Banner_Jqgrid_Admin_Advertisement';
	protected $crud = 'Banner_CRUD_Admin_Advertisement';
	
	private $_images;
        const ORIGINALS_PREFIX = 'original_';
        
        public function init() {
                parent::init();
                $this->_images = 'public/modules/Banner/front/oglo/';
        }

	protected function _add() {
		$formClassName = $this->crud->getFormClassName();

		$form = new Banner_Form_Admin_Newadvertisement();

		if ($this->_isPost()) {
			$post = $this->_getPost();

			if (isset($post['Anuluj'])) {
				$this->_redirectIndex();
			}

			if ($form->isValid($post) && $beanId = $this->crud->create($post)) {
				if ($form->slide->isUploaded()) {
					$originalSlidePath = $this->_images . $beanId . '.' . Eppe_Tools::getFileExtension($form->slide->getValue());

					//zapis plików
					try {
						$this->saveFormFileElement($form->slide, $originalSlidePath, 306);
						$bean = $this->crud->findBean($beanId);
						$bean->extension = Eppe_Tools::getFileExtension($originalSlidePath);
						R::store($bean);
					} catch (Exception $e) {
						$this->_addMessage($e->getMessage(), 'error');
						$this->_helper->redirector->gotoSimple('index', 'admin_advertisement', 'Banner');
					}
				}
				$this->_addMessage(__('Zmiany zostały zapisane'));
				$this->_redirectIndex();
			} else {
				$form->populate($this->_getPost());
			}
		}

		$this->view->form = $form;
	}

	protected function _edit($beanId) {
		$bean = $this->crud->findBean($beanId);

		$formClassName = $this->crud->getFormClassName();

		$form = new $formClassName(array('bean' => $bean));

		if ($this->_isPost()) {
			$post = $this->_getPost();

			if (isset($post['Anuluj'])) {
				$this->_redirectIndex();
			}

			if ($form->isValid($post)) {
				if ($this->crud->update($beanId, $post)) {
					if ($form->slide->isUploaded()) {
						$config = Board_Module_Module::getInstance()->config;
						$originalSlidePath = $this->_images . $beanId . '.' . Eppe_Tools::getFileExtension($form->slide->getValue());

						//zapis plików
						try {
							$this->saveFormFileElement($form->slide, $originalSlidePath, 306);
							$bean = $this->crud->findBean($beanId);
							$bean->extension = Eppe_Tools::getFileExtension($originalSlidePath);
							R::store($bean);
						} catch (Exception $e) {
							$this->_addMessage($e->getMessage(), 'error');
							$this->_helper->redirector->gotoSimple('index', 'admin_advertisement', 'Banner');
						}
					}
					$this->_addMessage(__('Zmiany zostały zapisane'));
					$this->_redirectIndex();
				}
			} else {
				$form->populate($this->_getPost());
			}
		} else {
			$form->populateWithBean($bean);
		}


		if ($bean->extension) {
			$originalSlidePath = $this->_images . $bean->id . '.' . $bean->extension;
			$this->view->image = '/' . $originalSlidePath;
		} else {
			$this->view->image = null;
		}

		$this->view->form = $form;
	}

	public function ajaxToggleActiveAction() {
		$this->_disableView();

		$beanId = $this->_getParam('id');

		$bean = R::load('banneradvertisement', $beanId);

		$result = false; // w domyśle operacja kończy się niepowodzeniem
		if ($bean->active) {
			$reasons = $bean->whyCantBecomeInactive();
			if (!count($reasons)) {
				$result = $bean->makeInactive();
			} else {
				array_unshift($reasons, '<span class=\"error_caption\"><b>' . __('Element nie może być deaktywowany z następujących przyczyn') . ':</b></span>');
			}
		} else {
			$reasons = $bean->whyCantBecomeActive();
			if(date('Y-m-d H:i') > $bean->dtexpiry){
				$reasons[] = 'Elemeny już wygasł';
			}
			if (!count($reasons)) {
//				$result = $bean->makeActive();
				$bean->active = true;
				$bean->dtactived = date('Y-m-d H:i');
				$result = (bool) R::store($bean);
			} else {
				array_unshift($reasons, '<span class=\"error_caption\"><b>' . __('Element nie może być aktywowany z następujących przyczyn') . ':</b></span>');
			}
		}

		$result = array('result' => $result, 'msg' => join('<br />', $reasons));

		if ($result['result'] == true) {
			$this->ajaxResponse->evalJS = 'new Success("' . __('Znacznik aktywności został zmieniony') . '"); return true;';
		} else {
			$this->ajaxResponse->evalJS = 'new Warning("' . __($result['msg']) . '"); return false;';
		}
	}

	/**
	 *
	 * @param Zend_Form_Element_File $formFileElement 
	 * @param string $destination Miejsce zapisania obrazka
	 * @return bool 
	 */
	private function saveFormFileElement($formFileElement, $destination, $width, $height = 0) {
		$thumb = PhpThumb_PhpThumbFactory::create($formFileElement->getFileName(), array('correctPermissions' => true, 'resizeUp' => true));
		$thumb->resize($width, $height);

		return $thumb->save(ROOT_PATH . $destination);
	}
}
