Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ModelDataProviderUtil.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  {
56  $modelAttributeToDataProviderAdapter,
58  $joinTablesAdapter,
59  $onTableAliasName = null)
60  {
61  assert('is_string($onTableAliasName) || $onTableAliasName == null');
62  $builder = new ModelJoinBuilder($modelAttributeToDataProviderAdapter,
63  $joinTablesAdapter);
64  $tableAliasName = $builder->resolveJoins($onTableAliasName,
65  self::resolveCanUseFromJoins($onTableAliasName));
66  $shouldConcatenate = false;
67  if ($modelAttributeToDataProviderAdapter->hasRelatedAttribute())
68  {
69  if ($modelAttributeToDataProviderAdapter->relatedAttributesSortUsesTwoAttributes())
70  {
71  $resolvedSortColumnName = $modelAttributeToDataProviderAdapter->getRelatedAttributeColumnNameByPosition(0);
72  $sortColumnsNameString = self::resolveSortColumnNameString($tableAliasName, $resolvedSortColumnName);
73  $resolvedSortColumnName = $modelAttributeToDataProviderAdapter->getRelatedAttributeColumnNameByPosition(1);
74  $shouldConcatenate = true;
75  }
76  else
77  {
78  $resolvedSortColumnName = $modelAttributeToDataProviderAdapter->getRelatedAttributeColumnName();
79  }
80  }
81  else
82  {
83  if ($modelAttributeToDataProviderAdapter->sortUsesTwoAttributes())
84  {
85  $resolvedSortColumnName = $modelAttributeToDataProviderAdapter->getColumnNameByPosition(0);
86  $sortColumnsNameString = self::resolveSortColumnNameString($tableAliasName, $resolvedSortColumnName);
87  $resolvedSortColumnName = $modelAttributeToDataProviderAdapter->getColumnNameByPosition(1);
88  $shouldConcatenate = true;
89  }
90  else
91  {
92  $resolvedSortColumnName = $modelAttributeToDataProviderAdapter->getColumnName();
93  }
94  }
95  if ($shouldConcatenate)
96  {
97  $sortColumnsNameString = DatabaseCompatibilityUtil::concat(
98  array($sortColumnsNameString,
99  self::resolveSortColumnNameString($tableAliasName, $resolvedSortColumnName)));
100  }
101  else
102  {
103  $sortColumnsNameString = self::resolveSortColumnNameString($tableAliasName, $resolvedSortColumnName);
104  }
105  return $sortColumnsNameString;
106  }
107 
113  protected static function resolveConcatenation($string)
114  {
115  return "CONCAT(" . $string . ")";
116  }
117 
122  public static function resolveCanUseFromJoins($onTableAliasName)
123  {
124  assert('is_string($onTableAliasName) || $onTableAliasName == null');
125  if ($onTableAliasName != null)
126  {
127  return false;
128  }
129  return true;
130  }
131 
137  public static function resolveSortColumnNameString($tableAliasName, $resolvedSortColumnName)
138  {
139  assert('is_string($tableAliasName)');
140  assert('is_string($resolvedSortColumnName)');
141  $sort = DatabaseCompatibilityUtil::quoteString($tableAliasName);
142  $sort .= '.';
143  $sort .= DatabaseCompatibilityUtil::quoteString($resolvedSortColumnName);
144  return $sort;
145  }
146 
160  $modelAttributeToDataProviderAdapter,
162  $joinTablesAdapter,
163  $onTableAliasName = null)
164  {
165  assert('is_string($onTableAliasName) || $onTableAliasName == null');
166  $builder = new ModelJoinBuilder($modelAttributeToDataProviderAdapter, $joinTablesAdapter);
167  $tableAliasName = $builder->resolveJoins($onTableAliasName, self::resolveCanUseFromJoins($onTableAliasName));
168  if ($modelAttributeToDataProviderAdapter->hasRelatedAttribute())
169  {
170  $resolvedGroupByColumnName = $modelAttributeToDataProviderAdapter->getRelatedAttributeColumnName();
171  }
172  else
173  {
174  $resolvedGroupByColumnName = $modelAttributeToDataProviderAdapter->getColumnName();
175  }
176  return self::resolveGroupByColumnNameString($tableAliasName, $resolvedGroupByColumnName);
177  }
178 
188  public static function makeWhere($modelClassName, array $metadata, $joinTablesAdapter, $onTableAliasName = null)
189  {
190  assert('is_string($modelClassName) && $modelClassName != ""');
191  assert('$joinTablesAdapter instanceof RedBeanModelJoinTablesQueryAdapter');
192  assert('is_string($onTableAliasName) || $onTableAliasName == null');
193  if (empty($metadata))
194  {
195  return;
196  }
197  $where = array();
198  foreach ($metadata['clauses'] as $key => $clauseInformation)
199  {
200  static::processMetadataClause($modelClassName, $key, $clauseInformation, $where, $joinTablesAdapter, $onTableAliasName);
201  }
202  if (count($where)> 0)
203  {
204  return strtr(strtolower($metadata["structure"]), $where);
205  }
206  return;
207  }
208 
214  protected static function resolveGroupByColumnNameString($tableAliasName, $resolvedSortColumnName)
215  {
216  assert('is_string($tableAliasName)');
217  assert('is_string($resolvedSortColumnName)');
218  $groupBy = DatabaseCompatibilityUtil::quoteString($tableAliasName);
219  $groupBy .= '.';
220  $groupBy .= DatabaseCompatibilityUtil::quoteString($resolvedSortColumnName);
221  return $groupBy;
222  }
223 
233  protected static function processMetadataClause($modelClassName, $clausePosition, $clauseInformation, & $where,
234  & $joinTablesAdapter, $onTableAliasName = null)
235  {
236  assert('is_string($modelClassName) && $modelClassName != ""');
237  assert('$joinTablesAdapter instanceof RedBeanModelJoinTablesQueryAdapter');
238  assert('is_int($clausePosition)');
239  assert('is_string($onTableAliasName) || $onTableAliasName == null');
240  if (isset($clauseInformation['relatedModelData']))
241  {
242  static::processMetadataContainingRelatedModelDataClause($modelClassName,
243  $clausePosition,
244  $clauseInformation,
245  $where,
246  $joinTablesAdapter,
247  $onTableAliasName);
248  }
249  elseif (isset($clauseInformation['concatedAttributeNames']))
250  {
251  if (isset($clauseInformation['relatedAttributeName']) &&
252  $clauseInformation['relatedAttributeName'] != null)
253  {
254  throw new NotSupportedException();
255  }
256  $tableAliasAndColumnNames = self::makeTableAliasAndColumnNamesForNonRelatedConcatedAttributes(
257  $modelClassName, $clauseInformation['concatedAttributeNames'],
258  $joinTablesAdapter);
259  self::addWherePartByClauseInformationForConcatedAttributes($clauseInformation['operatorType'],
260  $clauseInformation['value'], $where, $clausePosition,
261  $tableAliasAndColumnNames);
262  }
263  else
264  {
265  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter(
266  $modelClassName,
267  $clauseInformation['attributeName'],
268  ArrayUtil::getArrayValue($clauseInformation, 'relatedAttributeName'));
269  $builder = new ModelWhereAndJoinBuilder($modelAttributeToDataProviderAdapter, $joinTablesAdapter, true,
270  ArrayUtil::getArrayValue($clauseInformation, 'modifierType'));
271  $builder->resolveJoinsAndBuildWhere( $clauseInformation['operatorType'],
272  $clauseInformation['value'], $clausePosition,
273  $where, $onTableAliasName,
274  static::resolveResolveSubqueryValue($clauseInformation));
275  }
276  }
277 
278  protected static function resolveResolveSubqueryValue(Array $clauseInformation)
279  {
280  if (null == $resolveAsSubquery = ArrayUtil::getArrayValue($clauseInformation, 'resolveAsSubquery'))
281  {
282  return false;
283  }
284  return $resolveAsSubquery;
285  }
286 
295  protected static function processMetadataContainingRelatedModelDataClause($modelClassName,
296  $clausePosition,
297  $clauseInformation,
298  & $where,
299  $joinTablesAdapter,
300  $onTableAliasName = null)
301  {
302  assert('is_string($modelClassName) && $modelClassName != ""');
303  assert('$joinTablesAdapter instanceof RedBeanModelJoinTablesQueryAdapter');
304  assert('is_int($clausePosition)');
305  assert('is_array($clauseInformation["relatedModelData"]) && count($clauseInformation["relatedModelData"]) > 0');
306  assert('is_string($onTableAliasName) || $onTableAliasName == null');
307  $relatedAttributeName = null;
308  if (isset($clauseInformation['relatedModelData']['attributeName']))
309  {
310  $relatedAttributeName = $clauseInformation['relatedModelData']['attributeName'];
311  }
312  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter(
313  $modelClassName,
314  $clauseInformation['attributeName'],
315  $relatedAttributeName);
316  $builder = new ModelWhereAndJoinBuilder($modelAttributeToDataProviderAdapter,
317  $joinTablesAdapter, true);
318  $builder->resolveJoins($onTableAliasName,
319  self::resolveCanUseFromJoins($onTableAliasName));
320  $relationModelClassName = $modelAttributeToDataProviderAdapter->getRelationModelClassName();
321  //if there is no more relatedModelData then we know this is the end of the nested information.
322  if (isset($clauseInformation['relatedModelData']['relatedModelData']) || isset($clauseInformation['relatedModelData']['concatedAttributeNames']))
323  {
324  return static::processMetadataClause($relationModelClassName, $clausePosition,
325  $clauseInformation['relatedModelData'],
326  $where, $joinTablesAdapter, $onTableAliasName);
327  }
328  //Supporting the use of relatedAttributeName. Alternatively you can use relatedModelData to produce the same results.
329  if (isset($clauseInformation['relatedModelData']['relatedAttributeName']))
330  {
331  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter(
332  $relationModelClassName,
333  $clauseInformation['relatedModelData']['attributeName'],
334  $clauseInformation['relatedModelData']['relatedAttributeName']);
335  $builder = new ModelWhereAndJoinBuilder($modelAttributeToDataProviderAdapter, $joinTablesAdapter, true,
336  ArrayUtil::getArrayValue($clauseInformation['relatedModelData'], 'modifierType'));
337  }
338  $builder->resolveJoinsAndBuildWhere(
339  $clauseInformation['relatedModelData']['operatorType'],
340  $clauseInformation['relatedModelData']['value'], $clausePosition, $where, $onTableAliasName);
341  }
342 
350  protected static function makeTableAliasAndColumnNamesForNonRelatedConcatedAttributes( $modelClassName,
351  $concatedAttributeNames,
352  $joinTablesAdapter,
353  $onTableAliasName = null)
354  {
355  assert('is_string($modelClassName)');
356  assert('is_array($concatedAttributeNames) && count($concatedAttributeNames) == 2');
357  assert('$joinTablesAdapter instanceof RedBeanModelJoinTablesQueryAdapter');
358  assert('is_string($onTableAliasName) || $onTableAliasName == null');
359  $tableAliasAndColumnNames = array();
360  foreach ($concatedAttributeNames as $attributeName)
361  {
362  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter(
363  $modelClassName, $attributeName);
364  $builder = new ModelWhereAndJoinBuilder(
365  $modelAttributeToDataProviderAdapter, $joinTablesAdapter, true);
366  $tableAliasName = $builder->resolveJoins($onTableAliasName,
367  self::resolveCanUseFromJoins($onTableAliasName));
368  $tableAliasAndColumnNames[] = array($tableAliasName,
369  $modelAttributeToDataProviderAdapter->getColumnName());
370  }
371  return $tableAliasAndColumnNames;
372  }
373 
383  protected static function addWherePartByClauseInformationForConcatedAttributes($operatorType, $value, &$where,
384  $whereKey, $tableAliasAndColumnNames)
385  {
386  assert('is_string($operatorType)');
387  assert('is_array($where)');
388  assert('is_int($whereKey)');
389  assert('is_array($tableAliasAndColumnNames) && count($tableAliasAndColumnNames) == 2');
391  if (is_string($value) || (is_array($value) && count($value) > 0) || $value !== null)
392  {
394  $tableAliasAndColumnNames[0][0], $tableAliasAndColumnNames[0][1]);
396  $tableAliasAndColumnNames[1][0], $tableAliasAndColumnNames[1][1]);
397  $concatedSqlPart = DatabaseCompatibilityUtil::concat(array($first, '\' \'', $second));
398  $where[$whereKey] = "($concatedSqlPart " . // Not Coding Standard
399  DatabaseCompatibilityUtil::getOperatorAndValueWherePart($operatorType, $value) . ")";
400  }
401  }
402  }
403 ?>
static addWherePartByClauseInformationForConcatedAttributes($operatorType, $value, &$where, $whereKey, $tableAliasAndColumnNames)
static resolveSortAttributeColumnName(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $onTableAliasName=null)
static processMetadataContainingRelatedModelDataClause($modelClassName, $clausePosition, $clauseInformation, &$where, $joinTablesAdapter, $onTableAliasName=null)
static resolveSortColumnNameString($tableAliasName, $resolvedSortColumnName)
static makeColumnNameWithTableAlias($tableAliasName, $columnName)
static resolveGroupByColumnNameString($tableAliasName, $resolvedSortColumnName)
static getArrayValue($array, $element, $defaultValue=null)
Definition: ArrayUtil.php:45
static resolveCanUseFromJoins($onTableAliasName)
static makeTableAliasAndColumnNamesForNonRelatedConcatedAttributes($modelClassName, $concatedAttributeNames, $joinTablesAdapter, $onTableAliasName=null)
static makeWhere($modelClassName, array $metadata, $joinTablesAdapter, $onTableAliasName=null)
static resolveConcatenation($string)
static resolveGroupByAttributeColumnName(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $onTableAliasName=null)
static processMetadataClause($modelClassName, $clausePosition, $clauseInformation, &$where, &$joinTablesAdapter, $onTableAliasName=null)
static getOperatorAndValueWherePart($operatorType, $value)
Generated on Tue Jul 7 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.