Bugfix - getting id from hash (#14508)
[civicrm-core.git] / CRM / Mailing / Page / Browse.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
35 * This implements the profile page for all contacts. It uses a selector
36 * object to do the actual dispay. The fields displayd are controlled by
37 * the admin
38 */
39class CRM_Mailing_Page_Browse extends CRM_Core_Page {
40
41 /**
fe482240 42 * All the fields that are listings related.
6a488035
TO
43 *
44 * @var array
6a488035
TO
45 */
46 protected $_fields;
47
48 /**
100fef9d 49 * The mailing id of the mailing we're operating on
6a488035 50 *
25606795 51 * @var int
6a488035
TO
52 */
53 protected $_mailingId;
54
55 /**
100fef9d 56 * The action that we are performing (in CRM_Core_Action terms)
6a488035 57 *
25606795 58 * @var int
6a488035
TO
59 */
60 protected $_action;
61
62 public $_sortByCharacter;
63
64 public $_unscheduled;
65 public $_archived;
66
67 /**
fe482240 68 * Scheduled mailing.
6a488035 69 *
b67daa72 70 * @var bool
6a488035
TO
71 */
72 public $_scheduled;
73
74 public $_sms;
75
76 /**
77 * Heart of the viewing process. The runner gets all the meta data for
78 * the contact and calls the appropriate type of page to view.
6a488035 79 */
00be9182 80 public function preProcess() {
734b09fb
CW
81 Civi::resources()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
82
6a488035 83 $this->_unscheduled = $this->_archived = $archiveLinks = FALSE;
353ffa53
TO
84 $this->_mailingId = CRM_Utils_Request::retrieve('mid', 'Positive', $this);
85 $this->_sms = CRM_Utils_Request::retrieve('sms', 'Positive', $this);
63483feb
MM
86
87 if ($this->_sms) {
88 // if this is an SMS page, check that the user has permission to browse SMS
89 if (!CRM_Core_Permission::check('send SMS')) {
90 CRM_Core_Error::fatal(ts('You do not have permission to send SMS'));
91 }
92 }
93 else {
94 // If this is not an SMS page, check that the user has an appropriate
95 // permission (specific permissions have been copied from
96 // CRM/Mailing/xml/Menu/Mailing.xml)
be2fb01f 97 if (!CRM_Core_Permission::check([['access CiviMail', 'approve mailings', 'create mailings', 'schedule mailings']])) {
63483feb
MM
98 CRM_Core_Error::fatal(ts('You do not have permission to view this page.'));
99 }
100 }
101
6a488035
TO
102 $this->assign('sms', $this->_sms);
103 // check that the user has permission to access mailing id
104 CRM_Mailing_BAO_Mailing::checkPermission($this->_mailingId);
105
106 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
107 $this->assign('action', $this->_action);
108
109 $showLinks = TRUE;
110 if (CRM_Mailing_Info::workflowEnabled()) {
111 if (CRM_Core_Permission::check('create mailings')) {
112 $archiveLinks = TRUE;
113 }
114 if (!CRM_Core_Permission::check('access CiviMail') &&
115 !CRM_Core_Permission::check('create mailings')
116 ) {
117 $showLinks = FALSE;
118 }
119 }
120 $this->assign('showLinks', $showLinks);
121 if (CRM_Core_Permission::check('access CiviMail')) {
122 $archiveLinks = TRUE;
123 }
124 if ($archiveLinks == TRUE) {
125 $this->assign('archiveLinks', $archiveLinks);
126 }
127 }
128
129 /**
100fef9d 130 * Run this page (figure out the action needed and perform it).
6a488035 131 */
00be9182 132 public function run() {
6a488035
TO
133 $this->preProcess();
134
39eb89f4 135 $newArgs = func_get_args();
78e6cd48
RN
136 // since we want only first function argument
137 $newArgs = $newArgs[0];
6a488035 138 if (isset($_GET['runJobs']) || CRM_Utils_Array::value('2', $newArgs) == 'queue') {
dc00ac6d
TO
139 $mailerJobSize = Civi::settings()->get('mailerJobSize');
140 CRM_Mailing_BAO_MailingJob::runJobs_pre($mailerJobSize);
9da8dc8c 141 CRM_Mailing_BAO_MailingJob::runJobs();
142 CRM_Mailing_BAO_MailingJob::runJobs_post();
6a488035
TO
143 }
144
e7483cbe
J
145 $this->_sortByCharacter
146 = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
6a488035 147
6a488035
TO
148 // CRM-11920 all should set sortByCharacter to null, not empty string
149 if (strtolower($this->_sortByCharacter) == 'all' || !empty($_POST)) {
35f7561f
TO
150 $this->_sortByCharacter = NULL;
151 $this->set('sortByCharacter', NULL);
6a488035
TO
152 }
153
154 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
155 $this->_unscheduled = TRUE;
156 }
157 $this->set('unscheduled', $this->_unscheduled);
158
159 if (CRM_Utils_Array::value(3, $newArgs) == 'archived') {
160 $this->_archived = TRUE;
161 }
162 $this->set('archived', $this->_archived);
163
164 if (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
165 $this->_scheduled = TRUE;
166 }
167 $this->set('scheduled', $this->_scheduled);
168
169 $this->_createdId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
170 if ($this->_createdId) {
171 $this->set('createdId', $this->_createdId);
172 }
173
174 if ($this->_sms) {
175 $this->set('sms', $this->_sms);
176 }
177
178 $session = CRM_Core_Session::singleton();
179 $context = $session->readUserContext();
180
181 if ($this->_action & CRM_Core_Action::DISABLE) {
182 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
9da8dc8c 183 CRM_Mailing_BAO_MailingJob::cancel($this->_mailingId);
67d4ed51 184 CRM_Core_Session::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success');
6a488035
TO
185 CRM_Utils_System::redirect($context);
186 }
187 else {
188 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
189 ts('Cancel Mailing'),
190 $this->_action
191 );
192 $controller->setEmbedded(TRUE);
193 $controller->run();
194 }
195 }
4aa8f804
JP
196 elseif ($this->_action & CRM_Core_Action::CLOSE) {
197 if (!CRM_Core_Permission::checkActionPermission('CiviMail', CRM_Core_Action::CLOSE)) {
198 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
199 }
200 CRM_Mailing_BAO_MailingJob::pause($this->_mailingId);
06b521e3 201 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');
4aa8f804
JP
202 CRM_Utils_System::redirect($context);
203 }
204 elseif ($this->_action & CRM_Core_Action::REOPEN) {
205 if (!CRM_Core_Permission::checkActionPermission('CiviMail', CRM_Core_Action::CLOSE)) {
206 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
207 }
208 CRM_Mailing_BAO_MailingJob::resume($this->_mailingId);
67d4ed51 209 CRM_Core_Session::setStatus(ts('The mailing has been resumed.'), ts('Resumed'), 'success');
4aa8f804
JP
210 CRM_Utils_System::redirect($context);
211 }
6a488035
TO
212 elseif ($this->_action & CRM_Core_Action::DELETE) {
213 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
214
215 // check for action permissions.
216 if (!CRM_Core_Permission::checkActionPermission('CiviMail', $this->_action)) {
0499b0ad 217 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
218 }
219
220 CRM_Mailing_BAO_Mailing::del($this->_mailingId);
221 CRM_Utils_System::redirect($context);
222 }
223 else {
224 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
225 ts('Delete Mailing'),
226 $this->_action
227 );
228 $controller->setEmbedded(TRUE);
229 $controller->run();
230 }
231 }
232 elseif ($this->_action & CRM_Core_Action::RENEW) {
25606795 233 // archive this mailing, CRM-3752.
6a488035 234 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
25606795 235 // set is_archived to 1
6a488035
TO
236 CRM_Core_DAO::setFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingId, 'is_archived', TRUE);
237 CRM_Utils_System::redirect($context);
238 }
239 else {
240 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
241 ts('Archive Mailing'),
242 $this->_action
243 );
244 $controller->setEmbedded(TRUE);
245 $controller->run();
246 }
247 }
248
249 $selector = new CRM_Mailing_Selector_Browse();
250 $selector->setParent($this);
251
252 $controller = new CRM_Core_Selector_Controller(
253 $selector,
254 $this->get(CRM_Utils_Pager::PAGE_ID),
255 $this->get(CRM_Utils_Sort::SORT_ID) . $this->get(CRM_Utils_Sort::SORT_DIRECTION),
256 CRM_Core_Action::VIEW,
257 $this,
258 CRM_Core_Selector_Controller::TEMPLATE
259 );
260
6a488035
TO
261 $controller->setEmbedded(TRUE);
262 $controller->run();
263
25606795 264 // hack to display results as per search
6a488035
TO
265 $rows = $controller->getRows($controller);
266
267 $this->assign('rows', $rows);
268
269 $urlParams = 'reset=1';
270 $urlString = 'civicrm/mailing/browse';
271 if ($this->get('sms')) {
272 $urlParams .= '&sms=1';
273 }
274 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
275 $urlString .= '/unscheduled';
276 $urlParams .= '&scheduled=false';
277 $this->assign('unscheduled', TRUE);
6a488035
TO
278 }
279 elseif (CRM_Utils_Array::value(3, $newArgs) == 'archived') {
280 $urlString .= '/archived';
281 $this->assign('archived', TRUE);
6a488035
TO
282 }
283 elseif (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
284 $urlString .= '/scheduled';
285 $urlParams .= '&scheduled=true';
6a488035 286 }
f6df2c32
DS
287 if ($this->get('sms')) {
288 CRM_Utils_System::setTitle(ts('Find Mass SMS'));
6a488035
TO
289 }
290
a3d827a7
CW
291 $crmRowCount = CRM_Utils_Request::retrieve('crmRowCount', 'Integer');
292 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer');
6a488035
TO
293 if ($crmRowCount || $crmPID) {
294 $urlParams .= '&force=1';
295 $urlParams .= $crmRowCount ? '&crmRowCount=' . $crmRowCount : '';
296 $urlParams .= $crmPID ? '&crmPID=' . $crmPID : '';
297 }
298
a3d827a7 299 $crmSID = CRM_Utils_Request::retrieve('crmSID', 'Integer');
6a488035
TO
300 if ($crmSID) {
301 $urlParams .= '&crmSID=' . $crmSID;
302 }
303
304 $session = CRM_Core_Session::singleton();
305 $url = CRM_Utils_System::url($urlString, $urlParams);
306 $session->pushUserContext($url);
307
25606795
SB
308 // CRM-6862 -run form cotroller after
309 // selector, since it erase $_POST
6a488035
TO
310 $this->search();
311
312 return parent::run();
313 }
314
00be9182 315 public function search() {
35f7561f 316 if ($this->_action & (CRM_Core_Action::ADD |
6a488035
TO
317 CRM_Core_Action::UPDATE
318 )
319 ) {
320 return;
321 }
322
323 $form = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Search',
324 ts('Search Mailings'),
325 CRM_Core_Action::ADD
326 );
327 $form->setEmbedded(TRUE);
328 $form->setParent($this);
329 $form->process();
330 $form->run();
331 }
332
e0ef6999 333 /**
c490a46a 334 * @param array $params
e0ef6999
EM
335 * @param bool $sortBy
336 *
337 * @return string
338 */
00be9182 339 public function whereClause(&$params, $sortBy = TRUE) {
be2fb01f 340 $values = [];
6a488035 341
be2fb01f 342 $clauses = [];
6a488035 343 $title = $this->get('mailing_name');
25606795 344 // echo " name=$title ";
6a488035
TO
345 if ($title) {
346 $clauses[] = 'name LIKE %1';
347 if (strpos($title, '%') !== FALSE) {
be2fb01f 348 $params[1] = [$title, 'String', FALSE];
6a488035
TO
349 }
350 else {
be2fb01f 351 $params[1] = [$title, 'String', TRUE];
6a488035
TO
352 }
353 }
354
355 if ($sortBy &&
356 $this->_sortByCharacter !== NULL
357 ) {
358 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
359 }
360
361 $campainIds = $this->get('campaign_id');
362 if (!CRM_Utils_System::isNull($campainIds)) {
363 if (!is_array($campainIds)) {
be2fb01f 364 $campaignIds = [$campaignIds];
6a488035
TO
365 }
366 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
367 }
368
369 return implode(' AND ', $clauses);
370 }
96025800 371
6a488035 372}