3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
37 class CRM_Campaign_Form_Search
extends CRM_Core_Form_Search
{
40 * The params that are sent to the query.
44 protected $_queryParams;
47 * Are we restricting ourselves to a single contact.
51 protected $_single = FALSE;
54 * Are we restricting ourselves to a single contact.
58 protected $_limit = NULL;
61 * Prefix for the controller.
63 protected $_prefix = "survey_";
66 private $_operation = 'reserve';
69 * Processing needed for buildForm and later.
71 public function preProcess() {
73 $this->_defaults
= array();
75 //set the button name.
76 $this->_searchButtonName
= $this->getButtonName('refresh');
77 $this->_printButtonName
= $this->getButtonName('next', 'print');
78 $this->_actionButtonName
= $this->getButtonName('next', 'action');
80 $this->loadStandardSearchOptionsFromUrl();
82 //operation for state machine.
83 $this->_operation
= CRM_Utils_Request
::retrieve('op', 'String', $this, FALSE, 'reserve');
85 if (!in_array($this->_operation
, array(
91 $this->_operation
= 'reserve';
92 $this->set('op', $this->_operation
);
94 $this->set('searchVoterFor', $this->_operation
);
95 $this->assign('searchVoterFor', $this->_operation
);
96 $this->assign('isFormSubmitted', $this->isSubmitted());
98 //do check permissions.
99 if (!CRM_Core_Permission
::check('administer CiviCampaign') &&
100 !CRM_Core_Permission
::check('manage campaign') &&
101 !CRM_Core_Permission
::check("{$this->_operation} campaign contacts")
103 CRM_Utils_System
::permissionDenied();
104 CRM_Utils_System
::civiExit();
107 $this->assign("context", $this->_context
);
109 // get user submitted values
110 // get it from controller only if form has been submitted, else preProcess has set this
113 $this->_formValues
= $this->get('formValues');
116 $this->_formValues
= $this->controller
->exportValues($this->_name
);
120 $this->postProcess();
121 $this->set('force', 0);
125 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
126 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
127 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
131 //get the voter clause.
132 $voterClause = $this->voterClause();
134 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
136 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
144 if ($this->_context
== 'user') {
145 $prefix = $this->_prefix
;
148 $this->assign("{$prefix}limit", $this->_limit
);
149 $this->assign("{$prefix}single", $this->_single
);
151 $controller = new CRM_Core_Selector_Controller($selector,
152 $this->get(CRM_Utils_Pager
::PAGE_ID
),
154 CRM_Core_Action
::VIEW
,
156 CRM_Core_Selector_Controller
::TRANSFER
,
160 $controller->setEmbedded(TRUE);
161 $controller->moveFromSessionToTemplate();
163 //append breadcrumb to survey dashboard.
164 if (CRM_Campaign_BAO_Campaign
::accessCampaign()) {
165 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
166 CRM_Utils_System
::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
169 //set the form title.
170 CRM_Utils_System
::setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation
))));
174 * Load the default survey for all actions.
178 public function setDefaultValues() {
179 if (empty($this->_defaults
)) {
180 $defaultSurveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
181 if ($defaultSurveyId) {
182 $this->_defaults
['campaign_survey_id'] = $defaultSurveyId;
186 return $this->_defaults
;
190 * Build the form object.
192 public function buildQuickForm() {
193 parent
::buildQuickForm();
194 //build the search form.
195 CRM_Campaign_BAO_Query
::buildSearchForm($this);
197 $rows = $this->get('rows');
198 if (is_array($rows)) {
199 if (!$this->_single
) {
200 $this->addRowSelectors($rows);
203 $allTasks = CRM_Campaign_Task
::permissionedTaskTitles(CRM_Core_Permission
::getPermission());
205 //hack to serve right page to state machine.
206 $taskMapping = array(
207 'interview' => CRM_Campaign_Task
::INTERVIEW
,
208 'reserve' => CRM_Campaign_Task
::RESERVE
,
209 'release' => CRM_Campaign_Task
::RELEASE
,
212 $currentTaskValue = CRM_Utils_Array
::value($this->_operation
, $taskMapping);
213 $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]);
214 if ($this->_operation
== 'interview' && !empty($this->_formValues
['campaign_survey_id'])) {
215 $activityTypes = CRM_Core_PseudoConstant
::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
217 $surveyTypeId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
218 $this->_formValues
['campaign_survey_id'],
222 $currentTaskValue => ts('Record %1 Responses',
223 array(1 => $activityTypes[$surveyTypeId])
228 $this->addTaskMenu($taskValue);
234 * The post processing of the form gets done here.
236 * Key things done during post processing are
237 * - check for reset or next request. if present, skip post procesing.
238 * - now check if user requested running a saved search, if so, then
239 * the form values associated with the saved search are used for searching.
240 * - if user has done a submit with new values the regular post submissing is
242 * The processing consists of using a Selector / Controller framework for getting the
245 public function postProcess() {
252 if (!empty($_POST)) {
253 $this->_formValues
= $this->controller
->exportValues($this->_name
);
256 $this->fixFormValues();
258 //format params as per task.
259 $this->formatParams();
261 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
263 $this->set('formValues', $this->_formValues
);
264 $this->set('queryParams', $this->_queryParams
);
266 $buttonName = $this->controller
->getButtonName();
267 if ($buttonName == $this->_actionButtonName
) {
268 // check actionName and if next, then do not repeat a search, since we are going to the next page
270 // hack, make sure we reset the task values
271 $stateMachine = $this->controller
->getStateMachine();
272 $formName = $stateMachine->getTaskFormName();
274 $this->controller
->resetPage($formName);
279 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
280 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
281 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
285 //get the voter clause.
286 $voterClause = $this->voterClause();
288 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
295 $selector->setKey($this->controller
->_key
);
298 if ($this->_context
== 'basic' ||
299 $this->_context
== 'user'
301 $prefix = $this->_prefix
;
304 $controller = new CRM_Core_Selector_Controller($selector,
305 $this->get(CRM_Utils_Pager
::PAGE_ID
),
307 CRM_Core_Action
::VIEW
,
309 CRM_Core_Selector_Controller
::SESSION
,
312 $controller->setEmbedded(TRUE);
313 $query = $selector->getQuery();
314 if ($this->_context
== 'user') {
315 $query->setSkipPermission(TRUE);
320 public function formatParams() {
321 $interviewerId = CRM_Utils_Array
::value('survey_interviewer_id', $this->_formValues
);
322 if ($interviewerId) {
323 $this->set('interviewerId', $interviewerId);
326 //format multi-select group and contact types.
327 foreach (array('group', 'contact_type') as $param) {
331 $paramValue = CRM_Utils_Array
::value($param, $this->_formValues
);
332 if ($paramValue && is_array($paramValue)) {
333 unset($this->_formValues
[$param]);
334 foreach ($paramValue as $key => $value) {
335 $this->_formValues
[$param][$value] = 1;
340 //apply filter of survey contact type for search.
341 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType(CRM_Utils_Array
::value('campaign_survey_id', $this->_formValues
));
342 if ($contactType && in_array($this->_operation
, array(
347 $this->_formValues
['contact_type'][$contactType] = 1;
350 if ($this->_operation
== 'reserve') {
351 if (!empty($this->_formValues
['campaign_survey_id'])) {
352 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
353 $this->_formValues
['campaign_survey_id'],
357 //allow voter search in sub-part of given constituents,
358 //but make sure in case user does not select any group.
359 //get all associated campaign groups in where filter, CRM-7406
360 $groups = CRM_Utils_Array
::value('group', $this->_formValues
);
361 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
362 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
363 foreach ($campGroups as $id => $title) {
364 $this->_formValues
['group'][$id] = 1;
368 //carry servey id w/ this.
369 $this->set('surveyId', $this->_formValues
['campaign_survey_id']);
370 unset($this->_formValues
['campaign_survey_id']);
372 unset($this->_formValues
['survey_interviewer_id']);
374 elseif ($this->_operation
== 'interview' ||
375 $this->_operation
== 'release'
377 //to conduct interview / release activity status should be scheduled.
378 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
379 if ($scheduledStatusId = array_search('Scheduled', $activityStatus)) {
380 $this->_formValues
['survey_status_id'] = $scheduledStatusId;
384 //pass voter search operation.
385 $this->_formValues
['campaign_search_voter_for'] = $this->_operation
;
388 public function fixFormValues() {
389 // if this search has been forced
390 // then see if there are any get values, and if so over-ride the post values
391 // note that this means that GET over-rides POST :)
393 //since we have qfKey, no need to manipulate set defaults.
394 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String');
396 if (!$this->_force || CRM_Utils_Rule
::qfKey($qfKey)) {
401 $surveyId = CRM_Utils_Request
::retrieve('sid', 'Positive');
404 $surveyId = CRM_Utils_Type
::escape($surveyId, 'Integer');
407 // use default survey id
408 $surveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
411 CRM_Core_Error
::fatal('Could not find valid Survey Id.');
413 $this->_formValues
['campaign_survey_id'] = $this->_formValues
['campaign_survey_id'] = $surveyId;
415 $session = CRM_Core_Session
::singleton();
416 $userId = $session->get('userID');
418 // get interviewer id
419 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
420 CRM_Core_DAO
::$_nullObject, FALSE, $userId
422 //to force other contact as interviewer, user should be admin.
423 if ($cid != $userId &&
424 !CRM_Core_Permission
::check('administer CiviCampaign')
426 CRM_Utils_System
::permissionDenied();
427 CRM_Utils_System
::civiExit();
429 $this->_formValues
['survey_interviewer_id'] = $cid;
430 //get all in defaults.
431 $this->_defaults
= $this->_formValues
;
432 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
438 public function voterClause() {
439 $params = array('campaign_search_voter_for' => $this->_operation
);
441 $clauseFields = array(
442 'surveyId' => 'campaign_survey_id',
443 'interviewerId' => 'survey_interviewer_id',
446 foreach ($clauseFields as $param => $key) {
447 $params[$key] = CRM_Utils_Array
::value($key, $this->_formValues
);
448 if (!$params[$key]) {
449 $params[$key] = $this->get($param);
454 $voterClause = CRM_Campaign_BAO_Query
::voterClause($params);
460 * Return a descriptive name for the page, used in wizard header
464 public function getTitle() {
465 return ts('Find Respondents');