<?php

Goweb::loadClass('Goweb_Controller_Action');

/**
 * Goweb Sitemanager 5 Framework
 * @version    5.0.19
 * @category   Frontoffice
 * @package    Amf
 * @subpackage Controller
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 *
 * A classe _Controller para o Front-Office disponibiliza
 * as accoes referentes a cada Modulo.
 *
 */
class Amf_GenericController extends Goweb_Controller_Action {

    /**
     * Construtor da Classe
     */
    public function init() {
        $this->_helper->viewRenderer->setNoRender();
        parent::init();
    }

    /**
     * Acao executada se nenhuma outra acao for especificada
     */
    public function indexAction() {
        throw new Goweb_Exception('Not Implemented');
    }
    
    public function serverAction(){
        require_once 'Zend/Amf/Server.php';

        // inicia servidor
        $server = new Zend_Amf_Server();
        $server->setClass("Goweb_Amf");

        echo($server->handle());
    }
    
    public function testAction(){
        $test = new Goweb_Amf_Generic();
        goweb::dump($test->getNewsletterForm());
    }
     
    /**
     * Accao executada se a accao chamada nao existir neste modulo
     *
     * @param string
     * @param array
     */
    public function __call($action, $arguments) {
        if('Action' == substr($action, -6)) {
            $this->_forward('list');
        } else {
            throw new Goweb_Exception("A fun&ccedil;&atilde;o n&atilde;o existe em ". get_class($this));
        }
    }
}

class Goweb_Amf_Generic {
    public function __construct(){
        // Iniciar Models
            
    }
 
    public function getNewsletterForm(){
        goweb::loadClass('Form_Registo', APPLICATION_PATH . '/Newsletter');
        /**
         * @see Goweb_Form
         */
        $form = new Form_Registo();
        
        $formFlash = array();
        $formFlash[0] = $form->getName();
        $formFlash[1] = $form->getAction();
        $formFlash[2] = $form->getMethod();
        $formFlash[3] = $form->getEnctype();
        $formFlash[4] = array();
        foreach ($form->getElementsNames() as $element){
            $formFlash[4][$element] = array();
            $formFlash[4][$element]['name'] = $element;
            $formFlash[4][$element]['type'] = $form->getElement($element)->type;
            $formFlash[4][$element]['required'] = $form->getElement($element)->isRequired() ? '1' : '0';
        }
        //return array(0=>'ola');
        return $formFlash;
    }

    public function getBanners($id = 0){
        // Iniciar Models
        $model = Goweb::getModelInstance('Banners_Model_Banner', 'db', false);
        // Retornar valor para o Flash
        $model->reset();
        if($id) {
            $model->where($model->getTableName().'.categoria_id = ' . $id);
        }
        $model->where($model->getTableName().'.activo = ?', 1);
        $model->where($model->getTableName().'.data_inicio <= ?', date('Y-m-d'));
        $model->where($model->getTableName().'.data_fim >= ?', date('Y-m-d'));

        $model->order('id');
        $result = $model->fetchAll();
        if ($result) return $result->toArray();
        else return array();
    }
}


