<?php

/**
 * Generated: 2014-03-24 15:01:35
 * @author Rtn_Module_Tool
 */
class Conference_Controller_IndexController extends Eppe_Frontend_Controller_Action {

	private $_images;
	
	public function init() {
                parent::init();
                $this->_images = 'public' . Conference_Module_Module::getInstance()->getSiteImgPath();
        }
	
	public function indexAction() {
		if (NULL === $beanId = $this->_getParam('id')) {

			$this->_redirect('/');
		}

		if (NULL === $page = $this->_getParam('page')) {
			$page = 1;
		}

		$beans = R::findAll('viewconferenceitem', 'active = ? AND conferencegroup_id = ? ORDER BY `order` DESC', [true, $beanId]);

		$conferences = array();
		foreach ($beans as $bean) {
			$conferences[] = $bean->export();
		}

		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($conferences));
		$paginator->setCurrentPageNumber($page);
		$paginator->setItemCountPerPage(5);

		$this->view->paginator = $paginator;
		$this->view->group = R::findOne('conferencegroup', 'id = ?', [$beanId]);
		$this->view->imagesPath = '/' . $this->_images;
	}

	public function showAction() {
		if (NULL === $beanId = $this->_getParam('id')) {
			$this->_redirect('/');
		}
		$bean = R::findOne('viewconferenceitem', 'active = ? AND id = ?', [true, $beanId]);

		if (NULL === $bean) {
			$this->_redirect('/');
		}
		
		if($bean->forms_id){
			$form = R::findOne('forms', "WHERE `id` = {$bean->forms_id} ORDER BY `id` desc");
			
			$formFormFieldsBeans = R::findAll('formformfields', "WHERE `forms_id` = {$bean->forms_id} ORDER BY `order`");
			$fields = array();
			foreach($formFormFieldsBeans as $ben){
				$fields[] = $ben->formfields_id;
			}
			
			$fieldsData = array();
			$fieldsData[] = array( //anty spam bot
				    "id" => '0',
				    "inputname" => 'fake',
				    "label" => 'name',
				    "fieldid" => '',
				    "required" => 1,
				    "class" => '',
				);
			
			
			foreach($fields as $field){
				$fieldBean = R::findOne('viewformfields', "WHERE `id` = {$field}");
				$fieldsData[] = array(
				    "id" => $fieldBean->id,
				    "inputname" => $fieldBean->inputname,
				    "label" => $fieldBean->label,
				    "fieldid" => $fieldBean->fieldid,
				    "required" => $fieldBean->required,
				    "class" => $fieldBean->required,
				);
			}
			$this->view->form = $form;
			$this->view->fields = $fieldsData;
		}
		
		if($bean->extension){
			$originalSlidePath = $this->_images . $bean->id . '.' . $bean->extension;
			$this->view->image = '/' . $originalSlidePath;
		}else{
			$this->view->image = null;
		}

		$this->view->conference = $bean;
	}

}
