3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
39 class CRM_Campaign_Form_Search
extends CRM_Core_Form_Search
{
42 * The params that are sent to the query.
46 protected $_queryParams;
49 * Are we restricting ourselves to a single contact.
53 protected $_single = FALSE;
56 * Are we restricting ourselves to a single contact.
60 protected $_limit = NULL;
63 * Prefix for the controller.
65 protected $_prefix = "survey_";
68 private $_operation = 'reserve';
71 * Processing needed for buildForm and later.
75 public function preProcess() {
77 $this->_defaults
= array();
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');
84 //we allow the controller to set force/reset externally,
85 //useful when we are being driven by the wizard framework
86 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
87 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
88 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
89 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
91 //operation for state machine.
92 $this->_operation
= CRM_Utils_Request
::retrieve('op', 'String', $this, FALSE, 'reserve');
94 if (!in_array($this->_operation
, array(
100 $this->_operation
= 'reserve';
101 $this->set('op', $this->_operation
);
103 $this->set('searchVoterFor', $this->_operation
);
104 $this->assign('searchVoterFor', $this->_operation
);
105 $this->assign('isFormSubmitted', $this->isSubmitted());
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")
112 CRM_Utils_System
::permissionDenied();
113 CRM_Utils_System
::civiExit();
116 $this->assign("context", $this->_context
);
118 // get user submitted values
119 // get it from controller only if form has been submitted, else preProcess has set this
122 $this->_formValues
= $this->get('formValues');
125 $this->_formValues
= $this->controller
->exportValues($this->_name
);
129 $this->postProcess();
130 $this->set('force', 0);
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
)
140 //get the voter clause.
141 $voterClause = $this->voterClause();
143 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
145 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
153 if ($this->_context
== 'user') {
154 $prefix = $this->_prefix
;
157 $this->assign("{$prefix}limit", $this->_limit
);
158 $this->assign("{$prefix}single", $this->_single
);
160 $controller = new CRM_Core_Selector_Controller($selector,
161 $this->get(CRM_Utils_Pager
::PAGE_ID
),
163 CRM_Core_Action
::VIEW
,
165 CRM_Core_Selector_Controller
::TRANSFER
,
169 $controller->setEmbedded(TRUE);
170 $controller->moveFromSessionToTemplate();
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)));
178 //set the form title.
179 CRM_Utils_System
::setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation
))));
182 public function setDefaultValues() {
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;
191 return $this->_defaults
;
195 * Build the form object.
200 public function buildQuickForm() {
201 parent
::buildQuickForm();
202 //build the search form.
203 CRM_Campaign_BAO_Query
::buildSearchForm($this);
205 $rows = $this->get('rows');
206 if (is_array($rows)) {
207 if (!$this->_single
) {
208 $this->addRowSelectors($rows);
211 $permission = CRM_Core_Permission
::getPermission();
212 $allTasks = CRM_Campaign_Task
::permissionedTaskTitles($permission);
214 //hack to serve right page to state machine.
215 $taskMapping = array(
221 $currentTaskValue = CRM_Utils_Array
::value($this->_operation
, $taskMapping);
222 $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]);
223 if ($this->_operation
== 'interview' && !empty($this->_formValues
['campaign_survey_id'])) {
224 $activityTypes = CRM_Core_PseudoConstant
::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
226 $surveyTypeId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
227 $this->_formValues
['campaign_survey_id'],
231 $currentTaskValue => ts('Record %1 Responses',
232 array(1 => $activityTypes[$surveyTypeId])
237 $this->addTaskMenu($taskValue);
243 * The post processing of the form gets done here.
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
251 * The processing consists of using a Selector / Controller framework for getting the
258 public function postProcess() {
265 if (!empty($_POST)) {
266 $this->_formValues
= $this->controller
->exportValues($this->_name
);
269 $this->fixFormValues();
271 //format params as per task.
272 $this->formatParams();
274 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
276 $this->set('formValues', $this->_formValues
);
277 $this->set('queryParams', $this->_queryParams
);
279 $buttonName = $this->controller
->getButtonName();
280 if ($buttonName == $this->_actionButtonName
) {
281 // check actionName and if next, then do not repeat a search, since we are going to the next page
283 // hack, make sure we reset the task values
284 $stateMachine = $this->controller
->getStateMachine();
285 $formName = $stateMachine->getTaskFormName();
287 $this->controller
->resetPage($formName);
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
)
298 //get the voter clause.
299 $voterClause = $this->voterClause();
301 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
308 $selector->setKey($this->controller
->_key
);
311 if ($this->_context
== 'basic' ||
312 $this->_context
== 'user'
314 $prefix = $this->_prefix
;
317 $controller = new CRM_Core_Selector_Controller($selector,
318 $this->get(CRM_Utils_Pager
::PAGE_ID
),
320 CRM_Core_Action
::VIEW
,
322 CRM_Core_Selector_Controller
::SESSION
,
325 $controller->setEmbedded(TRUE);
326 $query = $selector->getQuery();
327 if ($this->_context
== 'user') {
328 $query->setSkipPermission(TRUE);
333 public function formatParams() {
334 $interviewerId = CRM_Utils_Array
::value('survey_interviewer_id', $this->_formValues
);
335 if ($interviewerId) {
336 $this->set('interviewerId', $interviewerId);
339 //format multi-select group and contact types.
340 foreach (array('group', 'contact_type') as $param) {
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;
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(
360 $this->_formValues
['contact_type'][$contactType] = 1;
363 if ($this->_operation
== 'reserve') {
364 if (!empty($this->_formValues
['campaign_survey_id'])) {
365 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
366 $this->_formValues
['campaign_survey_id'],
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) {
377 $this->_formValues
['group'][$id] = 1;
381 //carry servey id w/ this.
382 $this->set('surveyId', $this->_formValues
['campaign_survey_id']);
383 unset($this->_formValues
['campaign_survey_id']);
385 unset($this->_formValues
['survey_interviewer_id']);
387 elseif ($this->_operation
== 'interview' ||
388 $this->_operation
== 'release'
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;
397 //pass voter search operation.
398 $this->_formValues
['campaign_search_voter_for'] = $this->_operation
;
401 public function fixFormValues() {
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 :)
406 //since we have qfKey, no need to manipulate set defaults.
407 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject);
409 if (!$this->_force || CRM_Utils_Rule
::qfKey($qfKey)) {
414 $surveyId = CRM_Utils_Request
::retrieve('sid', 'Positive', CRM_Core_DAO
::$_nullObject);
417 $surveyId = CRM_Utils_Type
::escape($surveyId, 'Integer');
420 // use default survey id
421 $surveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
424 CRM_Core_Error
::fatal('Could not find valid Survey Id.');
426 $this->_formValues
['campaign_survey_id'] = $this->_formValues
['campaign_survey_id'] = $surveyId;
428 $session = CRM_Core_Session
::singleton();
429 $userId = $session->get('userID');
431 // get interviewer id
432 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
433 CRM_Core_DAO
::$_nullObject, FALSE, $userId
435 //to force other contact as interviewer, user should be admin.
436 if ($cid != $userId &&
437 !CRM_Core_Permission
::check('administer CiviCampaign')
439 CRM_Utils_System
::permissionDenied();
440 CRM_Utils_System
::civiExit();
442 $this->_formValues
['survey_interviewer_id'] = $cid;
443 //get all in defaults.
444 $this->_defaults
= $this->_formValues
;
445 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
451 public function voterClause() {
452 $params = array('campaign_search_voter_for' => $this->_operation
);
454 $clauseFields = array(
455 'surveyId' => 'campaign_survey_id',
456 'interviewerId' => 'survey_interviewer_id',
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);
467 $voterClause = CRM_Campaign_BAO_Query
::voterClause($params);
473 * Return a descriptive name for the page, used in wizard header
477 public function getTitle() {
478 return ts('Find Respondents');