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