3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class is used to browse past mailings.
39 class CRM_Mailing_Selector_Browse
extends CRM_Core_Selector_Base
implements CRM_Core_Selector_API
{
42 * Array of supported links, currenly null
47 static $_links = NULL;
50 * We use desc to remind us what that column is, name is used in the tpl
55 static $_columnHeaders;
63 * @return \CRM_Mailing_Selector_Browse
66 public function __construct() {
70 * This method returns the links that are given for each search row.
75 public static function &links() {
80 * Getter for array of the parameters required for creating pager.
83 * @param array $params
86 public function getPagerParams($action, &$params) {
87 $params['csvString'] = NULL;
88 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
89 $params['status'] = ts('Mailings %%StatusMessage%%');
90 $params['buttonTop'] = 'PagerTopButton';
91 $params['buttonBottom'] = 'PagerBottomButton';
95 * Returns the column headers as an array of tuples:
96 * (name, sortName (key to the sort array))
98 * @param string $action
99 * The action being performed.
100 * @param enum $output
101 * What should the result set include (web/email/csv).
103 * @return array the column headers that need to be displayed
105 public function &getColumnHeaders($action = NULL, $output = NULL) {
106 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
107 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
108 if (!isset(self
::$_columnHeaders)) {
109 $completedOrder = NULL;
111 // Set different default sort depending on type of mailings (CRM-7652)
112 $unscheduledOrder = $scheduledOrder = $archivedOrder = CRM_Utils_Sort
::DONTCARE
;
113 if ($this->_parent
->get('unscheduled')) {
114 $unscheduledOrder = CRM_Utils_Sort
::DESCENDING
;
116 elseif ($this->_parent
->get('scheduled')) {
117 $scheduledOrder = CRM_Utils_Sort
::DESCENDING
;
120 // sort by completed date for archived and undefined get
121 $completedOrder = CRM_Utils_Sort
::DESCENDING
;
123 $nameHeaderLabel = ($this->_parent
->get('sms')) ?
ts('SMS Name') : ts('Mailing Name');
125 self
::$_columnHeaders = array(
127 'name' => $nameHeaderLabel,
129 'direction' => CRM_Utils_Sort
::DONTCARE
,
132 'name' => ts('Status'),
134 'direction' => CRM_Utils_Sort
::DONTCARE
,
137 'name' => ts('Created By'),
138 'sort' => 'created_by',
139 'direction' => CRM_Utils_Sort
::DONTCARE
,
142 'name' => ts('Created Date'),
143 'sort' => 'created_date',
144 'direction' => $unscheduledOrder,
147 'name' => ts('Sent By'),
148 'sort' => 'scheduled_by',
149 'direction' => CRM_Utils_Sort
::DONTCARE
,
152 'name' => ts('Scheduled'),
153 'sort' => 'scheduled_date',
154 'direction' => $scheduledOrder,
157 'name' => ts('Started'),
158 'sort' => 'start_date',
159 'direction' => CRM_Utils_Sort
::DONTCARE
,
162 'name' => ts('Completed'),
163 'sort' => 'end_date',
164 'direction' => $completedOrder,
168 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable()) {
169 self
::$_columnHeaders[] = array(
170 'name' => ts('Campaign'),
171 'sort' => 'campaign_id',
172 'direction' => CRM_Utils_Sort
::DONTCARE
,
176 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
177 self
::$_columnHeaders[] = array('name' => ts('Action'));
180 return self
::$_columnHeaders;
184 * Returns total number of rows for the query.
188 * @return int Total number of rows
190 public function getTotalCount($action) {
191 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
192 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
193 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL();
195 //get the where clause.
197 $whereClause = "$mailingACL AND " . $this->whereClause($params);
199 // CRM-11919 added addition ON clauses to mailing_job to match getRows
201 SELECT COUNT( DISTINCT $mailing.id ) as count
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 )
208 return CRM_Core_DAO
::singleValueQuery($query, $params);
212 * Returns all the rows in the given offset and rowCount
214 * @param enum $action
215 * The action being performed.
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.
222 * @param enum $output
223 * What should the result set include (web/email/csv).
225 * @return int the total number of rows for this action
227 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
228 static $actionLinks = NULL;
229 if (empty($actionLinks)) {
230 $cancelExtra = ts('Are you sure you want to cancel this mailing?');
231 $deleteExtra = ts('Are you sure you want to delete this mailing?');
232 $archiveExtra = ts('Are you sure you want to archive this mailing?');
234 $actionLinks = array(
235 CRM_Core_Action
::ENABLE
=> array(
236 'name' => ts('Approve/Reject'),
237 'url' => 'civicrm/mailing/approve',
238 'qs' => 'mid=%%mid%%&reset=1',
239 'title' => ts('Approve/Reject Mailing'),
241 CRM_Core_Action
::VIEW
=> array(
242 'name' => ts('Report'),
243 'url' => 'civicrm/mailing/report',
244 'qs' => 'mid=%%mid%%&reset=1',
245 'title' => ts('View Mailing Report'),
247 CRM_Core_Action
::UPDATE
=> array(
248 'name' => ts('Re-Use'),
249 'url' => 'civicrm/mailing/send',
250 'qs' => 'mid=%%mid%%&reset=1',
251 'title' => ts('Re-Send Mailing'),
253 CRM_Core_Action
::DISABLE
=> array(
254 'name' => ts('Cancel'),
255 'url' => 'civicrm/mailing/browse',
256 'qs' => 'action=disable&mid=%%mid%%&reset=1',
257 'extra' => 'onclick="if (confirm(\'' . $cancelExtra . '\')) this.href+=\'&confirmed=1\'; else return false;"',
258 'title' => ts('Cancel Mailing'),
260 CRM_Core_Action
::PREVIEW
=> array(
261 'name' => ts('Continue'),
262 'url' => 'civicrm/mailing/send',
263 'qs' => 'mid=%%mid%%&continue=true&reset=1',
264 'title' => ts('Continue Mailing'),
266 CRM_Core_Action
::DELETE
=> array(
267 'name' => ts('Delete'),
268 'url' => 'civicrm/mailing/browse',
269 'qs' => 'action=delete&mid=%%mid%%&reset=1',
270 'extra' => 'onclick="if (confirm(\'' . $deleteExtra . '\')) this.href+=\'&confirmed=1\'; else return false;"',
271 'title' => ts('Delete Mailing'),
273 CRM_Core_Action
::RENEW
=> array(
274 'name' => ts('Archive'),
275 'url' => 'civicrm/mailing/browse/archived',
276 'qs' => 'action=renew&mid=%%mid%%&reset=1',
277 'extra' => 'onclick="if (confirm(\'' . $archiveExtra . '\')) this.href+=\'&confirmed=1\'; else return false;"',
278 'title' => ts('Archive Mailing'),
284 $workFlow = $showApprovalLinks = $showScheduleLinks = $showCreateLinks = FALSE;
285 if (CRM_Mailing_Info
::workflowEnabled()) {
288 //supercedes all permission
289 if (CRM_Core_Permission
::check('access CiviMail')) {
293 if (CRM_Core_Permission
::check('approve mailings')) {
294 $showApprovalLinks = TRUE;
297 if (CRM_Core_Permission
::check('create mailings')) {
298 $showCreateLinks = TRUE;
301 if (CRM_Core_Permission
::check('schedule mailings')) {
302 $showScheduleLinks = TRUE;
305 $mailing = new CRM_Mailing_BAO_Mailing();
309 $whereClause = ' AND ' . $this->whereClause($params);
311 if (empty($params)) {
312 $this->_parent
->assign('isSearch', 0);
315 $this->_parent
->assign('isSearch', 1);
317 $rows = &$mailing->getRows($offset, $rowCount, $sort, $whereClause, $params);
319 //get the search base mailing Ids, CRM-3711.
320 $searchMailings = $mailing->searchMailingIDs();
322 //check for delete CRM-4418
323 $allowToDelete = CRM_Core_Permission
::check('delete in CiviMail');
325 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
327 //create the appropriate $op to use for hook_civicrm_links
328 $pageTypes = array('view', 'mailing', 'browse');
329 if ($this->_parent
->_unscheduled
) {
330 $pageTypes[] = 'unscheduled';
332 if ($this->_parent
->_scheduled
) {
333 $pageTypes[] = 'scheduled';
335 if ($this->_parent
->_archived
) {
336 $pageTypes[] = 'archived';
338 $opString = implode('.', $pageTypes);
340 foreach ($rows as $key => $row) {
342 if ($row['sms_provider_id']) {
343 $actionLinks[CRM_Core_Action
::PREVIEW
]['url'] = 'civicrm/sms/send';
346 if (!($row['status'] == 'Not scheduled') && !$row['sms_provider_id']) {
347 if ($allAccess ||
$showCreateLinks) {
348 $actionMask = CRM_Core_Action
::VIEW
;
351 if (!in_array($row['id'], $searchMailings)) {
352 if ($allAccess ||
$showCreateLinks) {
353 $actionMask |
= CRM_Core_Action
::UPDATE
;
358 if ($allAccess ||
($showCreateLinks ||
$showScheduleLinks)) {
359 $actionMask = CRM_Core_Action
::PREVIEW
;
362 if (in_array($row['status'], array(
363 'Scheduled', 'Running', 'Paused'))) {
365 ($showApprovalLinks && $showCreateLinks && $showScheduleLinks)
368 $actionMask |
= CRM_Core_Action
::DISABLE
;
370 if ($row['status'] == 'Scheduled' &&
371 empty($row['approval_status_id'])
373 if ($workFlow && ($allAccess ||
$showApprovalLinks)) {
374 $actionMask |
= CRM_Core_Action
::ENABLE
;
379 if (in_array($row['status'], array('Complete', 'Canceled')) &&
381 if ($allAccess ||
$showCreateLinks) {
382 $actionMask |
= CRM_Core_Action
::RENEW
;
386 //check for delete permission.
387 if ($allowToDelete) {
388 $actionMask |
= CRM_Core_Action
::DELETE
;
391 if ($actionMask == NULL) {
392 $actionMask = CRM_Core_Action
::ADD
;
394 //get status strings as per locale settings CRM-4411.
395 $rows[$key]['status'] = CRM_Mailing_BAO_MailingJob
::status($row['status']);
397 $rows[$key]['action'] = CRM_Core_Action
::formLink($actionLinks,
399 array('mid' => $row['id']),
407 //unset($rows[$key]['id']);
408 // if the scheduled date is 0, replace it with an empty string
409 if ($rows[$key]['scheduled_iso'] == '0000-00-00 00:00:00') {
410 $rows[$key]['scheduled'] = '';
412 unset($rows[$key]['scheduled_iso']);
416 // also initialize the AtoZ pager
422 * Name of export file.
424 * @param string $output
427 * @return string name of the file
429 public function getExportFileName($output = 'csv') {
430 return ts('CiviMail Mailings');
436 public function setParent($parent) {
437 $this->_parent
= $parent;
441 * @param array $params
442 * @param bool $sortBy
446 public function whereClause(&$params, $sortBy = TRUE) {
447 $values = $clauses = array();
448 $isFormSubmitted = $this->_parent
->get('hidden_find_mailings');
450 $title = $this->_parent
->get('mailing_name');
452 $clauses[] = 'name LIKE %1';
453 if (strpos($title, '%') !== FALSE) {
454 $params[1] = array($title, 'String', FALSE);
457 $params[1] = array($title, 'String', TRUE);
461 $dateClause1 = $dateClause2 = array();
462 $from = $this->_parent
->get('mailing_from');
463 if (!CRM_Utils_System
::isNull($from)) {
464 if ($this->_parent
->get('unscheduled')) {
465 $dateClause1[] = 'civicrm_mailing.created_date >= %2';
468 $dateClause1[] = 'civicrm_mailing_job.start_date >= %2';
469 $dateClause2[] = 'civicrm_mailing_job.scheduled_date >= %2';
471 $params[2] = array($from, 'String');
474 $to = $this->_parent
->get('mailing_to');
475 if (!CRM_Utils_System
::isNull($to)) {
476 if ($this->_parent
->get('unscheduled')) {
477 $dateClause1[] = ' civicrm_mailing.created_date <= %3 ';
480 $dateClause1[] = 'civicrm_mailing_job.start_date <= %3';
481 $dateClause2[] = 'civicrm_mailing_job.scheduled_date <= %3';
483 $params[3] = array($to, 'String');
486 $dateClauses = array();
487 if (!empty($dateClause1)) {
488 $dateClauses[] = implode(' AND ', $dateClause1);
490 if (!empty($dateClause2)) {
491 $dateClauses[] = implode(' AND ', $dateClause2);
493 $dateClauses = implode(' OR ', $dateClauses);
494 if (!empty($dateClauses)) {
495 $clauses[] = "({$dateClauses})";
498 if ($this->_parent
->get('sms')) {
499 $clauses[] = "civicrm_mailing.sms_provider_id IS NOT NULL";
502 $clauses[] = "civicrm_mailing.sms_provider_id IS NULL";
505 // get values submitted by form
506 $isDraft = $this->_parent
->get('status_unscheduled');
507 $isArchived = $this->_parent
->get('is_archived');
508 $mailingStatus = $this->_parent
->get('mailing_status');
510 if (!$isFormSubmitted && $this->_parent
->get('scheduled')) {
511 // mimic default behavior for scheduled screen
513 $mailingStatus = array('Scheduled' => 1, 'Complete' => 1, 'Running' => 1, 'Canceled' => 1);
515 if (!$isFormSubmitted && $this->_parent
->get('archived')) {
516 // mimic default behavior for archived screen
519 if (!$isFormSubmitted && $this->_parent
->get('unscheduled')) {
520 // mimic default behavior for draft screen
524 $statusClauses = array();
526 $statusClauses[] = "civicrm_mailing.scheduled_id IS NULL";
528 if (!empty($mailingStatus)) {
529 $statusClauses[] = "civicrm_mailing_job.status IN ('" . implode("', '", array_keys($mailingStatus)) . "')";
531 if (!empty($statusClauses)) {
532 $clauses[] = "(" . implode(' OR ', $statusClauses) . ")";
535 if (isset($isArchived)) {
537 $clauses[] = "civicrm_mailing.is_archived = 1";
540 $clauses[] = "(civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0)";
545 $this->_parent
->_sortByCharacter
!== NULL
547 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO
::escapeWildCardString($this->_parent
->_sortByCharacter
)) . "%'";
550 // dont do a the below assignement when doing a
553 if (count($clauses) > 1) {
554 $this->_parent
->assign('isSearch', 1);
557 $this->_parent
->assign('isSearch', 0);
561 $createOrSentBy = $this->_parent
->get('sort_name');
562 if (!CRM_Utils_System
::isNull($createOrSentBy)) {
563 $clauses[] = '(createdContact.sort_name LIKE %4 OR scheduledContact.sort_name LIKE %4)';
564 $params[4] = array('%' . $createOrSentBy . '%', 'String');
567 $createdId = $this->_parent
->get('createdId');
569 $clauses[] = "(created_id = {$createdId})";
570 $params[5] = array($createdId, 'Integer');
573 $campainIds = $this->_parent
->get('campaign_id');
574 if (!CRM_Utils_System
::isNull($campainIds)) {
575 if (!is_array($campainIds)) {
576 $campaignIds = array($campaignIds);
578 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
581 if (empty($clauses)) {
585 return implode(' AND ', $clauses);
588 public function pagerAtoZ() {
591 $whereClause = $this->whereClause($params, FALSE);
594 SELECT DISTINCT UPPER(LEFT(name, 1)) as sort_name
596 LEFT JOIN civicrm_mailing_job ON (civicrm_mailing_job.mailing_id = civicrm_mailing.id)
597 LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
598 LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
600 ORDER BY LEFT(name, 1)
603 $dao = CRM_Core_DAO
::executeQuery($query, $params);
605 $aToZBar = CRM_Utils_PagerAToZ
::getAToZBar($dao, $this->_parent
->_sortByCharacter
, TRUE);
606 $this->_parent
->assign('aToZ', $aToZBar);