bulk comment fixes
[civicrm-core.git] / CRM / Mailing / Selector / Browse.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
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 *
62 * @internal param $
63 *
64 * @return \CRM_Mailing_Selector_Browse
65 @access public
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 *
86 * @param $action
87 * @param $params
88 *
89 * @internal param $
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();
113 $job = CRM_Mailing_BAO_MailingJob::getTableName();
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) {
197 $job = CRM_Mailing_BAO_MailingJob::getTableName();
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+=\'&amp;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+=\'&amp;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+=\'&amp;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
380 if (in_array($row['status'], array('Complete', 'Canceled')) &&
381 !$row['archived']) {
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.
396 $rows[$key]['status'] = CRM_Mailing_BAO_MailingJob::status($row['status']);
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
433 function setParent($parent) {
434 $this->_parent = $parent;
435 }
436
437 function whereClause(&$params, $sortBy = TRUE) {
438 $values = $clauses = array();
439 $isFormSubmitted = $this->_parent->get('hidden_find_mailings');
440
441 $title = $this->_parent->get('mailing_name');
442 if ($title) {
443 $clauses[] = 'name LIKE %1';
444 if (strpos($title, '%') !== FALSE) {
445 $params[1] = array($title, 'String', FALSE);
446 }
447 else {
448 $params[1] = array($title, 'String', TRUE);
449 }
450 }
451
452 $dateClause1 = $dateClause2 = array();
453 $from = $this->_parent->get('mailing_from');
454 if (!CRM_Utils_System::isNull($from)) {
455 if ($this->_parent->get('unscheduled')) {
456 $dateClause1[] = 'civicrm_mailing.created_date >= %2';
457 }
458 else {
459 $dateClause1[] = 'civicrm_mailing_job.start_date >= %2';
460 $dateClause2[] = 'civicrm_mailing_job.scheduled_date >= %2';
461 }
462 $params[2] = array($from, 'String');
463 }
464
465 $to = $this->_parent->get('mailing_to');
466 if (!CRM_Utils_System::isNull($to)) {
467 if ($this->_parent->get('unscheduled')) {
468 $dateClause1[] = ' civicrm_mailing.created_date <= %3 ';
469 }
470 else {
471 $dateClause1[] = 'civicrm_mailing_job.start_date <= %3';
472 $dateClause2[] = 'civicrm_mailing_job.scheduled_date <= %3';
473 }
474 $params[3] = array($to, 'String');
475 }
476
477 $dateClauses = array();
478 if (!empty($dateClause1)) {
479 $dateClauses[] = implode(' AND ', $dateClause1);
480 }
481 if (!empty($dateClause2)) {
482 $dateClauses[] = implode(' AND ', $dateClause2);
483 }
484 $dateClauses = implode(' OR ', $dateClauses);
485 if (!empty($dateClauses)) {
486 $clauses[] = "({$dateClauses})";
487 }
488
489 if ($this->_parent->get('sms')) {
490 $clauses[] = "civicrm_mailing.sms_provider_id IS NOT NULL";
491 }
492 else {
493 $clauses[] = "civicrm_mailing.sms_provider_id IS NULL";
494 }
495
496 // get values submitted by form
497 $isDraft = $this->_parent->get('status_unscheduled');
498 $isArchived = $this->_parent->get('is_archived');
499 $mailingStatus = $this->_parent->get('mailing_status');
500
501 if (!$isFormSubmitted && $this->_parent->get('scheduled')) {
502 // mimic default behavior for scheduled screen
503 $isArchived = 0;
504 $mailingStatus = array('Scheduled' => 1, 'Complete' => 1, 'Running' => 1, 'Canceled' => 1);
505 }
506 if (!$isFormSubmitted && $this->_parent->get('archived')) {
507 // mimic default behavior for archived screen
508 $isArchived = 1;
509 }
510 if (!$isFormSubmitted && $this->_parent->get('unscheduled')) {
511 // mimic default behavior for draft screen
512 $isDraft = 1;
513 }
514
515 $statusClauses = array();
516 if ($isDraft) {
517 $statusClauses[] = "civicrm_mailing.scheduled_id IS NULL";
518 }
519 if (!empty($mailingStatus)) {
520 $statusClauses[] = "civicrm_mailing_job.status IN ('" . implode("', '", array_keys($mailingStatus)) . "')";
521 }
522 if (!empty($statusClauses)) {
523 $clauses[] = "(" . implode(' OR ', $statusClauses) . ")";
524 }
525
526 if (isset($isArchived)) {
527 if ($isArchived) {
528 $clauses[] = "civicrm_mailing.is_archived = 1";
529 } else {
530 $clauses[] = "(civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0)";
531 }
532 }
533
534 if ($sortBy &&
535 $this->_parent->_sortByCharacter !== NULL
536 ) {
537 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_parent->_sortByCharacter)) . "%'";
538 }
539
540 // dont do a the below assignement when doing a
541 // AtoZ pager clause
542 if ($sortBy) {
543 if (count($clauses) > 1) {
544 $this->_parent->assign('isSearch', 1);
545 }
546 else {
547 $this->_parent->assign('isSearch', 0);
548 }
549 }
550
551 $createOrSentBy = $this->_parent->get('sort_name');
552 if (!CRM_Utils_System::isNull($createOrSentBy)) {
553 $clauses[] = '(createdContact.sort_name LIKE %4 OR scheduledContact.sort_name LIKE %4)';
554 $params[4] = array('%' . $createOrSentBy . '%', 'String');
555 }
556
557 $createdId = $this->_parent->get('createdId');
558 if ($createdId) {
559 $clauses[] = "(created_id = {$createdId})";
560 $params[5] = array($createdId, 'Integer');
561 }
562
563 $campainIds = $this->_parent->get('campaign_id');
564 if (!CRM_Utils_System::isNull($campainIds)) {
565 if (!is_array($campainIds)) {
566 $campaignIds = array($campaignIds);
567 }
568 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
569 }
570
571 if (empty($clauses)) {
572 return 1;
573 }
574
575 return implode(' AND ', $clauses);
576 }
577
578 function pagerAtoZ() {
579
580 $params = array();
581 $whereClause = $this->whereClause($params, FALSE);
582
583 $query = "
584 SELECT DISTINCT UPPER(LEFT(name, 1)) as sort_name
585 FROM civicrm_mailing
586 LEFT JOIN civicrm_mailing_job ON (civicrm_mailing_job.mailing_id = civicrm_mailing.id)
587 LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
588 LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
589 WHERE $whereClause
590 ORDER BY LEFT(name, 1)
591 ";
592
593 $dao = CRM_Core_DAO::executeQuery($query, $params);
594
595 $aToZBar = CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_parent->_sortByCharacter, TRUE);
596 $this->_parent->assign('aToZ', $aToZBar);
597 }
598 }
599 //end of class
600