3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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;
64 * @return CRM_Contact_Selector_Profile
67 function __construct() {
72 * This method returns the links that are given for each search row.
78 static function &links() {
84 * getter for array of the parameters required for creating pager.
89 function getPagerParams($action, &$params) {
90 $params['csvString'] = NULL;
91 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
92 $params['status'] = ts('Mailings %%StatusMessage%%');
93 $params['buttonTop'] = 'PagerTopButton';
94 $params['buttonBottom'] = 'PagerBottomButton';
99 * returns the column headers as an array of tuples:
100 * (name, sortName (key to the sort array))
102 * @param string $action the action being performed
103 * @param enum $output what should the result set include (web/email/csv)
105 * @return array the column headers that need to be displayed
108 function &getColumnHeaders($action = NULL, $output = NULL) {
109 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
110 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
111 if (!isset(self
::$_columnHeaders)) {
112 $completedOrder = NULL;
114 // Set different default sort depending on type of mailings (CRM-7652)
115 $unscheduledOrder = $scheduledOrder = $archivedOrder = CRM_Utils_Sort
::DONTCARE
;
116 if ($this->_parent
->get('unscheduled')) {
117 $unscheduledOrder = CRM_Utils_Sort
::DESCENDING
;
119 elseif ($this->_parent
->get('scheduled')) {
120 $scheduledOrder = CRM_Utils_Sort
::DESCENDING
;
123 // sort by completed date for archived and undefined get
124 $completedOrder = CRM_Utils_Sort
::DESCENDING
;
126 $nameHeaderLabel = ($this->_parent
->get('sms')) ?
ts('SMS Name') : ts('Mailing Name');
128 self
::$_columnHeaders = array(
130 'name' => $nameHeaderLabel,
132 'direction' => CRM_Utils_Sort
::DONTCARE
,
135 'name' => ts('Status'),
137 'direction' => CRM_Utils_Sort
::DONTCARE
,
140 'name' => ts('Created By'),
141 'sort' => 'created_by',
142 'direction' => CRM_Utils_Sort
::DONTCARE
,
145 'name' => ts('Created Date'),
146 'sort' => 'created_date',
147 'direction' => $unscheduledOrder,
150 'name' => ts('Sent By'),
151 'sort' => 'scheduled_by',
152 'direction' => CRM_Utils_Sort
::DONTCARE
,
155 'name' => ts('Scheduled'),
156 'sort' => 'scheduled_date',
157 'direction' => $scheduledOrder,
160 'name' => ts('Started'),
161 'sort' => 'start_date',
162 'direction' => CRM_Utils_Sort
::DONTCARE
,
165 'name' => ts('Completed'),
166 'sort' => 'end_date',
167 'direction' => $completedOrder,
171 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable()) {
172 self
::$_columnHeaders[] = array('name' => ts('Campaign'),
173 'sort' => 'campaign_id',
174 'direction' => CRM_Utils_Sort
::DONTCARE
,
178 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
179 self
::$_columnHeaders[] = array('name' => ts('Action'));
182 return self
::$_columnHeaders;
186 * Returns total number of rows for the query.
190 * @return int Total number of rows
193 function getTotalCount($action) {
194 $job = CRM_Mailing_BAO_MailingJob
::getTableName();
195 $mailing = CRM_Mailing_BAO_Mailing
::getTableName();
196 $mailingACL = CRM_Mailing_BAO_Mailing
::mailingACL();
198 //get the where clause.
200 $whereClause = "$mailingACL AND " . $this->whereClause($params);
202 // CRM-11919 added addition ON clauses to mailing_job to match getRows
204 SELECT COUNT( DISTINCT $mailing.id ) as count
206 LEFT JOIN $job ON ( $mailing.id = $job.mailing_id AND civicrm_mailing_job.is_test = 0 AND civicrm_mailing_job.parent_id IS NULL )
207 LEFT JOIN civicrm_contact createdContact ON ( $mailing.created_id = createdContact.id )
208 LEFT JOIN civicrm_contact scheduledContact ON ( $mailing.scheduled_id = scheduledContact.id )
211 return CRM_Core_DAO
::singleValueQuery($query, $params);
215 * returns all the rows in the given offset and rowCount
217 * @param enum $action the action being performed
218 * @param int $offset the row number to start from
219 * @param int $rowCount the number of rows to return
220 * @param string $sort the sql string that describes the sort order
221 * @param enum $output what should the result set include (web/email/csv)
223 * @return int the total number of rows for this action
225 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
226 static $actionLinks = NULL;
227 if (empty($actionLinks)) {
228 $cancelExtra = ts('Are you sure you want to cancel this mailing?');
229 $deleteExtra = ts('Are you sure you want to delete this mailing?');
230 $archiveExtra = ts('Are you sure you want to archive this mailing?');
232 $actionLinks = array(
233 CRM_Core_Action
::ENABLE
=> array(
234 'name' => ts('Approve/Reject'),
235 'url' => 'civicrm/mailing/approve',
236 'qs' => 'mid=%%mid%%&reset=1',
237 'title' => ts('Approve/Reject Mailing'),
239 CRM_Core_Action
::VIEW
=> array(
240 'name' => ts('Report'),
241 'url' => 'civicrm/mailing/report',
242 'qs' => 'mid=%%mid%%&reset=1',
243 'title' => ts('View Mailing Report'),
245 CRM_Core_Action
::UPDATE
=> array(
246 'name' => ts('Re-Use'),
247 'url' => 'civicrm/mailing/send',
248 'qs' => 'mid=%%mid%%&reset=1',
249 'title' => ts('Re-Send Mailing'),
251 CRM_Core_Action
::DISABLE
=> array(
252 'name' => ts('Cancel'),
253 'url' => 'civicrm/mailing/browse',
254 'qs' => 'action=disable&mid=%%mid%%&reset=1',
255 'extra' => 'onclick="if (confirm(\'' . $cancelExtra . '\')) this.href+=\'&confirmed=1\'; else return false;"',
256 'title' => ts('Cancel Mailing'),
258 CRM_Core_Action
::PREVIEW
=> array(
259 'name' => ts('Continue'),
260 'url' => 'civicrm/mailing/send',
261 'qs' => 'mid=%%mid%%&continue=true&reset=1',
262 'title' => ts('Continue Mailing'),
264 CRM_Core_Action
::DELETE
=> array(
265 'name' => ts('Delete'),
266 'url' => 'civicrm/mailing/browse',
267 'qs' => 'action=delete&mid=%%mid%%&reset=1',
268 'extra' => 'onclick="if (confirm(\'' . $deleteExtra . '\')) this.href+=\'&confirmed=1\'; else return false;"',
269 'title' => ts('Delete Mailing'),
271 CRM_Core_Action
::RENEW
=> array(
272 'name' => ts('Archive'),
273 'url' => 'civicrm/mailing/browse/archived',
274 'qs' => 'action=renew&mid=%%mid%%&reset=1',
275 'extra' => 'onclick="if (confirm(\'' . $archiveExtra . '\')) this.href+=\'&confirmed=1\'; else return false;"',
276 'title' => ts('Archive Mailing'),
282 $workFlow = $showApprovalLinks = $showScheduleLinks = $showCreateLinks = FALSE;
283 if (CRM_Mailing_Info
::workflowEnabled()) {
286 //supercedes all permission
287 if (CRM_Core_Permission
::check('access CiviMail')) {
291 if (CRM_Core_Permission
::check('approve mailings')) {
292 $showApprovalLinks = TRUE;
295 if (CRM_Core_Permission
::check('create mailings')) {
296 $showCreateLinks = TRUE;
299 if (CRM_Core_Permission
::check('schedule mailings')) {
300 $showScheduleLinks = TRUE;
303 $mailing = new CRM_Mailing_BAO_Mailing();
307 $whereClause = ' AND ' . $this->whereClause($params);
309 if (empty($params)) {
310 $this->_parent
->assign('isSearch', 0);
313 $this->_parent
->assign('isSearch', 1);
315 $rows = &$mailing->getRows($offset, $rowCount, $sort, $whereClause, $params);
317 //get the search base mailing Ids, CRM-3711.
318 $searchMailings = $mailing->searchMailingIDs();
320 //check for delete CRM-4418
321 $allowToDelete = CRM_Core_Permission
::check('delete in CiviMail');
323 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
325 //create the appropriate $op to use for hook_civicrm_links
326 $pageTypes = array('view', 'mailing', 'browse');
327 if ($this->_parent
->_unscheduled
) {
328 $pageTypes[] = 'unscheduled';
330 if ($this->_parent
->_scheduled
) {
331 $pageTypes[] = 'scheduled';
333 if ($this->_parent
->_archived
) {
334 $pageTypes[] = 'archived';
336 $opString = implode('.', $pageTypes);
338 foreach ($rows as $key => $row) {
340 if ($row['sms_provider_id']) {
341 $actionLinks[CRM_Core_Action
::PREVIEW
]['url'] = 'civicrm/sms/send';
344 if (!($row['status'] == 'Not scheduled') && !$row['sms_provider_id']) {
345 if ($allAccess ||
$showCreateLinks) {
346 $actionMask = CRM_Core_Action
::VIEW
;
349 if (!in_array($row['id'], $searchMailings)) {
350 if ($allAccess ||
$showCreateLinks) {
351 $actionMask |
= CRM_Core_Action
::UPDATE
;
356 if ($allAccess ||
($showCreateLinks ||
$showScheduleLinks)) {
357 $actionMask = CRM_Core_Action
::PREVIEW
;
360 if (in_array($row['status'], array(
361 'Scheduled', 'Running', 'Paused'))) {
363 ($showApprovalLinks && $showCreateLinks && $showScheduleLinks)
366 $actionMask |
= CRM_Core_Action
::DISABLE
;
368 if ($row['status'] == 'Scheduled' &&
369 empty($row['approval_status_id'])
371 if ($workFlow && ($allAccess ||
$showApprovalLinks)) {
372 $actionMask |
= CRM_Core_Action
::ENABLE
;
377 if (in_array($row['status'], array('Complete', 'Canceled')) &&
379 if ($allAccess ||
$showCreateLinks) {
380 $actionMask |
= CRM_Core_Action
::RENEW
;
384 //check for delete permission.
385 if ($allowToDelete) {
386 $actionMask |
= CRM_Core_Action
::DELETE
;
389 if ($actionMask == NULL) {
390 $actionMask = CRM_Core_Action
::ADD
;
392 //get status strings as per locale settings CRM-4411.
393 $rows[$key]['status'] = CRM_Mailing_BAO_MailingJob
::status($row['status']);
395 $rows[$key]['action'] = CRM_Core_Action
::formLink($actionLinks,
397 array('mid' => $row['id']),
405 //unset($rows[$key]['id']);
406 // if the scheduled date is 0, replace it with an empty string
407 if ($rows[$key]['scheduled_iso'] == '0000-00-00 00:00:00') {
408 $rows[$key]['scheduled'] = '';
410 unset($rows[$key]['scheduled_iso']);
414 // also initialize the AtoZ pager
420 * name of export file.
422 * @param string $output type of output
424 * @return string name of the file
426 function getExportFileName($output = 'csv') {
427 return ts('CiviMail Mailings');
430 function setParent($parent) {
431 $this->_parent
= $parent;
434 function whereClause(&$params, $sortBy = TRUE) {
435 $values = $clauses = array();
436 $isFormSubmitted = $this->_parent
->get('hidden_find_mailings');
438 $title = $this->_parent
->get('mailing_name');
440 $clauses[] = 'name LIKE %1';
441 if (strpos($title, '%') !== FALSE) {
442 $params[1] = array($title, 'String', FALSE);
445 $params[1] = array($title, 'String', TRUE);
449 $dateClause1 = $dateClause2 = array();
450 $from = $this->_parent
->get('mailing_from');
451 if (!CRM_Utils_System
::isNull($from)) {
452 if ($this->_parent
->get('unscheduled')) {
453 $dateClause1[] = 'civicrm_mailing.created_date >= %2';
456 $dateClause1[] = 'civicrm_mailing_job.start_date >= %2';
457 $dateClause2[] = 'civicrm_mailing_job.scheduled_date >= %2';
459 $params[2] = array($from, 'String');
462 $to = $this->_parent
->get('mailing_to');
463 if (!CRM_Utils_System
::isNull($to)) {
464 if ($this->_parent
->get('unscheduled')) {
465 $dateClause1[] = ' civicrm_mailing.created_date <= %3 ';
468 $dateClause1[] = 'civicrm_mailing_job.start_date <= %3';
469 $dateClause2[] = 'civicrm_mailing_job.scheduled_date <= %3';
471 $params[3] = array($to, 'String');
474 $dateClauses = array();
475 if (!empty($dateClause1)) {
476 $dateClauses[] = implode(' AND ', $dateClause1);
478 if (!empty($dateClause2)) {
479 $dateClauses[] = implode(' AND ', $dateClause2);
481 $dateClauses = implode(' OR ', $dateClauses);
482 if (!empty($dateClauses)) {
483 $clauses[] = "({$dateClauses})";
486 if ($this->_parent
->get('sms')) {
487 $clauses[] = "civicrm_mailing.sms_provider_id IS NOT NULL";
490 $clauses[] = "civicrm_mailing.sms_provider_id IS NULL";
493 // get values submitted by form
494 $isDraft = $this->_parent
->get('status_unscheduled');
495 $isArchived = $this->_parent
->get('is_archived');
496 $mailingStatus = $this->_parent
->get('mailing_status');
498 if (!$isFormSubmitted && $this->_parent
->get('scheduled')) {
499 // mimic default behavior for scheduled screen
501 $mailingStatus = array('Scheduled' => 1, 'Complete' => 1, 'Running' => 1, 'Canceled' => 1);
503 if (!$isFormSubmitted && $this->_parent
->get('archived')) {
504 // mimic default behavior for archived screen
507 if (!$isFormSubmitted && $this->_parent
->get('unscheduled')) {
508 // mimic default behavior for draft screen
512 $statusClauses = array();
514 $statusClauses[] = "civicrm_mailing.scheduled_id IS NULL";
516 if (!empty($mailingStatus)) {
517 $statusClauses[] = "civicrm_mailing_job.status IN ('" . implode("', '", array_keys($mailingStatus)) . "')";
519 if (!empty($statusClauses)) {
520 $clauses[] = "(" . implode(' OR ', $statusClauses) . ")";
523 if (isset($isArchived)) {
525 $clauses[] = "civicrm_mailing.is_archived = 1";
527 $clauses[] = "(civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0)";
532 $this->_parent
->_sortByCharacter
!== NULL
534 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO
::escapeWildCardString($this->_parent
->_sortByCharacter
)) . "%'";
537 // dont do a the below assignement when doing a
540 if (count($clauses) > 1) {
541 $this->_parent
->assign('isSearch', 1);
544 $this->_parent
->assign('isSearch', 0);
548 $createOrSentBy = $this->_parent
->get('sort_name');
549 if (!CRM_Utils_System
::isNull($createOrSentBy)) {
550 $clauses[] = '(createdContact.sort_name LIKE %4 OR scheduledContact.sort_name LIKE %4)';
551 $params[4] = array('%' . $createOrSentBy . '%', 'String');
554 $createdId = $this->_parent
->get('createdId');
556 $clauses[] = "(created_id = {$createdId})";
557 $params[5] = array($createdId, 'Integer');
560 $campainIds = $this->_parent
->get('campaign_id');
561 if (!CRM_Utils_System
::isNull($campainIds)) {
562 if (!is_array($campainIds)) {
563 $campaignIds = array($campaignIds);
565 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
568 if (empty($clauses)) {
572 return implode(' AND ', $clauses);
575 function pagerAtoZ() {
578 $whereClause = $this->whereClause($params, FALSE);
581 SELECT DISTINCT UPPER(LEFT(name, 1)) as sort_name
583 LEFT JOIN civicrm_mailing_job ON (civicrm_mailing_job.mailing_id = civicrm_mailing.id)
584 LEFT JOIN civicrm_contact createdContact ON ( civicrm_mailing.created_id = createdContact.id )
585 LEFT JOIN civicrm_contact scheduledContact ON ( civicrm_mailing.scheduled_id = scheduledContact.id )
587 ORDER BY LEFT(name, 1)
590 $dao = CRM_Core_DAO
::executeQuery($query, $params);
592 $aToZBar = CRM_Utils_PagerAToZ
::getAToZBar($dao, $this->_parent
->_sortByCharacter
, TRUE);
593 $this->_parent
->assign('aToZ', $aToZBar);