<?php
/**
 * Generated: 2014-03-14 09:25:59
 * @author Rtn_Module_Tool
 */
class News_Controller_Admin_IndexController extends Main_News_Controller_Admin_IndexController {
	
	private $_images;
	const ORIGINALS_PREFIX = 'original_';
	
	public function init() {
                parent::init();
                $this->_images = 'public' . News_Module_Module::getInstance()->getSiteImgPath();
        }
	
	protected function _add() {
		$formClassName = $this->crud->getFormClassName();

		$form = new $formClassName();

		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()){
//					$config = News_Module_Module::getInstance()->config;
//					$originalSlidePath = $this->_images . $beanId . '.' . Eppe_Tools::getFileExtension($form->slide->getValue());
//					
//					//zapis plików
//					try {
//						$this->saveFormFileElement($form->slide, $originalSlidePath, $config->imageThumbs->width, $config->imageThumbs->height);
//						$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_index', 'News');
//					}
//				}
				$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 = News_Module_Module::getInstance()->config;
//						$originalSlidePath = $this->_images . $beanId . '.' . Eppe_Tools::getFileExtension($form->slide->getValue());
//
//						//zapis plików
//						try {
//							$this->saveFormFileElement($form->slide, $originalSlidePath, $config->imageThumbs->width, $config->imageThumbs->height);
//							$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_index', 'News');
//						}
//					}
					$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;
		
	}
	
	
	/**
         *
         * @param Zend_Form_Element_File $formFileElement 
         * @param string $destination Miejsce zapisania obrazka
         * @return bool 
         */
        private function saveFormFileElement($formFileElement, $destination, $width, $height) {
                $thumb = PhpThumb_PhpThumbFactory::create($formFileElement->getFileName(), array('correctPermissions' => true));
                $thumb->resize($width, $height);

                return $thumb->save(ROOT_PATH . $destination);
        }
}