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