Change Re-use to Copy
[civicrm-core.git] / CRM / Mailing / Selector / Browse.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
19 * This class is used to browse past mailings.
20 */
21class CRM_Mailing_Selector_Browse extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
22
23 /**
b44e3f84 24 * Array of supported links, currently null
6a488035
TO
25 *
26 * @var array
6a488035 27 */
7e8c8317 28 public static $_links = NULL;
6a488035
TO
29
30 /**
100fef9d 31 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
32 *
33 * @var array
6a488035 34 */
7e8c8317 35 public static $_columnHeaders;
6a488035
TO
36
37 protected $_parent;
38
39 /**
fe482240 40 * Class constructor.
6a488035 41 *
6a488035 42 *
6c8f6e67 43 * @return \CRM_Mailing_Selector_Browse
6a488035 44 */
00be9182 45 public function __construct() {
6a488035 46 }
6a488035
TO
47
48 /**
49 * This method returns the links that are given for each search row.
50 *
51 * @return array
6a488035 52 */
00be9182 53 public static function &links() {
6a488035
TO
54 return self::$_links;
55 }
6a488035
TO
56
57 /**
100fef9d 58 * Getter for array of the parameters required for creating pager.
6a488035 59 *
dd244018 60 * @param $action
c490a46a 61 * @param array $params
6a488035 62 */
00be9182 63 public function getPagerParams($action, &$params) {
6a488035
TO
64 $params['csvString'] = NULL;
65 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
66 $params['status'] = ts('Mailings %%StatusMessage%%');
67 $params['buttonTop'] = 'PagerTopButton';
68 $params['buttonBottom'] = 'PagerBottomButton';
69 }
6a488035
TO
70
71 /**
100fef9d 72 * Returns the column headers as an array of tuples:
6a488035
TO
73 * (name, sortName (key to the sort array))
74 *
90c8230e
TO
75 * @param string $action
76 * The action being performed.
3f8d2862 77 * @param string $output
90c8230e 78 * What should the result set include (web/email/csv).
6a488035 79 *
a6c01b45
CW
80 * @return array
81 * the column headers that need to be displayed
6a488035 82 */
00be9182 83 public function &getColumnHeaders($action = NULL, $output = NULL) {
6a488035 84 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
9da8dc8c 85 $job = CRM_Mailing_BAO_MailingJob::getTableName();
6a488035
TO
86 if (!isset(self::$_columnHeaders)) {
87 $completedOrder = NULL;
88
89 // Set different default sort depending on type of mailings (CRM-7652)
90 $unscheduledOrder = $scheduledOrder = $archivedOrder = CRM_Utils_Sort::DONTCARE;
91 if ($this->_parent->get('unscheduled')) {
92 $unscheduledOrder = CRM_Utils_Sort::DESCENDING;
93 }
94 elseif ($this->_parent->get('scheduled')) {
95 $scheduledOrder = CRM_Utils_Sort::DESCENDING;
96 }
97 else {
98 // sort by completed date for archived and undefined get
99 $completedOrder = CRM_Utils_Sort::DESCENDING;
100 }
101 $nameHeaderLabel = ($this->_parent->get('sms')) ? ts('SMS Name') : ts('Mailing Name');
102
be2fb01f
CW
103 self::$_columnHeaders = [
104 [
6a488035
TO
105 'name' => $nameHeaderLabel,
106 'sort' => 'name',
107 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
108 ],
109 ];
cc6d1477
SV
110
111 if (CRM_Core_I18n::isMultilingual()) {
112 self::$_columnHeaders = array_merge(
113 self::$_columnHeaders,
be2fb01f
CW
114 [
115 [
cc6d1477
SV
116 'name' => ts('Language'),
117 'sort' => 'language',
118 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
119 ],
120 ]
cc6d1477
SV
121 );
122 }
123
124 self::$_columnHeaders = array_merge(
125 self::$_columnHeaders,
be2fb01f
CW
126 [
127 [
cc6d1477
SV
128 'name' => ts('Status'),
129 'sort' => 'status',
130 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
131 ],
132 [
cc6d1477
SV
133 'name' => ts('Created By'),
134 'sort' => 'created_by',
135 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
136 ],
137 [
cc6d1477
SV
138 'name' => ts('Created Date'),
139 'sort' => 'created_date',
140 'direction' => $unscheduledOrder,
be2fb01f
CW
141 ],
142 [
cc6d1477
SV
143 'name' => ts('Sent By'),
144 'sort' => 'scheduled_by',
145 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
146 ],
147 [
cc6d1477
SV
148 'name' => ts('Scheduled'),
149 'sort' => 'scheduled_date',
150 'direction' => $scheduledOrder,
be2fb01f
CW
151 ],
152 [
cc6d1477
SV
153 'name' => ts('Started'),
154 'sort' => 'start_date',
155 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f
CW
156 ],
157 [
cc6d1477
SV
158 'name' => ts('Completed'),
159 'sort' => 'end_date',
160 'direction' => $completedOrder,
be2fb01f
CW
161 ],
162 ]
6a488035
TO
163 );
164
165 if (CRM_Campaign_BAO_Campaign::isCampaignEnable()) {
be2fb01f 166 self::$_columnHeaders[] = [
353ffa53 167 'name' => ts('Campaign'),
6a488035
TO
168 'sort' => 'campaign_id',
169 'direction' => CRM_Utils_Sort::DONTCARE,
be2fb01f 170 ];
6a488035
TO
171 }
172
173 if ($output != CRM_Core_Selector_Controller::EXPORT) {
be2fb01f 174 self::$_columnHeaders[] = ['name' => ts('Action')];
6a488035
TO
175 }
176 }
cc6d1477
SV
177
178 CRM_Core_Smarty::singleton()->assign('multilingual', CRM_Core_I18n::isMultilingual());
6a488035
TO
179 return self::$_columnHeaders;
180 }
181
182 /**
183 * Returns total number of rows for the query.
184 *
25606795 185 * @param string $action
6a488035 186 *
a6c01b45
CW
187 * @return int
188 * Total number of rows
6a488035 189 */
00be9182 190 public function getTotalCount($action) {
353ffa53
TO
191 $job = CRM_Mailing_BAO_MailingJob::getTableName();
192 $mailing = CRM_Mailing_BAO_Mailing::getTableName();
6a488035
TO
193 $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL();
194
25606795 195 // get the where clause.
be2fb01f 196 $params = [];
6a488035
TO
197 $whereClause = "$mailingACL AND " . $this->whereClause($params);
198
199 // CRM-11919 added addition ON clauses to mailing_job to match getRows
200 $query = "
201 SELECT COUNT( DISTINCT $mailing.id ) as count
202 FROM $mailing
203LEFT JOIN $job ON ( $mailing.id = $job.mailing_id AND civicrm_mailing_job.is_test = 0 AND civicrm_mailing_job.parent_id IS NULL )
204LEFT JOIN civicrm_contact createdContact ON ( $mailing.created_id = createdContact.id )
205LEFT JOIN civicrm_contact scheduledContact ON ( $mailing.scheduled_id = scheduledContact.id )
206 WHERE $whereClause";
207
208 return CRM_Core_DAO::singleValueQuery($query, $params);
209 }
210
211 /**
fe482240 212 * Returns all the rows in the given offset and rowCount.
6a488035 213 *
3f8d2862 214 * @param string $action
90c8230e
TO
215 * The action being performed.
216 * @param int $offset
217 * The row number to start from.
218 * @param int $rowCount
219 * The number of rows to return.
220 * @param string $sort
221 * The sql string that describes the sort order.
3f8d2862 222 * @param string $output
90c8230e 223 * What should the result set include (web/email/csv).
6a488035 224 *
a6c01b45
CW
225 * @return int
226 * the total number of rows for this action
6a488035 227 */
00be9182 228 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
6a488035
TO
229 static $actionLinks = NULL;
230 if (empty($actionLinks)) {
353ffa53
TO
231 $cancelExtra = ts('Are you sure you want to cancel this mailing?');
232 $deleteExtra = ts('Are you sure you want to delete this mailing?');
6a488035
TO
233 $archiveExtra = ts('Are you sure you want to archive this mailing?');
234
be2fb01f
CW
235 $actionLinks = [
236 CRM_Core_Action::ENABLE => [
6a488035
TO
237 'name' => ts('Approve/Reject'),
238 'url' => 'civicrm/mailing/approve',
239 'qs' => 'mid=%%mid%%&reset=1',
240 'title' => ts('Approve/Reject Mailing'),
be2fb01f
CW
241 ],
242 CRM_Core_Action::VIEW => [
6a488035
TO
243 'name' => ts('Report'),
244 'url' => 'civicrm/mailing/report',
245 'qs' => 'mid=%%mid%%&reset=1',
246 'title' => ts('View Mailing Report'),
be2fb01f 247 ],
be2fb01f 248 CRM_Core_Action::DISABLE => [
6a488035
TO
249 'name' => ts('Cancel'),
250 'url' => 'civicrm/mailing/browse',
251 'qs' => 'action=disable&mid=%%mid%%&reset=1',
252 'extra' => 'onclick="if (confirm(\'' . $cancelExtra . '\')) this.href+=\'&amp;confirmed=1\'; else return false;"',
253 'title' => ts('Cancel Mailing'),
be2fb01f
CW
254 ],
255 CRM_Core_Action::PREVIEW => [
6a488035
TO
256 'name' => ts('Continue'),
257 'url' => 'civicrm/mailing/send',
258 'qs' => 'mid=%%mid%%&continue=true&reset=1',
259 'title' => ts('Continue Mailing'),
be2fb01f 260 ],
b4e1015b 261 CRM_Core_Action::UPDATE => [
19a1f888 262 'name' => ts('Copy'),
b4e1015b
LS
263 'url' => 'civicrm/mailing/send',
264 'qs' => 'mid=%%mid%%&reset=1',
19a1f888 265 'title' => ts('Copy Mailing'),
b4e1015b 266 ],
be2fb01f 267 CRM_Core_Action::DELETE => [
6a488035
TO
268 'name' => ts('Delete'),
269 'url' => 'civicrm/mailing/browse',
270 'qs' => 'action=delete&mid=%%mid%%&reset=1',
271 'extra' => 'onclick="if (confirm(\'' . $deleteExtra . '\')) this.href+=\'&amp;confirmed=1\'; else return false;"',
272 'title' => ts('Delete Mailing'),
be2fb01f
CW
273 ],
274 CRM_Core_Action::RENEW => [
6a488035
TO
275 'name' => ts('Archive'),
276 'url' => 'civicrm/mailing/browse/archived',
277 'qs' => 'action=renew&mid=%%mid%%&reset=1',
278 'extra' => 'onclick="if (confirm(\'' . $archiveExtra . '\')) this.href+=\'&amp;confirmed=1\'; else return false;"',
279 'title' => ts('Archive Mailing'),
be2fb01f
CW
280 ],
281 CRM_Core_Action::REOPEN => [
4aa8f804
JP
282 'name' => ts('Resume'),
283 'url' => 'civicrm/mailing/browse',
284 'qs' => 'action=reopen&mid=%%mid%%&reset=1',
285 'title' => ts('Resume mailing'),
be2fb01f
CW
286 ],
287 CRM_Core_Action::CLOSE => [
4aa8f804
JP
288 'name' => ts('Pause'),
289 'url' => 'civicrm/mailing/browse',
290 'qs' => 'action=close&mid=%%mid%%&reset=1',
291 'title' => ts('Pause mailing'),
be2fb01f
CW
292 ],
293 ];
6a488035
TO
294 }
295
296 $allAccess = TRUE;
297 $workFlow = $showApprovalLinks = $showScheduleLinks = $showCreateLinks = FALSE;
298 if (CRM_Mailing_Info::workflowEnabled()) {
299 $allAccess = FALSE;
300 $workFlow = TRUE;
25606795 301 // supercedes all permission
6a488035
TO
302 if (CRM_Core_Permission::check('access CiviMail')) {
303 $allAccess = TRUE;
304 }
305
306 if (CRM_Core_Permission::check('approve mailings')) {
307 $showApprovalLinks = TRUE;
308 }
309
310 if (CRM_Core_Permission::check('create mailings')) {
311 $showCreateLinks = TRUE;
312 }
313
314 if (CRM_Core_Permission::check('schedule mailings')) {
315 $showScheduleLinks = TRUE;
316 }
317 }
318 $mailing = new CRM_Mailing_BAO_Mailing();
319
be2fb01f 320 $params = [];
6a488035
TO
321
322 $whereClause = ' AND ' . $this->whereClause($params);
323
324 if (empty($params)) {
325 $this->_parent->assign('isSearch', 0);
326 }
327 else {
328 $this->_parent->assign('isSearch', 1);
329 }
330 $rows = &$mailing->getRows($offset, $rowCount, $sort, $whereClause, $params);
331
25606795 332 // get the search base mailing Ids, CRM-3711.
6a488035
TO
333 $searchMailings = $mailing->searchMailingIDs();
334
25606795 335 // check for delete CRM-4418
6a488035
TO
336 $allowToDelete = CRM_Core_Permission::check('delete in CiviMail');
337
338 if ($output != CRM_Core_Selector_Controller::EXPORT) {
339
25606795 340 // create the appropriate $op to use for hook_civicrm_links
be2fb01f 341 $pageTypes = ['view', 'mailing', 'browse'];
6a488035
TO
342 if ($this->_parent->_unscheduled) {
343 $pageTypes[] = 'unscheduled';
344 }
345 if ($this->_parent->_scheduled) {
346 $pageTypes[] = 'scheduled';
347 }
348 if ($this->_parent->_archived) {
349 $pageTypes[] = 'archived';
350 }
351 $opString = implode('.', $pageTypes);
352
cc6d1477
SV
353 // get languages for later conversion
354 $languages = CRM_Core_I18n::languages();
355
6a488035
TO
356 foreach ($rows as $key => $row) {
357 $actionMask = NULL;
358 if ($row['sms_provider_id']) {
359 $actionLinks[CRM_Core_Action::PREVIEW]['url'] = 'civicrm/sms/send';
360 }
361
362 if (!($row['status'] == 'Not scheduled') && !$row['sms_provider_id']) {
363 if ($allAccess || $showCreateLinks) {
364 $actionMask = CRM_Core_Action::VIEW;
365 }
6a488035
TO
366 }
367 else {
368 if ($allAccess || ($showCreateLinks || $showScheduleLinks)) {
369 $actionMask = CRM_Core_Action::PREVIEW;
370 }
371 }
be2fb01f 372 if (in_array($row['status'], [
353ffa53
TO
373 'Scheduled',
374 'Running',
af9b09df 375 'Paused',
be2fb01f 376 ])) {
6a488035
TO
377 if ($allAccess ||
378 ($showApprovalLinks && $showCreateLinks && $showScheduleLinks)
379 ) {
380
381 $actionMask |= CRM_Core_Action::DISABLE;
4aa8f804
JP
382 if ($row['status'] == "Paused") {
383 $actionMask |= CRM_Core_Action::REOPEN;
384 }
385 else {
386 $actionMask |= CRM_Core_Action::CLOSE;
387 }
6a488035
TO
388 }
389 if ($row['status'] == 'Scheduled' &&
390 empty($row['approval_status_id'])
391 ) {
392 if ($workFlow && ($allAccess || $showApprovalLinks)) {
393 $actionMask |= CRM_Core_Action::ENABLE;
394 }
395 }
396 }
397
be2fb01f 398 if (in_array($row['status'], ['Complete', 'Canceled']) &&
353ffa53
TO
399 !$row['archived']
400 ) {
6a488035
TO
401 if ($allAccess || $showCreateLinks) {
402 $actionMask |= CRM_Core_Action::RENEW;
403 }
404 }
405
b4e1015b
LS
406 if ($allAccess || $showCreateLinks) {
407 $actionMask |= CRM_Core_Action::UPDATE;
163aabbd 408 }
b4e1015b 409
25606795 410 // check for delete permission.
6a488035
TO
411 if ($allowToDelete) {
412 $actionMask |= CRM_Core_Action::DELETE;
413 }
414
415 if ($actionMask == NULL) {
416 $actionMask = CRM_Core_Action::ADD;
417 }
25606795 418 // get status strings as per locale settings CRM-4411.
9da8dc8c 419 $rows[$key]['status'] = CRM_Mailing_BAO_MailingJob::status($row['status']);
cc6d1477
SV
420
421 // get language string
1f2799b0 422 $rows[$key]['language'] = (isset($row['language']) ? $languages[$row['language']] : NULL);
cc6d1477 423
7535623a 424 $validLinks = $actionLinks;
425 if (($mailingUrl = CRM_Mailing_BAO_Mailing::getPublicViewUrl($row['id'])) != FALSE) {
be2fb01f 426 $validLinks[CRM_Core_Action::BROWSE] = [
7535623a 427 'name' => ts('Public View'),
428 'url' => 'civicrm/mailing/view',
9ed1c2ed 429 'qs' => 'id=%%hashOrMid%%&reset=1',
7535623a 430 'title' => ts('Public View'),
431 'fe' => TRUE,
be2fb01f 432 ];
28daab7e 433 $actionMask |= CRM_Core_Action::BROWSE;
7535623a 434 }
6a488035 435
9ed1c2ed 436 $hash = CRM_Mailing_BAO_Mailing::getMailingHash($row['id']);
7535623a 437 $rows[$key]['action'] = CRM_Core_Action::formLink(
438 $validLinks,
6a488035 439 $actionMask,
9ed1c2ed
J
440 [
441 'mid' => $row['id'],
442 'hashOrMid' => $hash ? $hash : $row['id'],
443 ],
6a488035
TO
444 "more",
445 FALSE,
446 $opString,
447 "Mailing",
448 $row['id']
449 );
450
25606795 451 // unset($rows[$key]['id']);
6a488035
TO
452 // if the scheduled date is 0, replace it with an empty string
453 if ($rows[$key]['scheduled_iso'] == '0000-00-00 00:00:00') {
454 $rows[$key]['scheduled'] = '';
455 }
456 unset($rows[$key]['scheduled_iso']);
457 }
458 }
459
460 // also initialize the AtoZ pager
461 $this->pagerAtoZ();
462 return $rows;
463 }
464
465 /**
100fef9d 466 * Name of export file.
6a488035 467 *
90c8230e
TO
468 * @param string $output
469 * Type of output.
6a488035 470 *
a6c01b45
CW
471 * @return string
472 * name of the file
6a488035 473 */
00be9182 474 public function getExportFileName($output = 'csv') {
6a488035
TO
475 return ts('CiviMail Mailings');
476 }
477
e0ef6999
EM
478 /**
479 * @param $parent
480 */
00be9182 481 public function setParent($parent) {
6a488035
TO
482 $this->_parent = $parent;
483 }
484
e0ef6999 485 /**
c490a46a 486 * @param array $params
e0ef6999
EM
487 * @param bool $sortBy
488 *
489 * @return int|string
490 */
00be9182 491 public function whereClause(&$params, $sortBy = TRUE) {
be2fb01f 492 $values = $clauses = [];
353ffa53 493 $isFormSubmitted = $this->_parent->get('hidden_find_mailings');
6a488035 494
f5d7a1ab 495 $title = $this->_parent->get('mailing_name');
6a488035
TO
496 if ($title) {
497 $clauses[] = 'name LIKE %1';
498 if (strpos($title, '%') !== FALSE) {
be2fb01f 499 $params[1] = [$title, 'String', FALSE];
6a488035
TO
500 }
501 else {
be2fb01f 502 $params[1] = [$title, 'String', TRUE];
6a488035
TO
503 }
504 }
505
be2fb01f 506 $dateClause1 = $dateClause2 = [];
6de4fe34 507 $from = $this->_parent->get('mailing_low');
6a488035 508 if (!CRM_Utils_System::isNull($from)) {
2d882552
PJ
509 if ($this->_parent->get('unscheduled')) {
510 $dateClause1[] = 'civicrm_mailing.created_date >= %2';
511 }
512 else {
513 $dateClause1[] = 'civicrm_mailing_job.start_date >= %2';
514 $dateClause2[] = 'civicrm_mailing_job.scheduled_date >= %2';
515 }
be2fb01f 516 $params[2] = [$from, 'String'];
6a488035
TO
517 }
518
6de4fe34 519 $to = $this->_parent->get('mailing_high');
6a488035 520 if (!CRM_Utils_System::isNull($to)) {
2d882552
PJ
521 if ($this->_parent->get('unscheduled')) {
522 $dateClause1[] = ' civicrm_mailing.created_date <= %3 ';
523 }
524 else {
525 $dateClause1[] = 'civicrm_mailing_job.start_date <= %3';
526 $dateClause2[] = 'civicrm_mailing_job.scheduled_date <= %3';
527 }
be2fb01f 528 $params[3] = [$to, 'String'];
6a488035
TO
529 }
530
be2fb01f 531 $dateClauses = [];
6a488035 532 if (!empty($dateClause1)) {
f6df2c32
DS
533 $dateClauses[] = implode(' AND ', $dateClause1);
534 }
535 if (!empty($dateClause2)) {
536 $dateClauses[] = implode(' AND ', $dateClause2);
537 }
538 $dateClauses = implode(' OR ', $dateClauses);
539 if (!empty($dateClauses)) {
540 $clauses[] = "({$dateClauses})";
6a488035
TO
541 }
542
6a488035 543 if ($this->_parent->get('sms')) {
f6df2c32 544 $clauses[] = "civicrm_mailing.sms_provider_id IS NOT NULL";
6a488035
TO
545 }
546 else {
f6df2c32 547 $clauses[] = "civicrm_mailing.sms_provider_id IS NULL";
6a488035
TO
548 }
549
f5d7a1ab 550 // get values submitted by form
353ffa53
TO
551 $isDraft = $this->_parent->get('status_unscheduled');
552 $isArchived = $this->_parent->get('is_archived');
f6df2c32
DS
553 $mailingStatus = $this->_parent->get('mailing_status');
554
f5d7a1ab
DS
555 if (!$isFormSubmitted && $this->_parent->get('scheduled')) {
556 // mimic default behavior for scheduled screen
557 $isArchived = 0;
be2fb01f 558 $mailingStatus = ['Scheduled' => 1, 'Complete' => 1, 'Running' => 1, 'Paused' => 1, 'Canceled' => 1];
f5d7a1ab
DS
559 }
560 if (!$isFormSubmitted && $this->_parent->get('archived')) {
561 // mimic default behavior for archived screen
562 $isArchived = 1;
563 }
564 if (!$isFormSubmitted && $this->_parent->get('unscheduled')) {
565 // mimic default behavior for draft screen
566 $isDraft = 1;
567 }
568
be2fb01f 569 $statusClauses = [];
f5d7a1ab 570 if ($isDraft) {
753f3087 571 $statusClauses[] = "civicrm_mailing.scheduled_id IS NULL";
f5d7a1ab 572 }
f6df2c32 573 if (!empty($mailingStatus)) {
753f3087
DS
574 $statusClauses[] = "civicrm_mailing_job.status IN ('" . implode("', '", array_keys($mailingStatus)) . "')";
575 }
576 if (!empty($statusClauses)) {
577 $clauses[] = "(" . implode(' OR ', $statusClauses) . ")";
2d882552 578 }
cbb4cb7b 579
f6df2c32
DS
580 if (isset($isArchived)) {
581 if ($isArchived) {
753f3087 582 $clauses[] = "civicrm_mailing.is_archived = 1";
0db6c3e1
TO
583 }
584 else {
f6df2c32 585 $clauses[] = "(civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0)";
6a488035 586 }
2d882552
PJ
587 }
588
6a488035
TO
589 if ($sortBy &&
590 $this->_parent->_sortByCharacter !== NULL
591 ) {
592 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_parent->_sortByCharacter)) . "%'";
593 }
594
595 // dont do a the below assignement when doing a
596 // AtoZ pager clause
597 if ($sortBy) {
598 if (count($clauses) > 1) {
599 $this->_parent->assign('isSearch', 1);
600 }
601 else {
602 $this->_parent->assign('isSearch', 0);
603 }
604 }
605
606 $createOrSentBy = $this->_parent->get('sort_name');
607 if (!CRM_Utils_System::isNull($createOrSentBy)) {
608 $clauses[] = '(createdContact.sort_name LIKE %4 OR scheduledContact.sort_name LIKE %4)';
be2fb01f 609 $params[4] = ['%' . $createOrSentBy . '%', 'String'];
6a488035
TO
610 }
611
612 $createdId = $this->_parent->get('createdId');
613 if ($createdId) {
614 $clauses[] = "(created_id = {$createdId})";
be2fb01f 615 $params[5] = [$createdId, 'Integer'];
6a488035
TO
616 }
617
618 $campainIds = $this->_parent->get('campaign_id');
619 if (!CRM_Utils_System::isNull($campainIds)) {
620 if (!is_array($campainIds)) {
be2fb01f 621 $campaignIds = [$campaignIds];
6a488035
TO
622 }
623 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
624 }
625
c00b95ef
ML
626 if ($language = $this->_parent->get('language')) {
627 $clauses[] = "civicrm_mailing.language = %6";
be2fb01f 628 $params[6] = [$language, 'String'];
c00b95ef
ML
629 }
630
6a488035
TO
631 if (empty($clauses)) {
632 return 1;
633 }
634
635 return implode(' AND ', $clauses);
636 }
637
00be9182 638 public function pagerAtoZ() {
6a488035 639
be2fb01f 640 $params = [];
6a488035
TO
641 $whereClause = $this->whereClause($params, FALSE);
642
643 $query = "
644SELECT DISTINCT UPPER(LEFT(name, 1)) as sort_name
645FROM civicrm_mailing
646LEFT JOIN civicrm_mailing_job ON (civicrm_mailing_job.mailing_id = civicrm_mailing.id)
647LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
648LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
649WHERE $whereClause
bad98dd5 650ORDER BY UPPER(LEFT(name, 1))
6a488035
TO
651";
652
653 $dao = CRM_Core_DAO::executeQuery($query, $params);
654
655 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_parent->_sortByCharacter, TRUE);
656 $this->_parent->assign('aToZ', $aToZBar);
657 }
96025800 658
6a488035 659}