more comment fixes
[civicrm-core.git] / CRM / Campaign / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 /**
fe482240 111 * Represent the type of selector.
6a488035
TO
112 *
113 * @var int
6a488035
TO
114 */
115 protected $_action;
116
117 /**
fe482240 118 * The additional clause that we restrict the search with.
6a488035
TO
119 *
120 * @var string
121 */
122 protected $_surveyClause = NULL;
123
124 /**
fe482240 125 * The query object.
6a488035
TO
126 *
127 * @var string
128 */
129 protected $_query;
130
131 /**
fe482240 132 * Class constructor.
6a488035 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 */
28a04ea9 148 public function __construct(
5c2ea586 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 185 */
28a04ea9 186 static public function &links() {
6a488035
TO
187 return self::$_links = array();
188 }
189
190 /**
100fef9d 191 * Getter for array of the parameters required for creating pager.
6a488035 192 *
da6b46f4 193 * @param $action
c490a46a 194 * @param array $params
6a488035 195 */
00be9182 196 public function getPagerParams($action, &$params) {
6a488035
TO
197 $params['csvString'] = NULL;
198 $params['status'] = ts('Respondents') . ' %%StatusMessage%%';
199 $params['rowCount'] = ($this->_limit) ? $this->_limit : CRM_Utils_Pager::ROWCOUNT;
200 $params['buttonTop'] = 'PagerTopButton';
201 $params['buttonBottom'] = 'PagerBottomButton';
202 }
203
204 /**
205 * Returns total number of rows for the query.
206 *
207 * @param
208 *
a6c01b45
CW
209 * @return int
210 * Total number of rows
6a488035 211 */
00be9182 212 public function getTotalCount($action) {
6a488035
TO
213 return $this->_query->searchQuery(0, 0, NULL,
214 TRUE, FALSE,
215 FALSE, FALSE, FALSE,
216 $this->_campaignWhereClause,
217 NULL,
218 $this->_campaignFromClause
219 );
220 }
221
222 /**
fe482240 223 * Returns all the rows in the given offset and rowCount.
6a488035 224 *
3f8d2862 225 * @param string $action
7aaf6db0
TO
226 * The action being performed.
227 * @param int $offset
228 * The row number to start from.
229 * @param int $rowCount
230 * The number of rows to return.
231 * @param string $sort
232 * The sql string that describes the sort order.
3f8d2862 233 * @param string $output
7aaf6db0 234 * What should the result set include (web/email/csv).
6a488035 235 *
a6c01b45
CW
236 * @return int
237 * the total number of rows for this action
6a488035 238 */
00be9182 239 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
240 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
241 FALSE, FALSE,
242 FALSE, FALSE,
243 FALSE, $this->_campaignWhereClause,
244 NULL,
245 $this->_campaignFromClause
246 );
247
6a488035
TO
248 // process the result of the query
249 $rows = array();
250
94880218 251 while ($result->fetch()) {
d9ab802d 252 $this->_query->convertToPseudoNames($result);
6a488035
TO
253 $row = array();
254 // the columns we are interested in
255 foreach (self::$_properties as $property) {
256 if (property_exists($result, $property)) {
257 $row[$property] = $result->$property;
258 }
259 }
260 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->contact_id;
261 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_type, FALSE, $result->contact_id);
262
263 $rows[] = $row;
264 }
265 $this->buildPrevNextCache($sort);
266
267 return $rows;
268 }
269
30c4e065
EM
270 /**
271 * @param $sort
272 */
00be9182 273 public function buildPrevNextCache($sort) {
6a488035
TO
274 //for prev/next pagination
275 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
276
277 if (!$crmPID) {
278 $cacheKey = "civicrm search {$this->_key}";
279 CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey, 'civicrm_contact');
91da6cd5 280
6a488035
TO
281 $sql = $this->_query->searchQuery(0, 0, $sort,
282 FALSE, FALSE,
283 FALSE, FALSE,
284 TRUE, $this->_campaignWhereClause,
285 NULL,
286 $this->_campaignFromClause
287 );
288 list($select, $from) = explode(' FROM ', $sql);
289 $insertSQL = "
290INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data )
91da6cd5 291SELECT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.display_name
6a488035
TO
292FROM {$from}
293";
6a4257d4 294 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
6a488035 295 $result = CRM_Core_DAO::executeQuery($insertSQL);
6a4257d4 296 unset($errorScope);
6a488035
TO
297
298 if (is_a($result, 'DB_Error')) {
299 return;
300 }
301 // also record an entry in the cache key table, so we can delete it periodically
302 CRM_Core_BAO_Cache::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey);
303 }
304 }
305
306 /**
a6c01b45
CW
307 * @return array
308 * which contains an array of strings
28a04ea9 309 */
6a488035
TO
310 public function getQILL() {
311 return $this->_query->qill();
312 }
313
314 /**
100fef9d 315 * Returns the column headers as an array of tuples:
6a488035
TO
316 * (name, sortName (key to the sort array))
317 *
7aaf6db0
TO
318 * @param string $action
319 * The action being performed.
3f8d2862 320 * @param string $output
7aaf6db0 321 * What should the result set include (web/email/csv).
6a488035 322 *
a6c01b45
CW
323 * @return array
324 * the column headers that need to be displayed
6a488035
TO
325 */
326 public function &getColumnHeaders($action = NULL, $output = NULL) {
327 self::$_columnHeaders = array();
328
329 if (!$this->_single) {
330 $contactDetails = array(
94880218 331 array(
353ffa53 332 'name' => ts('Contact Name'),
6a488035
TO
333 'sort' => 'sort_name',
334 'direction' => CRM_Utils_Sort::ASCENDING,
335 ),
94880218 336 array(
353ffa53 337 'name' => ts('Street Number'),
6a488035
TO
338 'sort' => 'street_number',
339 ),
94880218 340 array(
353ffa53 341 'name' => ts('Street Name'),
6a488035
TO
342 'sort' => 'street_name',
343 ),
344 array('name' => ts('Street Address')),
94880218 345 array(
353ffa53 346 'name' => ts('City'),
6a488035
TO
347 'sort' => 'city',
348 ),
94880218 349 array(
353ffa53 350 'name' => ts('Postal Code'),
6a488035
TO
351 'sort' => 'postal_code',
352 ),
94880218 353 array(
353ffa53 354 'name' => ts('State'),
6a488035
TO
355 'sort' => 'state_province_name',
356 ),
357 array('name' => ts('Country')),
358 array('name' => ts('Email')),
359 array('name' => ts('Phone')),
360 );
361 self::$_columnHeaders = array_merge($contactDetails, self::$_columnHeaders);
362 }
363
364 return self::$_columnHeaders;
365 }
366
30c4e065
EM
367 /**
368 * @return string
369 */
00be9182 370 public function &getQuery() {
6a488035
TO
371 return $this->_query;
372 }
373
374 /**
100fef9d 375 * Name of export file.
6a488035 376 *
7aaf6db0
TO
377 * @param string $output
378 * Type of output.
6a488035 379 *
a6c01b45
CW
380 * @return string
381 * name of the file
6a488035 382 */
00be9182 383 public function getExportFileName($output = 'csv') {
6a488035
TO
384 return ts('CiviCRM Respondent Search');
385 }
96025800 386
6a488035 387}