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