commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Contact / Form / Search / Custom / Base.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 /**
44 * @param $formValues
45 */
46 public function __construct(&$formValues) {
47 $this->_formValues = &$formValues;
48 }
49
50 /**
51 * Builds the list of tasks or actions that a searcher can perform on a result set.
52 *
53 * The returned array completely replaces the task list, so a child class that
54 * wants to modify the existing list should manipulate the result of this method.
55 *
56 * @param CRM_Core_Form_Search $form
57 * @return array
58 */
59 public function buildTaskList(CRM_Core_Form_Search $form) {
60 return $form->getVar('_taskList');
61 }
62
63 /**
64 * @return null|string
65 */
66 public function count() {
67 return CRM_Core_DAO::singleValueQuery($this->sql('count(distinct contact_a.id) as total'));
68 }
69
70 /**
71 * @return null
72 */
73 public function summary() {
74 return NULL;
75 }
76
77 /**
78 * @param int $offset
79 * @param int $rowcount
80 * @param null $sort
81 * @param bool $returnSQL
82 *
83 * @return string
84 */
85 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
86 $sql = $this->sql(
87 'contact_a.id as contact_id',
88 $offset,
89 $rowcount,
90 $sort
91 );
92 $this->validateUserSQL($sql);
93
94 if ($returnSQL) {
95 return $sql;
96 }
97
98 return CRM_Core_DAO::composeQuery($sql, CRM_Core_DAO::$_nullArray);
99 }
100
101 /**
102 * @param $selectClause
103 * @param int $offset
104 * @param int $rowcount
105 * @param null $sort
106 * @param bool $includeContactIDs
107 * @param null $groupBy
108 *
109 * @return string
110 */
111 public function sql(
112 $selectClause,
113 $offset = 0,
114 $rowcount = 0,
115 $sort = NULL,
116 $includeContactIDs = FALSE,
117 $groupBy = NULL
118 ) {
119
120 $sql = "SELECT $selectClause " . $this->from();
121 $where = $this->where();
122 if (!empty($where)) {
123 $sql .= " WHERE " . $where;
124 }
125
126 if ($includeContactIDs) {
127 $this->includeContactIDs($sql,
128 $this->_formValues
129 );
130 }
131
132 if ($groupBy) {
133 $sql .= " $groupBy ";
134 }
135
136 $this->addSortOffset($sql, $offset, $rowcount, $sort);
137 return $sql;
138 }
139
140 /**
141 * @return null
142 */
143 public function templateFile() {
144 return NULL;
145 }
146
147 public function &columns() {
148 return $this->_columns;
149 }
150
151 /**
152 * @param $sql
153 * @param $formValues
154 */
155 public static function includeContactIDs(&$sql, &$formValues) {
156 $contactIDs = array();
157 foreach ($formValues as $id => $value) {
158 if ($value &&
159 substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX
160 ) {
161 $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN);
162 }
163 }
164
165 if (!empty($contactIDs)) {
166 $contactIDs = implode(', ', $contactIDs);
167 $sql .= " AND contact_a.id IN ( $contactIDs )";
168 }
169 }
170
171 /**
172 * @param $sql
173 * @param $offset
174 * @param $rowcount
175 * @param $sort
176 */
177 public function addSortOffset(&$sql, $offset, $rowcount, $sort) {
178 if (!empty($sort)) {
179 if (is_string($sort)) {
180 $sort = CRM_Utils_Type::escape($sort, 'String');
181 $sql .= " ORDER BY $sort ";
182 }
183 else {
184 $sql .= " ORDER BY " . trim($sort->orderBy());
185 }
186 }
187
188 if ($rowcount > 0 && $offset >= 0) {
189 $offset = CRM_Utils_Type::escape($offset, 'Int');
190 $rowcount = CRM_Utils_Type::escape($rowcount, 'Int');
191
192 $sql .= " LIMIT $offset, $rowcount ";
193 }
194 }
195
196 /**
197 * @param $sql
198 * @param bool $onlyWhere
199 *
200 * @throws Exception
201 */
202 public function validateUserSQL(&$sql, $onlyWhere = FALSE) {
203 $includeStrings = array('contact_a');
204 $excludeStrings = array('insert', 'delete', 'update');
205
206 if (!$onlyWhere) {
207 $includeStrings += array('select', 'from', 'where', 'civicrm_contact');
208 }
209
210 foreach ($includeStrings as $string) {
211 if (stripos($sql, $string) === FALSE) {
212 CRM_Core_Error::fatal(ts('Could not find \'%1\' string in SQL clause.',
213 array(1 => $string)
214 ));
215 }
216 }
217
218 foreach ($excludeStrings as $string) {
219 if (preg_match('/(\s' . $string . ')|(' . $string . '\s)/i', $sql)) {
220 CRM_Core_Error::fatal(ts('Found illegal \'%1\' string in SQL clause.',
221 array(1 => $string)
222 ));
223 }
224 }
225 }
226
227 /**
228 * @param $where
229 * @param array $params
230 *
231 * @return string
232 */
233 public function whereClause(&$where, &$params) {
234 return CRM_Core_DAO::composeQuery($where, $params, TRUE);
235 }
236
237 /**
238 * override this method to define the contact query object
239 * used for creating $sql
240 * @return null
241 */
242 public function getQueryObj() {
243 return NULL;
244 }
245
246 /**
247 * Set the title.
248 *
249 * @param string $title
250 */
251 public function setTitle($title) {
252 if ($title) {
253 CRM_Utils_System::setTitle($title);
254 }
255 else {
256 CRM_Utils_System::setTitle(ts('Search'));
257 }
258 }
259
260 }