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
46 static $_links = NULL;
49 * We use desc to remind us what that column is, name is used in the tpl
53 static $_columnHeaders;
61 * @return \CRM_Mailing_Selector_Browse
64 public function __construct() {
68 * This method returns the links that are given for each search row.
72 public static function &links() {
77 * Getter for array of the parameters required for creating pager.
80 * @param array $params
82 public function getPagerParams($action, &$params) {
83 $params['csvString'] = NULL;
84 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
85 $params['status'] = ts('Mailings %%StatusMessage%%');
86 $params['buttonTop'] = 'PagerTopButton';
87 $params['buttonBottom'] = 'PagerBottomButton';
91 * Returns the column headers as an array of tuples:
92 * (name, sortName (key to the sort array))
94 * @param string $action
95 * The action being performed.
96 * @param string $output
97 * What should the result set include (web/email/csv).
100 * the column headers that need to be displayed
102 public function &getColumnHeaders($action = NULL, $output = NULL) {
103 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
104 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
105 if (!isset(self
::$_columnHeaders)) {
106 $completedOrder = NULL;
108 // Set different default sort depending on type of mailings (CRM-7652)
109 $unscheduledOrder = $scheduledOrder = $archivedOrder = CRM_Utils_Sort
::DONTCARE
;
110 if ($this->_parent
->get('unscheduled')) {
111 $unscheduledOrder = CRM_Utils_Sort
::DESCENDING
;
113 elseif ($this->_parent
->get('scheduled')) {
114 $scheduledOrder = CRM_Utils_Sort
::DESCENDING
;
117 // sort by completed date for archived and undefined get
118 $completedOrder = CRM_Utils_Sort
::DESCENDING
;
120 $nameHeaderLabel = ($this->_parent
->get('sms')) ?
ts('SMS Name') : ts('Mailing Name');
122 self
::$_columnHeaders = array(
124 'name' => $nameHeaderLabel,
126 'direction' => CRM_Utils_Sort
::DONTCARE
,
129 'name' => ts('Status'),
131 'direction' => CRM_Utils_Sort
::DONTCARE
,
134 'name' => ts('Created By'),
135 'sort' => 'created_by',
136 'direction' => CRM_Utils_Sort
::DONTCARE
,
139 'name' => ts('Created Date'),
140 'sort' => 'created_date',
141 'direction' => $unscheduledOrder,
144 'name' => ts('Sent By'),
145 'sort' => 'scheduled_by',
146 'direction' => CRM_Utils_Sort
::DONTCARE
,
149 'name' => ts('Scheduled'),
150 'sort' => 'scheduled_date',
151 'direction' => $scheduledOrder,
154 'name' => ts('Started'),
155 'sort' => 'start_date',
156 'direction' => CRM_Utils_Sort
::DONTCARE
,
159 'name' => ts('Completed'),
160 'sort' => 'end_date',
161 'direction' => $completedOrder,
165 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable()) {
166 self
::$_columnHeaders[] = array(
167 'name' => ts('Campaign'),
168 'sort' => 'campaign_id',
169 'direction' => CRM_Utils_Sort
::DONTCARE
,
173 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
174 self
::$_columnHeaders[] = array('name' => ts('Action'));
177 return self
::$_columnHeaders;
181 * Returns total number of rows for the query.
186 * Total number of rows
188 public function getTotalCount($action) {
189 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
190 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
191 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL();
193 //get the where clause.
195 $whereClause = "$mailingACL AND " . $this->whereClause($params);
197 // CRM-11919 added addition ON clauses to mailing_job to match getRows
199 SELECT COUNT( DISTINCT $mailing.id ) as count
201 LEFT JOIN $job ON ( $mailing.id = $job.mailing_id AND civicrm_mailing_job.is_test = 0 AND civicrm_mailing_job.parent_id IS NULL )
202 LEFT JOIN civicrm_contact createdContact ON ( $mailing.created_id = createdContact.id )
203 LEFT JOIN civicrm_contact scheduledContact ON ( $mailing.scheduled_id = scheduledContact.id )
206 return CRM_Core_DAO
::singleValueQuery($query, $params);
210 * Returns all the rows in the given offset and rowCount
212 * @param string $action
213 * The action being performed.
215 * The row number to start from.
216 * @param int $rowCount
217 * The number of rows to return.
218 * @param string $sort
219 * The sql string that describes the sort order.
220 * @param string $output
221 * What should the result set include (web/email/csv).
224 * the total number of rows for this action
226 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
227 static $actionLinks = NULL;
228 if (empty($actionLinks)) {
229 $cancelExtra = ts('Are you sure you want to cancel this mailing?');
230 $deleteExtra = ts('Are you sure you want to delete this mailing?');
231 $archiveExtra = ts('Are you sure you want to archive this mailing?');
233 $actionLinks = array(
234 CRM_Core_Action
::ENABLE
=> array(
235 'name' => ts('Approve/Reject'),
236 'url' => 'civicrm/mailing/approve',
237 'qs' => 'mid=%%mid%%&reset=1',
238 'title' => ts('Approve/Reject Mailing'),
240 CRM_Core_Action
::VIEW
=> array(
241 'name' => ts('Report'),
242 'url' => 'civicrm/mailing/report',
243 'qs' => 'mid=%%mid%%&reset=1',
244 'title' => ts('View Mailing Report'),
246 CRM_Core_Action
::UPDATE
=> array(
247 'name' => ts('Re-Use'),
248 'url' => 'civicrm/mailing/send',
249 'qs' => 'mid=%%mid%%&reset=1',
250 'title' => ts('Re-Send Mailing'),
252 CRM_Core_Action
::DISABLE
=> array(
253 'name' => ts('Cancel'),
254 'url' => 'civicrm/mailing/browse',
255 'qs' => 'action=disable&mid=%%mid%%&reset=1',
256 'extra' => 'onclick="if (confirm(\'' . $cancelExtra . '\')) this.href+=\'&confirmed=1\'; else return false;"',
257 'title' => ts('Cancel Mailing'),
259 CRM_Core_Action
::PREVIEW
=> array(
260 'name' => ts('Continue'),
261 'url' => 'civicrm/mailing/send',
262 'qs' => 'mid=%%mid%%&continue=true&reset=1',
263 'title' => ts('Continue Mailing'),
265 CRM_Core_Action
::DELETE
=> array(
266 'name' => ts('Delete'),
267 'url' => 'civicrm/mailing/browse',
268 'qs' => 'action=delete&mid=%%mid%%&reset=1',
269 'extra' => 'onclick="if (confirm(\'' . $deleteExtra . '\')) this.href+=\'&confirmed=1\'; else return false;"',
270 'title' => ts('Delete Mailing'),
272 CRM_Core_Action
::RENEW
=> array(
273 'name' => ts('Archive'),
274 'url' => 'civicrm/mailing/browse/archived',
275 'qs' => 'action=renew&mid=%%mid%%&reset=1',
276 'extra' => 'onclick="if (confirm(\'' . $archiveExtra . '\')) this.href+=\'&confirmed=1\'; else return false;"',
277 'title' => ts('Archive Mailing'),
283 $workFlow = $showApprovalLinks = $showScheduleLinks = $showCreateLinks = FALSE;
284 if (CRM_Mailing_Info
::workflowEnabled()) {
287 //supercedes all permission
288 if (CRM_Core_Permission
::check('access CiviMail')) {
292 if (CRM_Core_Permission
::check('approve mailings')) {
293 $showApprovalLinks = TRUE;
296 if (CRM_Core_Permission
::check('create mailings')) {
297 $showCreateLinks = TRUE;
300 if (CRM_Core_Permission
::check('schedule mailings')) {
301 $showScheduleLinks = TRUE;
304 $mailing = new CRM_Mailing_BAO_Mailing();
308 $whereClause = ' AND ' . $this->whereClause($params);
310 if (empty($params)) {
311 $this->_parent
->assign('isSearch', 0);
314 $this->_parent
->assign('isSearch', 1);
316 $rows = &$mailing->getRows($offset, $rowCount, $sort, $whereClause, $params);
318 //get the search base mailing Ids, CRM-3711.
319 $searchMailings = $mailing->searchMailingIDs();
321 //check for delete CRM-4418
322 $allowToDelete = CRM_Core_Permission
::check('delete in CiviMail');
324 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
326 //create the appropriate $op to use for hook_civicrm_links
327 $pageTypes = array('view', 'mailing', 'browse');
328 if ($this->_parent
->_unscheduled
) {
329 $pageTypes[] = 'unscheduled';
331 if ($this->_parent
->_scheduled
) {
332 $pageTypes[] = 'scheduled';
334 if ($this->_parent
->_archived
) {
335 $pageTypes[] = 'archived';
337 $opString = implode('.', $pageTypes);
339 foreach ($rows as $key => $row) {
341 if ($row['sms_provider_id']) {
342 $actionLinks[CRM_Core_Action
::PREVIEW
]['url'] = 'civicrm/sms/send';
345 if (!($row['status'] == 'Not scheduled') && !$row['sms_provider_id']) {
346 if ($allAccess ||
$showCreateLinks) {
347 $actionMask = CRM_Core_Action
::VIEW
;
350 if (!in_array($row['id'], $searchMailings)) {
351 if ($allAccess ||
$showCreateLinks) {
352 $actionMask |
= CRM_Core_Action
::UPDATE
;
357 if ($allAccess ||
($showCreateLinks ||
$showScheduleLinks)) {
358 $actionMask = CRM_Core_Action
::PREVIEW
;
361 if (in_array($row['status'], array(
367 ($showApprovalLinks && $showCreateLinks && $showScheduleLinks)
370 $actionMask |
= CRM_Core_Action
::DISABLE
;
372 if ($row['status'] == 'Scheduled' &&
373 empty($row['approval_status_id'])
375 if ($workFlow && ($allAccess ||
$showApprovalLinks)) {
376 $actionMask |
= CRM_Core_Action
::ENABLE
;
381 if (in_array($row['status'], array('Complete', 'Canceled')) &&
384 if ($allAccess ||
$showCreateLinks) {
385 $actionMask |
= CRM_Core_Action
::RENEW
;
389 //check for delete permission.
390 if ($allowToDelete) {
391 $actionMask |
= CRM_Core_Action
::DELETE
;
394 if ($actionMask == NULL) {
395 $actionMask = CRM_Core_Action
::ADD
;
397 //get status strings as per locale settings CRM-4411.
398 $rows[$key]['status'] = CRM_Mailing_BAO_MailingJob
::status($row['status']);
400 $rows[$key]['action'] = CRM_Core_Action
::formLink($actionLinks,
402 array('mid' => $row['id']),
410 //unset($rows[$key]['id']);
411 // if the scheduled date is 0, replace it with an empty string
412 if ($rows[$key]['scheduled_iso'] == '0000-00-00 00:00:00') {
413 $rows[$key]['scheduled'] = '';
415 unset($rows[$key]['scheduled_iso']);
419 // also initialize the AtoZ pager
425 * Name of export file.
427 * @param string $output
433 public function getExportFileName($output = 'csv') {
434 return ts('CiviMail Mailings');
440 public function setParent($parent) {
441 $this->_parent
= $parent;
445 * @param array $params
446 * @param bool $sortBy
450 public function whereClause(&$params, $sortBy = TRUE) {
451 $values = $clauses = array();
452 $isFormSubmitted = $this->_parent
->get('hidden_find_mailings');
454 $title = $this->_parent
->get('mailing_name');
456 $clauses[] = 'name LIKE %1';
457 if (strpos($title, '%') !== FALSE) {
458 $params[1] = array($title, 'String', FALSE);
461 $params[1] = array($title, 'String', TRUE);
465 $dateClause1 = $dateClause2 = array();
466 $from = $this->_parent
->get('mailing_from');
467 if (!CRM_Utils_System
::isNull($from)) {
468 if ($this->_parent
->get('unscheduled')) {
469 $dateClause1[] = 'civicrm_mailing.created_date >= %2';
472 $dateClause1[] = 'civicrm_mailing_job.start_date >= %2';
473 $dateClause2[] = 'civicrm_mailing_job.scheduled_date >= %2';
475 $params[2] = array($from, 'String');
478 $to = $this->_parent
->get('mailing_to');
479 if (!CRM_Utils_System
::isNull($to)) {
480 if ($this->_parent
->get('unscheduled')) {
481 $dateClause1[] = ' civicrm_mailing.created_date <= %3 ';
484 $dateClause1[] = 'civicrm_mailing_job.start_date <= %3';
485 $dateClause2[] = 'civicrm_mailing_job.scheduled_date <= %3';
487 $params[3] = array($to, 'String');
490 $dateClauses = array();
491 if (!empty($dateClause1)) {
492 $dateClauses[] = implode(' AND ', $dateClause1);
494 if (!empty($dateClause2)) {
495 $dateClauses[] = implode(' AND ', $dateClause2);
497 $dateClauses = implode(' OR ', $dateClauses);
498 if (!empty($dateClauses)) {
499 $clauses[] = "({$dateClauses})";
502 if ($this->_parent
->get('sms')) {
503 $clauses[] = "civicrm_mailing.sms_provider_id IS NOT NULL";
506 $clauses[] = "civicrm_mailing.sms_provider_id IS NULL";
509 // get values submitted by form
510 $isDraft = $this->_parent
->get('status_unscheduled');
511 $isArchived = $this->_parent
->get('is_archived');
512 $mailingStatus = $this->_parent
->get('mailing_status');
514 if (!$isFormSubmitted && $this->_parent
->get('scheduled')) {
515 // mimic default behavior for scheduled screen
517 $mailingStatus = array('Scheduled' => 1, 'Complete' => 1, 'Running' => 1, 'Canceled' => 1);
519 if (!$isFormSubmitted && $this->_parent
->get('archived')) {
520 // mimic default behavior for archived screen
523 if (!$isFormSubmitted && $this->_parent
->get('unscheduled')) {
524 // mimic default behavior for draft screen
528 $statusClauses = array();
530 $statusClauses[] = "civicrm_mailing.scheduled_id IS NULL";
532 if (!empty($mailingStatus)) {
533 $statusClauses[] = "civicrm_mailing_job.status IN ('" . implode("', '", array_keys($mailingStatus)) . "')";
535 if (!empty($statusClauses)) {
536 $clauses[] = "(" . implode(' OR ', $statusClauses) . ")";
539 if (isset($isArchived)) {
541 $clauses[] = "civicrm_mailing.is_archived = 1";
544 $clauses[] = "(civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0)";
549 $this->_parent
->_sortByCharacter
!== NULL
551 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO
::escapeWildCardString($this->_parent
->_sortByCharacter
)) . "%'";
554 // dont do a the below assignement when doing a
557 if (count($clauses) > 1) {
558 $this->_parent
->assign('isSearch', 1);
561 $this->_parent
->assign('isSearch', 0);
565 $createOrSentBy = $this->_parent
->get('sort_name');
566 if (!CRM_Utils_System
::isNull($createOrSentBy)) {
567 $clauses[] = '(createdContact.sort_name LIKE %4 OR scheduledContact.sort_name LIKE %4)';
568 $params[4] = array('%' . $createOrSentBy . '%', 'String');
571 $createdId = $this->_parent
->get('createdId');
573 $clauses[] = "(created_id = {$createdId})";
574 $params[5] = array($createdId, 'Integer');
577 $campainIds = $this->_parent
->get('campaign_id');
578 if (!CRM_Utils_System
::isNull($campainIds)) {
579 if (!is_array($campainIds)) {
580 $campaignIds = array($campaignIds);
582 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
585 if (empty($clauses)) {
589 return implode(' AND ', $clauses);
592 public function pagerAtoZ() {
595 $whereClause = $this->whereClause($params, FALSE);
598 SELECT DISTINCT UPPER(LEFT(name, 1)) as sort_name
600 LEFT JOIN civicrm_mailing_job ON (civicrm_mailing_job.mailing_id = civicrm_mailing.id)
601 LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
602 LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
604 ORDER BY LEFT(name, 1)
607 $dao = CRM_Core_DAO
::executeQuery($query, $params);
609 $aToZBar = CRM_Utils_PagerAToZ
::getAToZBar($dao, $this->_parent
->_sortByCharacter
, TRUE);
610 $this->_parent
->assign('aToZ', $aToZBar);