Set version to 5.20.beta1
[civicrm-core.git] / CRM / Mailing / Selector / Search.php
CommitLineData
2cc569f2
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
2cc569f2 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
2cc569f2
PJ
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 */
2cc569f2
PJ
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
2cc569f2
PJ
32 */
33
34/**
35 * This class is used to retrieve and display a range of
36 * contacts that match the given criteria (specifically for
37 * results of advanced search options.
2cc569f2
PJ
38 */
39class CRM_Mailing_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
40
41 /**
42 * This defines two actions- View and Edit.
43 *
44 * @var array
2cc569f2 45 */
7e8c8317 46 public static $_links = NULL;
2cc569f2
PJ
47
48 /**
100fef9d 49 * We use desc to remind us what that column is, name is used in the tpl
2cc569f2
PJ
50 *
51 * @var array
2cc569f2 52 */
7e8c8317 53 public static $_columnHeaders;
2cc569f2
PJ
54
55 /**
56 * Properties of contact we're interested in displaying
57 * @var array
2cc569f2 58 */
7e8c8317 59 public static $_properties = [
2cc569f2
PJ
60 'contact_id',
61 'mailing_id',
62 'mailing_name',
c00b95ef 63 'language',
2cc569f2
PJ
64 'sort_name',
65 'email',
66 'mailing_subject',
67 'email_on_hold',
68 'contact_opt_out',
69 'mailing_job_status',
21dfd5f5 70 'mailing_job_end_date',
be2fb01f 71 ];
2cc569f2
PJ
72
73 /**
100fef9d 74 * Are we restricting ourselves to a single contact
2cc569f2 75 *
d51c6add 76 * @var bool
2cc569f2
PJ
77 */
78 protected $_single = FALSE;
79
80 /**
100fef9d 81 * Are we restricting ourselves to a single contact
2cc569f2 82 *
d51c6add 83 * @var bool
2cc569f2
PJ
84 */
85 protected $_limit = NULL;
86
87 /**
100fef9d 88 * What context are we being invoked from
2cc569f2 89 *
2cc569f2
PJ
90 * @var string
91 */
92 protected $_context = NULL;
93
94 /**
100fef9d 95 * What component context are we being invoked from
2cc569f2 96 *
2cc569f2
PJ
97 * @var string
98 */
99 protected $_compContext = NULL;
100
101 /**
100fef9d 102 * QueryParams is the array returned by exportValues called on
2cc569f2
PJ
103 * the HTML_QuickForm_Controller for that page.
104 *
105 * @var array
2cc569f2
PJ
106 */
107 public $_queryParams;
108
109 /**
fe482240 110 * Represent the type of selector.
2cc569f2
PJ
111 *
112 * @var int
2cc569f2
PJ
113 */
114 protected $_action;
115
116 /**
fe482240 117 * The additional clause that we restrict the search with.
2cc569f2
PJ
118 *
119 * @var string
120 */
121 protected $_mailingClause = NULL;
122
123 /**
fe482240 124 * The query object.
2cc569f2
PJ
125 *
126 * @var string
127 */
128 protected $_query;
129
130 /**
fe482240 131 * Class constructor.
2cc569f2 132 *
90c8230e
TO
133 * @param array $queryParams
134 * Array of parameters for query.
da6b46f4 135 * @param \const|int $action - action of search basic or advanced.
90c8230e
TO
136 * @param string $mailingClause
137 * If the caller wants to further restrict the search.
138 * @param bool $single
139 * Are we dealing only with one contact?.
140 * @param int $limit
141 * How many mailing do we want returned.
2cc569f2 142 *
da6b46f4
EM
143 * @param string $context
144 * @param null $compContext
145 *
146 * @return \CRM_Mailing_Selector_Search
2cc569f2 147 */
2da40d21 148 public function __construct(
a3d7e8ee 149 &$queryParams,
2cc569f2
PJ
150 $action = CRM_Core_Action::NONE,
151 $mailingClause = NULL,
152 $single = FALSE,
153 $limit = NULL,
154 $context = 'search',
155 $compContext = NULL
156 ) {
157 // submitted form values
158 $this->_queryParams = &$queryParams;
159
353ffa53
TO
160 $this->_single = $single;
161 $this->_limit = $limit;
162 $this->_context = $context;
2cc569f2
PJ
163 $this->_compContext = $compContext;
164
165 $this->_mailingClause = $mailingClause;
166
167 // type of selector
168 $this->_action = $action;
2cc569f2
PJ
169 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
170 CRM_Mailing_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_MAILING,
171 FALSE
172 ),
173 NULL, FALSE, FALSE,
174 CRM_Contact_BAO_Query::MODE_MAILING
175 );
335038eb
PJ
176
177 $this->_query->_distinctComponentClause = " civicrm_mailing_recipients.id ";
2cc569f2 178 }
2cc569f2
PJ
179
180 /**
181 * This method returns the links that are given for each search row.
182 * currently the links added for each row are
183 *
184 * - View
185 * - Edit
186 *
187 * @return array
2cc569f2 188 */
00be9182 189 public static function &links() {
2cc569f2 190 if (!(self::$_links)) {
35f7561f
TO
191 list($context, $key) = func_get_args();
192 $extraParams = ($key) ? "&key={$key}" : NULL;
193 $searchContext = ($context) ? "&context=$context" : NULL;
2cc569f2 194
be2fb01f
CW
195 self::$_links = [
196 CRM_Core_Action::VIEW => [
353ffa53
TO
197 'name' => ts('View'),
198 'url' => 'civicrm/contact/view',
199 'qs' => "reset=1&cid=%%cid%%{$searchContext}{$extraParams}",
200 'title' => ts('View Contact Details'),
be2fb01f
CW
201 ],
202 CRM_Core_Action::UPDATE => [
353ffa53
TO
203 'name' => ts('Edit'),
204 'url' => 'civicrm/contact/add',
205 'qs' => "reset=1&action=update&cid=%%cid%%{$searchContext}{$extraParams}",
206 'title' => ts('Edit Contact Details'),
be2fb01f
CW
207 ],
208 CRM_Core_Action::DELETE => [
353ffa53
TO
209 'name' => ts('Delete'),
210 'url' => 'civicrm/contact/view/delete',
211 'qs' => "reset=1&delete=1&cid=%%cid%%{$searchContext}{$extraParams}",
212 'title' => ts('Delete Contact'),
be2fb01f
CW
213 ],
214 ];
2cc569f2
PJ
215 }
216 return self::$_links;
217 }
2cc569f2
PJ
218
219 /**
100fef9d 220 * Getter for array of the parameters required for creating pager.
2cc569f2 221 *
dd244018 222 * @param $action
c490a46a 223 * @param array $params
2cc569f2 224 */
00be9182 225 public function getPagerParams($action, &$params) {
2cc569f2
PJ
226 $params['status'] = ts('Mailing Recipient') . ' %%StatusMessage%%';
227 $params['csvString'] = NULL;
228 if ($this->_limit) {
229 $params['rowCount'] = $this->_limit;
230 }
231 else {
232 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
233 }
234
235 $params['buttonTop'] = 'PagerTopButton';
236 $params['buttonBottom'] = 'PagerBottomButton';
237 }
2cc569f2
PJ
238
239 /**
240 * Returns total number of rows for the query.
241 *
25606795 242 * @param string $action
2cc569f2 243 *
a6c01b45
CW
244 * @return int
245 * Total number of rows
2cc569f2 246 */
00be9182 247 public function getTotalCount($action) {
2cc569f2
PJ
248 return $this->_query->searchQuery(0, 0, NULL,
249 TRUE, FALSE,
250 FALSE, FALSE,
251 FALSE,
252 $this->_mailingClause
253 );
254 }
255
256 /**
fe482240 257 * Returns all the rows in the given offset and rowCount.
2cc569f2 258 *
3f8d2862 259 * @param string $action
90c8230e
TO
260 * The action being performed.
261 * @param int $offset
262 * The row number to start from.
263 * @param int $rowCount
264 * The number of rows to return.
265 * @param string $sort
266 * The sql string that describes the sort order.
3f8d2862 267 * @param string $output
90c8230e 268 * What should the result set include (web/email/csv).
2cc569f2 269 *
a6c01b45
CW
270 * @return int
271 * the total number of rows for this action
2cc569f2 272 */
00be9182 273 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
2cc569f2
PJ
274 $result = $this->_query->searchQuery($offset, $rowCount, $sort,
275 FALSE, FALSE,
276 FALSE, FALSE,
277 FALSE,
278 $this->_mailingClause
279 );
280
281 // process the result of the query
be2fb01f
CW
282 $rows = [];
283 $permissions = [CRM_Core_Permission::getPermission()];
2cc569f2
PJ
284 if (CRM_Core_Permission::check('delete contacts')) {
285 $permissions[] = CRM_Core_Permission::DELETE;
286 }
287 $mask = CRM_Core_Action::mask($permissions);
288 $qfKey = $this->_key;
289
35f7561f 290 while ($result->fetch()) {
be2fb01f 291 $row = [];
2cc569f2
PJ
292 // the columns we are interested in
293 foreach (self::$_properties as $property) {
294 if (property_exists($result, $property)) {
295 $row[$property] = $result->$property;
296 }
297 }
298
335038eb 299 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->mailing_recipients_id;
2cc569f2 300
be2fb01f 301 $actions = [
2cc569f2
PJ
302 'cid' => $result->contact_id,
303 'cxt' => $this->_context,
be2fb01f 304 ];
2cc569f2
PJ
305
306 $row['action'] = CRM_Core_Action::formLink(
307 self::links($qfKey, $this->_context),
dd244018 308 $mask,
87dab4a4
AH
309 $actions,
310 ts('more'),
311 FALSE,
312 'contact.mailing.row',
313 'Contact',
314 $result->contact_id
2cc569f2 315 );
35f7561f 316 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
2cc569f2
PJ
317 );
318
319 $rows[] = $row;
320 }
321 return $rows;
322 }
323
324 /**
1054415f 325 * @inheritDoc
2cc569f2 326 */
2cc569f2
PJ
327 public function getQILL() {
328 return $this->_query->qill();
329 }
330
331 /**
100fef9d 332 * Returns the column headers as an array of tuples:
2cc569f2
PJ
333 * (name, sortName (key to the sort array))
334 *
90c8230e
TO
335 * @param string $action
336 * The action being performed.
3f8d2862 337 * @param string $output
90c8230e 338 * What should the result set include (web/email/csv).
2cc569f2 339 *
a6c01b45
CW
340 * @return array
341 * the column headers that need to be displayed
2cc569f2
PJ
342 */
343 public function &getColumnHeaders($action = NULL, $output = NULL) {
762f768e 344
2cc569f2 345 if (!isset(self::$_columnHeaders)) {
ee3db6c8 346 $isMultiLingual = CRM_Core_I18n::isMultiLingual();
762f768e 347 $headers = [
be2fb01f
CW
348 ['desc' => ts('Contact Type')],
349 [
2cc569f2
PJ
350 'name' => ts('Name'),
351 'sort' => 'sort_name',
352 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
353 ],
354 [
2cc569f2
PJ
355 'name' => ts('Email'),
356 'sort' => 'email',
357 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
358 ],
359 [
2cc569f2
PJ
360 'name' => ts('Mailing Name'),
361 'sort' => 'mailing_name',
362 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f 363 ],
762f768e
LS
364 ];
365
366 // Check to see if languages column should be displayed.
367 if ($isMultiLingual) {
f1f5de38 368 $headers[] = [
c00b95ef
ML
369 'name' => ts('Language'),
370 'sort' => 'language',
371 'direction' => CRM_Utils_Sort::DONTCARE,
762f768e
LS
372 ];
373 }
374 self::$_columnHeaders = array_merge($headers, [
be2fb01f 375 [
2cc569f2
PJ
376 'name' => ts('Mailing Subject'),
377 'sort' => 'mailing_subject',
378 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
379 ],
380 [
2cc569f2
PJ
381 'name' => ts('Mailing Status'),
382 'sort' => 'mailing_job_status',
383 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
384 ],
385 [
2cc569f2
PJ
386 'name' => ts('Completed Date'),
387 'sort' => 'mailing_job_end_date',
388 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
389 ],
390 ['desc' => ts('Actions')],
762f768e 391 ]);
2cc569f2
PJ
392 }
393 return self::$_columnHeaders;
394 }
395
e0ef6999
EM
396 /**
397 * @return mixed
398 */
00be9182 399 public function alphabetQuery() {
52cda5dc 400 return $this->_query->alphabetQuery();
2cc569f2
PJ
401 }
402
e0ef6999
EM
403 /**
404 * @return string
405 */
00be9182 406 public function &getQuery() {
2cc569f2
PJ
407 return $this->_query;
408 }
409
410 /**
100fef9d 411 * Name of export file.
2cc569f2 412 *
90c8230e
TO
413 * @param string $output
414 * Type of output.
2cc569f2 415 *
a6c01b45
CW
416 * @return string
417 * name of the file
2cc569f2 418 */
00be9182 419 public function getExportFileName($output = 'csv') {
2cc569f2
PJ
420 return ts('CiviCRM Mailing Search');
421 }
96025800 422
dd244018 423}