PageRenderTime 68ms CodeModel.GetById 58ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/atk4-addons/mvc/MVCForm.php

https://github.com/mahimarathore/mahi
PHP | 119 lines | 77 code | 5 blank | 37 comment | 22 complexity | 6bccdb622039a59bfa6f86068c5a7b70 MD5 | raw file
  1<?php
  2/**
  3 * @copyright Agile Technologies Limited
  4 */
  5class MVCForm extends Form{
  6	protected $type_correspondence='form';
  7
  8	function setController($controller_classname){
  9		parent::setController($controller_classname);
 10		// initializing form
 11		$this->controller->initForm();
 12		//$this->dq=$this->controller->getModel()->edit_dsql();
 13		return $this;
 14	}
 15	function setTitle($txt){
 16		$this->template->trySet('form_title',$txt);
 17		return $this;
 18	}
 19	function update($additional_data=array()){
 20		if(!$this->getController())return parent::update($additional_data);
 21		return $this->getController()->update(array_merge($this->getAllData(),$additional_data));
 22	}
 23	protected function getFieldType($field,$field_name=null){
 24		return $this->getController()->formatType($field->datatype(),$this->type_correspondence,$field_name);
 25	}
 26	function addFieldMVC($field_name,$chunk=null,$label=null){
 27		// Normally label is not specified, but if it does, we use addFieldPlain
 28		if(!$this->getController()){
 29			return $this->addField($field_name,$chunk,$label);
 30		}
 31
 32		$field=$this->getController()->getModel()->getField($field_name);
 33		if(is_null($field))throw new Exception_InitError("Field '$field_name' is not defined in the ".
 34			get_class($this->getController()->getModel())." model");
 35		// readonly fields are skipped
 36		if($field->readonly()===true)return $this;
 37        if ($field->display(null, 'form')=='file'){
 38            $field->datatype('file');
 39        }
 40		$field_type=$this->getFieldType($field,$field_name);
 41		$r=$this->addField($field_type,$field_name,$field->caption());
 42
 43		if($field_type=='checkbox')$r->default_value='N';
 44        if($field->datatype()=='list')$r->setValueList($field->listData());
 45        if($field->datatype()=='radio')$r->setValueList($field->listData());
 46        if($field->datatype()=='reference_id')$r->setValueList($field->refModel(),$field);
 47		if($field->datatype()=='image')$r->setController($field->refModel());
 48		if($field->datatype()=='file')$r->setController($field->refModel());
 49		/*
 50		   when adding fields for reference fields - 'reference' field type (or field type based on it) should
 51		   be used. You should also call if possible:
 52
 53		   last_field->setController($ctl)	- this will be used for adding new entries
 54		   last_field->setAddURL() 			- alternatively show form from this URL for adding new entries
 55		   last_field->setValueList()		- model or array.
 56
 57		   further you are able to control behavor of the field by using functions
 58
 59		   last_field->allowAdd(bool)		- by default if field have sufficient info, it will provide ways to add entries.
 60		   last_value->emptyValue(str)		- specify label for when no selection is made
 61		   */
 62
 63
 64		// get default from Model
 65		if($field->defaultValue()!=='**not_set**' && !is_null($field->defaultValue())){
 66			if($field->datatype()=='boolean')$r->set($field->defaultValue()===true?'Y':'N');
 67			else $r->set($field->defaultValue());
 68		}
 69		// mandatory flag
 70		if($field->mandatory()!==false)$r->validateNotNull($field->mandatory()===true?null:$field->mandatory());
 71        // below is not good, as it does not allow list to contain "null" value.. e.g. setValueList(array(0,1,2,3)) -- won't allow 0!
 72        //if($field->datatype()=='list')$r->validateField('$this->get()');
 73		return $r;
 74	}
 75    function getElement($short_name, $obligatory = true) {
 76        if($short_name=='Save'){
 77            $this->addSubmit('Save');
 78        }
 79        return parent::getElement($short_name,$obligatory);
 80    }
 81	/**
 82	 * Generic addField()
 83	 */
 84    /*
 85	function addField($type,$name,$caption=null,$attr=null){
 86		$r=parent::addField($type,$name,$caption,$attr);
 87		return $r;
 88	}
 89    */
 90	function addCondition($field,$value=null){
 91		if(!$this->getController())return parent::addCondition($field,$value);
 92		$this->getController()->getModel()->setCondition('edit_dsql',$field,$value);
 93		// TODO: make it work with arrays of values
 94		$this->conditions[$field]=$value;
 95		return $this;
 96	}
 97	function setCondition($field,$value=null){
 98		return $this->addCondition($field,$value);
 99	}
100	function loadData(){
101		if($this->bail_out)return;
102		// loading from controller/model
103		// if controller is not set, use parent
104		if(!$this->getController())return parent::loadData();
105		if(empty($this->conditions))$this->addCondition('id',null);
106		try{
107			$data=$this->getController()->get();//->getModel()->edit_dsql()->do_getHash();
108		}catch(Exception $e){
109			// data was not loaded, it is new record
110		}
111		if(isset($data)){
112			$this->set($data);
113			$this->loaded_from_db=true;
114		}
115	}
116	function hasField($name){
117		return isset($this->elements[$name])?$this->elements[$name]:false;
118	}
119}