Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ModelAttributeToOperatorTypeUtil.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 
41  {
42  const AVAILABLE_OPERATORS_TYPE_BOOLEAN = 'Boolean';
43 
44  const AVAILABLE_OPERATORS_TYPE_STRING = 'String';
45 
46  const AVAILABLE_OPERATORS_TYPE_NUMBER = 'Number';
47 
48  const AVAILABLE_OPERATORS_TYPE_DROPDOWN = 'DropDown';
49 
50  const AVAILABLE_OPERATORS_TYPE_HAS_ONE = 'HasOne';
51 
52  public static function resolveOperatorsToIncludeByType(& $data, $type)
53  {
54  $data[OperatorRules::TYPE_EQUALS] =
55  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_EQUALS);
56  $data[OperatorRules::TYPE_DOES_NOT_EQUAL] =
57  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_DOES_NOT_EQUAL);
58  static::resolveIsNullAndIsNotNullOperatorsToInclude($data, $type);
59  if ($type == ModelAttributeToOperatorTypeUtil::AVAILABLE_OPERATORS_TYPE_STRING)
60  {
61  $data[OperatorRules::TYPE_STARTS_WITH] =
62  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_STARTS_WITH);
63  $data[OperatorRules::TYPE_DOES_NOT_STARTS_WITH] =
64  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_DOES_NOT_STARTS_WITH);
65  $data[OperatorRules::TYPE_ENDS_WITH] =
66  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_ENDS_WITH);
67  $data[OperatorRules::TYPE_DOES_NOT_ENDS_WITH] =
68  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_DOES_NOT_ENDS_WITH);
69  $data[OperatorRules::TYPE_CONTAINS] =
70  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_CONTAINS);
71  $data[OperatorRules::TYPE_DOES_NOT_CONTAINS] =
72  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_DOES_NOT_CONTAINS);
73  $data[OperatorRules::TYPE_IS_EMPTY] =
74  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_IS_EMPTY);
75  $data[OperatorRules::TYPE_IS_NOT_EMPTY] =
76  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_IS_NOT_EMPTY);
77  }
78  elseif ($type == ModelAttributeToOperatorTypeUtil::AVAILABLE_OPERATORS_TYPE_NUMBER)
79  {
80  $data[OperatorRules::TYPE_GREATER_THAN_OR_EQUAL_TO] =
81  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_GREATER_THAN_OR_EQUAL_TO);
82  $data[OperatorRules::TYPE_LESS_THAN_OR_EQUAL_TO] =
83  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_LESS_THAN_OR_EQUAL_TO);
84  $data[OperatorRules::TYPE_GREATER_THAN] =
85  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_GREATER_THAN);
86  $data[OperatorRules::TYPE_LESS_THAN] =
87  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_LESS_THAN);
88  $data[OperatorRules::TYPE_BETWEEN] =
89  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_BETWEEN);
90  }
91  elseif ($type == ModelAttributeToOperatorTypeUtil::AVAILABLE_OPERATORS_TYPE_DROPDOWN)
92  {
93  $data[OperatorRules::TYPE_ONE_OF] =
94  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_ONE_OF);
95  }
96  elseif ($type == ModelAttributeToOperatorTypeUtil::AVAILABLE_OPERATORS_TYPE_HAS_ONE)
97  {
98  return;
99  }
100  else
101  {
102  throw new NotSupportedException();
103  }
104  }
105 
106  protected static function resolveIsNullAndIsNotNullOperatorsToInclude(& $data, $type)
107  {
108  $data[OperatorRules::TYPE_IS_NULL] =
109  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_IS_NULL);
110  $data[OperatorRules::TYPE_IS_NOT_NULL] =
111  OperatorRules::getTranslatedTypeLabel(OperatorRules::TYPE_IS_NOT_NULL);
112  }
113 
125  public static function getOperatorType($model, $attributeName)
126  {
127  assert('$model instanceof RedBeanModel || $model instanceof RedBeanModels || $model instanceof ModelForm');
128  assert('is_string($attributeName) && $attributeName != ""');
129  if ($model instanceof CustomField || $attributeName == 'id')
130  {
131  return 'equals';
132  }
133  if ($model instanceof MultipleValuesCustomField)
134  {
135  return 'oneOf';
136  }
137  $metadata = $model->getMetadata();
138  foreach ($metadata as $className => $perClassMetadata)
139  {
140  if (isset($perClassMetadata['elements'][$attributeName]))
141  {
142  $operatorType = self::getOperatorTypeFromModelMetadataElement($perClassMetadata['elements'][$attributeName]);
143  if ($operatorType == null)
144  {
145  break;
146  }
147  else
148  {
149  return $operatorType;
150  }
151  }
152  }
153  if ($model->isRelation($attributeName))
154  {
155  throw new NotSupportedException('Unsupported operator type for Model Class: ' . get_class($model) .
156  ' with attribute: ' . $attributeName);
157  }
158  else
159  {
160  $validators = $model->getValidators($attributeName);
161  foreach ($validators as $validator)
162  {
163  switch(get_class($validator))
164  {
165  case 'CBooleanValidator':
166  return 'equals';
167 
168  case 'CEmailValidator':
169  return 'startsWith';
170 
171  case 'RedBeanModelTypeValidator':
172  case 'TypeValidator':
173  switch ($validator->type)
174  {
175  case 'date':
176  return 'equals';
177 
178  case 'datetime':
179  return 'equals';
180 
181  case 'integer':
182  return 'equals';
183 
184  case 'float':
185  return 'equals';
186 
187  case 'time':
188  return 'equals';
189 
190  case 'array':
191  throw new NotSupportedException();
192  }
193  break;
194 
195  case 'CUrlValidator':
196  return 'contains';
197  }
198  }
199  }
200  return 'startsWith';
201  }
202 
203  protected static function getOperatorTypeFromModelMetadataElement($element)
204  {
205  assert('is_string($element)');
206  switch ($element)
207  {
208  case 'CurrencyValue': //todo: once currency has validation rules, this can be removed.
209  return 'equals';
210 
211  case 'DropDown':
212  return 'equals';
213 
214  case 'MultiSelectDropDown': //tbd.
215  return 'equals'; //tbd.
216 
217  case 'Phone':
218  return 'startsWith';
219 
220  case 'RadioDropDown':
221  return 'equals';
222 
223  case 'TextArea':
224  return 'contains';
225 
226  default :
227  null;
228  }
229  }
230 
240  public static function getAvailableOperatorsType($model, $attributeName)
241  {
242  if ($attributeName == 'id')
243  {
244  return null;
245  }
246  if ($model->$attributeName instanceof MultipleValuesCustomField ||
247  $model->$attributeName instanceof CustomField)
248  {
249  return self::AVAILABLE_OPERATORS_TYPE_DROPDOWN;
250  }
251  $metadata = $model->getMetadata();
252  foreach ($metadata as $className => $perClassMetadata)
253  {
254  if (isset($perClassMetadata['elements'][$attributeName]))
255  {
256  $operatorType = self::getAvailableOperatorsTypeFromModelMetadataElement(
257  $perClassMetadata['elements'][$attributeName]);
258  if ($operatorType == null)
259  {
260  break;
261  }
262  else
263  {
264  return $operatorType;
265  }
266  }
267  }
268  if ($model->isRelation($attributeName))
269  {
270  throw new NotSupportedException('Unsupported available operators type for Model Class: ' . get_class($model) .
271  ' with attribute: ' . $attributeName);
272  }
273  else
274  {
275  $validators = $model->getValidators($attributeName);
276  foreach ($validators as $validator)
277  {
278  switch(get_class($validator))
279  {
280  case 'CBooleanValidator':
281  return static::getAvailableOperatorsTypeForBoolean();
282 
283  case 'CEmailValidator':
284  return self::AVAILABLE_OPERATORS_TYPE_STRING;
285 
286  case 'RedBeanModelTypeValidator':
287  case 'TypeValidator':
288  switch ($validator->type)
289  {
290  case 'date':
291  return null; //managed through valueType not operator
292 
293  case 'datetime':
294  return null; //managed through valueType not operator
295 
296  case 'integer':
297  return self::AVAILABLE_OPERATORS_TYPE_NUMBER;
298 
299  case 'float':
300  return self::AVAILABLE_OPERATORS_TYPE_NUMBER;
301 
302  case 'time':
303  return null; //todo:
304 
305  case 'array':
306  throw new NotSupportedException();
307  case 'string':
308  return self::AVAILABLE_OPERATORS_TYPE_STRING;
309  }
310  break;
311 
312  case 'CUrlValidator':
313  return self::AVAILABLE_OPERATORS_TYPE_STRING;
314  }
315  }
316  }
317  return null;
318  }
319 
320  protected static function getAvailableOperatorsTypeFromModelMetadataElement($element)
321  {
322  assert('is_string($element)');
323  switch ($element)
324  {
325  case 'CurrencyValue':
326  return self::AVAILABLE_OPERATORS_TYPE_NUMBER;
327  case 'Phone':
328  return self::AVAILABLE_OPERATORS_TYPE_STRING;
329  case 'TextArea':
330  return self::AVAILABLE_OPERATORS_TYPE_STRING;
331  default :
332  null;
333  }
334  }
335 
336  protected static function getAvailableOperatorsTypeForBoolean()
337  {
338  return null;
339  }
340  }
341 ?>
static getAvailableOperatorsType($model, $attributeName)
Generated on Sun Jul 12 2020 07:10:28
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.