<?php

Goweb::loadClass('Goweb_Form');

/**
 * Goweb Sitemanager 5 Framework
 * @version    5.1.2
 * @category   Backoffice
 * @package    Index
 * @subpackage Form
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
*/
class Form_Edit extends Goweb_Form {
    
    /**
     * Formulario de pesquisa generica
    */
    protected function _setup() {
        $this->ficheiros = $this->Select();
        $this->ficheiros->label = translateAdmin('Escolha o Ficheiro a Editar');
        goweb::loadClass('Zend_Cache');
        $cache = Zend_Registry::get('cache');
        if(!$options = $cache->load('getConfigFiles')) {
            $options = $this->_getConfigFiles();
            if (is_writable(ROOT_PATH . '/config.ini')){
                $options['config.ini'] = 'config.ini';
            }
            asort($options);
        }
        $this->ficheiros->options = $options;
        $this->ficheiros->setAttrib('onchange', 'updateFileContents(this.value)');
        $this->edit = $this->Textarea();
        $this->edit->label = translateAdmin('Ficheiro');
        $this->submit = $this->Submit();
        $this->submit->value = translate('Gravar');
        $this->submit->class = 'form_submit';
    }
    
    /**
     * Obter os ficheiros de configuração
     *
     * @param String $directory
     * @return Array ini files
     */
    protected function _getConfigFiles($directory = APPLICATION_PATH){
        $array_items = array();
        if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != ".." && substr($file,0,1) != '.') {
                    if (is_dir($directory. "/" . $file)) {
                        $array_items = array_merge($array_items, $this->_getConfigFiles($directory. "/" . $file));
                    } else {
                        if (!strcmp('config.ini', $file)){
                            if (is_writeable($directory. "/" .$file)){
                                $file = str_replace(APPLICATION_PATH . DIRECTORY_SEPARATOR, '', $directory) . "/" . $file;
                                $file = preg_replace("/\/\//si", "/", $file);
                                $array_items[$file] = $file;
                            }
                        }
                    }
                }
            }
            closedir($handle);
        }
        return $array_items;
    }
}
