Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
232624b1 | 5 | | CiviCRM version 4.4 | |
6a488035 TO |
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 | */ | |
39eb89f4 | 127 | function run() { |
6a488035 TO |
128 | $this->preProcess(); |
129 | ||
39eb89f4 | 130 | $newArgs = func_get_args(); |
78e6cd48 RN |
131 | // since we want only first function argument |
132 | $newArgs = $newArgs[0]; | |
6a488035 TO |
133 | if (isset($_GET['runJobs']) || CRM_Utils_Array::value('2', $newArgs) == 'queue') { |
134 | $config = CRM_Core_Config::singleton(); | |
9da8dc8c | 135 | CRM_Mailing_BAO_MailingJob::runJobs_pre($config->mailerJobSize); |
136 | CRM_Mailing_BAO_MailingJob::runJobs(); | |
137 | CRM_Mailing_BAO_MailingJob::runJobs_post(); | |
6a488035 TO |
138 | } |
139 | ||
39eb89f4 DL |
140 | $this->_sortByCharacter = |
141 | CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this); | |
6a488035 TO |
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)) { | |
9da8dc8c | 179 | CRM_Mailing_BAO_MailingJob::cancel($this->_mailingId); |
6a488035 TO |
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); | |
6a488035 TO |
258 | } |
259 | elseif (CRM_Utils_Array::value(3, $newArgs) == 'archived') { | |
260 | $urlString .= '/archived'; | |
261 | $this->assign('archived', TRUE); | |
6a488035 TO |
262 | } |
263 | elseif (CRM_Utils_Array::value(3, $newArgs) == 'scheduled') { | |
264 | $urlString .= '/scheduled'; | |
265 | $urlParams .= '&scheduled=true'; | |
6a488035 | 266 | } |
f6df2c32 DS |
267 | if ($this->get('sms')) { |
268 | CRM_Utils_System::setTitle(ts('Find Mass SMS')); | |
6a488035 TO |
269 | } |
270 | ||
271 | $crmRowCount = CRM_Utils_Request::retrieve('crmRowCount', 'Integer', CRM_Core_DAO::$_nullObject); | |
272 | $crmPID = CRM_Utils_Request::retrieve('crmPID', 'Integer', CRM_Core_DAO::$_nullObject); | |
273 | if ($crmRowCount || $crmPID) { | |
274 | $urlParams .= '&force=1'; | |
275 | $urlParams .= $crmRowCount ? '&crmRowCount=' . $crmRowCount : ''; | |
276 | $urlParams .= $crmPID ? '&crmPID=' . $crmPID : ''; | |
277 | } | |
278 | ||
279 | $crmSID = CRM_Utils_Request::retrieve('crmSID', 'Integer', CRM_Core_DAO::$_nullObject); | |
280 | if ($crmSID) { | |
281 | $urlParams .= '&crmSID=' . $crmSID; | |
282 | } | |
283 | ||
284 | $session = CRM_Core_Session::singleton(); | |
285 | $url = CRM_Utils_System::url($urlString, $urlParams); | |
286 | $session->pushUserContext($url); | |
287 | ||
288 | //CRM-6862 -run form cotroller after | |
289 | //selector, since it erase $_POST | |
290 | $this->search(); | |
291 | ||
292 | return parent::run(); | |
293 | } | |
294 | ||
295 | function search() { | |
296 | if ($this->_action & | |
297 | (CRM_Core_Action::ADD | | |
298 | CRM_Core_Action::UPDATE | |
299 | ) | |
300 | ) { | |
301 | return; | |
302 | } | |
303 | ||
304 | $form = new CRM_Core_Controller_Simple('CRM_Mailing_Form_Search', | |
305 | ts('Search Mailings'), | |
306 | CRM_Core_Action::ADD | |
307 | ); | |
308 | $form->setEmbedded(TRUE); | |
309 | $form->setParent($this); | |
310 | $form->process(); | |
311 | $form->run(); | |
312 | } | |
313 | ||
314 | function whereClause(&$params, $sortBy = TRUE) { | |
315 | $values = array(); | |
316 | ||
317 | $clauses = array(); | |
318 | $title = $this->get('mailing_name'); | |
319 | //echo " name=$title "; | |
320 | if ($title) { | |
321 | $clauses[] = 'name LIKE %1'; | |
322 | if (strpos($title, '%') !== FALSE) { | |
323 | $params[1] = array($title, 'String', FALSE); | |
324 | } | |
325 | else { | |
326 | $params[1] = array($title, 'String', TRUE); | |
327 | } | |
328 | } | |
329 | ||
330 | if ($sortBy && | |
331 | $this->_sortByCharacter !== NULL | |
332 | ) { | |
333 | $clauses[] = "name LIKE '" . strtolower(CRM_Core_DAO::escapeWildCardString($this->_sortByCharacter)) . "%'"; | |
334 | } | |
335 | ||
336 | $campainIds = $this->get('campaign_id'); | |
337 | if (!CRM_Utils_System::isNull($campainIds)) { | |
338 | if (!is_array($campainIds)) { | |
339 | $campaignIds = array($campaignIds); | |
340 | } | |
341 | $clauses[] = '( campaign_id IN ( ' . implode(' , ', array_values($campainIds)) . ' ) )'; | |
342 | } | |
343 | ||
344 | return implode(' AND ', $clauses); | |
345 | } | |
346 | } | |
347 |