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.
74 public static function &links() {
79 * Getter for array of the parameters required for creating pager.
82 * @param array $params
84 public function getPagerParams($action, &$params) {
85 $params['csvString'] = NULL;
86 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
87 $params['status'] = ts('Mailings %%StatusMessage%%');
88 $params['buttonTop'] = 'PagerTopButton';
89 $params['buttonBottom'] = 'PagerBottomButton';
93 * Returns the column headers as an array of tuples:
94 * (name, sortName (key to the sort array))
96 * @param string $action
97 * The action being performed.
99 * What should the result set include (web/email/csv).
102 * the column headers that need to be displayed
104 public function &getColumnHeaders($action = NULL, $output = NULL) {
105 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
106 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
107 if (!isset(self
::$_columnHeaders)) {
108 $completedOrder = NULL;
110 // Set different default sort depending on type of mailings (CRM-7652)
111 $unscheduledOrder = $scheduledOrder = $archivedOrder = CRM_Utils_Sort
::DONTCARE
;
112 if ($this->_parent
->get('unscheduled')) {
113 $unscheduledOrder = CRM_Utils_Sort
::DESCENDING
;
115 elseif ($this->_parent
->get('scheduled')) {
116 $scheduledOrder = CRM_Utils_Sort
::DESCENDING
;
119 // sort by completed date for archived and undefined get
120 $completedOrder = CRM_Utils_Sort
::DESCENDING
;
122 $nameHeaderLabel = ($this->_parent
->get('sms')) ?
ts('SMS Name') : ts('Mailing Name');
124 self
::$_columnHeaders = array(
126 'name' => $nameHeaderLabel,
128 'direction' => CRM_Utils_Sort
::DONTCARE
,
131 'name' => ts('Status'),
133 'direction' => CRM_Utils_Sort
::DONTCARE
,
136 'name' => ts('Created By'),
137 'sort' => 'created_by',
138 'direction' => CRM_Utils_Sort
::DONTCARE
,
141 'name' => ts('Created Date'),
142 'sort' => 'created_date',
143 'direction' => $unscheduledOrder,
146 'name' => ts('Sent By'),
147 'sort' => 'scheduled_by',
148 'direction' => CRM_Utils_Sort
::DONTCARE
,
151 'name' => ts('Scheduled'),
152 'sort' => 'scheduled_date',
153 'direction' => $scheduledOrder,
156 'name' => ts('Started'),
157 'sort' => 'start_date',
158 'direction' => CRM_Utils_Sort
::DONTCARE
,
161 'name' => ts('Completed'),
162 'sort' => 'end_date',
163 'direction' => $completedOrder,
167 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable()) {
168 self
::$_columnHeaders[] = array(
169 'name' => ts('Campaign'),
170 'sort' => 'campaign_id',
171 'direction' => CRM_Utils_Sort
::DONTCARE
,
175 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
176 self
::$_columnHeaders[] = array('name' => ts('Action'));
179 return self
::$_columnHeaders;
183 * Returns total number of rows for the query.
188 * 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).
226 * the total number of rows for this action
228 public 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?');
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'),
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'),
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'),
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'),
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'),
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'),
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'),
285 $workFlow = $showApprovalLinks = $showScheduleLinks = $showCreateLinks = FALSE;
286 if (CRM_Mailing_Info
::workflowEnabled()) {
289 //supercedes all permission
290 if (CRM_Core_Permission
::check('access CiviMail')) {
294 if (CRM_Core_Permission
::check('approve mailings')) {
295 $showApprovalLinks = TRUE;
298 if (CRM_Core_Permission
::check('create mailings')) {
299 $showCreateLinks = TRUE;
302 if (CRM_Core_Permission
::check('schedule mailings')) {
303 $showScheduleLinks = TRUE;
306 $mailing = new CRM_Mailing_BAO_Mailing();
310 $whereClause = ' AND ' . $this->whereClause($params);
312 if (empty($params)) {
313 $this->_parent
->assign('isSearch', 0);
316 $this->_parent
->assign('isSearch', 1);
318 $rows = &$mailing->getRows($offset, $rowCount, $sort, $whereClause, $params);
320 //get the search base mailing Ids, CRM-3711.
321 $searchMailings = $mailing->searchMailingIDs();
323 //check for delete CRM-4418
324 $allowToDelete = CRM_Core_Permission
::check('delete in CiviMail');
326 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
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';
333 if ($this->_parent
->_scheduled
) {
334 $pageTypes[] = 'scheduled';
336 if ($this->_parent
->_archived
) {
337 $pageTypes[] = 'archived';
339 $opString = implode('.', $pageTypes);
341 foreach ($rows as $key => $row) {
343 if ($row['sms_provider_id']) {
344 $actionLinks[CRM_Core_Action
::PREVIEW
]['url'] = 'civicrm/sms/send';
347 if (!($row['status'] == 'Not scheduled') && !$row['sms_provider_id']) {
348 if ($allAccess ||
$showCreateLinks) {
349 $actionMask = CRM_Core_Action
::VIEW
;
352 if (!in_array($row['id'], $searchMailings)) {
353 if ($allAccess ||
$showCreateLinks) {
354 $actionMask |
= CRM_Core_Action
::UPDATE
;
359 if ($allAccess ||
($showCreateLinks ||
$showScheduleLinks)) {
360 $actionMask = CRM_Core_Action
::PREVIEW
;
363 if (in_array($row['status'], array(
369 ($showApprovalLinks && $showCreateLinks && $showScheduleLinks)
372 $actionMask |
= CRM_Core_Action
::DISABLE
;
374 if ($row['status'] == 'Scheduled' &&
375 empty($row['approval_status_id'])
377 if ($workFlow && ($allAccess ||
$showApprovalLinks)) {
378 $actionMask |
= CRM_Core_Action
::ENABLE
;
383 if (in_array($row['status'], array('Complete', 'Canceled')) &&
386 if ($allAccess ||
$showCreateLinks) {
387 $actionMask |
= CRM_Core_Action
::RENEW
;
391 //check for delete permission.
392 if ($allowToDelete) {
393 $actionMask |
= CRM_Core_Action
::DELETE
;
396 if ($actionMask == NULL) {
397 $actionMask = CRM_Core_Action
::ADD
;
399 //get status strings as per locale settings CRM-4411.
400 $rows[$key]['status'] = CRM_Mailing_BAO_MailingJob
::status($row['status']);
402 $rows[$key]['action'] = CRM_Core_Action
::formLink($actionLinks,
404 array('mid' => $row['id']),
412 //unset($rows[$key]['id']);
413 // if the scheduled date is 0, replace it with an empty string
414 if ($rows[$key]['scheduled_iso'] == '0000-00-00 00:00:00') {
415 $rows[$key]['scheduled'] = '';
417 unset($rows[$key]['scheduled_iso']);
421 // also initialize the AtoZ pager
427 * Name of export file.
429 * @param string $output
435 public function getExportFileName($output = 'csv') {
436 return ts('CiviMail Mailings');
442 public function setParent($parent) {
443 $this->_parent
= $parent;
447 * @param array $params
448 * @param bool $sortBy
452 public function whereClause(&$params, $sortBy = TRUE) {
453 $values = $clauses = array();
454 $isFormSubmitted = $this->_parent
->get('hidden_find_mailings');
456 $title = $this->_parent
->get('mailing_name');
458 $clauses[] = 'name LIKE %1';
459 if (strpos($title, '%') !== FALSE) {
460 $params[1] = array($title, 'String', FALSE);
463 $params[1] = array($title, 'String', TRUE);
467 $dateClause1 = $dateClause2 = array();
468 $from = $this->_parent
->get('mailing_from');
469 if (!CRM_Utils_System
::isNull($from)) {
470 if ($this->_parent
->get('unscheduled')) {
471 $dateClause1[] = 'civicrm_mailing.created_date >= %2';
474 $dateClause1[] = 'civicrm_mailing_job.start_date >= %2';
475 $dateClause2[] = 'civicrm_mailing_job.scheduled_date >= %2';
477 $params[2] = array($from, 'String');
480 $to = $this->_parent
->get('mailing_to');
481 if (!CRM_Utils_System
::isNull($to)) {
482 if ($this->_parent
->get('unscheduled')) {
483 $dateClause1[] = ' civicrm_mailing.created_date <= %3 ';
486 $dateClause1[] = 'civicrm_mailing_job.start_date <= %3';
487 $dateClause2[] = 'civicrm_mailing_job.scheduled_date <= %3';
489 $params[3] = array($to, 'String');
492 $dateClauses = array();
493 if (!empty($dateClause1)) {
494 $dateClauses[] = implode(' AND ', $dateClause1);
496 if (!empty($dateClause2)) {
497 $dateClauses[] = implode(' AND ', $dateClause2);
499 $dateClauses = implode(' OR ', $dateClauses);
500 if (!empty($dateClauses)) {
501 $clauses[] = "({$dateClauses})";
504 if ($this->_parent
->get('sms')) {
505 $clauses[] = "civicrm_mailing.sms_provider_id IS NOT NULL";
508 $clauses[] = "civicrm_mailing.sms_provider_id IS NULL";
511 // get values submitted by form
512 $isDraft = $this->_parent
->get('status_unscheduled');
513 $isArchived = $this->_parent
->get('is_archived');
514 $mailingStatus = $this->_parent
->get('mailing_status');
516 if (!$isFormSubmitted && $this->_parent
->get('scheduled')) {
517 // mimic default behavior for scheduled screen
519 $mailingStatus = array('Scheduled' => 1, 'Complete' => 1, 'Running' => 1, 'Canceled' => 1);
521 if (!$isFormSubmitted && $this->_parent
->get('archived')) {
522 // mimic default behavior for archived screen
525 if (!$isFormSubmitted && $this->_parent
->get('unscheduled')) {
526 // mimic default behavior for draft screen
530 $statusClauses = array();
532 $statusClauses[] = "civicrm_mailing.scheduled_id IS NULL";
534 if (!empty($mailingStatus)) {
535 $statusClauses[] = "civicrm_mailing_job.status IN ('" . implode("', '", array_keys($mailingStatus)) . "')";
537 if (!empty($statusClauses)) {
538 $clauses[] = "(" . implode(' OR ', $statusClauses) . ")";
541 if (isset($isArchived)) {
543 $clauses[] = "civicrm_mailing.is_archived = 1";
546 $clauses[] = "(civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0)";
551 $this->_parent
->_sortByCharacter
!== NULL
553 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO
::escapeWildCardString($this->_parent
->_sortByCharacter
)) . "%'";
556 // dont do a the below assignement when doing a
559 if (count($clauses) > 1) {
560 $this->_parent
->assign('isSearch', 1);
563 $this->_parent
->assign('isSearch', 0);
567 $createOrSentBy = $this->_parent
->get('sort_name');
568 if (!CRM_Utils_System
::isNull($createOrSentBy)) {
569 $clauses[] = '(createdContact.sort_name LIKE %4 OR scheduledContact.sort_name LIKE %4)';
570 $params[4] = array('%' . $createOrSentBy . '%', 'String');
573 $createdId = $this->_parent
->get('createdId');
575 $clauses[] = "(created_id = {$createdId})";
576 $params[5] = array($createdId, 'Integer');
579 $campainIds = $this->_parent
->get('campaign_id');
580 if (!CRM_Utils_System
::isNull($campainIds)) {
581 if (!is_array($campainIds)) {
582 $campaignIds = array($campaignIds);
584 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
587 if (empty($clauses)) {
591 return implode(' AND ', $clauses);
594 public function pagerAtoZ() {
597 $whereClause = $this->whereClause($params, FALSE);
600 SELECT DISTINCT UPPER(LEFT(name, 1)) as sort_name
602 LEFT JOIN civicrm_mailing_job ON (civicrm_mailing_job.mailing_id = civicrm_mailing.id)
603 LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
604 LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
606 ORDER BY LEFT(name, 1)
609 $dao = CRM_Core_DAO
::executeQuery($query, $params);
611 $aToZBar = CRM_Utils_PagerAToZ
::getAToZBar($dao, $this->_parent
->_sortByCharacter
, TRUE);
612 $this->_parent
->assign('aToZ', $aToZBar);