<?php
/**
 * Generated: 2014-03-24 15:01:35
 * @author Rtn_Module_Tool
 * @descrition Odpowiad za widoczne formularze w systemie.
 */
class Forms_Controller_Admin_HistoryController extends Eppe_Crud_Controller_Action {
	
	protected $grid = 'Forms_Jqgrid_Admin_History';
	protected $crud = 'Forms_CRUD_Admin_History';
	
	public function init() {
                parent::init();
        }
	
	public function indexAction() {
		$bean = R::findOne('forms', "WHERE `id` = {$this->getParam('id')} ORDER BY `id` desc");
		
		$session = new Zend_Session_Namespace('forms');
		$session->children_id = $bean->id;
		
		if ($this->isAjax) {
			$this->ajaxResponse->data = $this->grid->render();
		}
		else {
			$this->view->grid = $this->grid->render();
		}
		$this->view->formId = $bean->id;
		$this->view->formName = $bean->name;
		$this->view->formData = $bean->data;
	}
	
	
	public function ajaxLoadDatagridAction() {
		$session = new Zend_Session_Namespace('forms');
		echo $this->grid->loadDatagrid($session->children_id);
		exit;
	}
	
	protected function _show($beanId) {
		$this->view->bean = $bean = $this->crud->findBean($beanId);
		$this->view->beanParent = R::findOne('forms', "WHERE `id` = {$bean->forms_id} ORDER BY `id` desc");
		if($bean->file){
			$this->view->beanFile = R::findOne('formfiles', "WHERE `id` = {$bean->file}");
		}else{
			$this->view->beanFile = null;
		}
		
	}
	
	public function exportAction(){
		$this->_disableView();
		
		$bean = R::findOne('forms', "WHERE `id` = {$this->getParam('id')} ORDER BY `id` desc");
		
		$filename = 'formularze_'. str_replace(' ', '_', $bean->name) . str_replace(' ', '_', $bean->data) . '.csv';
		
                $handle = fopen(Forms_Module_Module::getInstance()->getResourcesPath().$filename, 'w');
		
		$formBeans = R::findAll('formdata', "WHERE `forms_id` = {$this->getParam('id')} ORDER BY `id` desc");
		
		if(count($formBeans) == 0){
			fputcsv($handle, array("Brak danych"));
			header( 'Content-Type: text/csv' );
			header( 'Content-Disposition: attachment;filename='.$filename);
			echo file_get_contents(Forms_Module_Module::getInstance()->getResourcesPath().$filename);
			fclose($handle);
			exit;
		}
		$singleBean = R::findOne('formdata', "WHERE `forms_id` = {$this->getParam('id')} ORDER BY `id` desc");
		
		
		$fields = json_decode($singleBean->fields);
		$fieldsName = array_keys(get_object_vars($fields));
		
		$fieldsName[] = 'Załacznik';
		fputcsv($handle, $fieldsName);
		
		foreach($formBeans as $formBean){
			if($formBean->file){
				$beanFile = R::findOne('formfiles', "WHERE `id` = {$formBean->file}");
				$href = $this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . substr($beanFile->name, strpos($beanFile->name, '/modules'));
			}else{
				$href = '';
			}
			
			$tmpDataToCsv = (array)json_decode($formBean->fields);
			$tmpDataToCsv['Załacznik'] = $href;
			
			$dataToCsv = array();
			foreach($fieldsName as $fieldName){
				$dataToCsv[$fieldName] = (isset($tmpDataToCsv[$fieldName])) ? $tmpDataToCsv[$fieldName] : ' ';
			}
			fputcsv($handle, $dataToCsv);
		}
		
		header( 'Content-Type: text/csv' );
                header( 'Content-Disposition: attachment;filename='.$filename);
                echo file_get_contents(Forms_Module_Module::getInstance()->getResourcesPath().$filename);
		fclose($handle);
	}
	
}