Merge pull request #1625 from pradpnayak/CRM-13340
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / Base.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35 class CRM_Contact_Form_Search_Custom_Base {
36
37 protected $_formValues;
38
39 protected $_columns;
40
41 protected $_stateID;
42
43 function __construct(&$formValues) {
44 $this->_formValues = &$formValues;
45 }
46
47 function count() {
48 return CRM_Core_DAO::singleValueQuery($this->sql('count(distinct contact_a.id) as total'));
49 }
50
51 function summary() {
52 return NULL;
53 }
54
55 function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
56 $sql = $this->sql(
57 'contact_a.id as contact_id',
58 $offset,
59 $rowcount,
60 $sort
61 );
62 $this->validateUserSQL($sql);
63
64 if ($returnSQL) {
65 return $sql;
66 }
67
68 return CRM_Core_DAO::composeQuery($sql, CRM_Core_DAO::$_nullArray);
69 }
70
71 function sql(
72 $selectClause,
73 $offset = 0,
74 $rowcount = 0,
75 $sort = NULL,
76 $includeContactIDs = FALSE,
77 $groupBy = NULL
78 ) {
79
80 $sql = "SELECT $selectClause " . $this->from();
81 $where = $this->where();
82 if (!empty($where)) {
83 $sql .= " WHERE " . $where;
84 }
85
86 if ($includeContactIDs) {
87 $this->includeContactIDs($sql,
88 $this->_formValues
89 );
90 }
91
92 if ($groupBy) {
93 $sql .= " $groupBy ";
94 }
95
96 $this->addSortOffset($sql, $offset, $rowcount, $sort);
97 return $sql;
98 }
99
100 function templateFile() {
101 return NULL;
102 }
103
104 function &columns() {
105 return $this->_columns;
106 }
107
108 static function includeContactIDs(&$sql, &$formValues) {
109 $contactIDs = array();
110 foreach ($formValues as $id => $value) {
111 if ($value &&
112 substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX
113 ) {
114 $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN);
115 }
116 }
117
118 if (!empty($contactIDs)) {
119 $contactIDs = implode(', ', $contactIDs);
120 $sql .= " AND contact_a.id IN ( $contactIDs )";
121 }
122 }
123
124 function addSortOffset(&$sql, $offset, $rowcount, $sort) {
125 if (!empty($sort)) {
126 if (is_string($sort)) {
127 $sql .= " ORDER BY $sort ";
128 }
129 else {
130 $sql .= " ORDER BY " . trim($sort->orderBy());
131 }
132 }
133
134 if ($rowcount > 0 && $offset >= 0) {
135 $sql .= " LIMIT $offset, $rowcount ";
136 }
137 }
138
139 function validateUserSQL(&$sql, $onlyWhere = FALSE) {
140 $includeStrings = array('contact_a');
141 $excludeStrings = array('insert', 'delete', 'update');
142
143 if (!$onlyWhere) {
144 $includeStrings += array('select', 'from', 'where', 'civicrm_contact');
145 }
146
147 foreach ($includeStrings as $string) {
148 if (stripos($sql, $string) === FALSE) {
149 CRM_Core_Error::fatal(ts('Could not find \'%1\' string in SQL clause.',
150 array(1 => $string)
151 ));
152 }
153 }
154
155 foreach ($excludeStrings as $string) {
156 if (preg_match('/(\s' . $string . ')|(' . $string . '\s)/i', $sql)) {
157 CRM_Core_Error::fatal(ts('Found illegal \'%1\' string in SQL clause.',
158 array(1 => $string)
159 ));
160 }
161 }
162 }
163
164 function whereClause(&$where, &$params) {
165 return CRM_Core_DAO::composeQuery($where, $params, TRUE);
166 }
167
168 // override this method to define the contact query object
169 // used for creating $sql
170 function getQueryObj() {
171 return NULL;
172 }
173 }