3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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;
64 * @return \CRM_Mailing_Selector_Browse
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.
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';
102 * returns the column headers as an array of tuples:
103 * (name, sortName (key to the sort array))
105 * @param string $action the action being performed
106 * @param enum $output what should the result set include (web/email/csv)
108 * @return array the column headers that need to be displayed
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;
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
;
122 elseif ($this->_parent
->get('scheduled')) {
123 $scheduledOrder = CRM_Utils_Sort
::DESCENDING
;
126 // sort by completed date for archived and undefined get
127 $completedOrder = CRM_Utils_Sort
::DESCENDING
;
129 $nameHeaderLabel = ($this->_parent
->get('sms')) ?
ts('SMS Name') : ts('Mailing Name');
131 self
::$_columnHeaders = array(
133 'name' => $nameHeaderLabel,
135 'direction' => CRM_Utils_Sort
::DONTCARE
,
138 'name' => ts('Status'),
140 'direction' => CRM_Utils_Sort
::DONTCARE
,
143 'name' => ts('Created By'),
144 'sort' => 'created_by',
145 'direction' => CRM_Utils_Sort
::DONTCARE
,
148 'name' => ts('Created Date'),
149 'sort' => 'created_date',
150 'direction' => $unscheduledOrder,
153 'name' => ts('Sent By'),
154 'sort' => 'scheduled_by',
155 'direction' => CRM_Utils_Sort
::DONTCARE
,
158 'name' => ts('Scheduled'),
159 'sort' => 'scheduled_date',
160 'direction' => $scheduledOrder,
163 'name' => ts('Started'),
164 'sort' => 'start_date',
165 'direction' => CRM_Utils_Sort
::DONTCARE
,
168 'name' => ts('Completed'),
169 'sort' => 'end_date',
170 'direction' => $completedOrder,
174 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable()) {
175 self
::$_columnHeaders[] = array('name' => ts('Campaign'),
176 'sort' => 'campaign_id',
177 'direction' => CRM_Utils_Sort
::DONTCARE
,
181 if ($output != CRM_Core_Selector_Controller
::EXPORT
) {
182 self
::$_columnHeaders[] = array('name' => ts('Action'));
185 return self
::$_columnHeaders;
189 * Returns total number of rows for the query.
193 * @return int Total number of rows
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();
201 //get the where clause.
203 $whereClause = "$mailingACL AND " . $this->whereClause($params);
205 // CRM-11919 added addition ON clauses to mailing_job to match getRows
207 SELECT COUNT( DISTINCT $mailing.id ) as count
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 )
214 return CRM_Core_DAO
::singleValueQuery($query, $params);
218 * returns all the rows in the given offset and rowCount
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)
226 * @return int the total number of rows for this action
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?');
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(
364 'Scheduled', 'Running', 'Paused'))) {
366 ($showApprovalLinks && $showCreateLinks && $showScheduleLinks)
369 $actionMask |
= CRM_Core_Action
::DISABLE
;
371 if ($row['status'] == 'Scheduled' &&
372 empty($row['approval_status_id'])
374 if ($workFlow && ($allAccess ||
$showApprovalLinks)) {
375 $actionMask |
= CRM_Core_Action
::ENABLE
;
380 if (in_array($row['status'], array('Complete', 'Canceled')) &&
382 if ($allAccess ||
$showCreateLinks) {
383 $actionMask |
= CRM_Core_Action
::RENEW
;
387 //check for delete permission.
388 if ($allowToDelete) {
389 $actionMask |
= CRM_Core_Action
::DELETE
;
392 if ($actionMask == NULL) {
393 $actionMask = CRM_Core_Action
::ADD
;
395 //get status strings as per locale settings CRM-4411.
396 $rows[$key]['status'] = CRM_Mailing_BAO_MailingJob
::status($row['status']);
398 $rows[$key]['action'] = CRM_Core_Action
::formLink($actionLinks,
400 array('mid' => $row['id']),
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'] = '';
413 unset($rows[$key]['scheduled_iso']);
417 // also initialize the AtoZ pager
423 * name of export file.
425 * @param string $output type of output
427 * @return string name of the file
429 function getExportFileName($output = 'csv') {
430 return ts('CiviMail Mailings');
433 function setParent($parent) {
434 $this->_parent
= $parent;
437 function whereClause(&$params, $sortBy = TRUE) {
438 $values = $clauses = array();
439 $isFormSubmitted = $this->_parent
->get('hidden_find_mailings');
441 $title = $this->_parent
->get('mailing_name');
443 $clauses[] = 'name LIKE %1';
444 if (strpos($title, '%') !== FALSE) {
445 $params[1] = array($title, 'String', FALSE);
448 $params[1] = array($title, 'String', TRUE);
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';
459 $dateClause1[] = 'civicrm_mailing_job.start_date >= %2';
460 $dateClause2[] = 'civicrm_mailing_job.scheduled_date >= %2';
462 $params[2] = array($from, 'String');
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 ';
471 $dateClause1[] = 'civicrm_mailing_job.start_date <= %3';
472 $dateClause2[] = 'civicrm_mailing_job.scheduled_date <= %3';
474 $params[3] = array($to, 'String');
477 $dateClauses = array();
478 if (!empty($dateClause1)) {
479 $dateClauses[] = implode(' AND ', $dateClause1);
481 if (!empty($dateClause2)) {
482 $dateClauses[] = implode(' AND ', $dateClause2);
484 $dateClauses = implode(' OR ', $dateClauses);
485 if (!empty($dateClauses)) {
486 $clauses[] = "({$dateClauses})";
489 if ($this->_parent
->get('sms')) {
490 $clauses[] = "civicrm_mailing.sms_provider_id IS NOT NULL";
493 $clauses[] = "civicrm_mailing.sms_provider_id IS NULL";
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');
501 if (!$isFormSubmitted && $this->_parent
->get('scheduled')) {
502 // mimic default behavior for scheduled screen
504 $mailingStatus = array('Scheduled' => 1, 'Complete' => 1, 'Running' => 1, 'Canceled' => 1);
506 if (!$isFormSubmitted && $this->_parent
->get('archived')) {
507 // mimic default behavior for archived screen
510 if (!$isFormSubmitted && $this->_parent
->get('unscheduled')) {
511 // mimic default behavior for draft screen
515 $statusClauses = array();
517 $statusClauses[] = "civicrm_mailing.scheduled_id IS NULL";
519 if (!empty($mailingStatus)) {
520 $statusClauses[] = "civicrm_mailing_job.status IN ('" . implode("', '", array_keys($mailingStatus)) . "')";
522 if (!empty($statusClauses)) {
523 $clauses[] = "(" . implode(' OR ', $statusClauses) . ")";
526 if (isset($isArchived)) {
528 $clauses[] = "civicrm_mailing.is_archived = 1";
530 $clauses[] = "(civicrm_mailing.is_archived IS NULL OR civicrm_mailing.is_archived = 0)";
535 $this->_parent
->_sortByCharacter
!== NULL
537 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO
::escapeWildCardString($this->_parent
->_sortByCharacter
)) . "%'";
540 // dont do a the below assignement when doing a
543 if (count($clauses) > 1) {
544 $this->_parent
->assign('isSearch', 1);
547 $this->_parent
->assign('isSearch', 0);
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');
557 $createdId = $this->_parent
->get('createdId');
559 $clauses[] = "(created_id = {$createdId})";
560 $params[5] = array($createdId, 'Integer');
563 $campainIds = $this->_parent
->get('campaign_id');
564 if (!CRM_Utils_System
::isNull($campainIds)) {
565 if (!is_array($campainIds)) {
566 $campaignIds = array($campaignIds);
568 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
571 if (empty($clauses)) {
575 return implode(' AND ', $clauses);
578 function pagerAtoZ() {
581 $whereClause = $this->whereClause($params, FALSE);
584 SELECT DISTINCT UPPER(LEFT(name, 1)) as sort_name
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 )
590 ORDER BY LEFT(name, 1)
593 $dao = CRM_Core_DAO
::executeQuery($query, $params);
595 $aToZBar = CRM_Utils_PagerAToZ
::getAToZBar($dao, $this->_parent
->_sortByCharacter
, TRUE);
596 $this->_parent
->assign('aToZ', $aToZBar);