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