Merge pull request #4925 from KarinG/patch-1
[civicrm-core.git] / CRM / Campaign / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class is used to retrieve and display a range of
38 * contacts that match the given criteria (specifically for
39 * results of advanced search options.
40 *
41 */
42class CRM_Campaign_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
43
44 /**
45 * This defines two actions- View and Edit.
46 *
47 * @var array
6a488035
TO
48 */
49 static $_links = NULL;
50
51 /**
100fef9d 52 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
53 *
54 * @var array
6a488035
TO
55 */
56 static $_columnHeaders;
57
58 /**
59 * Properties of contact we're interested in displaying
60 * @var array
6a488035
TO
61 */
62 static $_properties = array(
63 'contact_id',
64 'sort_name',
65 'street_unit',
66 'street_name',
67 'street_number',
68 'street_address',
69 'city',
70 'postal_code',
71 'state_province',
72 'country',
73 'email',
74 'phone',
75 'campaign_id',
76 'survey_activity_id',
77 'survey_activity_target_id',
78 'survey_activity_target_contact_id',
79 );
80
81 /**
100fef9d 82 * Are we restricting ourselves to a single contact
6a488035 83 *
6a488035
TO
84 * @var boolean
85 */
86 protected $_single = FALSE;
87
88 /**
100fef9d 89 * Are we restricting ourselves to a single contact
6a488035 90 *
6a488035
TO
91 * @var boolean
92 */
93 protected $_limit = NULL;
94
95 /**
100fef9d 96 * What context are we being invoked from
6a488035 97 *
6a488035
TO
98 * @var string
99 */
100 protected $_context = NULL;
101
102 /**
100fef9d 103 * QueryParams is the array returned by exportValues called on
6a488035
TO
104 * the HTML_QuickForm_Controller for that page.
105 *
106 * @var array
6a488035
TO
107 */
108 public $_queryParams;
109
110 /**
100fef9d 111 * Represent the type of selector
6a488035
TO
112 *
113 * @var int
6a488035
TO
114 */
115 protected $_action;
116
117 /**
118 * The additional clause that we restrict the search with
119 *
120 * @var string
121 */
122 protected $_surveyClause = NULL;
123
124 /**
125 * The query object
126 *
127 * @var string
128 */
129 protected $_query;
130
131 /**
132 * Class constructor
133 *
7aaf6db0
TO
134 * @param array $queryParams
135 * Array of parameters for query.
dd244018 136 * @param \const|int $action - action of search basic or advanced.
7aaf6db0
TO
137 * @param string $surveyClause
138 * If the caller wants to further restrict the search.
139 * @param bool $single
140 * Are we dealing only with one contact?.
141 * @param int $limit
142 * How many voters do we want returned.
6a488035 143 *
dd244018
EM
144 * @param string $context
145 *
146 * @return \CRM_Campaign_Selector_Search
dd244018 147 */
5c2ea586
TO
148 function __construct(
149 &$queryParams,
94880218 150 $action = CRM_Core_Action::NONE,
6a488035 151 $surveyClause = NULL,
94880218
TO
152 $single = FALSE,
153 $limit = NULL,
154 $context = 'search'
6a488035
TO
155 ) {
156 // submitted form values
157 $this->_queryParams = &$queryParams;
158
353ffa53
TO
159 $this->_single = $single;
160 $this->_limit = $limit;
6a488035
TO
161 $this->_context = $context;
162
163 $this->_campaignClause = $surveyClause;
164 $this->_campaignFromClause = CRM_Utils_Array::value('fromClause', $surveyClause);
165 $this->_campaignWhereClause = CRM_Utils_Array::value('whereClause', $surveyClause);
166
167 // type of selector
168 $this->_action = $action;
169
170 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
171 NULL, NULL, FALSE, FALSE,
172 CRM_Contact_BAO_Query::MODE_CAMPAIGN,
173 TRUE
174 );
175 }
6a488035
TO
176
177 /**
178 * This method returns the links that are given for each search row.
179 * currently the links added for each row are
180 *
181 * - View
182 * - Edit
183 *
184 * @return array
6a488035
TO
185 */
186 static
00be9182 187 public function &links() {
6a488035
TO
188 return self::$_links = array();
189 }
190
191 /**
100fef9d 192 * Getter for array of the parameters required for creating pager.
6a488035 193 *
da6b46f4 194 * @param $action
c490a46a 195 * @param array $params
6a488035 196 */
00be9182 197 public function getPagerParams($action, &$params) {
6a488035
TO
198 $params['csvString'] = NULL;
199 $params['status'] = ts('Respondents') . ' %%StatusMessage%%';
200 $params['rowCount'] = ($this->_limit) ? $this->_limit : CRM_Utils_Pager::ROWCOUNT;
201 $params['buttonTop'] = 'PagerTopButton';
202 $params['buttonBottom'] = 'PagerBottomButton';
203 }
204
205 /**
206 * Returns total number of rows for the query.
207 *
208 * @param
209 *
a6c01b45
CW
210 * @return int
211 * Total number of rows
6a488035 212 */
00be9182 213 public function getTotalCount($action) {
6a488035
TO
214 return $this->_query->searchQuery(0, 0, NULL,
215 TRUE, FALSE,
216 FALSE, FALSE, FALSE,
217 $this->_campaignWhereClause,
218 NULL,
219 $this->_campaignFromClause
220 );
221 }
222
223 /**
100fef9d 224 * Returns all the rows in the given offset and rowCount
6a488035 225 *
3f8d2862 226 * @param string $action
7aaf6db0
TO
227 * The action being performed.
228 * @param int $offset
229 * The row number to start from.
230 * @param int $rowCount
231 * The number of rows to return.
232 * @param string $sort
233 * The sql string that describes the sort order.
3f8d2862 234 * @param string $output
7aaf6db0 235 * What should the result set include (web/email/csv).
6a488035 236 *
a6c01b45
CW
237 * @return int
238 * the total number of rows for this action
6a488035 239 */
00be9182 240 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
241 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
242 FALSE, FALSE,
243 FALSE, FALSE,
244 FALSE, $this->_campaignWhereClause,
245 NULL,
246 $this->_campaignFromClause
247 );
248
6a488035
TO
249 // process the result of the query
250 $rows = array();
251
94880218 252 while ($result->fetch()) {
d9ab802d 253 $this->_query->convertToPseudoNames($result);
6a488035
TO
254 $row = array();
255 // the columns we are interested in
256 foreach (self::$_properties as $property) {
257 if (property_exists($result, $property)) {
258 $row[$property] = $result->$property;
259 }
260 }
261 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contact_id;
262 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_type, FALSE, $result->contact_id);
263
264 $rows[] = $row;
265 }
266 $this->buildPrevNextCache($sort);
267
268 return $rows;
269 }
270
30c4e065
EM
271 /**
272 * @param $sort
273 */
00be9182 274 public function buildPrevNextCache($sort) {
6a488035
TO
275 //for prev/next pagination
276 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
277
278 if (!$crmPID) {
279 $cacheKey = "civicrm search {$this->_key}";
280 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact');
91da6cd5 281
6a488035
TO
282 $sql = $this->_query->searchQuery(0, 0, $sort,
283 FALSE, FALSE,
284 FALSE, FALSE,
285 TRUE, $this->_campaignWhereClause,
286 NULL,
287 $this->_campaignFromClause
288 );
289 list($select, $from) = explode(' FROM ', $sql);
290 $insertSQL = "
291INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data )
91da6cd5 292SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name
6a488035
TO
293FROM {$from}
294";
6a4257d4 295 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
6a488035 296 $result = CRM_Core_DAO::executeQuery($insertSQL);
6a4257d4 297 unset($errorScope);
6a488035
TO
298
299 if (is_a($result, 'DB_Error')) {
300 return;
301 }
302 // also record an entry in the cache key table, so we can delete it periodically
303 CRM_Core_BAO_Cache::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey);
304 }
305 }
306
307 /**
a6c01b45
CW
308 * @return array
309 * which contains an array of strings
6a488035
TO
310 **/
311 public function getQILL() {
312 return $this->_query->qill();
313 }
314
315 /**
100fef9d 316 * Returns the column headers as an array of tuples:
6a488035
TO
317 * (name, sortName (key to the sort array))
318 *
7aaf6db0
TO
319 * @param string $action
320 * The action being performed.
3f8d2862 321 * @param string $output
7aaf6db0 322 * What should the result set include (web/email/csv).
6a488035 323 *
a6c01b45
CW
324 * @return array
325 * the column headers that need to be displayed
6a488035
TO
326 */
327 public function &getColumnHeaders($action = NULL, $output = NULL) {
328 self::$_columnHeaders = array();
329
330 if (!$this->_single) {
331 $contactDetails = array(
94880218 332 array(
353ffa53 333 'name' => ts('Contact Name'),
6a488035
TO
334 'sort' => 'sort_name',
335 'direction' => CRM_Utils_Sort::ASCENDING,
336 ),
94880218 337 array(
353ffa53 338 'name' => ts('Street Number'),
6a488035
TO
339 'sort' => 'street_number',
340 ),
94880218 341 array(
353ffa53 342 'name' => ts('Street Name'),
6a488035
TO
343 'sort' => 'street_name',
344 ),
345 array('name' => ts('Street Address')),
94880218 346 array(
353ffa53 347 'name' => ts('City'),
6a488035
TO
348 'sort' => 'city',
349 ),
94880218 350 array(
353ffa53 351 'name' => ts('Postal Code'),
6a488035
TO
352 'sort' => 'postal_code',
353 ),
94880218 354 array(
353ffa53 355 'name' => ts('State'),
6a488035
TO
356 'sort' => 'state_province_name',
357 ),
358 array('name' => ts('Country')),
359 array('name' => ts('Email')),
360 array('name' => ts('Phone')),
361 );
362 self::$_columnHeaders = array_merge($contactDetails, self::$_columnHeaders);
363 }
364
365 return self::$_columnHeaders;
366 }
367
30c4e065
EM
368 /**
369 * @return string
370 */
00be9182 371 public function &getQuery() {
6a488035
TO
372 return $this->_query;
373 }
374
375 /**
100fef9d 376 * Name of export file.
6a488035 377 *
7aaf6db0
TO
378 * @param string $output
379 * Type of output.
6a488035 380 *
a6c01b45
CW
381 * @return string
382 * name of the file
6a488035 383 */
00be9182 384 public function getExportFileName($output = 'csv') {
6a488035
TO
385 return ts('CiviCRM Respondent Search');
386 }
387}