Location: PHPKode > projects > The Fresh Toolkit > fresh-0.1.213/code_templates/actions/PropelCRUDFFormAction.tpl
<?php
class /*$actionName*/Action extends  FreshLoggedAction
{
    private $form;
    private $propelObj;
    
    public function getPkString()
    {
    	return '/*$actionName*/Form_pk';
    }
    
    public function getFForm() 
    {
        if ($this->form) return $this->form;
        $Fresh = $this->request->getAttribute('FreshController');
        $form = new FForm($this->getContext() , "/*$actionName*/Form");
        $form->setParameter("AJAXTargetModule","/*$moduleName*/");
        $form->setParameter("AJAXTargetAction","/*$actionName*/");
        $form->setParameter("title","/*$actionName*/_title");
        $obj = $this->getPropelObj();  
		if($obj) {
	        $columns = $obj->getPeer()->getTableMap()->getColumns();
			foreach($columns as $column) 
		        $form->addField($column->getColumnName(), FForm::Cnt_TextField);
		}
		//------------------------- FORM CUSTOMIZATION HERE ------------------
		
		//--------------------------------------------------------------------
        $this->form = $form;
        return $this->getFForm();
    }
    
    public function getPropelObj()
    {
    	if($this->propelObj) return $this->propelObj;
    	
    	if($this->request->hasAttribute($this->getPkString()))
    		$pk = $this->request->getAttribute($this->getPkString()); 
    	elseif($this->request->hasParameter($this->getPkString())) 
    		$pk = $this->request->getParameter($this->getPkString()); 
		if ($pk) 
		{
			// 1. Change Peer to a suitable one
			$this->propelObj = Peer::retrieveByPk($pk); 
			if($this->propelObj) return $this->propelObj;
		} 
        return false;
    }
    
    
    public function execute() 
    {
        if ($this->getFForm()->submitted()) {
            $this->getFForm()->fill($this->request->getParameters()); 
            //$this->getFForm()->addMessage("/*$actionName*/Form Submitted");
            if($this->getFForm()->validate())
            {
				$obj = $this->getPropelObj();
				if($obj)
				{           
					$this->getFForm()->fillPropelObj($obj);
					$obj->save();
					//comment the next line if you want not to update the form with object content (stick to request)
					$this->propelObj = $obj->getPeer()->retrieveByPk($obj->getPrimaryKey());
					$this->getFForm()->addMessage("Obj saved");
				} else $this->getFForm()->addError("Bad PK"); 
            } else $this->getFForm()->addError("/*$actionName*/Form did not validate");  
        }
        return $this->getDefaultView();
    }
    
    public function getDefaultView() 
    {
		if($this->request->hasParameter($this->getFForm()->getName().'swExp')) 
		{
			$propname = $this->getFForm()->getName().'ExpStatus';
			$current =  $this->user->getAttribute($propname);
			$this->user->setAttribute($propname, $current=="contracted"?"expanded":"contracted"); 
		}
		$obj=$this->getPropelObj();
		if($obj)
		{
			$this->getFForm()->addField($this->getPkString(), FForm::Cnt_Hidden  , $obj->getPrimaryKey(), array(array()));
			$this->getFForm()->fillFromPropelObj($obj);
			$this->getFForm()->setParameter('isNew',$obj->isNew());
		}
        $this->request->setAttribute('form', $this->getFForm()->getFormArray());
        $this->request->setAttribute('pkstring', $this->getPkString());
        return View::INPUT;
    }
    
    public function getRequestMethods() 
    {
        return Request::POST;
    }
    public function initialize($context) 
    {
        $parent = parent::initialize($context);
        $this->request = $context->getRequest();
        $this->user = $context->getUser();
        $this->controller = $context->getController();
        return $parent;
    }
}
?>
Return current item: The Fresh Toolkit