Location: PHPKode > projects > cataBlog > cataBlog-0.1-pre_alpha/0.1-pre_alpha/Module/cataBlog/Form.php
<?php
/**
 * @license PHP
 * @author  Jackson Miller <hide@address.com>
 * @version $Id: Form.php,v 1.3 2004/09/13 15:23:12 jaxn Exp $
 * @package cataBlog
 */

require_once('CEP/Module/cataBlog.php');
require_once('CEP/Library/cataBlog.php');
require_once('HTML/QuickForm.php');

/**
 * cataBlog :: a multi-user, categorized blogging toolset for CEP
 * @todo Add preview states...
 */
class CEP_Module_cataBlog_Form extends CEP_Module_cataBlog {

    public  $state = 'Add';
    public  $entry;
    private $form;

    
    /**
     * Initializes the cataBlog entry management form.
     */
    function cepInit($cep_get_vars) {
        parent::cepInit($cep_get_vars);

        // I should be able to find a global way to do this
        // set data object options
        $options = &PEAR::getStaticProperty('DB_DataObject','options');
        $options = array(
            'database'         => CEP_DSN
            );
        
        // create a data object for a blog entry
        $this->entry = new CEP_DataObject_cataBlog_Entry();
        $this->entry->db = $GLOBALS['cepDB'];
        
        // load a specific entry if requested
        if (isset($this->cep_get_vars['entryId']) && 0 < $this->cep_get_vars['entryId']) {
            $this->entry->get($this->cep_get_vars['entryId']);
        }
        
        // create the form object using the entry object
        $form_builder =& DB_DataObject_FormBuilder::create($this->entry);
        $this->form =& $form_builder->getForm($this->cepCreateUri(array($this->cep_get_vars));
    }
    
    
    /**
     * Processes forms for adding cataBlog entries.
     */
    function _preRenderAdd() {
        if ($this->form->validate() && $this->cepCheckPerm('Write',$this->cep_module_instance_name)) {
            // set the posted values as the entry object properties
            $this->entry->setFrom($this->form->getSubmitValues());
            // insert the new entry
            if (!$this->cepError('Adding cataBlog entry', $this->entry->insert(), CEP_ERRORMODE_BOTH, 'Unable to add entry')) {
                // redirect to the display view.
                $this->cepRedirect(array('state'=>'Display','entry'=>$this->entry->cb_entry_id));
                return;
            }
        }
    }
    
    /**
     * Processes forms for editing cataBlog entries. 
     */
    function _preRenderEdit() {
        if ($this->form->validate() && $this->cepCheckPerm('Edit',$this->cep_module_instance_name)) {
            // update the entry object properties to include the posted values
            $this->entry->setFrom($this->form->getSubmitValues());
            
            if (!$this->cepError('Adding cataBlog entry', $this->entry->update(), CEP_ERRORMODE_BOTH, 'Unable to edit entry')) {
                $this->cepRedirect(array('state'=>'Display','entry'=>$this->entry->cb_entry_id));
                return;
            }
        }
    }
    
    /**
     * Processes requests to delete cataBlog entries
     * @uses CEP_Module::cepCheckPerm()
     * @uses CEP_Module::cepERror()
     * @uses CEP_Library_cataBlog::deleteBlogEntry()
     */
    function _preRenderDelete() {
        if ($this->cepCheckPerm('Admin',$this->cep_module_instance_id) && isset($this->cep_get_vars['entryId']) && 0 < $this->cep_get_vars['entryId']) {
            $this->cepError('Deleting a cataBlog entry',$this->entry->delete(),CEP_ERRORMODE_BOTH,'Unable to delete entry!');
        }
    }
    
    /**
     * Displays a form for adding cataBlog entries.
     */
    function _renderAdd() {
        $this->form->addElement('submit','submit','Add Entry!');
        
        $this->cepAddData($this->form->toHtml());
        return;
    }
    
    /**
     * Displays a form for editing cataBlog entries.
     */
    function _renderEdit() {
        $this->form->addElement('submit','submit','Edit Entry!');
        
        $this->cepAddData($this->form->toHtml());
        return;
    }
    
}
?>
Return current item: cataBlog