Guest User

Untitled

a guest
Jul 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. <? defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * A Formo enhanced version of ORM.
  4. *
  5. * @todo - credits/license.
  6. *
  7. */
  8. class FOORM extends ORM {
  9.  
  10. protected $_formo = null;
  11.  
  12.  
  13. /**
  14. * make it so that __set calls to the orm will automatically populate the formo fields. Also when trying to save, it assumes that the formo
  15. * fields are the most recent and saves them
  16. *
  17. * @var boolean
  18. */
  19. public $auto_formo = false;
  20.  
  21.  
  22. /**
  23. * Load the formo object when needed - for most ORM actions that will not need to set values, formo isn't needed.
  24. *
  25. * @param unknown_type $column
  26. * @return unknown
  27. */
  28. public function __get($column) {
  29.  
  30. if ($column == 'formo') {
  31. if (!isset($this->_formo)) {
  32. $this->_formo = $this->formo();
  33. }
  34. return $this->_formo;
  35. }
  36.  
  37. return parent::__get($column);
  38. }
  39.  
  40.  
  41. /**
  42. * if it's an formo field - set it there also. This is needed because formo is going to be the default model validation
  43. * so anytime a field is set in the ORM model, it needs to be set in the internal Formo object so the validation will work.
  44. * Otherwise - you could do
  45. * $user->name = $invalid_username
  46. * $user->validate()===true (since validate is now using the formo validation)
  47. *
  48. * @param unknown_type $column
  49. * @param unknown_type $value
  50. * @return unknown
  51. */
  52. public function __set($column,$value) {
  53. //@todo - should I be able to set this here?
  54. if ($column == 'formo') {
  55. $this->_formo = $value;
  56. return;
  57. }
  58.  
  59. if ($this->auto_formo && isset($this->_formo->$column)) {
  60. $this->_formo->$column->value = $value;
  61. }
  62. return parent::__set($column,$value);
  63. }
  64.  
  65.  
  66. /**
  67. * In case the formo has been changed but the orm not - we copy the formo to the orm.
  68. *
  69. * @return unknown
  70. */
  71. public function save() {
  72.  
  73. if ($this->auto_formo ) {
  74. // if the _formo object is null -
  75. // to validate need to create the formo object then map my orm values to it.
  76. if ($this->_formo===null) {
  77. $this->formo();
  78. }
  79. $this->map_formo_orm();
  80. if (!$this->_formo->validate()) {
  81. Kohana::log('info','formo validation failed');
  82. return false;
  83. }
  84. // $this->formo->errors will have the errors
  85. }
  86. return parent::save();
  87. }
  88.  
  89.  
  90. /**
  91. * Internal Formo object used for all the validation
  92. *
  93. * @return unknown
  94. */
  95. public function validate() {
  96. if ($this->_formo==null) {
  97. $this->formo();
  98. }
  99. return $this->_formo->validate();
  100. }
  101.  
  102. /**
  103. * Create an empty formo object if one is not passed, otherwise create one.
  104. * Children will call this at the top to simply do this work.
  105. *
  106. * // example of child formo
  107. * public function formo($formo=null) {
  108. * parent::formo($formo);
  109. *
  110. * $this->_formo->add('fields');
  111. *
  112. * $this->map_orm_formo();
  113. * return $this->_formo;
  114. * }
  115. *
  116. *
  117. * @param Formo $formo
  118. * @return Formo
  119. */
  120. public function formo($formo = null) {
  121. // allow them to pass in a formo object with some things already set, like orm, defaults,etc.
  122.  
  123. if ($formo == null) {
  124. $this->_formo = Formo::Factory();
  125. } elseif (!is_a($formo,'Formo')) {
  126. throw(new Exception('formo() expecting Formo'));
  127. } else {
  128. $this->_formo = $formo;
  129. }
  130.  
  131. return $this->_formo;
  132. }
  133.  
  134.  
  135. /**
  136. * Map the orm variables to the formo object - used when first creating the formo - or reloading
  137. *
  138. */
  139. protected function map_orm_formo() {
  140. foreach ($this->_formo->get_fields() as $field=>$value) {
  141. // there maybe formo elements that are not ORM fields (eg csrf) - only use object elements
  142. if (isset($this->$field)) $this->_formo->$field->value = $this->$field;
  143. }
  144. }
  145.  
  146.  
  147. /**
  148. * copy the formo variables to the orm - used when a form has been posted and validated it's time to save.
  149. *
  150. */
  151. protected function map_formo_orm() {
  152. foreach ($this->_formo->get_values() as $field=>$value) {
  153. if (isset($this->table_columns[$field])) {
  154. $this->$field = $value;
  155. }
  156. }
  157. }
  158.  
  159. }
Add Comment
Please, Sign In to add comment