<?php

Goweb::loadClass('Goweb_Form');

/**
 * Goweb Sitemanager 5 Framework
 * @version    5.0.18
 * @category   Backoffice
 * @package    PainelGoweb
 * @subpackage Form
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
*/
class Form_Model extends Goweb_Form {
    
    /**
     * Formulario de pesquisa generica
    */
    protected function _setup() {
        $this->model = $this->Select();
        $this->model->label = translateAdmin('Escolha o Model a actualizar');
        goweb::loadClass('Zend_Cache');
        $cache = Zend_Registry::get('cache');
        if(!$options = $cache->load('getModels')) {
            $options = $this->_getModels();
            $cache->save($options, 'getModels');
        }
        $this->model->options = $options;
        $this->submit = $this->Submit();
        $this->submit->value = translate('Actualiza');
        $this->submit->class = 'form_submit';
    }
    
    /**
     * Obter os models
     *
     * @return Array Models
     */
    protected function _getModels(){
        $searchables = array();
        $searchables[] = html_entity_decode(translate('Escolha uma op&ccedil;&atilde;o'), ENT_NOQUOTES, CHARSET);
        $models = glob(APPLICATION_PATH.'/*/Model/*');
        foreach ($models as $modelName){
            $modelName = str_replace(array(APPLICATION_PATH . DIRECTORY_SEPARATOR, '.php'), '', $modelName);
            $modelName = str_replace(DIRECTORY_SEPARATOR, '_', $modelName);
            goweb::loadModel($modelName);
            $model = new $modelName();
            if ($model->getUseSearchIndex()){
                $searchables[$modelName] = $modelName;
            }
            $model = null;
            unset($model);
        }
        return $searchables;
    }
}
