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