<?php

/**
 * IndexController
 *
 * @author Arkadiusz Rychlik <a.rychlik at eppe.com.pl>
 */
class Main_News_Controller_Admin_IndexController extends Eppe_Crud_Controller_Action {
	
	protected $crud = 'News_CRUD_Newsentry';
	protected $grid = 'News_Jqgrid_Newsentry';
	protected $beanName = 'newsentry';
	protected $multiLang = false;

	protected function _ajaxAdd() {
		if(!News_Module_Module::getInstance()->photosEnabled()){
			return parent::_ajaxAdd();
		}
		
		// Zend Upload Ajax Bugfix workaround
		if(empty($_FILES)){
			$_FILES['image'] = [
				'name' => '',
				'type' => '',
				'tmp_name' => '',
				'error' => 4,
				'size' => 0,
			];
		}
		
		$formClassName = $this->crud->getFormClassName();
		$form = new $formClassName();

		if($this->_isPost() && $this->_getPost('buttonValue')=='cancel'){
			$this->ajaxResponse->evalJS = 'ajx.loader.show(); window.location = "'.$this->_getIndexUrl(). '"';
			return false;
		}
		
		$view = new Zend_View();
		$view->setScriptPath(Rtn_Finder_File::find('views/Eppe/CRUD/', 'ajax-add.phtml'));
		
		$viewFile = [
			'view' => $view,
			'file' => 'ajax-add.phtml'
		];

		$this->commonAjaxForm($form, $viewFile, function($form) {
			$fileContents = file_get_contents($_FILES['image']['tmp_name']);
			
			$rowId = $this->crud->create($form->getValues());
			$row = R::findOne('newsentry','id = ?',[$rowId]);

			file_put_contents(News_Newsentry_Storage::factory($row)->getPath().'image.jpg', $fileContents);
			
			$ajaxResponse = new Rtn_Ajax_Response();
			$ajaxResponse->evalJS = 'ajx.loader.show(); window.location = "'.$this->_getIndexUrl(). '"';
			return $ajaxResponse;
		});
		
	}

	protected function _edit($beanId) {
		parent::_edit($beanId);
		$this->view->beanId = $beanId;
	}
	
	protected function _ajaxEdit($beanId) {
		if(!News_Module_Module::getInstance()->photosEnabled()){
			return parent::_ajaxEdit($beanId);
		}
		
		// Zend Upload Ajax Bugfix workaround
		if(empty($_FILES)){
			$_FILES['image'] = [
				'name' => '',
				'type' => '',
				'tmp_name' => '',
				'error' => 4,
				'size' => 0,
			];
		}
				
		$bean = $this->crud->findBean($beanId);
		$formClassName = $this->crud->getFormClassName();

		$form = new $formClassName([
			'bean' => $bean,
			'attribs' => [
				'beanId' => $beanId
			]
		]);
		
		if($this->_isPost() && $this->_getPost('buttonValue')=='cancel'){
			$this->ajaxResponse->evalJS = 'ajx.loader.show(); window.location = "'.$this->_getIndexUrl(). '"';
			return false;
		}
		
		$view = new Zend_View();
		$view->setScriptPath(Rtn_Finder_File::find('views/Eppe/CRUD/', 'ajax-add.phtml'));
		$view->assign('beanId', $beanId);
		
		$viewFile = [
			'view' => $view,
			'file' => 'ajax-add.phtml'
		];

		$this->commonAjaxForm($form, $viewFile, function($form) {
			$fileContents = file_exists($_FILES['image']['tmp_name']) ? 
				file_get_contents($_FILES['image']['tmp_name']) : 
				null;
			
			$beanId = $form->getAttrib('beanId');
			$this->crud->update($beanId, $form->getValues());
			$row = R::findOne('newsentry','id = ?',[$beanId]);

			if($fileContents){
				file_put_contents(News_Newsentry_Storage::factory($row)->getPath().'image.jpg', $fileContents);
			}
			
			$ajaxResponse = new Rtn_Ajax_Response();
			$ajaxResponse->evalJS = 'ajx.loader.show(); window.location = "'.$this->_getIndexUrl(). '"';
			return $ajaxResponse;
		});
		
		
	}
		
}
