Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
DynamicSearchDataProviderMetadataAdapter.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 NOT_USED_STRUCTURE_POSITION = 'notUsed';
43 
44  protected $metadata;
45 
46  protected $model;
47 
48  protected $userId;
49 
50  protected $sanitizedDynamicSearchAttributes;
51 
52  protected $dynamicStructure;
53 
54  public function __construct(array $metadata, SearchForm $model, $userId, $sanitizedDynamicSearchAttributes, $dynamicStructure)
55  {
56  assert('array($metadata)');
57  assert('isset($metadata["clauses"])');
58  assert('isset($metadata["structure"])');
59  assert('is_int($userId)');
60  assert('is_array($sanitizedDynamicSearchAttributes) && count($sanitizedDynamicSearchAttributes) > 0');
61  assert('is_string($dynamicStructure)');
62  $this->metadata = $metadata;
63  $this->model = $model;
64  $this->userId = $userId;
65  $this->sanitizedDynamicSearchAttributes = $sanitizedDynamicSearchAttributes;
66  $this->dynamicStructure = $dynamicStructure;
67  }
68 
74  {
75  $metadata = $this->metadata;
76  $clauseCount = count($metadata['clauses']);
77  $structure = $this->dynamicStructure;
78  $correctlyPositionedClauses = array();
79  $this->processData($this->sanitizedDynamicSearchAttributes, $clauseCount, $correctlyPositionedClauses, $metadata);
80  krsort($correctlyPositionedClauses);
81  //Resolve any unused clauses first before replacing real clauses.
82  foreach ($correctlyPositionedClauses as $position => $correctlyPositionedClauseData)
83  {
84  if ($correctlyPositionedClauseData[$position] == self::NOT_USED_STRUCTURE_POSITION)
85  {
86  $structure = strtr(strtolower($structure), $correctlyPositionedClauseData);
87  unset($correctlyPositionedClauses[$position]);
88  }
89  }
90  //add mapping to alpha code and back again. This avoids mismatches with over 10 clauses for example.
91  $alphaToNumberMap = array();
92  foreach ($correctlyPositionedClauses as $position => $correctlyPositionedClauseData)
93  {
94  $alphaCode = static::getAlphaCodeByInteger((int)$position);
95  $correctlyPositionedClauses[$position][$position] = $alphaCode;
96  $alphaToNumberMap[] = array($alphaCode => $correctlyPositionedClauseData[$position]);
97  }
98  //Replace clauses still used.
99  foreach ($correctlyPositionedClauses as $position => $correctlyPositionedClauseData)
100  {
101  $structure = strtr(strtolower($structure), $correctlyPositionedClauseData);
102  }
103  foreach ($alphaToNumberMap as $alphaCodeToNumber)
104  {
105  $structure = strtr(strtolower($structure), $alphaCodeToNumber);
106  }
107  //Now resolve and remove any unused clauses and nearby operators.
108  $structure = str_ireplace(' or ' . self::NOT_USED_STRUCTURE_POSITION, '', $structure);
109  $structure = str_ireplace(' and ' . self::NOT_USED_STRUCTURE_POSITION, '', $structure);
110  $structure = str_ireplace('(' . self::NOT_USED_STRUCTURE_POSITION . ' or ', '(', $structure);
111  $structure = str_ireplace('(' . self::NOT_USED_STRUCTURE_POSITION . ' and ', '(', $structure);
112  if (empty($metadata['structure']))
113  {
114  $metadata['structure'] = '(' . $structure . ')';
115  }
116  else
117  {
118  $metadata['structure'] = '(' . $metadata['structure'] . ') and (' . $structure . ')';
119  }
120  return $metadata;
121  }
122 
127  protected static function getAlphaCodeByInteger($integer)
128  {
129  assert('is_int($integer)');
131  return str_pad($alphaCode, 6, "z");
132  }
133 
134  protected function processData($searchAttributes, & $clauseCount, & $correctlyPositionedClauses, & $metadata)
135  {
136  foreach ($searchAttributes as $position => $searchAttribute)
137  {
138  $structurePosition = self::resolveUnsetAndGetSructurePosition($searchAttribute);
139  self::resolveUnsetAttributeIndexOrDerivedType($searchAttribute);
140  $metadataAdapter = new SearchDataProviderMetadataAdapter(
141  $this->model,
142  $this->userId,
143  $searchAttribute
144  );
145  $searchItemMetadata = $metadataAdapter->getAdaptedMetadata(true, ($clauseCount + 1));
146  if (count($searchItemMetadata['clauses']) > 0)
147  {
148  $metadata['clauses'] = $metadata['clauses'] + $searchItemMetadata['clauses'];
149  $clauseCount = $clauseCount + count($searchItemMetadata['clauses']);
150  $correctlyPositionedClauses
151  [$structurePosition][$structurePosition] = $searchItemMetadata['structure'];
152  }
153  else
154  {
155  $correctlyPositionedClauses
156  [$structurePosition][$structurePosition] = self::NOT_USED_STRUCTURE_POSITION;
157  }
158  }
159  }
160 
161  protected static function resolveUnsetAndGetSructurePosition(& $searchAttribute)
162  {
163  if (isset($searchAttribute['structurePosition']))
164  {
165  $structurePosition = $searchAttribute['structurePosition'];
166  unset($searchAttribute['structurePosition']);
167  return $structurePosition;
168  }
169  }
170 
171  protected static function resolveUnsetAttributeIndexOrDerivedType(& $searchAttribute)
172  {
173  if (isset($searchAttribute['attributeIndexOrDerivedType']))
174  {
175  unset($searchAttribute['attributeIndexOrDerivedType']);
176  }
177  }
178 
186  public static function numberToLetter($num, $uppercase = false)
187  {
188  $num -= 1;
189  $letter = chr(($num % 26) + 97);
190  $letter .= (floor($num / 26) > 0) ? str_repeat($letter, floor($num / 26)) : '';
191  return ($uppercase ? strtoupper($letter) : $letter);
192  }
193  }
194 ?>
Generated on Thu Jul 9 2020 07:10:28
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.