Merge pull request #13170 from elisseck/dev/core/485
[civicrm-core.git] / CRM / Mailing / Page / Browse.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
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 */
39 class CRM_Mailing_Page_Browse extends CRM_Core_Page {
40
41 /**
42 * All the fields that are listings related.
43 *
44 * @var array
45 */
46 protected $_fields;
47
48 /**
49 * The mailing id of the mailing we're operating on
50 *
51 * @var int
52 */
53 protected $_mailingId;
54
55 /**
56 * The action that we are performing (in CRM_Core_Action terms)
57 *
58 * @var int
59 */
60 protected $_action;
61
62 public $_sortByCharacter;
63
64 public $_unscheduled;
65 public $_archived;
66
67 /**
68 * Scheduled mailing.
69 *
70 * @var boolean
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.
79 */
80 public function preProcess() {
81 Civi::resources()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
82
83 $this->_unscheduled = $this->_archived = $archiveLinks = FALSE;
84 $this->_mailingId = CRM_Utils_Request::retrieve('mid', 'Positive', $this);
85 $this->_sms = CRM_Utils_Request::retrieve('sms', 'Positive', $this);
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)
97 if (!CRM_Core_Permission::check(array(array('access CiviMail', 'approve mailings', 'create mailings', 'schedule mailings')))) {
98 CRM_Core_Error::fatal(ts('You do not have permission to view this page.'));
99 }
100 }
101
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 /**
130 * Run this page (figure out the action needed and perform it).
131 */
132 public function run() {
133 $this->preProcess();
134
135 $newArgs = func_get_args();
136 // since we want only first function argument
137 $newArgs = $newArgs[0];
138 if (isset($_GET['runJobs']) || CRM_Utils_Array::value('2', $newArgs) == 'queue') {
139 $mailerJobSize = Civi::settings()->get('mailerJobSize');
140 CRM_Mailing_BAO_MailingJob::runJobs_pre($mailerJobSize);
141 CRM_Mailing_BAO_MailingJob::runJobs();
142 CRM_Mailing_BAO_MailingJob::runJobs_post();
143 }
144
145 $this->_sortByCharacter
146 = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
147
148 // CRM-11920 all should set sortByCharacter to null, not empty string
149 if (strtolower($this->_sortByCharacter) == 'all' || !empty($_POST)) {
150 $this->_sortByCharacter = NULL;
151 $this->set('sortByCharacter', NULL);
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)) {
183 CRM_Mailing_BAO_MailingJob::cancel($this->_mailingId);
184 CRM_Core_Session::setStatus(ts('The mailing has been canceled.'), ts('Canceled'), 'success');
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 }
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);
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');
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);
209 CRM_Core_Session::setStatus(ts('The mailing has been resumed.'), ts('Resumed'), 'success');
210 CRM_Utils_System::redirect($context);
211 }
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)) {
217 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
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) {
233 // archive this mailing, CRM-3752.
234 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
235 // set is_archived to 1
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
261 $controller->setEmbedded(TRUE);
262 $controller->run();
263
264 // hack to display results as per search
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);
278 }
279 elseif (CRM_Utils_Array::value(3, $newArgs) == 'archived') {
280 $urlString .= '/archived';
281 $this->assign('archived', TRUE);
282 }
283 elseif (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
284 $urlString .= '/scheduled';
285 $urlParams .= '&scheduled=true';
286 }
287 if ($this->get('sms')) {
288 CRM_Utils_System::setTitle(ts('Find Mass SMS'));
289 }
290
291 $crmRowCount = CRM_Utils_Request::retrieve('crmRowCount', 'Integer');
292 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer');
293 if ($crmRowCount || $crmPID) {
294 $urlParams .= '&force=1';
295 $urlParams .= $crmRowCount ? '&crmRowCount=' . $crmRowCount : '';
296 $urlParams .= $crmPID ? '&crmPID=' . $crmPID : '';
297 }
298
299 $crmSID = CRM_Utils_Request::retrieve('crmSID', 'Integer');
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
308 // CRM-6862 -run form cotroller after
309 // selector, since it erase $_POST
310 $this->search();
311
312 return parent::run();
313 }
314
315 public function search() {
316 if ($this->_action & (CRM_Core_Action::ADD |
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
333 /**
334 * @param array $params
335 * @param bool $sortBy
336 *
337 * @return string
338 */
339 public function whereClause(&$params, $sortBy = TRUE) {
340 $values = array();
341
342 $clauses = array();
343 $title = $this->get('mailing_name');
344 // echo " name=$title ";
345 if ($title) {
346 $clauses[] = 'name LIKE %1';
347 if (strpos($title, '%') !== FALSE) {
348 $params[1] = array($title, 'String', FALSE);
349 }
350 else {
351 $params[1] = array($title, 'String', TRUE);
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)) {
364 $campaignIds = array($campaignIds);
365 }
366 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
367 }
368
369 return implode(' AND ', $clauses);
370 }
371
372 }