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