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