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