Commit | Line | Data |
---|---|---|
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
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 | 39 | class CRM_Campaign_Form_Search extends CRM_Core_Form_Search { |
6a488035 | 40 | |
6a488035 | 41 | /** |
fe482240 | 42 | * The params that are sent to the query. |
6a488035 TO |
43 | * |
44 | * @var array | |
6a488035 TO |
45 | */ |
46 | protected $_queryParams; | |
47 | ||
6a488035 | 48 | /** |
fe482240 | 49 | * Are we restricting ourselves to a single contact. |
6a488035 | 50 | * |
6a488035 TO |
51 | * @var boolean |
52 | */ | |
53 | protected $_single = FALSE; | |
54 | ||
55 | /** | |
fe482240 | 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 | /** | |
fe482240 | 65 | * Prefix for the controller. |
6a488035 TO |
66 | */ |
67 | protected $_prefix = "survey_"; | |
68 | ||
69 | ||
70 | private $_operation = 'reserve'; | |
71 | ||
72 | /** | |
fe482240 | 73 | * Processing needed for buildForm and later. |
6a488035 TO |
74 | * |
75 | * @return void | |
95ea96be | 76 | */ |
acb1052e | 77 | public 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 | |
353ffa53 TO |
88 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); |
89 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); | |
6a488035 | 90 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); |
353ffa53 | 91 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); |
6a488035 TO |
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( | |
353ffa53 TO |
97 | 'reserve', |
98 | 'release', | |
acb1052e | 99 | 'interview', |
353ffa53 TO |
100 | )) |
101 | ) { | |
6a488035 TO |
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 | ||
00be9182 | 184 | public function setDefaultValues() { |
6a488035 TO |
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 | /** | |
fe482240 | 197 | * Build the form object. |
6a488035 | 198 | * |
6a488035 TO |
199 | * |
200 | * @return void | |
201 | */ | |
00be9182 | 202 | public function buildQuickForm() { |
3efb5b86 | 203 | parent::buildQuickForm(); |
6a488035 TO |
204 | //build the search form. |
205 | CRM_Campaign_BAO_Query::buildSearchForm($this); | |
206 | ||
6a488035 TO |
207 | $rows = $this->get('rows'); |
208 | if (is_array($rows)) { | |
209 | if (!$this->_single) { | |
8d36b801 | 210 | $this->addRowSelectors($rows); |
6a488035 TO |
211 | } |
212 | ||
6a488035 TO |
213 | $permission = CRM_Core_Permission::getPermission(); |
214 | $allTasks = CRM_Campaign_Task::permissionedTaskTitles($permission); | |
215 | ||
216 | //hack to serve right page to state machine. | |
217 | $taskMapping = array( | |
218 | 'interview' => 1, | |
219 | 'reserve' => 2, | |
220 | 'release' => 3, | |
221 | ); | |
222 | ||
223 | $currentTaskValue = CRM_Utils_Array::value($this->_operation, $taskMapping); | |
224 | $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]); | |
8cc574cf | 225 | if ($this->_operation == 'interview' && !empty($this->_formValues['campaign_survey_id'])) { |
6a488035 TO |
226 | $activityTypes = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE); |
227 | ||
228 | $surveyTypeId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', | |
229 | $this->_formValues['campaign_survey_id'], | |
230 | 'activity_type_id' | |
231 | ); | |
232 | $taskValue = array( | |
233 | $currentTaskValue => ts('Record %1 Responses', | |
234 | array(1 => $activityTypes[$surveyTypeId]) | |
acb1052e | 235 | ), |
353ffa53 | 236 | ); |
6a488035 TO |
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 | |
6a488035 | 259 | */ |
00be9182 | 260 | public function postProcess() { |
6a488035 TO |
261 | if ($this->_done) { |
262 | return; | |
263 | } | |
264 | ||
265 | $this->_done = TRUE; | |
266 | ||
267 | if (!empty($_POST)) { | |
268 | $this->_formValues = $this->controller->exportValues($this->_name); | |
269 | } | |
270 | ||
271 | $this->fixFormValues(); | |
272 | ||
273 | //format params as per task. | |
274 | $this->formatParams(); | |
275 | ||
276 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); | |
277 | ||
278 | $this->set('formValues', $this->_formValues); | |
279 | $this->set('queryParams', $this->_queryParams); | |
280 | ||
281 | $buttonName = $this->controller->getButtonName(); | |
45008844 | 282 | if ($buttonName == $this->_actionButtonName) { |
6a488035 TO |
283 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
284 | ||
285 | // hack, make sure we reset the task values | |
286 | $stateMachine = $this->controller->getStateMachine(); | |
287 | $formName = $stateMachine->getTaskFormName(); | |
288 | ||
289 | $this->controller->resetPage($formName); | |
290 | return; | |
291 | } | |
292 | ||
293 | $sortID = NULL; | |
294 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { | |
295 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), | |
296 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) | |
297 | ); | |
298 | } | |
299 | ||
300 | //get the voter clause. | |
301 | $voterClause = $this->voterClause(); | |
302 | ||
303 | $selector = new CRM_Campaign_Selector_Search($this->_queryParams, | |
304 | $this->_action, | |
305 | $voterClause, | |
306 | $this->_single, | |
307 | $this->_limit, | |
308 | $this->_context | |
309 | ); | |
310 | $selector->setKey($this->controller->_key); | |
311 | ||
312 | $prefix = NULL; | |
313 | if ($this->_context == 'basic' || | |
314 | $this->_context == 'user' | |
315 | ) { | |
316 | $prefix = $this->_prefix; | |
317 | } | |
318 | ||
319 | $controller = new CRM_Core_Selector_Controller($selector, | |
320 | $this->get(CRM_Utils_Pager::PAGE_ID), | |
321 | $sortID, | |
322 | CRM_Core_Action::VIEW, | |
323 | $this, | |
324 | CRM_Core_Selector_Controller::SESSION, | |
325 | $prefix | |
326 | ); | |
327 | $controller->setEmbedded(TRUE); | |
328 | $query = $selector->getQuery(); | |
329 | if ($this->_context == 'user') { | |
330 | $query->setSkipPermission(TRUE); | |
331 | } | |
332 | $controller->run(); | |
333 | } | |
334 | ||
00be9182 | 335 | public function formatParams() { |
6a488035 | 336 | $interviewerId = CRM_Utils_Array::value('survey_interviewer_id', $this->_formValues); |
64e716d7 JM |
337 | if ($interviewerId) { |
338 | $this->set('interviewerId', $interviewerId); | |
6a488035 TO |
339 | } |
340 | ||
341 | //format multi-select group and contact types. | |
56ecdf7b | 342 | foreach (array('group', 'contact_type') as $param) { |
6a488035 TO |
343 | if ($this->_force) { |
344 | continue; | |
345 | } | |
346 | $paramValue = CRM_Utils_Array::value($param, $this->_formValues); | |
347 | if ($paramValue && is_array($paramValue)) { | |
348 | unset($this->_formValues[$param]); | |
349 | foreach ($paramValue as $key => $value) { | |
350 | $this->_formValues[$param][$value] = 1; | |
351 | } | |
352 | } | |
353 | } | |
354 | ||
355 | //apply filter of survey contact type for search. | |
356 | $contactType = CRM_Campaign_BAO_Survey::getSurveyContactType(CRM_Utils_Array::value('campaign_survey_id', $this->_formValues)); | |
357 | if ($contactType && in_array($this->_operation, array( | |
353ffa53 | 358 | 'reserve', |
acb1052e | 359 | 'interview', |
353ffa53 TO |
360 | )) |
361 | ) { | |
6a488035 TO |
362 | $this->_formValues['contact_type'][$contactType] = 1; |
363 | } | |
364 | ||
365 | if ($this->_operation == 'reserve') { | |
a7488080 | 366 | if (!empty($this->_formValues['campaign_survey_id'])) { |
6a488035 TO |
367 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', |
368 | $this->_formValues['campaign_survey_id'], | |
369 | 'campaign_id' | |
370 | ); | |
371 | ||
372 | //allow voter search in sub-part of given constituents, | |
373 | //but make sure in case user does not select any group. | |
374 | //get all associated campaign groups in where filter, CRM-7406 | |
375 | $groups = CRM_Utils_Array::value('group', $this->_formValues); | |
376 | if ($campaignId && CRM_Utils_System::isNull($groups)) { | |
377 | $campGroups = CRM_Campaign_BAO_Campaign::getCampaignGroups($campaignId); | |
353ffa53 TO |
378 | foreach ($campGroups as $id => $title) { |
379 | $this->_formValues['group'][$id] = 1; | |
380 | } | |
6a488035 TO |
381 | } |
382 | ||
383 | //carry servey id w/ this. | |
384 | $this->set('surveyId', $this->_formValues['campaign_survey_id']); | |
385 | unset($this->_formValues['campaign_survey_id']); | |
386 | } | |
387 | unset($this->_formValues['survey_interviewer_id']); | |
388 | } | |
389 | elseif ($this->_operation == 'interview' || | |
390 | $this->_operation == 'release' | |
391 | ) { | |
392 | //to conduct interview / release activity status should be scheduled. | |
393 | $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); | |
394 | if ($scheduledStatusId = array_search('Scheduled', $activityStatus)) { | |
395 | $this->_formValues['survey_status_id'] = $scheduledStatusId; | |
396 | } | |
397 | } | |
398 | ||
399 | //pass voter search operation. | |
400 | $this->_formValues['campaign_search_voter_for'] = $this->_operation; | |
401 | } | |
402 | ||
00be9182 | 403 | public function fixFormValues() { |
6a488035 TO |
404 | // if this search has been forced |
405 | // then see if there are any get values, and if so over-ride the post values | |
406 | // note that this means that GET over-rides POST :) | |
407 | ||
408 | //since we have qfKey, no need to manipulate set defaults. | |
409 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', CRM_Core_DAO::$_nullObject); | |
410 | ||
411 | if (!$this->_force || CRM_Utils_Rule::qfKey($qfKey)) { | |
412 | return; | |
413 | } | |
414 | ||
415 | // get survey id | |
416 | $surveyId = CRM_Utils_Request::retrieve('sid', 'Positive', CRM_Core_DAO::$_nullObject); | |
417 | ||
418 | if ($surveyId) { | |
419 | $surveyId = CRM_Utils_Type::escape($surveyId, 'Integer'); | |
420 | } | |
421 | else { | |
422 | // use default survey id | |
423 | $surveyId = key(CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE)); | |
424 | } | |
425 | if (!$surveyId) { | |
426 | CRM_Core_Error::fatal('Could not find valid Survey Id.'); | |
427 | } | |
428 | $this->_formValues['campaign_survey_id'] = $this->_formValues['campaign_survey_id'] = $surveyId; | |
429 | ||
430 | $session = CRM_Core_Session::singleton(); | |
431 | $userId = $session->get('userID'); | |
432 | ||
433 | // get interviewer id | |
434 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', | |
435 | CRM_Core_DAO::$_nullObject, FALSE, $userId | |
436 | ); | |
437 | //to force other contact as interviewer, user should be admin. | |
438 | if ($cid != $userId && | |
439 | !CRM_Core_Permission::check('administer CiviCampaign') | |
440 | ) { | |
441 | CRM_Utils_System::permissionDenied(); | |
442 | CRM_Utils_System::civiExit(); | |
443 | } | |
444 | $this->_formValues['survey_interviewer_id'] = $cid; | |
6a488035 TO |
445 | //get all in defaults. |
446 | $this->_defaults = $this->_formValues; | |
447 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); | |
448 | } | |
449 | ||
30c4e065 EM |
450 | /** |
451 | * @return array | |
452 | */ | |
00be9182 | 453 | public function voterClause() { |
6a488035 TO |
454 | $params = array('campaign_search_voter_for' => $this->_operation); |
455 | ||
456 | $clauseFields = array( | |
457 | 'surveyId' => 'campaign_survey_id', | |
458 | 'interviewerId' => 'survey_interviewer_id', | |
459 | ); | |
460 | ||
461 | foreach ($clauseFields as $param => $key) { | |
462 | $params[$key] = CRM_Utils_Array::value($key, $this->_formValues); | |
463 | if (!$params[$key]) { | |
464 | $params[$key] = $this->get($param); | |
465 | } | |
466 | } | |
467 | ||
468 | //build the clause. | |
469 | $voterClause = CRM_Campaign_BAO_Query::voterClause($params); | |
470 | ||
471 | return $voterClause; | |
472 | } | |
473 | ||
474 | /** | |
475 | * Return a descriptive name for the page, used in wizard header | |
476 | * | |
477 | * @return string | |
6a488035 TO |
478 | */ |
479 | public function getTitle() { | |
480 | return ts('Find Respondents'); | |
481 | } | |
96025800 | 482 | |
6a488035 | 483 | } |