All Data Structures Functions Variables Pages
SearchUtil.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  class SearchUtil
43  {
52  public static function resolveSearchAttributesFromArray($getArrayName, $formModelClassName, $sourceData)
53  {
54  assert('is_string($getArrayName)');
55  assert('is_string($formModelClassName) && is_subclass_of($formModelClassName, "SearchForm")');
56  $searchAttributes = array();
57  if (!empty($sourceData[$getArrayName]))
58  {
59  $searchAttributes = SearchUtil::getSearchAttributesFromSearchArray($sourceData[$getArrayName]);
60  foreach ($formModelClassName::getNonSearchableAttributes() as $attribute)
61  {
62  if (isset($searchAttributes[$attribute]) ||
63  key_exists($attribute, $searchAttributes))
64  {
65  unset($searchAttributes[$attribute]);
66  }
67  }
68  }
69  return $searchAttributes;
70  }
71 
80  public static function resolveAnyMixedAttributesScopeForSearchModelFromArray($searchModel, $getArrayName, $sourceData)
81  {
82  assert('$searchModel instanceof RedBeanModel || $searchModel instanceof ModelForm');
83  assert('is_string($getArrayName)');
84  if (!empty($sourceData[$getArrayName]) && isset($sourceData[$getArrayName][SearchForm::ANY_MIXED_ATTRIBUTES_SCOPE_NAME]))
85  {
86  assert('$searchModel instanceof SearchForm');
87  if (!is_array($sourceData[$getArrayName][SearchForm::ANY_MIXED_ATTRIBUTES_SCOPE_NAME]))
88  {
89  $sanitizedAnyMixedAttributesScope = null;
90  }
91  elseif (count($sourceData[$getArrayName][SearchForm::ANY_MIXED_ATTRIBUTES_SCOPE_NAME]) == 1 &&
92  $sourceData[$getArrayName][SearchForm::ANY_MIXED_ATTRIBUTES_SCOPE_NAME][0] == 'All')
93  {
94  $sanitizedAnyMixedAttributesScope = null;
95  }
96  else
97  {
98  $sanitizedAnyMixedAttributesScope = $sourceData[$getArrayName][SearchForm::ANY_MIXED_ATTRIBUTES_SCOPE_NAME];
99  }
100  $searchModel->setAnyMixedAttributesScope($sanitizedAnyMixedAttributesScope);
101  }
102  }
103 
111  public static function resolveSelectedListAttributesForSearchModelFromArray($searchModel, $getArrayName, $sourceData)
112  {
113  assert('$searchModel instanceof RedBeanModel || $searchModel instanceof ModelForm');
114  assert('is_string($getArrayName)');
115  if ($searchModel->getListAttributesSelector() != null &&
116  !empty($sourceData[$getArrayName]) &&
117  isset($sourceData[$getArrayName][SearchForm::SELECTED_LIST_ATTRIBUTES]))
118  {
119  assert('$searchModel instanceof SearchForm');
120  if (!is_array($sourceData[$getArrayName][SearchForm::SELECTED_LIST_ATTRIBUTES]))
121  {
122  $sanitizedListAttributes = null;
123  }
124  else
125  {
126  $sanitizedListAttributes = $sourceData[$getArrayName][SearchForm::SELECTED_LIST_ATTRIBUTES];
127  }
128  $searchModel->getListAttributesSelector()->setSelected($sanitizedListAttributes);
129  }
130  }
131 
132  public static function resolveSortFromStickyData($getArrayPrefixName, $uniqueLayoutId)
133  {
134  $key = $uniqueLayoutId;
135  $sortAttribute = static::resolveSortAttributeFromArray($getArrayPrefixName, $_GET);
136  $sortDescending = static::resolveSortDescendingFromArray($getArrayPrefixName, $_GET);
137  if (!$sortAttribute)
138  {
139  $stickyData = StickyUtil::getDataByKey($key);
140  $sortAttribute = $stickyData[0];
141  $sortDescending = $stickyData[1] ? $stickyData[1] : false;
142  return array($sortAttribute, $sortDescending);
143  }
144  StickyUtil::setDataByKeyAndData($key, array($sortAttribute, $sortDescending));
145  return array($sortAttribute, $sortDescending);
146  }
147 
148  public static function resolveSearchFormByStickyFilterByStarredData
149  (array $getData, SearchForm $searchForm, $stickyData)
150  {
151  if (isset($stickyData['filterByStarred']))
152  {
153  $searchForm->filterByStarred = $stickyData['filterByStarred'];
154  }
155  }
156 
157  public static function resolveSearchFormByStickyFilteredByData
158  (array $getData, SearchForm $searchForm, $stickyData)
159  {
160  if (isset($stickyData['filteredBy']))
161  {
162  $searchForm->filteredBy = $stickyData['filteredBy'];
163  }
164  }
165 
173  public static function resolveSortAttributeFromArray($getArrayPrefixName, $sourceData)
174  {
175  $sortAttribute = null;
176  if (!empty($sourceData[$getArrayPrefixName . '_sort']))
177  {
178  $sortAttribute = SearchUtil::getSortAttributeFromSortString($sourceData[$getArrayPrefixName . '_sort']);
179  }
180  return $sortAttribute;
181  }
182 
190  public static function resolveSortDescendingFromArray($getArrayPrefixName, $sourceData)
191  {
192  $sortDescending = false;
193  if (!empty($sourceData[$getArrayPrefixName . '_sort']))
194  {
195  $sortDescending = SearchUtil::isSortDescending($sourceData[$getArrayPrefixName . '_sort']);
196  }
197  else
198  {
199  return null;
200  }
201  return $sortDescending;
202  }
203 
209  public static function resolveFilterByStarredFromArray($searchModel, $getArrayName, $sourceData)
210  {
211  $filterByStarred = static::getFilterByStarredFromArray($getArrayName, $sourceData);
212  if (isset($filterByStarred))
213  {
214  $searchModel->filterByStarred = $filterByStarred;
215  }
216  }
217 
218  public static function resolveFilteredByFromArray($searchModel, $getArrayName, $sourceData)
219  {
220  $filteredBy = static::getFilteredByFromArray($getArrayName, $sourceData);
221  if (isset($filteredBy))
222  {
223  $searchModel->filteredBy = $filteredBy;
224  }
225  }
226 
231  public static function getSortAttributeFromSortString($sortString)
232  {
233  $sortInformation = explode(".", $sortString);
234  if ( count($sortInformation) == 2)
235  {
236  $sortAttribute = $sortInformation[0];
237  }
238  elseif ( count($sortInformation) == 1)
239  {
240  $sortAttribute = $sortInformation[0];
241  }
242  return $sortAttribute;
243  }
244 
248  public static function isSortDescending($sortString)
249  {
250  $sortInformation = explode(".", $sortString);
251  if (count($sortInformation) == 2)
252  {
253  if ($sortInformation[1] == 'desc')
254  {
255  return true;
256  }
257  }
258  return false;
259  }
260 
266  public static function getSearchAttributesFromSearchArray($searchArray)
267  {
268  assert('$searchArray != null');
269  array_walk_recursive($searchArray, 'SearchUtil::changeEmptyValueToNull');
270  self::changeEmptyArrayValuesToNull($searchArray);
271  return $searchArray;
272  }
273 
278  private static function changeEmptyValueToNull(&$value, $key)
279  {
280  if (empty($value) && $value !== '0')
281  {
282  $value = null;
283  }
284  }
285 
290  private static function changeEmptyArrayValuesToNull(& $searchArray)
291  {
292  $keysToUnset = array();
293  foreach ($searchArray as $key => $value)
294  {
295  if (is_array($value) && isset($value['values']) && is_array($value['values']))
296  {
297  foreach ($value['values'] as $subKey => $subValue)
298  {
299  if ($subValue == null)
300  {
301  unset($searchArray[$key]['values'][$subKey]);
302  $searchArray[$key]['values'] = array_values($searchArray[$key]['values']);
303  }
304  }
305  if (count($searchArray[$key]) == 1 && count($searchArray[$key]['values']) == 0)
306  {
307  $keysToUnset[] = $key;
308  }
309  }
310  if (is_array($value) && isset($value['value']) && is_array($value['value']))
311  {
312  foreach ($value['value'] as $subKey => $subValue)
313  {
314  if ($subValue == null)
315  {
316  unset($searchArray[$key]['value'][$subKey]);
317  $searchArray[$key]['value'] = array_values($searchArray[$key]['value']);
318  }
319  }
320  if (count($searchArray[$key]) == 1 && count($searchArray[$key]['value']) == 0)
321  {
322  $keysToUnset[] = $key;
323  }
324  }
325  elseif (is_array($value))
326  {
327  self::changeEmptyArrayValuesToNull($searchArray[$key]);
328  }
329  }
330  foreach ($keysToUnset as $key)
331  {
332  unset($searchArray[$key]);
333  }
334  }
335 
343  {
344  array_walk_recursive($searchArray, 'SearchUtil::changeEmptyValueToNullExceptNumeric');
345  self::changeEmptyArrayValuesToNull($searchArray);
346  return $searchArray;
347  }
348 
353  private static function changeEmptyValueToNullExceptNumeric(&$value, $key)
354  {
355  if (empty($value) && !is_numeric($value))
356  {
357  $value = null;
358  }
359  }
360 
361  public static function adaptSearchAttributesToSetInRedBeanModel($searchAttributes, $model)
362  {
363  assert('$model instanceof RedBeanModel || $model instanceof SearchForm');
364  $searchAttributesReadyToSetToModel = array();
365  if ($model instanceof SearchForm)
366  {
367  $modelToUse = $model->getModel();
368  }
369  else
370  {
371  $modelToUse = $model;
372  }
373  foreach ($searchAttributes as $attributeName => $data)
374  {
375  if ($modelToUse->isAttribute($attributeName))
376  {
377  $type = ModelAttributeToMixedTypeUtil::getType($modelToUse, $attributeName);
378  switch($type)
379  {
380  case 'CheckBox':
381 
382  if (is_array($data) && isset($data['value']))
383  {
384  $data = $data['value'];
385  }
386  elseif (is_array($data) && $data['value'] == null)
387  {
388  $data = null;
389  }
390  default :
391  continue;
392  }
393  }
394  $searchAttributesReadyToSetToModel[$attributeName] = $data;
395  }
396  return $searchAttributesReadyToSetToModel;
397  }
398 
404  public static function getDynamicSearchAttributesFromArray($getArrayName, $sourceData)
405  {
406  assert('is_string($getArrayName)');
407  if (!empty($sourceData[$getArrayName]) &&
408  isset($sourceData[$getArrayName][DynamicSearchForm::DYNAMIC_NAME]))
409  {
410  $dynamicSearchAttributes = SearchUtil::getSearchAttributesFromSearchArray($sourceData[$getArrayName][DynamicSearchForm::DYNAMIC_NAME]);
411  if (isset($dynamicSearchAttributes[DynamicSearchForm::DYNAMIC_STRUCTURE_NAME]))
412  {
413  unset($dynamicSearchAttributes[DynamicSearchForm::DYNAMIC_STRUCTURE_NAME]);
414  }
415  foreach ($dynamicSearchAttributes as $key => $data)
416  {
417  if (is_string($data) && $data == 'undefined' || $data == null)
418  {
419  unset($dynamicSearchAttributes[$key]);
420  }
421  }
422  return $dynamicSearchAttributes;
423  }
424  }
425 
431  $dynamicSearchAttributes)
432  {
433  assert('is_array($dynamicSearchAttributes)');
434  $sanitizedDynamicSearchAttributes = array();
435  foreach ($dynamicSearchAttributes as $key => $searchAttributeData)
436  {
437  $attributeIndexOrDerivedType = $searchAttributeData['attributeIndexOrDerivedType'];
438  $structurePosition = $searchAttributeData['structurePosition'];
439  unset($searchAttributeData['attributeIndexOrDerivedType']);
440  unset($searchAttributeData['structurePosition']);
441  self::processDynamicSearchAttributesDataForSavingModelRecursively($searchModel, $searchAttributeData);
442  $sanitizedDynamicSearchAttributes[$key] = $searchAttributeData;
443  $sanitizedDynamicSearchAttributes[$key]['attributeIndexOrDerivedType'] = $attributeIndexOrDerivedType;
444  $sanitizedDynamicSearchAttributes[$key]['structurePosition'] = $structurePosition;
445  }
446  return $sanitizedDynamicSearchAttributes;
447  }
448 
449  protected static function processDynamicSearchAttributesDataForSavingModelRecursively($searchModel, & $searchAttributeData)
450  {
451  $processRecursively = false;
452  foreach ($searchAttributeData as $attributeName => $attributeData)
453  {
454  if ( isset($attributeData['relatedModelData']) &&
455  is_array($attributeData) &&
456  $attributeData['relatedModelData'] == true)
457  {
458  assert('count($attributeData) == 2');
459  $processRecursively = true;
460  break;
461  }
462  }
463  if ($processRecursively)
464  {
465  $modelToUse = self::resolveModelToUseByModelAndAttributeName($searchModel, $attributeName);
466  self::processDynamicSearchAttributesDataForSavingModelRecursively($modelToUse,
467  $searchAttributeData[$attributeName]);
468  }
469  else
470  {
471  $searchAttributeData = GetUtil::sanitizePostByDesignerTypeForSavingModel($searchModel, $searchAttributeData);
472  }
473  }
474 
481  public static function resolveModelToUseByModelAndAttributeName($model, $attributeName)
482  {
483  assert('$model instanceof SearchForm || $model instanceof RedBeanModel');
484  assert('is_string($attributeName)');
485  $modelToUse = SearchDataProviderMetadataAdapter::resolveAsRedBeanModel($model->$attributeName);
486  $moduleClassName = $modelToUse->getModuleClassName();
487  if ($moduleClassName != null)
488  {
489  $formClassName = $moduleClassName::getGlobalSearchFormClassName();
490  if ($formClassName != null)
491  {
492  $modelToUse = new $formClassName($modelToUse);
493  }
494  }
495  return $modelToUse;
496  }
497 
503  public static function getDynamicSearchStructureFromArray($getArrayName, $sourceData)
504  {
505  assert('is_string($getArrayName)');
506  if (!empty($sourceData[$getArrayName]) &&
507  isset($sourceData[$getArrayName][DynamicSearchForm::DYNAMIC_STRUCTURE_NAME]))
508  {
509  return $sourceData[$getArrayName][DynamicSearchForm::DYNAMIC_STRUCTURE_NAME];
510  }
511  }
512 
518  public static function getFilterByStarredFromArray($getArrayName, $sourceData)
519  {
520  assert('is_string($getArrayName)');
521  if (!empty($sourceData[$getArrayName]) && isset($sourceData[$getArrayName]['filterByStarred']))
522  {
523  $filterByStarred = $sourceData[$getArrayName]['filterByStarred'];
524  return $filterByStarred;
525  }
526  }
527 
528  public static function getFilteredByFromArray($getArrayName, $sourceData)
529  {
530  assert('is_string($getArrayName)');
531  if (!empty($sourceData[$getArrayName]) && isset($sourceData[$getArrayName]['filteredBy']))
532  {
533  $filteredBy = $sourceData[$getArrayName]['filteredBy'];
534  return $filteredBy;
535  }
536  }
537  }
538 ?>
static resolveSelectedListAttributesForSearchModelFromArray($searchModel, $getArrayName, $sourceData)
Definition: SearchUtil.php:111
static sanitizeDynamicSearchAttributesByDesignerTypeForSavingModel(DynamicSearchForm $searchModel, $dynamicSearchAttributes)
Definition: SearchUtil.php:430
static getSearchAttributesFromSearchArray($searchArray)
Definition: SearchUtil.php:266
static getDynamicSearchStructureFromArray($getArrayName, $sourceData)
Definition: SearchUtil.php:503
static getSortAttributeFromSortString($sortString)
Definition: SearchUtil.php:231
static getFilterByStarredFromArray($getArrayName, $sourceData)
Definition: SearchUtil.php:518
static getSearchAttributesFromSearchArrayForSavingExistingSearchCriteria($searchArray)
Definition: SearchUtil.php:342
static getDynamicSearchAttributesFromArray($getArrayName, $sourceData)
Definition: SearchUtil.php:404
static sanitizePostByDesignerTypeForSavingModel($model, $postData)
Definition: GetUtil.php:98
static resolveAnyMixedAttributesScopeForSearchModelFromArray($searchModel, $getArrayName, $sourceData)
Definition: SearchUtil.php:80
static resolveFilterByStarredFromArray($searchModel, $getArrayName, $sourceData)
Definition: SearchUtil.php:209
static isSortDescending($sortString)
Definition: SearchUtil.php:248
static resolveSearchAttributesFromArray($getArrayName, $formModelClassName, $sourceData)
Definition: SearchUtil.php:52
static getType($model, $attributeName)
static resolveSortAttributeFromArray($getArrayPrefixName, $sourceData)
Definition: SearchUtil.php:173
static resolveModelToUseByModelAndAttributeName($model, $attributeName)
Definition: SearchUtil.php:481
static resolveSortDescendingFromArray($getArrayPrefixName, $sourceData)
Definition: SearchUtil.php:190
Generated on Sun May 24 2020 07:10:28