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