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