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