Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
GroupedAttributeCountUtil.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  {
49  public static function getCountData(
50  $modelClassName,
51  $attributeName,
52  $filterByAttributeName = null,
53  $filterByAttributeValue = null)
54  {
55  assert('($filterByAttributeName == null && $filterByAttributeValue == null) ||
56  ($filterByAttributeName != null && $filterByAttributeValue != null)');
57  $tableName = $modelClassName::getTableName();
58  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($modelClassName);
59  $selectQueryAdapter = new RedBeanModelSelectQueryAdapter();
60  if ($modelClassName::isRelation($attributeName) && $modelClassName::getRelationType($attributeName) == RedBeanModel::HAS_MANY)
61  {
62  assert('$attributeName == "values"'); //until we expand support on this method.
63  $relationModelClassName = $modelClassName::getRelationModelClassName($attributeName);
64  $attributeTableName = $relationModelClassName::getTableName();
65  $columnName = 'value';
66  $relationTableAliasName = $joinTablesAdapter->addLeftTableAndGetAliasName(
67  $attributeTableName,
68  'id',
69  $tableName,
70  $tableName . '_id');
71  }
72  else
73  {
74  $attributeTableName = $tableName;
75  $columnName = $modelClassName::getColumnNameByAttribute($attributeName);
76  }
77 
78  $where = null;
79  if ($filterByAttributeName != null)
80  {
81  $attributeModelClassName = $modelClassName::resolveAttributeModelClassName($filterByAttributeName);
82  $filterByAttributeTableName = $attributeModelClassName::getTableName();
83  $filterByColumnName = $modelClassName::getColumnNameByAttribute($filterByAttributeName);
84  $where = $filterByAttributeTableName . '.' . $filterByColumnName . '=' . $filterByAttributeValue;
85  if ($filterByAttributeTableName != $tableName)
86  {
87  $joinTablesAdapter->addFromTableAndGetAliasName($filterByAttributeTableName, $filterByAttributeTableName . '_id', $tableName);
88  }
89  }
90  $where = $where;
91  $selectDistinct = false;
92  $selectQueryAdapter->addClause($attributeTableName, $columnName, 'attribute');
93  $selectQueryAdapter->addCountClause($tableName, 'id', 'count');
94  $groupBy = static::getGroupBySqlPart($attributeTableName, $columnName);
95  $sql = SQLQueryUtil::makeQuery($tableName, $selectQueryAdapter,
96  $joinTablesAdapter, null, null, $where, null, $groupBy);
97  return static::runQueryBySqlAndGetCountData($sql);
98  }
99 
100  protected static function getGroupBySqlPart($tableName, $columnName)
101  {
102  assert('$tableName');
103  assert('$columnName');
105  return "{$quote}$tableName{$quote}.{$quote}$columnName{$quote}";
106  }
107 
108  protected static function runQueryBySqlAndGetCountData($sql)
109  {
110  assert('is_string($sql)');
111  $countData = array();
112  $rows = ZurmoRedBean::getAll($sql);
113  foreach ($rows as $row)
114  {
115  $countData[$row['attribute']] = $row['count'];
116  }
117  return $countData;
118  }
119  }
120 ?>
static getCountData($modelClassName, $attributeName, $filterByAttributeName=null, $filterByAttributeValue=null)
const HAS_MANY
Definition: BeanModel.php:71
Generated on Thu Jul 9 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.