Merge pull request #4896 from totten/master-movedep
[civicrm-core.git] / CRM / Mailing / Page / Browse.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
39de6fd5 5 | CiviCRM version 4.6 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36
37/**
38 * This implements the profile page for all contacts. It uses a selector
39 * object to do the actual dispay. The fields displayd are controlled by
40 * the admin
41 */
42class CRM_Mailing_Page_Browse extends CRM_Core_Page {
43
44 /**
100fef9d 45 * All the fields that are listings related
6a488035
TO
46 *
47 * @var array
6a488035
TO
48 */
49 protected $_fields;
50
51 /**
100fef9d 52 * The mailing id of the mailing we're operating on
6a488035
TO
53 *
54 * @int
6a488035
TO
55 */
56 protected $_mailingId;
57
58 /**
100fef9d 59 * The action that we are performing (in CRM_Core_Action terms)
6a488035
TO
60 *
61 * @int
6a488035
TO
62 */
63 protected $_action;
64
65 public $_sortByCharacter;
66
67 public $_unscheduled;
68 public $_archived;
69
70 /**
100fef9d 71 * Scheduled mailing
6a488035
TO
72 *
73 * @boolean
6a488035
TO
74 */
75 public $_scheduled;
76
77 public $_sms;
78
79 /**
80 * Heart of the viewing process. The runner gets all the meta data for
81 * the contact and calls the appropriate type of page to view.
82 *
83 * @return void
6a488035 84 */
00be9182 85 public function preProcess() {
6a488035 86 $this->_unscheduled = $this->_archived = $archiveLinks = FALSE;
353ffa53
TO
87 $this->_mailingId = CRM_Utils_Request::retrieve('mid', 'Positive', $this);
88 $this->_sms = CRM_Utils_Request::retrieve('sms', 'Positive', $this);
6a488035
TO
89 $this->assign('sms', $this->_sms);
90 // check that the user has permission to access mailing id
91 CRM_Mailing_BAO_Mailing::checkPermission($this->_mailingId);
92
93 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
94 $this->assign('action', $this->_action);
95
96 $showLinks = TRUE;
97 if (CRM_Mailing_Info::workflowEnabled()) {
98 if (CRM_Core_Permission::check('create mailings')) {
99 $archiveLinks = TRUE;
100 }
101 if (!CRM_Core_Permission::check('access CiviMail') &&
102 !CRM_Core_Permission::check('create mailings')
103 ) {
104 $showLinks = FALSE;
105 }
106 }
107 $this->assign('showLinks', $showLinks);
108 if (CRM_Core_Permission::check('access CiviMail')) {
109 $archiveLinks = TRUE;
110 }
111 if ($archiveLinks == TRUE) {
112 $this->assign('archiveLinks', $archiveLinks);
113 }
114 }
115
116 /**
100fef9d 117 * Run this page (figure out the action needed and perform it).
6a488035
TO
118 *
119 * @return void
120 */
00be9182 121 public function run() {
6a488035
TO
122 $this->preProcess();
123
39eb89f4 124 $newArgs = func_get_args();
78e6cd48
RN
125 // since we want only first function argument
126 $newArgs = $newArgs[0];
6a488035
TO
127 if (isset($_GET['runJobs']) || CRM_Utils_Array::value('2', $newArgs) == 'queue') {
128 $config = CRM_Core_Config::singleton();
9da8dc8c 129 CRM_Mailing_BAO_MailingJob::runJobs_pre($config->mailerJobSize);
130 CRM_Mailing_BAO_MailingJob::runJobs();
131 CRM_Mailing_BAO_MailingJob::runJobs_post();
6a488035
TO
132 }
133
39eb89f4
DL
134 $this->_sortByCharacter =
135 CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
6a488035 136
6a488035
TO
137 // CRM-11920 all should set sortByCharacter to null, not empty string
138 if (strtolower($this->_sortByCharacter) == 'all' || !empty($_POST)) {
35f7561f
TO
139 $this->_sortByCharacter = NULL;
140 $this->set('sortByCharacter', NULL);
6a488035
TO
141 }
142
143 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
144 $this->_unscheduled = TRUE;
145 }
146 $this->set('unscheduled', $this->_unscheduled);
147
148 if (CRM_Utils_Array::value(3, $newArgs) == 'archived') {
149 $this->_archived = TRUE;
150 }
151 $this->set('archived', $this->_archived);
152
153 if (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
154 $this->_scheduled = TRUE;
155 }
156 $this->set('scheduled', $this->_scheduled);
157
158 $this->_createdId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
159 if ($this->_createdId) {
160 $this->set('createdId', $this->_createdId);
161 }
162
163 if ($this->_sms) {
164 $this->set('sms', $this->_sms);
165 }
166
167 $session = CRM_Core_Session::singleton();
168 $context = $session->readUserContext();
169
170 if ($this->_action & CRM_Core_Action::DISABLE) {
171 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
9da8dc8c 172 CRM_Mailing_BAO_MailingJob::cancel($this->_mailingId);
6a488035
TO
173 CRM_Utils_System::redirect($context);
174 }
175 else {
176 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
177 ts('Cancel Mailing'),
178 $this->_action
179 );
180 $controller->setEmbedded(TRUE);
181 $controller->run();
182 }
183 }
184 elseif ($this->_action & CRM_Core_Action::DELETE) {
185 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
186
187 // check for action permissions.
188 if (!CRM_Core_Permission::checkActionPermission('CiviMail', $this->_action)) {
0499b0ad 189 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
190 }
191
192 CRM_Mailing_BAO_Mailing::del($this->_mailingId);
193 CRM_Utils_System::redirect($context);
194 }
195 else {
196 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
197 ts('Delete Mailing'),
198 $this->_action
199 );
200 $controller->setEmbedded(TRUE);
201 $controller->run();
202 }
203 }
204 elseif ($this->_action & CRM_Core_Action::RENEW) {
205 //archive this mailing, CRM-3752.
206 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
207 //set is_archived to 1
208 CRM_Core_DAO::setFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingId, 'is_archived', TRUE);
209 CRM_Utils_System::redirect($context);
210 }
211 else {
212 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
213 ts('Archive Mailing'),
214 $this->_action
215 );
216 $controller->setEmbedded(TRUE);
217 $controller->run();
218 }
219 }
220
221 $selector = new CRM_Mailing_Selector_Browse();
222 $selector->setParent($this);
223
224 $controller = new CRM_Core_Selector_Controller(
225 $selector,
226 $this->get(CRM_Utils_Pager::PAGE_ID),
227 $this->get(CRM_Utils_Sort::SORT_ID) . $this->get(CRM_Utils_Sort::SORT_DIRECTION),
228 CRM_Core_Action::VIEW,
229 $this,
230 CRM_Core_Selector_Controller::TEMPLATE
231 );
232
6a488035
TO
233 $controller->setEmbedded(TRUE);
234 $controller->run();
235
236 //hack to display results as per search
237 $rows = $controller->getRows($controller);
238
239 $this->assign('rows', $rows);
240
241 $urlParams = 'reset=1';
242 $urlString = 'civicrm/mailing/browse';
243 if ($this->get('sms')) {
244 $urlParams .= '&sms=1';
245 }
246 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
247 $urlString .= '/unscheduled';
248 $urlParams .= '&scheduled=false';
249 $this->assign('unscheduled', TRUE);
6a488035
TO
250 }
251 elseif (CRM_Utils_Array::value(3, $newArgs) == 'archived') {
252 $urlString .= '/archived';
253 $this->assign('archived', TRUE);
6a488035
TO
254 }
255 elseif (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
256 $urlString .= '/scheduled';
257 $urlParams .= '&scheduled=true';
6a488035 258 }
f6df2c32
DS
259 if ($this->get('sms')) {
260 CRM_Utils_System::setTitle(ts('Find Mass SMS'));
6a488035
TO
261 }
262
263 $crmRowCount = CRM_Utils_Request::retrieve('crmRowCount', 'Integer', CRM_Core_DAO::$_nullObject);
264 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
265 if ($crmRowCount || $crmPID) {
266 $urlParams .= '&force=1';
267 $urlParams .= $crmRowCount ? '&crmRowCount=' . $crmRowCount : '';
268 $urlParams .= $crmPID ? '&crmPID=' . $crmPID : '';
269 }
270
271 $crmSID = CRM_Utils_Request::retrieve('crmSID', 'Integer', CRM_Core_DAO::$_nullObject);
272 if ($crmSID) {
273 $urlParams .= '&crmSID=' . $crmSID;
274 }
275
276 $session = CRM_Core_Session::singleton();
277 $url = CRM_Utils_System::url($urlString, $urlParams);
278 $session->pushUserContext($url);
279
280 //CRM-6862 -run form cotroller after
281 //selector, since it erase $_POST
282 $this->search();
283
284 return parent::run();
285 }
286
00be9182 287 public function search() {
35f7561f 288 if ($this->_action & (CRM_Core_Action::ADD |
6a488035
TO
289 CRM_Core_Action::UPDATE
290 )
291 ) {
292 return;
293 }
294
295 $form = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Search',
296 ts('Search Mailings'),
297 CRM_Core_Action::ADD
298 );
299 $form->setEmbedded(TRUE);
300 $form->setParent($this);
301 $form->process();
302 $form->run();
303 }
304
e0ef6999 305 /**
c490a46a 306 * @param array $params
e0ef6999
EM
307 * @param bool $sortBy
308 *
309 * @return string
310 */
00be9182 311 public function whereClause(&$params, $sortBy = TRUE) {
6a488035
TO
312 $values = array();
313
314 $clauses = array();
315 $title = $this->get('mailing_name');
316 //echo " name=$title ";
317 if ($title) {
318 $clauses[] = 'name LIKE %1';
319 if (strpos($title, '%') !== FALSE) {
320 $params[1] = array($title, 'String', FALSE);
321 }
322 else {
323 $params[1] = array($title, 'String', TRUE);
324 }
325 }
326
327 if ($sortBy &&
328 $this->_sortByCharacter !== NULL
329 ) {
330 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
331 }
332
333 $campainIds = $this->get('campaign_id');
334 if (!CRM_Utils_System::isNull($campainIds)) {
335 if (!is_array($campainIds)) {
336 $campaignIds = array($campaignIds);
337 }
338 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
339 }
340
341 return implode(' AND ', $clauses);
342 }
343}