Merge pull request #4892 from colemanw/INFRA-132
[civicrm-core.git] / CRM / Mailing / Page / Browse.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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
32 * @copyright CiviCRM LLC (c) 2004-2014
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 */
42 class CRM_Mailing_Page_Browse extends CRM_Core_Page {
43
44 /**
45 * All the fields that are listings related
46 *
47 * @var array
48 */
49 protected $_fields;
50
51 /**
52 * The mailing id of the mailing we're operating on
53 *
54 * @int
55 */
56 protected $_mailingId;
57
58 /**
59 * The action that we are performing (in CRM_Core_Action terms)
60 *
61 * @int
62 */
63 protected $_action;
64
65 public $_sortByCharacter;
66
67 public $_unscheduled;
68 public $_archived;
69
70 /**
71 * Scheduled mailing
72 *
73 * @boolean
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
84 *
85 */
86 public function preProcess() {
87 $this->_unscheduled = $this->_archived = $archiveLinks = FALSE;
88 $this->_mailingId = CRM_Utils_Request::retrieve('mid', 'Positive', $this);
89 $this->_sms = CRM_Utils_Request::retrieve('sms', 'Positive', $this);
90 $this->assign('sms', $this->_sms);
91 // check that the user has permission to access mailing id
92 CRM_Mailing_BAO_Mailing::checkPermission($this->_mailingId);
93
94 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
95 $this->assign('action', $this->_action);
96
97 $showLinks = TRUE;
98 if (CRM_Mailing_Info::workflowEnabled()) {
99 if (CRM_Core_Permission::check('create mailings')) {
100 $archiveLinks = TRUE;
101 }
102 if (!CRM_Core_Permission::check('access CiviMail') &&
103 !CRM_Core_Permission::check('create mailings')
104 ) {
105 $showLinks = FALSE;
106 }
107 }
108 $this->assign('showLinks', $showLinks);
109 if (CRM_Core_Permission::check('access CiviMail')) {
110 $archiveLinks = TRUE;
111 }
112 if ($archiveLinks == TRUE) {
113 $this->assign('archiveLinks', $archiveLinks);
114 }
115 }
116
117 /**
118 * Run this page (figure out the action needed and perform it).
119 *
120 * @return void
121 */
122 public function run() {
123 $this->preProcess();
124
125 $newArgs = func_get_args();
126 // since we want only first function argument
127 $newArgs = $newArgs[0];
128 if (isset($_GET['runJobs']) || CRM_Utils_Array::value('2', $newArgs) == 'queue') {
129 $config = CRM_Core_Config::singleton();
130 CRM_Mailing_BAO_MailingJob::runJobs_pre($config->mailerJobSize);
131 CRM_Mailing_BAO_MailingJob::runJobs();
132 CRM_Mailing_BAO_MailingJob::runJobs_post();
133 }
134
135 $this->_sortByCharacter =
136 CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
137
138 // CRM-11920 all should set sortByCharacter to null, not empty string
139 if (strtolower($this->_sortByCharacter) == 'all' || !empty($_POST)) {
140 $this->_sortByCharacter = NULL;
141 $this->set('sortByCharacter', NULL);
142 }
143
144 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
145 $this->_unscheduled = TRUE;
146 }
147 $this->set('unscheduled', $this->_unscheduled);
148
149 if (CRM_Utils_Array::value(3, $newArgs) == 'archived') {
150 $this->_archived = TRUE;
151 }
152 $this->set('archived', $this->_archived);
153
154 if (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
155 $this->_scheduled = TRUE;
156 }
157 $this->set('scheduled', $this->_scheduled);
158
159 $this->_createdId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
160 if ($this->_createdId) {
161 $this->set('createdId', $this->_createdId);
162 }
163
164 if ($this->_sms) {
165 $this->set('sms', $this->_sms);
166 }
167
168 $session = CRM_Core_Session::singleton();
169 $context = $session->readUserContext();
170
171 if ($this->_action & CRM_Core_Action::DISABLE) {
172 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
173 CRM_Mailing_BAO_MailingJob::cancel($this->_mailingId);
174 CRM_Utils_System::redirect($context);
175 }
176 else {
177 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
178 ts('Cancel Mailing'),
179 $this->_action
180 );
181 $controller->setEmbedded(TRUE);
182 $controller->run();
183 }
184 }
185 elseif ($this->_action & CRM_Core_Action::DELETE) {
186 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
187
188 // check for action permissions.
189 if (!CRM_Core_Permission::checkActionPermission('CiviMail', $this->_action)) {
190 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
191 }
192
193 CRM_Mailing_BAO_Mailing::del($this->_mailingId);
194 CRM_Utils_System::redirect($context);
195 }
196 else {
197 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
198 ts('Delete Mailing'),
199 $this->_action
200 );
201 $controller->setEmbedded(TRUE);
202 $controller->run();
203 }
204 }
205 elseif ($this->_action & CRM_Core_Action::RENEW) {
206 //archive this mailing, CRM-3752.
207 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
208 //set is_archived to 1
209 CRM_Core_DAO::setFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingId, 'is_archived', TRUE);
210 CRM_Utils_System::redirect($context);
211 }
212 else {
213 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
214 ts('Archive Mailing'),
215 $this->_action
216 );
217 $controller->setEmbedded(TRUE);
218 $controller->run();
219 }
220 }
221
222 $selector = new CRM_Mailing_Selector_Browse();
223 $selector->setParent($this);
224
225 $controller = new CRM_Core_Selector_Controller(
226 $selector,
227 $this->get(CRM_Utils_Pager::PAGE_ID),
228 $this->get(CRM_Utils_Sort::SORT_ID) . $this->get(CRM_Utils_Sort::SORT_DIRECTION),
229 CRM_Core_Action::VIEW,
230 $this,
231 CRM_Core_Selector_Controller::TEMPLATE
232 );
233
234 $controller->setEmbedded(TRUE);
235 $controller->run();
236
237 //hack to display results as per search
238 $rows = $controller->getRows($controller);
239
240 $this->assign('rows', $rows);
241
242 $urlParams = 'reset=1';
243 $urlString = 'civicrm/mailing/browse';
244 if ($this->get('sms')) {
245 $urlParams .= '&sms=1';
246 }
247 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
248 $urlString .= '/unscheduled';
249 $urlParams .= '&scheduled=false';
250 $this->assign('unscheduled', TRUE);
251 }
252 elseif (CRM_Utils_Array::value(3, $newArgs) == 'archived') {
253 $urlString .= '/archived';
254 $this->assign('archived', TRUE);
255 }
256 elseif (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
257 $urlString .= '/scheduled';
258 $urlParams .= '&scheduled=true';
259 }
260 if ($this->get('sms')) {
261 CRM_Utils_System::setTitle(ts('Find Mass SMS'));
262 }
263
264 $crmRowCount = CRM_Utils_Request::retrieve('crmRowCount', 'Integer', CRM_Core_DAO::$_nullObject);
265 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
266 if ($crmRowCount || $crmPID) {
267 $urlParams .= '&force=1';
268 $urlParams .= $crmRowCount ? '&crmRowCount=' . $crmRowCount : '';
269 $urlParams .= $crmPID ? '&crmPID=' . $crmPID : '';
270 }
271
272 $crmSID = CRM_Utils_Request::retrieve('crmSID', 'Integer', CRM_Core_DAO::$_nullObject);
273 if ($crmSID) {
274 $urlParams .= '&crmSID=' . $crmSID;
275 }
276
277 $session = CRM_Core_Session::singleton();
278 $url = CRM_Utils_System::url($urlString, $urlParams);
279 $session->pushUserContext($url);
280
281 //CRM-6862 -run form cotroller after
282 //selector, since it erase $_POST
283 $this->search();
284
285 return parent::run();
286 }
287
288 public function search() {
289 if ($this->_action & (CRM_Core_Action::ADD |
290 CRM_Core_Action::UPDATE
291 )
292 ) {
293 return;
294 }
295
296 $form = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Search',
297 ts('Search Mailings'),
298 CRM_Core_Action::ADD
299 );
300 $form->setEmbedded(TRUE);
301 $form->setParent($this);
302 $form->process();
303 $form->run();
304 }
305
306 /**
307 * @param array $params
308 * @param bool $sortBy
309 *
310 * @return string
311 */
312 public function whereClause(&$params, $sortBy = TRUE) {
313 $values = array();
314
315 $clauses = array();
316 $title = $this->get('mailing_name');
317 //echo " name=$title ";
318 if ($title) {
319 $clauses[] = 'name LIKE %1';
320 if (strpos($title, '%') !== FALSE) {
321 $params[1] = array($title, 'String', FALSE);
322 }
323 else {
324 $params[1] = array($title, 'String', TRUE);
325 }
326 }
327
328 if ($sortBy &&
329 $this->_sortByCharacter !== NULL
330 ) {
331 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
332 }
333
334 $campainIds = $this->get('campaign_id');
335 if (!CRM_Utils_System::isNull($campainIds)) {
336 if (!is_array($campainIds)) {
337 $campaignIds = array($campaignIds);
338 }
339 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
340 }
341
342 return implode(' AND ', $clauses);
343 }
344 }