Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ModelAttributesAdapter.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  {
43  protected $model;
44 
45  public function __construct(RedBeanModel $model)
46  {
47  assert('$model !== null');
48  $this->model = $model;
49  }
50 
51  public function getModel()
52  {
53  return $this->model;
54  }
55 
62  public function getAttributes()
63  {
64  $attributes = array();
66  $attributes,
67  'id',
68  $this->model->getAttributeLabel('id'),
69  'Text'
70  );
71  $modelMetadata = $this->model->getMetadata();
72  foreach ($this->model->attributeNames() as $attributeName)
73  {
74  if (!$this->model->isRelation($attributeName) ||
75  $this->model->getRelationType($attributeName) == RedBeanModel::HAS_ONE ||
76  $this->model->getRelationType($attributeName) == RedBeanModel::HAS_MANY_BELONGS_TO)
77  {
78  if ($this->model instanceof Item)
79  {
80  $isAudited = $this->model->isAttributeAudited($attributeName);
81  }
82  else
83  {
84  $isAudited = false;
85  }
86 
87  $customFieldName = null;
88  if ($this->model->isRelation($attributeName) &&
89  $this->model->{$attributeName} instanceof BaseCustomField)
90  {
91  foreach ($modelMetadata as $modelClassName => $modelClassMetadata)
92  {
93  if (isset($modelMetadata[$modelClassName]['customFields']) &&
94  isset($modelMetadata[$modelClassName]['customFields'][$attributeName]))
95  {
96  $customFieldName = $modelMetadata[$modelClassName]['customFields'][$attributeName];
97  }
98  }
99  }
100 
102  $attributes,
103  $attributeName,
104  $this->model->getAttributeLabel($attributeName),
105  ModelAttributeToDesignerTypeUtil::getDesignerType($this->model, $attributeName),
106  $this->model->isAttributeRequired($attributeName),
107  $this->model->isAttributeReadOnly($attributeName),
108  $isAudited,
109  $customFieldName
110  );
111  }
112  }
113  return $attributes;
114  }
115 
120  public function getStandardAttributes()
121  {
122  $attributes = $this->getAttributes();
123  $defaultAttributeNames = $this->getStandardAttributeNames();
124  foreach ($attributes as $attributeName => $notUsed)
125  {
126  if (!in_array($attributeName, $defaultAttributeNames))
127  {
128  unset($attributes[$attributeName]);
129  }
130  }
131  return $attributes;
132  }
133 
137  public function isStandardAttribute($attributeName)
138  {
139  $defaultAttributeNames = $this->getStandardAttributeNames();
140  if (in_array($attributeName, $defaultAttributeNames))
141  {
142  return true;
143  }
144  return false;
145  }
146 
151  public function getCustomAttributes()
152  {
153  $attributes = $this->getAttributes();
154  $defaultAttributeNames = $this->getStandardAttributeNames();
155  foreach ($attributes as $attributeName => $notUsed)
156  {
157  if (in_array($attributeName, $defaultAttributeNames))
158  {
159  unset($attributes[$attributeName]);
160  }
161  }
162  return $attributes;
163  }
164 
165  private function getStandardAttributeNames()
166  {
167  $defaultAttributeNames = array('id');
168  $metadata = $this->model->getDefaultMetadata();
169  foreach ($metadata as $className => $perClassMetadata)
170  {
171  foreach ($perClassMetadata as $key => $value)
172  {
173  if ($key == 'members')
174  {
175  $defaultAttributeNames = array_merge($defaultAttributeNames, $value);
176  }
177  elseif ($key == 'relations')
178  {
179  $defaultAttributeNames = array_merge($defaultAttributeNames, array_keys($value));
180  }
181  }
182  }
183  return $defaultAttributeNames;
184  }
185 
186  public function setAttributeMetadataFromForm(AttributeForm $attributeForm)
187  {
188  $modelClassName = get_class($this->model);
189  $attributeName = $attributeForm->attributeName;
190  $attributeLabels = $attributeForm->attributeLabels;
191  $defaultValue = $attributeForm->defaultValue;
192  $elementType = $attributeForm->getAttributeTypeName();
193  $partialTypeRule = $attributeForm->getModelAttributePartialRule();
194 
195  //should we keep this here with (boolean)?
196  $isRequired = (boolean)$attributeForm->isRequired;
197  $isAudited = (boolean)$attributeForm->isAudited;
198  if (!$attributeForm instanceof DropDownAttributeForm)
199  {
200  if ($defaultValue === '')
201  {
202  $defaultValue = null;
203  }
204  if ($attributeForm instanceof MaxLengthAttributeForm)
205  {
206  if ($attributeForm->maxLength != null)
207  {
208  $maxLength = (int)$attributeForm->maxLength;
209  }
210  else
211  {
212  $maxLength = null;
213  }
214  }
215  else
216  {
217  $maxLength = null;
218  }
219  if ($attributeForm instanceof MinMaxValueAttributeForm)
220  {
221  if ($attributeForm->minValue != null)
222  {
223  $minValue = (int)$attributeForm->minValue;
224  }
225  else
226  {
227  $minValue = null;
228  }
229  if ($attributeForm->maxValue != null)
230  {
231  $maxValue = (int)$attributeForm->maxValue;
232  }
233  else
234  {
235  $maxValue = null;
236  }
237  }
238  else
239  {
240  $minValue = null;
241  $maxValue = null;
242  }
243  if ($attributeForm instanceof DecimalAttributeForm)
244  {
245  $precision = (int)$attributeForm->precisionLength;
246  }
247  else
248  {
249  $precision = null;
250  }
251  ModelMetadataUtil::addOrUpdateMember($modelClassName,
252  $attributeName,
253  $attributeLabels,
254  $defaultValue,
255  $maxLength,
256  $minValue,
257  $maxValue,
258  $precision,
259  $isRequired,
260  $isAudited,
261  $elementType,
262  $partialTypeRule);
263  $this->resolveDatabaseSchemaForModel($modelClassName);
264  }
265  else
266  {
267  throw new NotSupportedException();
268  }
269  }
270 
274  public function removeAttributeMetadata($attributeName)
275  {
276  assert('is_string($attributeName) && $attributeName != ""');
277  $modelClassName = get_class($this->model);
278  ModelMetadataUtil::removeAttribute($modelClassName, $attributeName);
279  }
280 
281  public function resolveDatabaseSchemaForModel($modelClassName)
282  {
283  assert('is_string($modelClassName) && $modelClassName != ""');
284  Yii::app()->gameHelper->muteScoringModelsOnSave();
285  $messageLogger = new MessageLogger();
286  RedBeanModelsToTablesAdapter::generateTablesFromModelClassNames(array($modelClassName), $messageLogger);
287  Yii::app()->gameHelper->unmuteScoringModelsOnSave();
288  if ($messageLogger->isErrorMessagePresent())
289  {
290  throw new FailedDatabaseSchemaChangeException($messageLogger->printMessages(true, true));
291  }
292  }
293 
301  public function isStandardAttributeRequiredByDefault($attributeName)
302  {
303  assert('is_string($attributeName)');
304  if (!$this->isStandardAttribute($attributeName))
305  {
306  throw new NotSupportedException();
307  }
308  $modelClassName = get_class($this->model);
309  $modelClassName = $modelClassName::getAttributeModelClassName($attributeName);
310  $metadata = $modelClassName::getDefaultMetadata();
311  if (isset($metadata[$modelClassName]['rules']))
312  {
313  foreach ($metadata[$modelClassName]['rules'] as $validatorMetadata)
314  {
315  assert('isset($validatorMetadata[0])');
316  assert('isset($validatorMetadata[1])');
317  if ($validatorMetadata[0] == $attributeName && $validatorMetadata[1] == 'required')
318  {
319  return true;
320  }
321  }
322  }
323  return false;
324  }
325  }
326 ?>
isStandardAttributeRequiredByDefault($attributeName)
static generateTablesFromModelClassNames(array $modelClassNames, &$messageLogger)
static getDesignerType($model, $attributeName)
const HAS_ONE
Definition: BeanModel.php:64
static addOrUpdateMember($modelClassName, $memberName, $attributeLabels, $defaultValue, $maxLength, $minValue, $maxValue, $precision, $isRequired, $isAudited, $elementType, array $partialTypeRule, array $mixedRule=null)
Definition: Item.php:37
static populateCollection(&$collection, $attributeName, $attributeLabel, $elementType, $isRequired=false, $isReadOnly=false, $isAudited=false, $customFieldName=null)
const HAS_MANY_BELONGS_TO
Definition: BeanModel.php:58
static removeAttribute($modelClassName, $attributeName)
getModelAttributePartialRule()
removeAttributeMetadata($attributeName)
Generated on Fri Sep 25 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.