Commit | Line | Data |
---|---|---|
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 | */ | |
21 | class 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 | |
203 | LEFT JOIN $job ON ( $mailing.id = $job.mailing_id AND civicrm_mailing_job.is_test = 0 AND civicrm_mailing_job.parent_id IS NULL ) | |
204 | LEFT JOIN civicrm_contact createdContact ON ( $mailing.created_id = createdContact.id ) | |
205 | LEFT 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 CW |
247 | ], |
248 | CRM_Core_Action::UPDATE => [ | |
6a488035 TO |
249 | 'name' => ts('Re-Use'), |
250 | 'url' => 'civicrm/mailing/send', | |
251 | 'qs' => 'mid=%%mid%%&reset=1', | |
252 | 'title' => ts('Re-Send Mailing'), | |
be2fb01f CW |
253 | ], |
254 | CRM_Core_Action::DISABLE => [ | |
6a488035 TO |
255 | 'name' => ts('Cancel'), |
256 | 'url' => 'civicrm/mailing/browse', | |
257 | 'qs' => 'action=disable&mid=%%mid%%&reset=1', | |
258 | 'extra' => 'onclick="if (confirm(\'' . $cancelExtra . '\')) this.href+=\'&confirmed=1\'; else return false;"', | |
259 | 'title' => ts('Cancel Mailing'), | |
be2fb01f CW |
260 | ], |
261 | CRM_Core_Action::PREVIEW => [ | |
6a488035 TO |
262 | 'name' => ts('Continue'), |
263 | 'url' => 'civicrm/mailing/send', | |
264 | 'qs' => 'mid=%%mid%%&continue=true&reset=1', | |
265 | 'title' => ts('Continue Mailing'), | |
be2fb01f CW |
266 | ], |
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+=\'&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+=\'&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 | } | |
366 | ||
367 | if (!in_array($row['id'], $searchMailings)) { | |
368 | if ($allAccess || $showCreateLinks) { | |
369 | $actionMask |= CRM_Core_Action::UPDATE; | |
370 | } | |
371 | } | |
372 | } | |
373 | else { | |
374 | if ($allAccess || ($showCreateLinks || $showScheduleLinks)) { | |
375 | $actionMask = CRM_Core_Action::PREVIEW; | |
376 | } | |
377 | } | |
be2fb01f | 378 | if (in_array($row['status'], [ |
353ffa53 TO |
379 | 'Scheduled', |
380 | 'Running', | |
af9b09df | 381 | 'Paused', |
be2fb01f | 382 | ])) { |
6a488035 TO |
383 | if ($allAccess || |
384 | ($showApprovalLinks && $showCreateLinks && $showScheduleLinks) | |
385 | ) { | |
386 | ||
387 | $actionMask |= CRM_Core_Action::DISABLE; | |
4aa8f804 JP |
388 | if ($row['status'] == "Paused") { |
389 | $actionMask |= CRM_Core_Action::REOPEN; | |
390 | } | |
391 | else { | |
392 | $actionMask |= CRM_Core_Action::CLOSE; | |
393 | } | |
6a488035 TO |
394 | } |
395 | if ($row['status'] == 'Scheduled' && | |
396 | empty($row['approval_status_id']) | |
397 | ) { | |
398 | if ($workFlow && ($allAccess || $showApprovalLinks)) { | |
399 | $actionMask |= CRM_Core_Action::ENABLE; | |
400 | } | |
401 | } | |
402 | } | |
403 | ||
be2fb01f | 404 | if (in_array($row['status'], ['Complete', 'Canceled']) && |
353ffa53 TO |
405 | !$row['archived'] |
406 | ) { | |
6a488035 TO |
407 | if ($allAccess || $showCreateLinks) { |
408 | $actionMask |= CRM_Core_Action::RENEW; | |
409 | } | |
410 | } | |
411 | ||
25606795 | 412 | // check for delete permission. |
6a488035 TO |
413 | if ($allowToDelete) { |
414 | $actionMask |= CRM_Core_Action::DELETE; | |
415 | } | |
416 | ||
417 | if ($actionMask == NULL) { | |
418 | $actionMask = CRM_Core_Action::ADD; | |
419 | } | |
25606795 | 420 | // get status strings as per locale settings CRM-4411. |
9da8dc8c | 421 | $rows[$key]['status'] = CRM_Mailing_BAO_MailingJob::status($row['status']); |
cc6d1477 SV |
422 | |
423 | // get language string | |
1f2799b0 | 424 | $rows[$key]['language'] = (isset($row['language']) ? $languages[$row['language']] : NULL); |
cc6d1477 | 425 | |
7535623a | 426 | $validLinks = $actionLinks; |
427 | if (($mailingUrl = CRM_Mailing_BAO_Mailing::getPublicViewUrl($row['id'])) != FALSE) { | |
be2fb01f | 428 | $validLinks[CRM_Core_Action::BROWSE] = [ |
7535623a | 429 | 'name' => ts('Public View'), |
430 | 'url' => 'civicrm/mailing/view', | |
9ed1c2ed | 431 | 'qs' => 'id=%%hashOrMid%%&reset=1', |
7535623a | 432 | 'title' => ts('Public View'), |
433 | 'fe' => TRUE, | |
be2fb01f | 434 | ]; |
28daab7e | 435 | $actionMask |= CRM_Core_Action::BROWSE; |
7535623a | 436 | } |
6a488035 | 437 | |
9ed1c2ed | 438 | $hash = CRM_Mailing_BAO_Mailing::getMailingHash($row['id']); |
7535623a | 439 | $rows[$key]['action'] = CRM_Core_Action::formLink( |
440 | $validLinks, | |
6a488035 | 441 | $actionMask, |
9ed1c2ed J |
442 | [ |
443 | 'mid' => $row['id'], | |
444 | 'hashOrMid' => $hash ? $hash : $row['id'], | |
445 | ], | |
6a488035 TO |
446 | "more", |
447 | FALSE, | |
448 | $opString, | |
449 | "Mailing", | |
450 | $row['id'] | |
451 | ); | |
452 | ||
25606795 | 453 | // unset($rows[$key]['id']); |
6a488035 TO |
454 | // if the scheduled date is 0, replace it with an empty string |
455 | if ($rows[$key]['scheduled_iso'] == '0000-00-00 00:00:00') { | |
456 | $rows[$key]['scheduled'] = ''; | |
457 | } | |
458 | unset($rows[$key]['scheduled_iso']); | |
459 | } | |
460 | } | |
461 | ||
462 | // also initialize the AtoZ pager | |
463 | $this->pagerAtoZ(); | |
464 | return $rows; | |
465 | } | |
466 | ||
467 | /** | |
100fef9d | 468 | * Name of export file. |
6a488035 | 469 | * |
90c8230e TO |
470 | * @param string $output |
471 | * Type of output. | |
6a488035 | 472 | * |
a6c01b45 CW |
473 | * @return string |
474 | * name of the file | |
6a488035 | 475 | */ |
00be9182 | 476 | public function getExportFileName($output = 'csv') { |
6a488035 TO |
477 | return ts('CiviMail Mailings'); |
478 | } | |
479 | ||
e0ef6999 EM |
480 | /** |
481 | * @param $parent | |
482 | */ | |
00be9182 | 483 | public function setParent($parent) { |
6a488035 TO |
484 | $this->_parent = $parent; |
485 | } | |
486 | ||
e0ef6999 | 487 | /** |
c490a46a | 488 | * @param array $params |
e0ef6999 EM |
489 | * @param bool $sortBy |
490 | * | |
491 | * @return int|string | |
492 | */ | |
00be9182 | 493 | public function whereClause(&$params, $sortBy = TRUE) { |
be2fb01f | 494 | $values = $clauses = []; |
353ffa53 | 495 | $isFormSubmitted = $this->_parent->get('hidden_find_mailings'); |
6a488035 | 496 | |
f5d7a1ab | 497 | $title = $this->_parent->get('mailing_name'); |
6a488035 TO |
498 | if ($title) { |
499 | $clauses[] = 'name LIKE %1'; | |
500 | if (strpos($title, '%') !== FALSE) { | |
be2fb01f | 501 | $params[1] = [$title, 'String', FALSE]; |
6a488035 TO |
502 | } |
503 | else { | |
be2fb01f | 504 | $params[1] = [$title, 'String', TRUE]; |
6a488035 TO |
505 | } |
506 | } | |
507 | ||
be2fb01f | 508 | $dateClause1 = $dateClause2 = []; |
6de4fe34 | 509 | $from = $this->_parent->get('mailing_low'); |
6a488035 | 510 | if (!CRM_Utils_System::isNull($from)) { |
2d882552 PJ |
511 | if ($this->_parent->get('unscheduled')) { |
512 | $dateClause1[] = 'civicrm_mailing.created_date >= %2'; | |
513 | } | |
514 | else { | |
515 | $dateClause1[] = 'civicrm_mailing_job.start_date >= %2'; | |
516 | $dateClause2[] = 'civicrm_mailing_job.scheduled_date >= %2'; | |
517 | } | |
be2fb01f | 518 | $params[2] = [$from, 'String']; |
6a488035 TO |
519 | } |
520 | ||
6de4fe34 | 521 | $to = $this->_parent->get('mailing_high'); |
6a488035 | 522 | if (!CRM_Utils_System::isNull($to)) { |
2d882552 PJ |
523 | if ($this->_parent->get('unscheduled')) { |
524 | $dateClause1[] = ' civicrm_mailing.created_date <= %3 '; | |
525 | } | |
526 | else { | |
527 | $dateClause1[] = 'civicrm_mailing_job.start_date <= %3'; | |
528 | $dateClause2[] = 'civicrm_mailing_job.scheduled_date <= %3'; | |
529 | } | |
be2fb01f | 530 | $params[3] = [$to, 'String']; |
6a488035 TO |
531 | } |
532 | ||
be2fb01f | 533 | $dateClauses = []; |
6a488035 | 534 | if (!empty($dateClause1)) { |
f6df2c32 DS |
535 | $dateClauses[] = implode(' AND ', $dateClause1); |
536 | } | |
537 | if (!empty($dateClause2)) { | |
538 | $dateClauses[] = implode(' AND ', $dateClause2); | |
539 | } | |
540 | $dateClauses = implode(' OR ', $dateClauses); | |
541 | if (!empty($dateClauses)) { | |
542 | $clauses[] = "({$dateClauses})"; | |
6a488035 TO |
543 | } |
544 | ||
6a488035 | 545 | if ($this->_parent->get('sms')) { |
f6df2c32 | 546 | $clauses[] = "civicrm_mailing.sms_provider_id IS NOT NULL"; |
6a488035 TO |
547 | } |
548 | else { | |
f6df2c32 | 549 | $clauses[] = "civicrm_mailing.sms_provider_id IS NULL"; |
6a488035 TO |
550 | } |
551 | ||
f5d7a1ab | 552 | // get values submitted by form |
353ffa53 TO |
553 | $isDraft = $this->_parent->get('status_unscheduled'); |
554 | $isArchived = $this->_parent->get('is_archived'); | |
f6df2c32 DS |
555 | $mailingStatus = $this->_parent->get('mailing_status'); |
556 | ||
f5d7a1ab DS |
557 | if (!$isFormSubmitted && $this->_parent->get('scheduled')) { |
558 | // mimic default behavior for scheduled screen | |
559 | $isArchived = 0; | |
be2fb01f | 560 | $mailingStatus = ['Scheduled' => 1, 'Complete' => 1, 'Running' => 1, 'Paused' => 1, 'Canceled' => 1]; |
f5d7a1ab DS |
561 | } |
562 | if (!$isFormSubmitted && $this->_parent->get('archived')) { | |
563 | // mimic default behavior for archived screen | |
564 | $isArchived = 1; | |
565 | } | |
566 | if (!$isFormSubmitted && $this->_parent->get('unscheduled')) { | |
567 | // mimic default behavior for draft screen | |
568 | $isDraft = 1; | |
569 | } | |
570 | ||
be2fb01f | 571 | $statusClauses = []; |
f5d7a1ab | 572 | if ($isDraft) { |
753f3087 | 573 | $statusClauses[] = "civicrm_mailing.scheduled_id IS NULL"; |
f5d7a1ab | 574 | } |
f6df2c32 | 575 | if (!empty($mailingStatus)) { |
753f3087 DS |
576 | $statusClauses[] = "civicrm_mailing_job.status IN ('" . implode("', '", array_keys($mailingStatus)) . "')"; |
577 | } | |
578 | if (!empty($statusClauses)) { | |
579 | $clauses[] = "(" . implode(' OR ', $statusClauses) . ")"; | |
2d882552 | 580 | } |
cbb4cb7b | 581 | |
f6df2c32 DS |
582 | if (isset($isArchived)) { |
583 | if ($isArchived) { | |
753f3087 | 584 | $clauses[] = "civicrm_mailing.is_archived = 1"; |
0db6c3e1 TO |
585 | } |
586 | else { | |
f6df2c32 | 587 | $clauses[] = "(civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0)"; |
6a488035 | 588 | } |
2d882552 PJ |
589 | } |
590 | ||
6a488035 TO |
591 | if ($sortBy && |
592 | $this->_parent->_sortByCharacter !== NULL | |
593 | ) { | |
594 | $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_parent->_sortByCharacter)) . "%'"; | |
595 | } | |
596 | ||
597 | // dont do a the below assignement when doing a | |
598 | // AtoZ pager clause | |
599 | if ($sortBy) { | |
600 | if (count($clauses) > 1) { | |
601 | $this->_parent->assign('isSearch', 1); | |
602 | } | |
603 | else { | |
604 | $this->_parent->assign('isSearch', 0); | |
605 | } | |
606 | } | |
607 | ||
608 | $createOrSentBy = $this->_parent->get('sort_name'); | |
609 | if (!CRM_Utils_System::isNull($createOrSentBy)) { | |
610 | $clauses[] = '(createdContact.sort_name LIKE %4 OR scheduledContact.sort_name LIKE %4)'; | |
be2fb01f | 611 | $params[4] = ['%' . $createOrSentBy . '%', 'String']; |
6a488035 TO |
612 | } |
613 | ||
614 | $createdId = $this->_parent->get('createdId'); | |
615 | if ($createdId) { | |
616 | $clauses[] = "(created_id = {$createdId})"; | |
be2fb01f | 617 | $params[5] = [$createdId, 'Integer']; |
6a488035 TO |
618 | } |
619 | ||
620 | $campainIds = $this->_parent->get('campaign_id'); | |
621 | if (!CRM_Utils_System::isNull($campainIds)) { | |
622 | if (!is_array($campainIds)) { | |
be2fb01f | 623 | $campaignIds = [$campaignIds]; |
6a488035 TO |
624 | } |
625 | $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )'; | |
626 | } | |
627 | ||
c00b95ef ML |
628 | if ($language = $this->_parent->get('language')) { |
629 | $clauses[] = "civicrm_mailing.language = %6"; | |
be2fb01f | 630 | $params[6] = [$language, 'String']; |
c00b95ef ML |
631 | } |
632 | ||
6a488035 TO |
633 | if (empty($clauses)) { |
634 | return 1; | |
635 | } | |
636 | ||
637 | return implode(' AND ', $clauses); | |
638 | } | |
639 | ||
00be9182 | 640 | public function pagerAtoZ() { |
6a488035 | 641 | |
be2fb01f | 642 | $params = []; |
6a488035 TO |
643 | $whereClause = $this->whereClause($params, FALSE); |
644 | ||
645 | $query = " | |
646 | SELECT DISTINCT UPPER(LEFT(name, 1)) as sort_name | |
647 | FROM civicrm_mailing | |
648 | LEFT JOIN civicrm_mailing_job ON (civicrm_mailing_job.mailing_id = civicrm_mailing.id) | |
649 | LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id ) | |
650 | LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id ) | |
651 | WHERE $whereClause | |
bad98dd5 | 652 | ORDER BY UPPER(LEFT(name, 1)) |
6a488035 TO |
653 | "; |
654 | ||
655 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
656 | ||
657 | $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_parent->_sortByCharacter, TRUE); | |
658 | $this->_parent->assign('aToZ', $aToZBar); | |
659 | } | |
96025800 | 660 | |
6a488035 | 661 | } |