INFRA-132 - Civi - PHPStorm cleanup
[civicrm-core.git] / CRM / Campaign / Form / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Files required
38 */
3efb5b86 39class CRM_Campaign_Form_Search extends CRM_Core_Form_Search {
6a488035 40
6a488035 41 /**
100fef9d 42 * The params that are sent to the query
6a488035
TO
43 *
44 * @var array
6a488035
TO
45 */
46 protected $_queryParams;
47
6a488035 48 /**
100fef9d 49 * Are we restricting ourselves to a single contact
6a488035 50 *
6a488035
TO
51 * @var boolean
52 */
53 protected $_single = FALSE;
54
55 /**
100fef9d 56 * Are we restricting ourselves to a single contact
6a488035 57 *
6a488035
TO
58 * @var boolean
59 */
60 protected $_limit = NULL;
61
6a488035
TO
62 protected $_defaults;
63
64 /**
100fef9d 65 * Prefix for the controller
6a488035
TO
66 */
67 protected $_prefix = "survey_";
68
69
70 private $_operation = 'reserve';
71
72 /**
100fef9d 73 * Processing needed for buildForm and later
6a488035
TO
74 *
75 * @return void
95ea96be
EM
76 */
77 function preProcess() {
6a488035
TO
78 $this->_done = FALSE;
79 $this->_defaults = array();
80
81 //set the button name.
82 $this->_searchButtonName = $this->getButtonName('refresh');
83 $this->_printButtonName = $this->getButtonName('next', 'print');
84 $this->_actionButtonName = $this->getButtonName('next', 'action');
85
86 //we allow the controller to set force/reset externally,
87 //useful when we are being driven by the wizard framework
88 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
89 $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE);
90 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search');
91 $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject);
92
93 //operation for state machine.
94 $this->_operation = CRM_Utils_Request::retrieve('op', 'String', $this, FALSE, 'reserve');
95 //validate operation.
96 if (!in_array($this->_operation, array(
97 'reserve', 'release', 'interview'))) {
98 $this->_operation = 'reserve';
99 $this->set('op', $this->_operation);
100 }
101 $this->set('searchVoterFor', $this->_operation);
102 $this->assign('searchVoterFor', $this->_operation);
103 $this->assign('isFormSubmitted', $this->isSubmitted());
104
105 //do check permissions.
106 if (!CRM_Core_Permission::check('administer CiviCampaign') &&
107 !CRM_Core_Permission::check('manage campaign') &&
108 !CRM_Core_Permission::check("{$this->_operation} campaign contacts")
109 ) {
110 CRM_Utils_System::permissionDenied();
111 CRM_Utils_System::civiExit();
112 }
113
114 $this->assign("context", $this->_context);
115
116 // get user submitted values
117 // get it from controller only if form has been submitted, else preProcess has set this
118
119 if (empty($_POST)) {
120 $this->_formValues = $this->get('formValues');
121 }
122 else {
123 $this->_formValues = $this->controller->exportValues($this->_name);
124 }
125
126 if ($this->_force) {
127 $this->postProcess();
128 $this->set('force', 0);
129 }
130
131 $sortID = NULL;
132 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
133 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
134 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
135 );
136 }
137
138 //get the voter clause.
139 $voterClause = $this->voterClause();
140
141 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
142
143 $selector = new CRM_Campaign_Selector_Search($this->_queryParams,
144 $this->_action,
145 $voterClause,
146 $this->_single,
147 $this->_limit,
148 $this->_context
149 );
150 $prefix = NULL;
151 if ($this->_context == 'user') {
152 $prefix = $this->_prefix;
153 }
154
155 $this->assign("{$prefix}limit", $this->_limit);
156 $this->assign("{$prefix}single", $this->_single);
157
158 $controller = new CRM_Core_Selector_Controller($selector,
159 $this->get(CRM_Utils_Pager::PAGE_ID),
160 $sortID,
161 CRM_Core_Action::VIEW,
162 $this,
163 CRM_Core_Selector_Controller::TRANSFER,
164 $prefix
165 );
166
167 $controller->setEmbedded(TRUE);
168 $controller->moveFromSessionToTemplate();
169
170 //append breadcrumb to survey dashboard.
171 if (CRM_Campaign_BAO_Campaign::accessCampaign()) {
172 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
173 CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
174 }
175
176 //set the form title.
177 CRM_Utils_System::setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation))));
178 }
179
00be9182 180 public function setDefaultValues() {
6a488035
TO
181 //load the default survey for all actions.
182 if (empty($this->_defaults)) {
183 $defaultSurveyId = key(CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE));
184 if ($defaultSurveyId) {
185 $this->_defaults['campaign_survey_id'] = $defaultSurveyId;
186 }
187 }
188
189 return $this->_defaults;
190 }
191
192 /**
c490a46a 193 * Build the form object
6a488035 194 *
6a488035
TO
195 *
196 * @return void
197 */
00be9182 198 public function buildQuickForm() {
3efb5b86 199 parent::buildQuickForm();
6a488035
TO
200 //build the search form.
201 CRM_Campaign_BAO_Query::buildSearchForm($this);
202
6a488035
TO
203 $rows = $this->get('rows');
204 if (is_array($rows)) {
205 if (!$this->_single) {
8d36b801 206 $this->addRowSelectors($rows);
6a488035
TO
207 }
208
6a488035
TO
209 $permission = CRM_Core_Permission::getPermission();
210 $allTasks = CRM_Campaign_Task::permissionedTaskTitles($permission);
211
212 //hack to serve right page to state machine.
213 $taskMapping = array(
214 'interview' => 1,
215 'reserve' => 2,
216 'release' => 3,
217 );
218
219 $currentTaskValue = CRM_Utils_Array::value($this->_operation, $taskMapping);
220 $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]);
8cc574cf 221 if ($this->_operation == 'interview' && !empty($this->_formValues['campaign_survey_id'])) {
6a488035
TO
222 $activityTypes = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
223
224 $surveyTypeId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey',
225 $this->_formValues['campaign_survey_id'],
226 'activity_type_id'
227 );
228 $taskValue = array(
229 $currentTaskValue => ts('Record %1 Responses',
230 array(1 => $activityTypes[$surveyTypeId])
231 ));
232 }
233
8d36b801 234 $this->addTaskMenu($taskValue);
6a488035
TO
235 }
236
6a488035
TO
237 }
238
239 /**
240 * The post processing of the form gets done here.
241 *
242 * Key things done during post processing are
243 * - check for reset or next request. if present, skip post procesing.
244 * - now check if user requested running a saved search, if so, then
245 * the form values associated with the saved search are used for searching.
246 * - if user has done a submit with new values the regular post submissing is
247 * done.
248 * The processing consists of using a Selector / Controller framework for getting the
249 * search results.
250 *
251 * @param
252 *
253 * @return void
6a488035 254 */
00be9182 255 public function postProcess() {
6a488035
TO
256 if ($this->_done) {
257 return;
258 }
259
260 $this->_done = TRUE;
261
262 if (!empty($_POST)) {
263 $this->_formValues = $this->controller->exportValues($this->_name);
264 }
265
266 $this->fixFormValues();
267
268 //format params as per task.
269 $this->formatParams();
270
271 $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues);
272
273 $this->set('formValues', $this->_formValues);
274 $this->set('queryParams', $this->_queryParams);
275
276 $buttonName = $this->controller->getButtonName();
45008844 277 if ($buttonName == $this->_actionButtonName) {
6a488035
TO
278 // check actionName and if next, then do not repeat a search, since we are going to the next page
279
280 // hack, make sure we reset the task values
281 $stateMachine = $this->controller->getStateMachine();
282 $formName = $stateMachine->getTaskFormName();
283
284 $this->controller->resetPage($formName);
285 return;
286 }
287
288 $sortID = NULL;
289 if ($this->get(CRM_Utils_Sort::SORT_ID)) {
290 $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID),
291 $this->get(CRM_Utils_Sort::SORT_DIRECTION)
292 );
293 }
294
295 //get the voter clause.
296 $voterClause = $this->voterClause();
297
298 $selector = new CRM_Campaign_Selector_Search($this->_queryParams,
299 $this->_action,
300 $voterClause,
301 $this->_single,
302 $this->_limit,
303 $this->_context
304 );
305 $selector->setKey($this->controller->_key);
306
307 $prefix = NULL;
308 if ($this->_context == 'basic' ||
309 $this->_context == 'user'
310 ) {
311 $prefix = $this->_prefix;
312 }
313
314 $controller = new CRM_Core_Selector_Controller($selector,
315 $this->get(CRM_Utils_Pager::PAGE_ID),
316 $sortID,
317 CRM_Core_Action::VIEW,
318 $this,
319 CRM_Core_Selector_Controller::SESSION,
320 $prefix
321 );
322 $controller->setEmbedded(TRUE);
323 $query = $selector->getQuery();
324 if ($this->_context == 'user') {
325 $query->setSkipPermission(TRUE);
326 }
327 $controller->run();
328 }
329
00be9182 330 public function formatParams() {
6a488035 331 $interviewerId = CRM_Utils_Array::value('survey_interviewer_id', $this->_formValues);
64e716d7
JM
332 if ($interviewerId) {
333 $this->set('interviewerId', $interviewerId);
6a488035
TO
334 }
335
336 //format multi-select group and contact types.
56ecdf7b 337 foreach (array('group', 'contact_type') as $param) {
6a488035
TO
338 if ($this->_force) {
339 continue;
340 }
341 $paramValue = CRM_Utils_Array::value($param, $this->_formValues);
342 if ($paramValue && is_array($paramValue)) {
343 unset($this->_formValues[$param]);
344 foreach ($paramValue as $key => $value) {
345 $this->_formValues[$param][$value] = 1;
346 }
347 }
348 }
349
350 //apply filter of survey contact type for search.
351 $contactType = CRM_Campaign_BAO_Survey::getSurveyContactType(CRM_Utils_Array::value('campaign_survey_id', $this->_formValues));
352 if ($contactType && in_array($this->_operation, array(
353 'reserve', 'interview'))) {
354 $this->_formValues['contact_type'][$contactType] = 1;
355 }
356
357 if ($this->_operation == 'reserve') {
a7488080 358 if (!empty($this->_formValues['campaign_survey_id'])) {
6a488035
TO
359 $campaignId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey',
360 $this->_formValues['campaign_survey_id'],
361 'campaign_id'
362 );
363
364 //allow voter search in sub-part of given constituents,
365 //but make sure in case user does not select any group.
366 //get all associated campaign groups in where filter, CRM-7406
367 $groups = CRM_Utils_Array::value('group', $this->_formValues);
368 if ($campaignId && CRM_Utils_System::isNull($groups)) {
369 $campGroups = CRM_Campaign_BAO_Campaign::getCampaignGroups($campaignId);
370 foreach ($campGroups as $id => $title) $this->_formValues['group'][$id] = 1;
371 }
372
373 //carry servey id w/ this.
374 $this->set('surveyId', $this->_formValues['campaign_survey_id']);
375 unset($this->_formValues['campaign_survey_id']);
376 }
377 unset($this->_formValues['survey_interviewer_id']);
378 }
379 elseif ($this->_operation == 'interview' ||
380 $this->_operation == 'release'
381 ) {
382 //to conduct interview / release activity status should be scheduled.
383 $activityStatus = CRM_Core_PseudoConstant::activityStatus('name');
384 if ($scheduledStatusId = array_search('Scheduled', $activityStatus)) {
385 $this->_formValues['survey_status_id'] = $scheduledStatusId;
386 }
387 }
388
389 //pass voter search operation.
390 $this->_formValues['campaign_search_voter_for'] = $this->_operation;
391 }
392
00be9182 393 public function fixFormValues() {
6a488035
TO
394 // if this search has been forced
395 // then see if there are any get values, and if so over-ride the post values
396 // note that this means that GET over-rides POST :)
397
398 //since we have qfKey, no need to manipulate set defaults.
399 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject);
400
401 if (!$this->_force || CRM_Utils_Rule::qfKey($qfKey)) {
402 return;
403 }
404
405 // get survey id
406 $surveyId = CRM_Utils_Request::retrieve('sid', 'Positive', CRM_Core_DAO::$_nullObject);
407
408 if ($surveyId) {
409 $surveyId = CRM_Utils_Type::escape($surveyId, 'Integer');
410 }
411 else {
412 // use default survey id
413 $surveyId = key(CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE));
414 }
415 if (!$surveyId) {
416 CRM_Core_Error::fatal('Could not find valid Survey Id.');
417 }
418 $this->_formValues['campaign_survey_id'] = $this->_formValues['campaign_survey_id'] = $surveyId;
419
420 $session = CRM_Core_Session::singleton();
421 $userId = $session->get('userID');
422
423 // get interviewer id
424 $cid = CRM_Utils_Request::retrieve('cid', 'Positive',
425 CRM_Core_DAO::$_nullObject, FALSE, $userId
426 );
427 //to force other contact as interviewer, user should be admin.
428 if ($cid != $userId &&
429 !CRM_Core_Permission::check('administer CiviCampaign')
430 ) {
431 CRM_Utils_System::permissionDenied();
432 CRM_Utils_System::civiExit();
433 }
434 $this->_formValues['survey_interviewer_id'] = $cid;
6a488035
TO
435 //get all in defaults.
436 $this->_defaults = $this->_formValues;
437 $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this);
438 }
439
30c4e065
EM
440 /**
441 * @return array
442 */
00be9182 443 public function voterClause() {
6a488035
TO
444 $params = array('campaign_search_voter_for' => $this->_operation);
445
446 $clauseFields = array(
447 'surveyId' => 'campaign_survey_id',
448 'interviewerId' => 'survey_interviewer_id',
449 );
450
451 foreach ($clauseFields as $param => $key) {
452 $params[$key] = CRM_Utils_Array::value($key, $this->_formValues);
453 if (!$params[$key]) {
454 $params[$key] = $this->get($param);
455 }
456 }
457
458 //build the clause.
459 $voterClause = CRM_Campaign_BAO_Query::voterClause($params);
460
461 return $voterClause;
462 }
463
464 /**
465 * Return a descriptive name for the page, used in wizard header
466 *
467 * @return string
6a488035
TO
468 */
469 public function getTitle() {
470 return ts('Find Respondents');
471 }
472}