Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanModelsDataProvider.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 
40  class RedBeanModelsDataProvider extends CDataProvider
41  {
42  private $modelClassNamesAndSortAttributes;
43  private $sortDescending;
44  private $modelClassNamesAndSearchAttributeData;
45 
62  public function __construct($id, array $modelClassNamesAndSortAttributes = null, $sortDescending = false,
63  array $modelClassNamesAndSearchAttributeData = null, array $config = array())
64  {
65  assert('is_string($id) && $id != ""');
66  assert('$modelClassNamesAndSortAttributes === null || count($modelClassNamesAndSortAttributes) > 0');
67  assert('$modelClassNamesAndSearchAttributeData === null || count($modelClassNamesAndSearchAttributeData) > 0');
68  assert('is_bool($sortDescending)');
69  $this->modelClassNamesAndSortAttributes = $modelClassNamesAndSortAttributes;
70  $this->sortDescending = $sortDescending;
71  $this->modelClassNamesAndSearchAttributeData = $modelClassNamesAndSearchAttributeData;
72  $this->setId($id);
73  foreach ($config as $key => $value)
74  {
75  $this->$key = $value;
76  }
77  $sort = new RedBeanSort();
78  $sort->sortVar = $this->getId().'_sort';
79  $this->setSort($sort);
80  }
81 
85  protected function fetchData()
86  {
87  $pagination = $this->getPagination();
88  if (isset($pagination))
89  {
90  $pagination->setItemCount($this->getTotalItemCount());
91  $offset = $pagination->getOffset();
92  $limit = $pagination->getLimit();
93  }
94  else
95  {
96  $offset = 0;
97  $limit = null;
98  }
99  if (count($this->modelClassNamesAndSearchAttributeData) == 0)
100  {
101  return array();
102  }
103  $unionSql = static::makeUnionSql($this->modelClassNamesAndSearchAttributeData,
104  $this->modelClassNamesAndSortAttributes,
105  $this->sortDescending, $offset, $limit);
106  return $this->makeModelsBySql($unionSql);
107  }
108 
112  public static function makeUnionSql(array $modelClassNamesAndSearchAttributeData,
113  array $modelClassNamesAndSortAttributes = null, $sortDescending = false,
114  $offset = null, $limit = null)
115  {
116  assert('is_bool($sortDescending)');
117  $sqlStatementsToUnion = array();
118  foreach ($modelClassNamesAndSearchAttributeData as $identifier => $modelClassNameAndSearchAttributeData)
119  {
120  foreach ($modelClassNameAndSearchAttributeData as $modelClassName => $searchAttributeData)
121  {
122  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($modelClassName);
123  $where = ModelDataProviderUtil::makeWhere( $modelClassName, $searchAttributeData,
124  $joinTablesAdapter);
125  $orderByColumnName = null;
126  $tableName = $modelClassName::getTableName();
127  if ($modelClassNamesAndSortAttributes !== null)
128  {
129  if (isset($modelClassNamesAndSortAttributes[$modelClassName]))
130  {
131  $orderByColumnName = RedBeanModelDataProvider::resolveSortAttributeColumnName($modelClassName,
132  $joinTablesAdapter, $modelClassNamesAndSortAttributes[$modelClassName]);
133  }
134  else
135  {
136  throw new notSupportedException();
137  }
138  }
139  $quotedExtraSelectColumnNameAndAliases = array();
140  $quotedExtraSelectColumnNameAndAliases["'" . $modelClassName . "'"] = 'modelClassName';
141  if ($orderByColumnName != null)
142  {
143  $quotedExtraSelectColumnNameAndAliases[$orderByColumnName] = 'orderByColumn';
144  }
145  $sqlStatementsToUnion[] = $modelClassName::makeSubsetOrCountSqlQuery($tableName,
146  $joinTablesAdapter, null, null, $where, null, false,
147  $joinTablesAdapter->getSelectDistinct(),
148  $quotedExtraSelectColumnNameAndAliases);
149  }
150  }
151  $orderBy = null;
152  if ($modelClassNamesAndSortAttributes !== null)
153  {
154  $orderBy = 'orderByColumn';
155  if ($sortDescending)
156  {
157  $orderBy .= ' desc';
158  }
159  }
160  return self::makeSubsetUnionSqlQuery($sqlStatementsToUnion, $offset, $limit, $orderBy);
161  }
162 
169  protected function makeModelsBySql($sql)
170  {
171  assert('is_string($sql)');
172  $models = array();
173  $idsAndModelClassNames = ZurmoRedBean::getAll($sql);
174 
175  foreach ($idsAndModelClassNames as $data)
176  {
177  $modelClassName = $data['modelClassName'];
178  $tableName = $modelClassName::getTableName();
179  $bean = ZurmoRedBean::load($tableName, $data['id']);
180  $models[] = $modelClassName::makeModel($bean, $modelClassName);
181  }
182  return $models;
183  }
184 
185  protected static function makeSubsetUnionSqlQuery($sqlStatementsToUnion, $offset = null, $count = null,
186  $orderBy = null)
187  {
188  assert('is_array($sqlStatementsToUnion) && count($sqlStatementsToUnion) > 0');
189  assert('$offset === null || is_integer($offset) && $offset >= 0');
190  assert('$count === null || is_integer($count) && $count >= 1');
191  assert('$orderBy === null || is_string ($orderBy) && $orderBy != ""');
192  $sql = null;
193  foreach ($sqlStatementsToUnion as $sqlToBeUnioned)
194  {
195  if ($sql != null)
196  {
197  $sql .= ' UNION ';
198  }
199  $sql .= '(' . $sqlToBeUnioned . ')';
200  }
201  if ($orderBy !== null)
202  {
203  $sql .= " order by $orderBy";
204  }
205  if ($count !== null)
206  {
207  $sql .= " limit $count";
208  }
209  if ($offset !== null)
210  {
211  $sql .= " offset $offset";
212  }
213  return $sql;
214  }
215 
219  public function getSort()
220  {
221  return false;
222  }
223 
230  public function calculateTotalItemCount()
231  {
232  $totalCount = 0;
233  foreach ($this->modelClassNamesAndSearchAttributeData as $identifier => $modelClassNameAndSearchAttributeData)
234  {
235  foreach ($modelClassNameAndSearchAttributeData as $modelClassName => $searchAttributeData)
236  {
237  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($modelClassName);
238  $where = ModelDataProviderUtil::makeWhere($modelClassName, $searchAttributeData, $joinTablesAdapter);
239  $totalCount = $totalCount + RedBeanModel::getCount($joinTablesAdapter, $where, $modelClassName, true);
240  }
241  }
242  return $totalCount;
243  }
244 
248  protected function fetchKeys()
249  {
250  $keys = array();
251  foreach ($this->getData() as $model)
252  {
253  $keys[] = $model->id;
254  }
255  return $keys;
256  }
257  }
258 ?>
__construct($id, array $modelClassNamesAndSortAttributes=null, $sortDescending=false, array $modelClassNamesAndSearchAttributeData=null, array $config=array())
static makeWhere($modelClassName, array $metadata, $joinTablesAdapter, $onTableAliasName=null)
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static makeUnionSql(array $modelClassNamesAndSearchAttributeData, array $modelClassNamesAndSortAttributes=null, $sortDescending=false, $offset=null, $limit=null)
Generated on Sun Jul 12 2020 07:10:26
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.