Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
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 | */ |
37 | class 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 | 269 | //for prev/next pagination |
a3d827a7 | 270 | $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer'); |
6a488035 TO |
271 | |
272 | if (!$crmPID) { | |
273 | $cacheKey = "civicrm search {$this->_key}"; | |
744b4e34 | 274 | Civi::service('prevnext')->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); | |
9df3fff5 | 284 | $selectSQL = " |
2c8b42d5 | 285 | SELECT %1, contact_a.id, contact_a.display_name |
6a488035 TO |
286 | FROM {$from} |
287 | "; | |
6a488035 | 288 | |
9df3fff5 | 289 | try { |
2c8b42d5 | 290 | Civi::service('prevnext')->fillWithSql($cacheKey, $selectSQL, [1 => [$cacheKey, 'String']]); |
9df3fff5 TO |
291 | } |
292 | catch (CRM_Core_Exception $e) { | |
293 | // Heavy handed, no? Seems like this merits an explanation. | |
6a488035 TO |
294 | return; |
295 | } | |
9df3fff5 | 296 | |
e37c68e1 TO |
297 | if (Civi::service('prevnext') instanceof CRM_Core_PrevNextCache_Sql) { |
298 | // SQL-backed prevnext cache uses an extra record for pruning the cache. | |
299 | CRM_Core_BAO_Cache::setItem($cacheKey, 'CiviCRM Search PrevNextCache', $cacheKey); | |
300 | } | |
6a488035 TO |
301 | } |
302 | } | |
303 | ||
304 | /** | |
a6c01b45 CW |
305 | * @return array |
306 | * which contains an array of strings | |
28a04ea9 | 307 | */ |
6a488035 TO |
308 | public function getQILL() { |
309 | return $this->_query->qill(); | |
310 | } | |
311 | ||
312 | /** | |
100fef9d | 313 | * Returns the column headers as an array of tuples: |
6a488035 TO |
314 | * (name, sortName (key to the sort array)) |
315 | * | |
7aaf6db0 TO |
316 | * @param string $action |
317 | * The action being performed. | |
3f8d2862 | 318 | * @param string $output |
7aaf6db0 | 319 | * What should the result set include (web/email/csv). |
6a488035 | 320 | * |
a6c01b45 CW |
321 | * @return array |
322 | * the column headers that need to be displayed | |
6a488035 TO |
323 | */ |
324 | public function &getColumnHeaders($action = NULL, $output = NULL) { | |
325 | self::$_columnHeaders = array(); | |
326 | ||
327 | if (!$this->_single) { | |
328 | $contactDetails = array( | |
94880218 | 329 | array( |
353ffa53 | 330 | 'name' => ts('Contact Name'), |
6a488035 TO |
331 | 'sort' => 'sort_name', |
332 | 'direction' => CRM_Utils_Sort::ASCENDING, | |
333 | ), | |
94880218 | 334 | array( |
353ffa53 | 335 | 'name' => ts('Street Number'), |
6a488035 TO |
336 | 'sort' => 'street_number', |
337 | ), | |
94880218 | 338 | array( |
353ffa53 | 339 | 'name' => ts('Street Name'), |
6a488035 TO |
340 | 'sort' => 'street_name', |
341 | ), | |
342 | array('name' => ts('Street Address')), | |
94880218 | 343 | array( |
353ffa53 | 344 | 'name' => ts('City'), |
6a488035 TO |
345 | 'sort' => 'city', |
346 | ), | |
94880218 | 347 | array( |
353ffa53 | 348 | 'name' => ts('Postal Code'), |
6a488035 TO |
349 | 'sort' => 'postal_code', |
350 | ), | |
94880218 | 351 | array( |
353ffa53 | 352 | 'name' => ts('State'), |
6a488035 TO |
353 | 'sort' => 'state_province_name', |
354 | ), | |
355 | array('name' => ts('Country')), | |
356 | array('name' => ts('Email')), | |
357 | array('name' => ts('Phone')), | |
358 | ); | |
359 | self::$_columnHeaders = array_merge($contactDetails, self::$_columnHeaders); | |
360 | } | |
361 | ||
362 | return self::$_columnHeaders; | |
363 | } | |
364 | ||
30c4e065 EM |
365 | /** |
366 | * @return string | |
367 | */ | |
00be9182 | 368 | public function &getQuery() { |
6a488035 TO |
369 | return $this->_query; |
370 | } | |
371 | ||
372 | /** | |
100fef9d | 373 | * Name of export file. |
6a488035 | 374 | * |
7aaf6db0 TO |
375 | * @param string $output |
376 | * Type of output. | |
6a488035 | 377 | * |
a6c01b45 CW |
378 | * @return string |
379 | * name of the file | |
6a488035 | 380 | */ |
00be9182 | 381 | public function getExportFileName($output = 'csv') { |
6a488035 TO |
382 | return ts('CiviCRM Respondent Search'); |
383 | } | |
96025800 | 384 | |
6a488035 | 385 | } |