<?php
/**
 * Generated: 2014-03-24 15:01:35
 * @author Rtn_Module_Tool
 */
class Board_Controller_Admin_IndexController extends Eppe_Crud_Controller_Action {
	
	protected $grid = 'Board_Jqgrid_Admin_Index';
	protected $crud = 'Board_CRUD_Admin_Index';
	
	private $_images;
	const ORIGINALS_PREFIX = 'original_';
	
	public function init() {
                parent::init();
                $this->_images = 'public' . Board_Module_Module::getInstance()->getSiteImgPath();
        }
	
	public function ajaxToggleActiveAction() {
		$this->_disableView();

		$beanId = $this->_getParam('id');

		$bean = R::load('boarditem', $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 (!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']) . '")';
		}
	}
	
//	protected function _boardd() {
//		$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->isUploboarded()){
//					$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, $config->imageThumbs->width, $config->imageThumbs->height);
//						$bean = $this->crud->findBean($beanId);
//						$bean->extension = Eppe_Tools::getFileExtension($originalSlidePath);
//						R::store($bean);
//					}
//					catch (Exception $e) {
//						$this->_boarddMessage($e->getMessage(), 'error');
//						$this->_helper->redirector->gotoSimple('index', 'boardmin_index', 'Board');
//					}
//				}
//				$this->_boarddMessage(__('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->isUploboarded()){
//						$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, $config->imageThumbs->width, $config->imageThumbs->height);
//							$bean = $this->crud->findBean($beanId);
//							$bean->extension = Eppe_Tools::getFileExtension($originalSlidePath);
//							R::store($bean);
//						}
//						catch (Exception $e) {
//							$this->_boarddMessage($e->getMessage(), 'error');
//							$this->_helper->redirector->gotoSimple('index', 'boardmin_index', 'Board');
//						}
//					}
//					$this->_boarddMessage(__('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);
        }
	
}