Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ModelForm.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
42  abstract class ModelForm extends CModel
43  {
44  private static $_names = array();
45 
46  protected $model;
47 
52  protected static function getRedBeanModelClassName()
53  {
54  throw new NotImplementedException();
55  }
56 
57  public function __construct(RedBeanModel $model)
58  {
59  $this->model = $model;
60  }
61 
62  public function getModel()
63  {
64  return $this->model;
65  }
66 
67  public function __toString()
68  {
69  return strval($this->model);
70  }
71 
72  public function __set($name, $value)
73  {
74  if (property_exists($this, $name))
75  {
76  $this->$name = $value;
77  }
78  else
79  {
80  $this->model->$name = $value;
81  }
82  }
83 
84  public function __get($name)
85  {
86  if (property_exists($this, $name))
87  {
88  return $this->$name;
89  }
90  return $this->model->$name;
91  }
92 
97  public function getMetadata()
98  {
99  return $this->model->getMetadata();
100  }
101 
106  public static function isOwnedRelation($attributeName)
107  {
108  if (property_exists(get_called_class(), $attributeName))
109  {
110  return false;
111  }
112  $modelClassName = static::getRedBeanModelClassName();
113  return $modelClassName::isOwnedRelation($attributeName);
114  }
115 
120  public static function isRelation($attributeName)
121  {
122  if (property_exists(get_called_class(), $attributeName))
123  {
124  return false;
125  }
126  $modelClassName = static::getRedBeanModelClassName();
127  return $modelClassName::isRelation($attributeName);
128  }
129 
134  public static function getRelationModelClassName($relationName)
135  {
136  if (property_exists(get_called_class(), $relationName))
137  {
138  return false;
139  }
140  $modelClassName = static::getRedBeanModelClassName();
141  return $modelClassName::getRelationModelClassName($relationName);
142  }
143 
148  public function isAttribute($attributeName)
149  {
150  assert('is_string($attributeName)');
151  assert('$attributeName != ""');
152  if (property_exists($this, $attributeName))
153  {
154  return true;
155  }
156  return $this->model->isAttribute($attributeName);
157  }
158 
164  public function isAttributeRequired($attribute)
165  {
166  if (property_exists($this, $attribute))
167  {
168  return parent::isAttributeRequired($attribute);
169  }
170  return $this->model->isAttributeRequired($attribute);
171  }
172 
179  public function attributeNames()
180  {
181  $className = get_class($this);
182  if (!isset(self::$_names[$className]))
183  {
184  $class = new ReflectionClass(get_class($this));
185  $names = array();
186  foreach ($class->getProperties() as $property)
187  {
188  $name = $property->getName();
189  if ($property->isPublic() && !$property->isStatic())
190  {
191  $names[] = $name;
192  }
193  }
194  return self::$_names[$className] = $names;
195  }
196  else
197  {
198  return self::$_names[$className];
199  }
200  }
201 
202  public function getAttributeLabel($attribute)
203  {
204  assert('is_string($attribute)');
205  assert('$attribute != ""');
206  $attributeLabels = $this->attributeLabels();
207  if (isset($attributeLabels[$attribute]))
208  {
209  return $attributeLabels[$attribute];
210  }
211  return $this->model->getAttributeLabel($attribute);
212  }
213 
219  public function setAttributes($values, $safeOnly = true)
220  {
221  $formValues = array();
222  $modelValues = array();
223  foreach ($values as $name => $value)
224  {
225  if (property_exists($this, $name))
226  {
227  $formValues[$name] = $value;
228  }
229  else
230  {
231  $modelValues[$name] = $value;
232  }
233  }
234  parent::setAttributes($formValues, $safeOnly);
235  $this->model->setAttributes($modelValues, $safeOnly);
236  }
237 
244  public function validate($attributes = null, $clearErrors = true)
245  {
246  assert('$clearErrors == true');
247  assert('$attributes == null');
248  $formValidatedSuccessfully = parent::validate($attributes);
249  $modelValidatedSuccessfully = $this->model->validate($attributes, static::shouldIgnoreRequiredValidator());
250  if (!$modelValidatedSuccessfully || !$formValidatedSuccessfully)
251  {
252  return false;
253  }
254  return true;
255  }
256 
263  public function getValidators($attribute = null)
264  {
265  if ($attribute != null && !property_exists($this, $attribute))
266  {
267  return $this->model->getValidators($attribute);
268  }
269  return parent::getValidators($attribute);
270  }
271 
275  protected static function shouldIgnoreRequiredValidator()
276  {
277  return false;
278  }
279 
284  public function getErrors($attributeNameOrNames = null)
285  {
286  $formErrors = parent::getErrors($attributeNameOrNames);
287  $modelErrors = $this->model->getErrors($attributeNameOrNames);
288  return array_merge($formErrors, $modelErrors);
289  }
290 
295  public function hasErrors($attributeNameOrNames = null)
296  {
297  $hasFormErrors = parent::hasErrors($attributeNameOrNames);
298  if ($hasFormErrors)
299  {
300  return true;
301  }
302  return false;
303  }
304 
308  public function save($runValidation = true, array $attributeNames = null)
309  {
310  assert('$attributeNames == null');
311  if (!$runValidation || $this->validate())
312  {
313  return $this->model->save($runValidation);
314  }
315  return false;
316  }
317 
324  public function getError($attribute)
325  {
326  $errors = $this->getErrors();
327  return isset($errors[$attribute]) ? reset($errors[$attribute]) : null;
328  }
329  }
330 ?>
getValidators($attribute=null)
Definition: ModelForm.php:263
static isOwnedRelation($attributeName)
Definition: ModelForm.php:106
static getRedBeanModelClassName()
Definition: ModelForm.php:52
isAttribute($attributeName)
Definition: ModelForm.php:148
hasErrors($attributeNameOrNames=null)
Definition: ModelForm.php:295
getError($attribute)
Definition: ModelForm.php:324
validate($attributes=null, $clearErrors=true)
Definition: ModelForm.php:244
save($runValidation=true, array $attributeNames=null)
Definition: ModelForm.php:308
static shouldIgnoreRequiredValidator()
Definition: ModelForm.php:275
static isRelation($attributeName)
Definition: ModelForm.php:120
attributeNames()
Definition: ModelForm.php:179
getErrors($attributeNameOrNames=null)
Definition: ModelForm.php:284
static getRelationModelClassName($relationName)
Definition: ModelForm.php:134
isAttributeRequired($attribute)
Definition: ModelForm.php:164
getMetadata()
Definition: ModelForm.php:97
setAttributes($values, $safeOnly=true)
Definition: ModelForm.php:219
Generated on Thu Jul 9 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.