Merge pull request #12072 from civicrm/5.1
[civicrm-core.git] / CRM / Mailing / Page / Browse.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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_Utils_System::redirect($context);
185 }
186 else {
187 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
188 ts('Cancel Mailing'),
189 $this->_action
190 );
191 $controller->setEmbedded(TRUE);
192 $controller->run();
193 }
194 }
195 elseif ($this->_action & CRM_Core_Action::DELETE) {
196 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
197
198 // check for action permissions.
199 if (!CRM_Core_Permission::checkActionPermission('CiviMail', $this->_action)) {
200 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
201 }
202
203 CRM_Mailing_BAO_Mailing::del($this->_mailingId);
204 CRM_Utils_System::redirect($context);
205 }
206 else {
207 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
208 ts('Delete Mailing'),
209 $this->_action
210 );
211 $controller->setEmbedded(TRUE);
212 $controller->run();
213 }
214 }
215 elseif ($this->_action & CRM_Core_Action::RENEW) {
216 // archive this mailing, CRM-3752.
217 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
218 // set is_archived to 1
219 CRM_Core_DAO::setFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingId, 'is_archived', TRUE);
220 CRM_Utils_System::redirect($context);
221 }
222 else {
223 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
224 ts('Archive Mailing'),
225 $this->_action
226 );
227 $controller->setEmbedded(TRUE);
228 $controller->run();
229 }
230 }
231
232 $selector = new CRM_Mailing_Selector_Browse();
233 $selector->setParent($this);
234
235 $controller = new CRM_Core_Selector_Controller(
236 $selector,
237 $this->get(CRM_Utils_Pager::PAGE_ID),
238 $this->get(CRM_Utils_Sort::SORT_ID) . $this->get(CRM_Utils_Sort::SORT_DIRECTION),
239 CRM_Core_Action::VIEW,
240 $this,
241 CRM_Core_Selector_Controller::TEMPLATE
242 );
243
244 $controller->setEmbedded(TRUE);
245 $controller->run();
246
247 // hack to display results as per search
248 $rows = $controller->getRows($controller);
249
250 $this->assign('rows', $rows);
251
252 $urlParams = 'reset=1';
253 $urlString = 'civicrm/mailing/browse';
254 if ($this->get('sms')) {
255 $urlParams .= '&sms=1';
256 }
257 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
258 $urlString .= '/unscheduled';
259 $urlParams .= '&scheduled=false';
260 $this->assign('unscheduled', TRUE);
261 }
262 elseif (CRM_Utils_Array::value(3, $newArgs) == 'archived') {
263 $urlString .= '/archived';
264 $this->assign('archived', TRUE);
265 }
266 elseif (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
267 $urlString .= '/scheduled';
268 $urlParams .= '&scheduled=true';
269 }
270 if ($this->get('sms')) {
271 CRM_Utils_System::setTitle(ts('Find Mass SMS'));
272 }
273
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 : '';
280 }
281
282 $crmSID = CRM_Utils_Request::retrieve('crmSID', 'Integer');
283 if ($crmSID) {
284 $urlParams .= '&crmSID=' . $crmSID;
285 }
286
287 $session = CRM_Core_Session::singleton();
288 $url = CRM_Utils_System::url($urlString, $urlParams);
289 $session->pushUserContext($url);
290
291 // CRM-6862 -run form cotroller after
292 // selector, since it erase $_POST
293 $this->search();
294
295 return parent::run();
296 }
297
298 public function search() {
299 if ($this->_action & (CRM_Core_Action::ADD |
300 CRM_Core_Action::UPDATE
301 )
302 ) {
303 return;
304 }
305
306 $form = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Search',
307 ts('Search Mailings'),
308 CRM_Core_Action::ADD
309 );
310 $form->setEmbedded(TRUE);
311 $form->setParent($this);
312 $form->process();
313 $form->run();
314 }
315
316 /**
317 * @param array $params
318 * @param bool $sortBy
319 *
320 * @return string
321 */
322 public function whereClause(&$params, $sortBy = TRUE) {
323 $values = array();
324
325 $clauses = array();
326 $title = $this->get('mailing_name');
327 // echo " name=$title ";
328 if ($title) {
329 $clauses[] = 'name LIKE %1';
330 if (strpos($title, '%') !== FALSE) {
331 $params[1] = array($title, 'String', FALSE);
332 }
333 else {
334 $params[1] = array($title, 'String', TRUE);
335 }
336 }
337
338 if ($sortBy &&
339 $this->_sortByCharacter !== NULL
340 ) {
341 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
342 }
343
344 $campainIds = $this->get('campaign_id');
345 if (!CRM_Utils_System::isNull($campainIds)) {
346 if (!is_array($campainIds)) {
347 $campaignIds = array($campaignIds);
348 }
349 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
350 }
351
352 return implode(' AND ', $clauses);
353 }
354
355 }