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