Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanModelDataProvider.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 RedBeanModelDataProvider extends CDataProvider
41  {
42  protected $modelClassName;
43  protected $sortAttribute;
44  protected $sortDescending;
45  protected $searchAttributeData;
46  protected $calculatedTotalItemCount;
47  protected $offset;
48 
52  public function __construct($modelClassName, $sortAttribute = null, $sortDescending = false, array $searchAttributeData = array(), array $config = array())
53  {
54  assert('is_string($modelClassName) && $modelClassName != ""');
55  assert('$sortAttribute === null || is_string($sortAttribute) && $sortAttribute != ""');
56  assert('$sortDescending == 0 || $sortDescending == 1 || is_bool($sortDescending) || $sortDescending === null');
57  $this->modelClassName = $modelClassName;
58  $this->sortAttribute = $sortAttribute;
59  $this->sortDescending = $sortDescending;
60  $this->searchAttributeData = $searchAttributeData;
61  $this->setId($this->modelClassName);
62  foreach ($config as $key => $value)
63  {
64  $this->$key = $value;
65  }
66  $sort = new RedBeanSort($this->modelClassName);
67  $sort->sortVar = $this->getId().'_sort';
68  $sort->setSortAttribute($sortAttribute);
69  $sort->setSortDescending($sortDescending);
70  $this->setSort($sort);
71  }
72 
73  public function getModelClassName()
74  {
75  return $this->modelClassName;
76  }
77 
83  public function setOffset($offset)
84  {
85  assert('is_int($offset)');
86  $this->offset = $offset;
87  }
88 
94  protected function fetchData()
95  {
96  $pagination = $this->getPagination();
97  if (isset($pagination))
98  {
99  $totalItemCount = $this->getTotalItemCount();
100  $pagination->setItemCount($totalItemCount);
101  $offset = $pagination->getOffset();
102  $limit = $pagination->getLimit();
103  }
104  else
105  {
106  $offset = 0;
107  $limit = null;
108  }
109  if ($this->offset !== null)
110  {
111  $offset = $this->offset;
112  }
113  if ($totalItemCount == 0)
114  {
115  return array();
116  }
117  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($this->modelClassName);
118  $where = $this->makeWhere($this->modelClassName, $this->searchAttributeData, $joinTablesAdapter);
119  $orderBy = null;
120  if ($this->sortAttribute !== null)
121  {
122  $orderBy = self::resolveSortAttributeColumnName($this->modelClassName,
123  $joinTablesAdapter, $this->sortAttribute);
124  if ($this->sortDescending)
125  {
126  $orderBy .= ' desc';
127  }
128  }
129  else
130  {
131  try
132  {
133  $sortAttribute = self::getSortAttributeName($this->modelClassName);
134  if ($sortAttribute != null)
135  {
136  $orderBy = self::resolveSortAttributeColumnName($this->modelClassName, $joinTablesAdapter, $sortAttribute);
137  if ($this->sortDescending)
138  {
139  $orderBy .= ' desc';
140  }
141  }
142  }
143  catch (NotImplementedException $e)
144  {
145  }
146  }
147  $modelClassName = $this->modelClassName;
148  $this->resolveExtraSql($joinTablesAdapter, $where);
149  return $modelClassName::getSubset($joinTablesAdapter, $offset, $limit, $where, $orderBy,
150  $this->modelClassName, $joinTablesAdapter->getSelectDistinct());
151  }
152 
156  public static function resolveSortAttributeColumnName($modelClassName, &$joinTablesAdapter, $sortAttribute)
157  {
158  assert('$sortAttribute === null || is_string($sortAttribute) && $sortAttribute != ""');
159  $sortRelatedAttribute = null;
160  if ($modelClassName::isRelation($sortAttribute))
161  {
162  $relationType = $modelClassName::getRelationType($sortAttribute);
163  //MANY_MANY not supported currently for sorting.
164  assert('$relationType != RedBeanModel::MANY_MANY');
165  $relationModelClassName = $modelClassName::getRelationModelClassName($sortAttribute);
166  $sortRelatedAttribute = self::getSortAttributeName($relationModelClassName);
167  }
168  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter(
169  $modelClassName, $sortAttribute, $sortRelatedAttribute);
170  return ModelDataProviderUtil::resolveSortAttributeColumnName($modelAttributeToDataProviderAdapter,
171  $joinTablesAdapter);
172  }
173 
177  public static function getSortAttributeName($modelClassName)
178  {
179  $metadata = $modelClassName::getMetadata();
180  while (!isset($metadata[$modelClassName]['defaultSortAttribute']))
181  {
182  $modelClassName = get_parent_class($modelClassName);
183  if ($modelClassName == 'RedBeanModel')
184  {
185  //This means the sortAttribute value was not found.
186  throw new NotImplementedException();
187  }
188  }
189  assert('isset($metadata[$modelClassName]["defaultSortAttribute"])');
190  return $metadata[$modelClassName]['defaultSortAttribute'];
191  }
192 
196  public function getSort()
197  {
198  if (($sort = parent::getSort()) !== false)
199  {
200  $sort->modelClass = $this->modelClassName;
201  }
202  return $sort;
203  }
204 
214  public static function makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
215  {
216  return ModelDataProviderUtil::makeWhere($modelClassName, $metadata, $joinTablesAdapter);
217  }
218 
223  public function calculateTotalItemCount()
224  {
225  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($this->modelClassName);
226  $where = $this->makeWhere($this->modelClassName, $this->searchAttributeData, $joinTablesAdapter);
227  $modelClassName = $this->modelClassName;
228  $this->resolveExtraSql($joinTablesAdapter, $where);
229  return $modelClassName::getCount($joinTablesAdapter, $where, $this->modelClassName, true);
230  }
231 
235  protected function fetchKeys()
236  {
237  $keys = array();
238  foreach ($this->getData() as $model)
239  {
240  $keys[] = $model->id;
241  }
242  return $keys;
243  }
244 
250  protected function resolveExtraSql(RedBeanModelJoinTablesQueryAdapter &$joinTablesAdapter, &$where)
251  {
252  }
253  }
254 ?>
resolveExtraSql(RedBeanModelJoinTablesQueryAdapter &$joinTablesAdapter, &$where)
static resolveSortAttributeColumnName(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $onTableAliasName=null)
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
static getSortAttributeName($modelClassName)
static makeWhere($modelClassName, array $metadata, $joinTablesAdapter, $onTableAliasName=null)
__construct($modelClassName, $sortAttribute=null, $sortDescending=false, array $searchAttributeData=array(), array $config=array())
Generated on Wed Dec 2 2020 07:10:23
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.