3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This implements the profile page for all contacts. It uses a selector
20 * object to do the actual dispay. The fields displayd are controlled by
23 class CRM_Mailing_Page_Browse
extends CRM_Core_Page
{
26 * All the fields that are listings related.
33 * The mailing id of the mailing we're operating on
37 protected $_mailingId;
40 * The action that we are performing (in CRM_Core_Action terms)
46 public $_sortByCharacter;
61 * Heart of the viewing process. The runner gets all the meta data for
62 * the contact and calls the appropriate type of page to view.
64 public function preProcess() {
65 Civi
::resources()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
67 $this->_unscheduled
= $archiveLinks = FALSE;
68 $this->_mailingId
= CRM_Utils_Request
::retrieve('mid', 'Positive', $this);
69 $this->_sms
= CRM_Utils_Request
::retrieve('sms', 'Positive', $this);
72 // if this is an SMS page, check that the user has permission to browse SMS
73 if (!CRM_Core_Permission
::check('send SMS')) {
74 CRM_Core_Error
::statusBounce(ts('You do not have permission to send SMS'));
78 // If this is not an SMS page, check that the user has an appropriate
79 // permission (specific permissions have been copied from
80 // CRM/Mailing/xml/Menu/Mailing.xml)
81 if (!CRM_Core_Permission
::check([['access CiviMail', 'approve mailings', 'create mailings', 'schedule mailings']])) {
82 CRM_Core_Error
::statusBounce(ts('You do not have permission to view this page.'));
86 $this->assign('sms', $this->_sms
);
87 // check that the user has permission to access mailing id
88 CRM_Mailing_BAO_Mailing
::checkPermission($this->_mailingId
);
90 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
91 $this->assign('action', $this->_action
);
94 if (CRM_Mailing_Info
::workflowEnabled()) {
95 if (CRM_Core_Permission
::check('create mailings')) {
98 if (!CRM_Core_Permission
::check('access CiviMail') &&
99 !CRM_Core_Permission
::check('create mailings')
104 $this->assign('showLinks', $showLinks);
105 if (CRM_Core_Permission
::check('access CiviMail')) {
106 $archiveLinks = TRUE;
108 if ($archiveLinks == TRUE) {
109 $this->assign('archiveLinks', $archiveLinks);
114 * Run this page (figure out the action needed and perform it).
116 public function run() {
119 $newArgs = func_get_args();
120 // since we want only first function argument
121 $newArgs = $newArgs[0];
122 $this->_isArchived
= $this->isArchived($newArgs);
123 if (isset($_GET['runJobs']) || CRM_Utils_Array
::value('2', $newArgs) == 'queue') {
124 $mailerJobSize = Civi
::settings()->get('mailerJobSize');
125 CRM_Mailing_BAO_MailingJob
::runJobs_pre($mailerJobSize);
126 CRM_Mailing_BAO_MailingJob
::runJobs();
127 CRM_Mailing_BAO_MailingJob
::runJobs_post();
130 $this->_sortByCharacter
131 = CRM_Utils_Request
::retrieve('sortByCharacter', 'String', $this);
133 // CRM-11920 all should set sortByCharacter to null, not empty string
134 if (strtolower($this->_sortByCharacter
) == 'all' ||
!empty($_POST)) {
135 $this->_sortByCharacter
= NULL;
136 $this->set('sortByCharacter', NULL);
139 if (CRM_Utils_Array
::value(3, $newArgs) == 'unscheduled') {
140 $this->_unscheduled
= TRUE;
142 $this->set('unscheduled', $this->_unscheduled
);
144 $this->set('archived', $this->isArchived($newArgs));
146 if (CRM_Utils_Array
::value(3, $newArgs) == 'scheduled') {
147 $this->_scheduled
= TRUE;
149 $this->set('scheduled', $this->_scheduled
);
151 $this->_createdId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, FALSE, 0);
152 if ($this->_createdId
) {
153 $this->set('createdId', $this->_createdId
);
157 $this->set('sms', $this->_sms
);
160 $session = CRM_Core_Session
::singleton();
161 $context = $session->readUserContext();
163 if ($this->_action
& CRM_Core_Action
::DISABLE
) {
164 if (CRM_Utils_Request
::retrieve('confirmed', 'Boolean', $this)) {
165 CRM_Mailing_BAO_MailingJob
::cancel($this->_mailingId
);
166 CRM_Core_Session
::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success');
167 CRM_Utils_System
::redirect($context);
170 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
171 ts('Cancel Mailing'),
174 $controller->setEmbedded(TRUE);
178 elseif ($this->_action
& CRM_Core_Action
::CLOSE
) {
179 if (!CRM_Core_Permission
::checkActionPermission('CiviMail', CRM_Core_Action
::CLOSE
)) {
180 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this page.'));
182 CRM_Mailing_BAO_MailingJob
::pause($this->_mailingId
);
183 CRM_Core_Session
::setStatus(ts('The mailing has been paused. Active message deliveries may continue for a few minutes, but CiviMail will not begin delivery of any more batches.'), ts('Paused'), 'success');
184 CRM_Utils_System
::redirect($context);
186 elseif ($this->_action
& CRM_Core_Action
::REOPEN
) {
187 if (!CRM_Core_Permission
::checkActionPermission('CiviMail', CRM_Core_Action
::CLOSE
)) {
188 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this page.'));
190 CRM_Mailing_BAO_MailingJob
::resume($this->_mailingId
);
191 CRM_Core_Session
::setStatus(ts('The mailing has been resumed.'), ts('Resumed'), 'success');
192 CRM_Utils_System
::redirect($context);
194 elseif ($this->_action
& CRM_Core_Action
::DELETE
) {
195 if (CRM_Utils_Request
::retrieve('confirmed', 'Boolean', $this)) {
197 // check for action permissions.
198 if (!CRM_Core_Permission
::checkActionPermission('CiviMail', $this->_action
)) {
199 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this page.'));
202 CRM_Mailing_BAO_Mailing
::del($this->_mailingId
);
203 CRM_Utils_System
::redirect($context);
206 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
207 ts('Delete Mailing'),
210 $controller->setEmbedded(TRUE);
214 elseif ($this->_action
& CRM_Core_Action
::RENEW
) {
215 // archive this mailing, CRM-3752.
216 if (CRM_Utils_Request
::retrieve('confirmed', 'Boolean', $this)) {
217 // set is_archived to 1
218 CRM_Core_DAO
::setFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingId
, 'is_archived', TRUE);
219 CRM_Utils_System
::redirect($context);
222 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
223 ts('Archive Mailing'),
226 $controller->setEmbedded(TRUE);
231 $selector = new CRM_Mailing_Selector_Browse();
232 $selector->setParent($this);
234 $controller = new CRM_Core_Selector_Controller(
236 $this->get(CRM_Utils_Pager
::PAGE_ID
),
237 $this->get(CRM_Utils_Sort
::SORT_ID
) . $this->get(CRM_Utils_Sort
::SORT_DIRECTION
),
238 CRM_Core_Action
::VIEW
,
240 CRM_Core_Selector_Controller
::TEMPLATE
243 $controller->setEmbedded(TRUE);
246 // hack to display results as per search
247 $rows = $controller->getRows($controller);
249 $this->assign('rows', $rows);
251 $urlParams = 'reset=1';
252 $urlString = 'civicrm/mailing/browse';
253 if ($this->get('sms')) {
254 $urlParams .= '&sms=1';
256 if (CRM_Utils_Array
::value(3, $newArgs) == 'unscheduled') {
257 $urlString .= '/unscheduled';
258 $urlParams .= '&scheduled=false';
259 $this->assign('unscheduled', TRUE);
262 if ($this->isArchived($newArgs)) {
263 $urlString .= '/archived';
264 $this->assign('archived', TRUE);
266 elseif (CRM_Utils_Array
::value(3, $newArgs) == 'scheduled') {
267 $urlString .= '/scheduled';
268 $urlParams .= '&scheduled=true';
270 if ($this->get('sms')) {
271 CRM_Utils_System
::setTitle(ts('Find Mass SMS'));
274 $crmRowCount = CRM_Utils_Request
::retrieve('crmRowCount', 'Integer');
275 $crmPID = CRM_Utils_Request
::retrieve('crmPID', 'Integer');
276 if ($crmRowCount ||
$crmPID) {
277 $urlParams .= '&force=1';
278 $urlParams .= $crmRowCount ?
'&crmRowCount=' . $crmRowCount : '';
279 $urlParams .= $crmPID ?
'&crmPID=' . $crmPID : '';
282 $crmSID = CRM_Utils_Request
::retrieve('crmSID', 'Integer');
284 $urlParams .= '&crmSID=' . $crmSID;
287 $session = CRM_Core_Session
::singleton();
288 $url = CRM_Utils_System
::url($urlString, $urlParams);
289 $session->pushUserContext($url);
291 // CRM-6862 -run form cotroller after
292 // selector, since it erase $_POST
295 return parent
::run();
298 public function search() {
299 if ($this->_action
& (CRM_Core_Action
::ADD |
300 CRM_Core_Action
::UPDATE
306 $form = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Search',
307 ts('Search Mailings'),
310 $form->setEmbedded(TRUE);
311 $form->setParent($this);
317 * @param array $params
318 * @param bool $sortBy
322 public function whereClause(&$params, $sortBy = TRUE) {
326 $title = $this->get('mailing_name');
327 // echo " name=$title ";
329 $clauses[] = 'name LIKE %1';
330 if (strpos($title, '%') !== FALSE) {
331 $params[1] = [$title, 'String', FALSE];
334 $params[1] = [$title, 'String', TRUE];
339 $this->_sortByCharacter
!== NULL
341 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO
::escapeWildCardString($this->_sortByCharacter
)) . "%'";
344 $campainIds = $this->get('campaign_id');
345 if (!CRM_Utils_System
::isNull($campainIds)) {
346 if (!is_array($campainIds)) {
347 $campaignIds = [$campaignIds];
349 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
352 return implode(' AND ', $clauses);
356 * Is the search limited to archived mailings.
358 * @param array $urlArguments
362 * @throws \CRM_Core_Exception
364 protected function isArchived($urlArguments): bool {
365 return in_array('archived', $urlArguments, TRUE) || CRM_Utils_Request
::retrieveValue('is_archived', 'Boolean');