Merge pull request #18246 from sunilpawar/report_45
[civicrm-core.git] / CRM / Mailing / Page / Browse.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
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
21 * the admin
22 */
23 class CRM_Mailing_Page_Browse extends CRM_Core_Page {
24
25 /**
26 * All the fields that are listings related.
27 *
28 * @var array
29 */
30 protected $_fields;
31
32 /**
33 * The mailing id of the mailing we're operating on
34 *
35 * @var int
36 */
37 protected $_mailingId;
38
39 /**
40 * The action that we are performing (in CRM_Core_Action terms)
41 *
42 * @var int
43 */
44 protected $_action;
45
46 public $_sortByCharacter;
47
48 public $_unscheduled;
49 public $_archived;
50
51 /**
52 * Scheduled mailing.
53 *
54 * @var bool
55 */
56 public $_scheduled;
57
58 public $_sms;
59
60 /**
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.
63 */
64 public function preProcess() {
65 Civi::resources()->addStyleFile('civicrm', 'css/searchForm.css', 1, 'html-header');
66
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);
70
71 if ($this->_sms) {
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'));
75 }
76 }
77 else {
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.'));
83 }
84 }
85
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);
89
90 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
91 $this->assign('action', $this->_action);
92
93 $showLinks = TRUE;
94 if (CRM_Mailing_Info::workflowEnabled()) {
95 if (CRM_Core_Permission::check('create mailings')) {
96 $archiveLinks = TRUE;
97 }
98 if (!CRM_Core_Permission::check('access CiviMail') &&
99 !CRM_Core_Permission::check('create mailings')
100 ) {
101 $showLinks = FALSE;
102 }
103 }
104 $this->assign('showLinks', $showLinks);
105 if (CRM_Core_Permission::check('access CiviMail')) {
106 $archiveLinks = TRUE;
107 }
108 if ($archiveLinks == TRUE) {
109 $this->assign('archiveLinks', $archiveLinks);
110 }
111 }
112
113 /**
114 * Run this page (figure out the action needed and perform it).
115 */
116 public function run() {
117 $this->preProcess();
118
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();
128 }
129
130 $this->_sortByCharacter
131 = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
132
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);
137 }
138
139 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
140 $this->_unscheduled = TRUE;
141 }
142 $this->set('unscheduled', $this->_unscheduled);
143
144 $this->set('archived', $this->isArchived($newArgs));
145
146 if (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
147 $this->_scheduled = TRUE;
148 }
149 $this->set('scheduled', $this->_scheduled);
150
151 $this->_createdId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
152 if ($this->_createdId) {
153 $this->set('createdId', $this->_createdId);
154 }
155
156 if ($this->_sms) {
157 $this->set('sms', $this->_sms);
158 }
159
160 $session = CRM_Core_Session::singleton();
161 $context = $session->readUserContext();
162
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);
168 }
169 else {
170 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
171 ts('Cancel Mailing'),
172 $this->_action
173 );
174 $controller->setEmbedded(TRUE);
175 $controller->run();
176 }
177 }
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.'));
181 }
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);
185 }
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.'));
189 }
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);
193 }
194 elseif ($this->_action & CRM_Core_Action::DELETE) {
195 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
196
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.'));
200 }
201
202 CRM_Mailing_BAO_Mailing::del($this->_mailingId);
203 CRM_Utils_System::redirect($context);
204 }
205 else {
206 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
207 ts('Delete Mailing'),
208 $this->_action
209 );
210 $controller->setEmbedded(TRUE);
211 $controller->run();
212 }
213 }
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);
220 }
221 else {
222 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
223 ts('Archive Mailing'),
224 $this->_action
225 );
226 $controller->setEmbedded(TRUE);
227 $controller->run();
228 }
229 }
230
231 $selector = new CRM_Mailing_Selector_Browse();
232 $selector->setParent($this);
233
234 $controller = new CRM_Core_Selector_Controller(
235 $selector,
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,
239 $this,
240 CRM_Core_Selector_Controller::TEMPLATE
241 );
242
243 $controller->setEmbedded(TRUE);
244 $controller->run();
245
246 $urlParams = 'reset=1';
247 $urlString = 'civicrm/mailing/browse';
248 if ($this->get('sms')) {
249 $urlParams .= '&sms=1';
250 }
251 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
252 $urlString .= '/unscheduled';
253 $urlParams .= '&scheduled=false';
254 $this->assign('unscheduled', TRUE);
255 }
256
257 if ($this->isArchived($newArgs)) {
258 $urlString .= '/archived';
259 $this->assign('archived', TRUE);
260 }
261 elseif (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
262 $urlString .= '/scheduled';
263 $urlParams .= '&scheduled=true';
264 }
265 if ($this->get('sms')) {
266 CRM_Utils_System::setTitle(ts('Find Mass SMS'));
267 }
268
269 $crmRowCount = CRM_Utils_Request::retrieve('crmRowCount', 'Integer');
270 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer');
271 if ($crmRowCount || $crmPID) {
272 $urlParams .= '&force=1';
273 $urlParams .= $crmRowCount ? '&crmRowCount=' . $crmRowCount : '';
274 $urlParams .= $crmPID ? '&crmPID=' . $crmPID : '';
275 }
276
277 $crmSID = CRM_Utils_Request::retrieve('crmSID', 'Integer');
278 if ($crmSID) {
279 $urlParams .= '&crmSID=' . $crmSID;
280 }
281
282 $session = CRM_Core_Session::singleton();
283 $url = CRM_Utils_System::url($urlString, $urlParams);
284 $session->pushUserContext($url);
285
286 // CRM-6862 -run form cotroller after
287 // selector, since it erase $_POST
288 $this->search();
289
290 return parent::run();
291 }
292
293 public function search() {
294 if ($this->_action & (CRM_Core_Action::ADD |
295 CRM_Core_Action::UPDATE
296 )
297 ) {
298 return;
299 }
300
301 $form = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Search',
302 ts('Search Mailings'),
303 CRM_Core_Action::ADD
304 );
305 $form->setEmbedded(TRUE);
306 $form->setParent($this);
307 $form->process();
308 $form->run();
309 }
310
311 /**
312 * @param array $params
313 * @param bool $sortBy
314 *
315 * @return string
316 */
317 public function whereClause(&$params, $sortBy = TRUE) {
318 $values = [];
319
320 $clauses = [];
321 $title = $this->get('mailing_name');
322 // echo " name=$title ";
323 if ($title) {
324 $clauses[] = 'name LIKE %1';
325 if (strpos($title, '%') !== FALSE) {
326 $params[1] = [$title, 'String', FALSE];
327 }
328 else {
329 $params[1] = [$title, 'String', TRUE];
330 }
331 }
332
333 if ($sortBy &&
334 $this->_sortByCharacter !== NULL
335 ) {
336 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
337 }
338
339 $campainIds = $this->get('campaign_id');
340 if (!CRM_Utils_System::isNull($campainIds)) {
341 if (!is_array($campainIds)) {
342 $campaignIds = [$campaignIds];
343 }
344 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
345 }
346
347 return implode(' AND ', $clauses);
348 }
349
350 /**
351 * Is the search limited to archived mailings.
352 *
353 * @param array $urlArguments
354 *
355 * @return bool
356 *
357 * @throws \CRM_Core_Exception
358 */
359 protected function isArchived($urlArguments): bool {
360 return in_array('archived', $urlArguments, TRUE) || CRM_Utils_Request::retrieveValue('is_archived', 'Boolean');
361 }
362
363 }