3ea830fc4088d89a9fcdd19fdbad30a6558be633
[civicrm-core.git] / CRM / Mailing / Page / Browse.php
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 */
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 * @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 */
127 function run() {
128 $this->preProcess();
129
130 $newArgs = func_get_args();
131 // since we want only first function argument
132 $newArgs = $newArgs[0];
133 if (isset($_GET['runJobs']) || CRM_Utils_Array::value('2', $newArgs) == 'queue') {
134 $config = CRM_Core_Config::singleton();
135 CRM_Mailing_BAO_Job::runJobs_pre($config->mailerJobSize);
136 CRM_Mailing_BAO_Job::runJobs();
137 CRM_Mailing_BAO_Job::runJobs_post();
138 }
139
140 $this->_sortByCharacter =
141 CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this);
142
143
144 // CRM-11920 all should set sortByCharacter to null, not empty string
145 if (strtolower($this->_sortByCharacter) == 'all' || !empty($_POST)) {
146 $this->_sortByCharacter = null;
147 $this->set('sortByCharacter', null);
148 }
149
150 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
151 $this->_unscheduled = TRUE;
152 }
153 $this->set('unscheduled', $this->_unscheduled);
154
155 if (CRM_Utils_Array::value(3, $newArgs) == 'archived') {
156 $this->_archived = TRUE;
157 }
158 $this->set('archived', $this->_archived);
159
160 if (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
161 $this->_scheduled = TRUE;
162 }
163 $this->set('scheduled', $this->_scheduled);
164
165 $this->_createdId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, 0);
166 if ($this->_createdId) {
167 $this->set('createdId', $this->_createdId);
168 }
169
170 if ($this->_sms) {
171 $this->set('sms', $this->_sms);
172 }
173
174 $session = CRM_Core_Session::singleton();
175 $context = $session->readUserContext();
176
177 if ($this->_action & CRM_Core_Action::DISABLE) {
178 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
179 CRM_Mailing_BAO_Job::cancel($this->_mailingId);
180 CRM_Utils_System::redirect($context);
181 }
182 else {
183 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
184 ts('Cancel Mailing'),
185 $this->_action
186 );
187 $controller->setEmbedded(TRUE);
188 $controller->run();
189 }
190 }
191 elseif ($this->_action & CRM_Core_Action::DELETE) {
192 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
193
194 // check for action permissions.
195 if (!CRM_Core_Permission::checkActionPermission('CiviMail', $this->_action)) {
196 CRM_Core_Error::fatal(ts('You do not have permission to access this page'));
197 }
198
199 CRM_Mailing_BAO_Mailing::del($this->_mailingId);
200 CRM_Utils_System::redirect($context);
201 }
202 else {
203 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
204 ts('Delete Mailing'),
205 $this->_action
206 );
207 $controller->setEmbedded(TRUE);
208 $controller->run();
209 }
210 }
211 elseif ($this->_action & CRM_Core_Action::RENEW) {
212 //archive this mailing, CRM-3752.
213 if (CRM_Utils_Request::retrieve('confirmed', 'Boolean', $this)) {
214 //set is_archived to 1
215 CRM_Core_DAO::setFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingId, 'is_archived', TRUE);
216 CRM_Utils_System::redirect($context);
217 }
218 else {
219 $controller = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Browse',
220 ts('Archive Mailing'),
221 $this->_action
222 );
223 $controller->setEmbedded(TRUE);
224 $controller->run();
225 }
226 }
227
228 $selector = new CRM_Mailing_Selector_Browse();
229 $selector->setParent($this);
230
231 $controller = new CRM_Core_Selector_Controller(
232 $selector,
233 $this->get(CRM_Utils_Pager::PAGE_ID),
234 $this->get(CRM_Utils_Sort::SORT_ID) . $this->get(CRM_Utils_Sort::SORT_DIRECTION),
235 CRM_Core_Action::VIEW,
236 $this,
237 CRM_Core_Selector_Controller::TEMPLATE
238 );
239
240
241 $controller->setEmbedded(TRUE);
242 $controller->run();
243
244 //hack to display results as per search
245 $rows = $controller->getRows($controller);
246
247 $this->assign('rows', $rows);
248
249 $urlParams = 'reset=1';
250 $urlString = 'civicrm/mailing/browse';
251 if ($this->get('sms')) {
252 $urlParams .= '&sms=1';
253 }
254 if (CRM_Utils_Array::value(3, $newArgs) == 'unscheduled') {
255 $urlString .= '/unscheduled';
256 $urlParams .= '&scheduled=false';
257 $this->assign('unscheduled', TRUE);
258
259 if ($this->get('sms')) {
260 CRM_Utils_System::setTitle(ts('Draft and Unscheduled Mass SMS'));
261 }
262 else {
263 CRM_Utils_System::setTitle(ts('Draft and Unscheduled Mailings'));
264 }
265 }
266 elseif (CRM_Utils_Array::value(3, $newArgs) == 'archived') {
267 $urlString .= '/archived';
268 $this->assign('archived', TRUE);
269
270 if ($this->get('sms')) {
271 CRM_Utils_System::setTitle(ts('Archived Mass SMS'));
272 }
273 else {
274 CRM_Utils_System::setTitle(ts('Archived Mailings'));
275 }
276 }
277 elseif (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') {
278 $urlString .= '/scheduled';
279 $urlParams .= '&scheduled=true';
280
281 if ($this->get('sms')) {
282 CRM_Utils_System::setTitle(ts('Scheduled and Sent Mass SMS'));
283 }
284 else {
285 CRM_Utils_System::setTitle(ts('Scheduled and Sent Mailings'));
286 }
287 }
288 else {
289 if ($this->get('sms')) {
290 CRM_Utils_System::setTitle(ts('Find Mass SMS'));
291 }
292 else {
293 CRM_Utils_System::setTitle(ts('Find Mailings'));
294 }
295 }
296
297 $crmRowCount = CRM_Utils_Request::retrieve('crmRowCount', 'Integer', CRM_Core_DAO::$_nullObject);
298 $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject);
299 if ($crmRowCount || $crmPID) {
300 $urlParams .= '&force=1';
301 $urlParams .= $crmRowCount ? '&crmRowCount=' . $crmRowCount : '';
302 $urlParams .= $crmPID ? '&crmPID=' . $crmPID : '';
303 }
304
305 $crmSID = CRM_Utils_Request::retrieve('crmSID', 'Integer', CRM_Core_DAO::$_nullObject);
306 if ($crmSID) {
307 $urlParams .= '&crmSID=' . $crmSID;
308 }
309
310 $session = CRM_Core_Session::singleton();
311 $url = CRM_Utils_System::url($urlString, $urlParams);
312 $session->pushUserContext($url);
313
314 //CRM-6862 -run form cotroller after
315 //selector, since it erase $_POST
316 $this->search();
317
318 return parent::run();
319 }
320
321 function search() {
322 if ($this->_action &
323 (CRM_Core_Action::ADD |
324 CRM_Core_Action::UPDATE
325 )
326 ) {
327 return;
328 }
329
330 $form = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Search',
331 ts('Search Mailings'),
332 CRM_Core_Action::ADD
333 );
334 $form->setEmbedded(TRUE);
335 $form->setParent($this);
336 $form->process();
337 $form->run();
338 }
339
340 function whereClause(&$params, $sortBy = TRUE) {
341 $values = array();
342
343 $clauses = array();
344 $title = $this->get('mailing_name');
345 //echo " name=$title ";
346 if ($title) {
347 $clauses[] = 'name LIKE %1';
348 if (strpos($title, '%') !== FALSE) {
349 $params[1] = array($title, 'String', FALSE);
350 }
351 else {
352 $params[1] = array($title, 'String', TRUE);
353 }
354 }
355
356 if ($sortBy &&
357 $this->_sortByCharacter !== NULL
358 ) {
359 $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'";
360 }
361
362 $campainIds = $this->get('campaign_id');
363 if (!CRM_Utils_System::isNull($campainIds)) {
364 if (!is_array($campainIds)) {
365 $campaignIds = array($campaignIds);
366 }
367 $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )';
368 }
369
370 return implode(' AND ', $clauses);
371 }
372 }
373