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