3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
39 class CRM_Campaign_Form_Search
extends CRM_Core_Form
{
42 * Are we forced to run a search
50 * name of search button
55 protected $_searchButtonName;
58 * name of print button
63 protected $_printButtonName;
66 * name of action button
71 protected $_actionButtonName;
74 * form values that we will be using
79 protected $_formValues;
82 * the params that are sent to the query
87 protected $_queryParams;
90 * have we already done this search
98 * are we restricting ourselves to a single contact
103 protected $_single = FALSE;
106 * are we restricting ourselves to a single contact
111 protected $_limit = NULL;
114 * what context are we being invoked from
119 protected $_context = NULL;
121 protected $_defaults;
124 * prefix for the controller
127 protected $_prefix = "survey_";
130 private $_operation = 'reserve';
133 * processing needed for buildForm and later
137 */ function preProcess() {
138 $this->_done
= FALSE;
139 $this->_defaults
= array();
141 //set the button name.
142 $this->_searchButtonName
= $this->getButtonName('refresh');
143 $this->_printButtonName
= $this->getButtonName('next', 'print');
144 $this->_actionButtonName
= $this->getButtonName('next', 'action');
146 //we allow the controller to set force/reset externally,
147 //useful when we are being driven by the wizard framework
148 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
149 $this->_force
= CRM_Utils_Request
::retrieve('force', 'Boolean', $this, FALSE);
150 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this, FALSE, 'search');
151 $this->_reset
= CRM_Utils_Request
::retrieve('reset', 'Boolean', CRM_Core_DAO
::$_nullObject);
153 //operation for state machine.
154 $this->_operation
= CRM_Utils_Request
::retrieve('op', 'String', $this, FALSE, 'reserve');
155 //validate operation.
156 if (!in_array($this->_operation
, array(
157 'reserve', 'release', 'interview'))) {
158 $this->_operation
= 'reserve';
159 $this->set('op', $this->_operation
);
161 $this->set('searchVoterFor', $this->_operation
);
162 $this->assign('searchVoterFor', $this->_operation
);
163 $this->assign('isFormSubmitted', $this->isSubmitted());
165 //do check permissions.
166 if (!CRM_Core_Permission
::check('administer CiviCampaign') &&
167 !CRM_Core_Permission
::check('manage campaign') &&
168 !CRM_Core_Permission
::check("{$this->_operation} campaign contacts")
170 CRM_Utils_System
::permissionDenied();
171 CRM_Utils_System
::civiExit();
174 $this->assign("context", $this->_context
);
176 // get user submitted values
177 // get it from controller only if form has been submitted, else preProcess has set this
180 $this->_formValues
= $this->get('formValues');
183 $this->_formValues
= $this->controller
->exportValues($this->_name
);
187 $this->postProcess();
188 $this->set('force', 0);
192 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
193 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
194 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
198 //get the voter clause.
199 $voterClause = $this->voterClause();
201 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
203 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
211 if ($this->_context
== 'user') {
212 $prefix = $this->_prefix
;
215 $this->assign("{$prefix}limit", $this->_limit
);
216 $this->assign("{$prefix}single", $this->_single
);
218 $controller = new CRM_Core_Selector_Controller($selector,
219 $this->get(CRM_Utils_Pager
::PAGE_ID
),
221 CRM_Core_Action
::VIEW
,
223 CRM_Core_Selector_Controller
::TRANSFER
,
227 $controller->setEmbedded(TRUE);
228 $controller->moveFromSessionToTemplate();
230 //append breadcrumb to survey dashboard.
231 if (CRM_Campaign_BAO_Campaign
::accessCampaign()) {
232 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
233 CRM_Utils_System
::appendBreadCrumb(array(array('title' => ts('Survey(s)'), 'url' => $url)));
236 //set the form title.
237 CRM_Utils_System
::setTitle(ts('Find Respondents To %1', array(1 => ucfirst($this->_operation
))));
240 function setDefaultValues() {
241 //load the default survey for all actions.
242 if (empty($this->_defaults
)) {
243 $defaultSurveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
244 if ($defaultSurveyId) {
245 $this->_defaults
['campaign_survey_id'] = $defaultSurveyId;
249 return $this->_defaults
;
259 function buildQuickForm() {
260 //build the search form.
261 CRM_Campaign_BAO_Query
::buildSearchForm($this);
264 * add form checkboxes for each row. This is needed out here to conform to QF protocol
265 * of all elements being declared in builQuickForm
269 $rows = $this->get('rows');
270 if (is_array($rows)) {
271 if (!$this->_single
) {
272 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);"));
273 foreach ($rows as $row) {
274 $this->addElement('checkbox', $row['checkbox'],
276 array('onclick' => "toggleTaskAction( true ); return checkSelectedBox('" . $row['checkbox'] . "');")
281 $total = $cancel = 0;
283 $permission = CRM_Core_Permission
::getPermission();
284 $allTasks = CRM_Campaign_Task
::permissionedTaskTitles($permission);
286 //hack to serve right page to state machine.
287 $taskMapping = array(
293 $currentTaskValue = CRM_Utils_Array
::value($this->_operation
, $taskMapping);
294 $taskValue = array($currentTaskValue => $allTasks[$currentTaskValue]);
295 if ($this->_operation
== 'interview' &&
296 CRM_Utils_Array
::value('campaign_survey_id', $this->_formValues
)
298 $activityTypes = CRM_Core_PseudoConstant
::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
300 $surveyTypeId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
301 $this->_formValues
['campaign_survey_id'],
305 $currentTaskValue => ts('Record %1 Responses',
306 array(1 => $activityTypes[$surveyTypeId])
310 $this->add('select', 'task', ts('Actions:') . ' ', $taskValue);
311 $this->setDefaults(array('task' => $currentTaskValue));
313 $this->add('submit', $this->_actionButtonName
, ts('Go'),
315 'class' => 'form-submit',
320 $this->add('submit', $this->_printButtonName
, ts('Print'),
322 'class' => 'form-submit',
323 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 1);",
327 // need to perform tasks on all or selected items ? using radio_ts(task selection) for it
328 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
329 $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('onclick' => $this->getName() . ".toggleSelect.checked = false; toggleCheckboxVals('mark_x_',this); toggleTaskAction( true );"));
333 $this->addButtons(array(
336 'name' => ts('Search'),
344 * The post processing of the form gets done here.
346 * Key things done during post processing are
347 * - check for reset or next request. if present, skip post procesing.
348 * - now check if user requested running a saved search, if so, then
349 * the form values associated with the saved search are used for searching.
350 * - if user has done a submit with new values the regular post submissing is
352 * The processing consists of using a Selector / Controller framework for getting the
360 function postProcess() {
367 if (!empty($_POST)) {
368 $this->_formValues
= $this->controller
->exportValues($this->_name
);
371 $this->fixFormValues();
373 //format params as per task.
374 $this->formatParams();
376 $this->_queryParams
= CRM_Contact_BAO_Query
::convertFormValues($this->_formValues
);
378 $this->set('formValues', $this->_formValues
);
379 $this->set('queryParams', $this->_queryParams
);
381 $buttonName = $this->controller
->getButtonName();
382 if ($buttonName == $this->_actionButtonName ||
$buttonName == $this->_printButtonName
) {
383 // check actionName and if next, then do not repeat a search, since we are going to the next page
385 // hack, make sure we reset the task values
386 $stateMachine = $this->controller
->getStateMachine();
387 $formName = $stateMachine->getTaskFormName();
389 $this->controller
->resetPage($formName);
394 if ($this->get(CRM_Utils_Sort
::SORT_ID
)) {
395 $sortID = CRM_Utils_Sort
::sortIDValue($this->get(CRM_Utils_Sort
::SORT_ID
),
396 $this->get(CRM_Utils_Sort
::SORT_DIRECTION
)
400 //get the voter clause.
401 $voterClause = $this->voterClause();
403 $selector = new CRM_Campaign_Selector_Search($this->_queryParams
,
410 $selector->setKey($this->controller
->_key
);
413 if ($this->_context
== 'basic' ||
414 $this->_context
== 'user'
416 $prefix = $this->_prefix
;
419 $controller = new CRM_Core_Selector_Controller($selector,
420 $this->get(CRM_Utils_Pager
::PAGE_ID
),
422 CRM_Core_Action
::VIEW
,
424 CRM_Core_Selector_Controller
::SESSION
,
427 $controller->setEmbedded(TRUE);
428 $query = $selector->getQuery();
429 if ($this->_context
== 'user') {
430 $query->setSkipPermission(TRUE);
435 function formatParams() {
436 $interviewerId = CRM_Utils_Array
::value('survey_interviewer_id', $this->_formValues
);
437 if (!$interviewerId) {
438 $session = CRM_Core_Session
::singleton();
439 $this->_formValues
['survey_interviewer_id'] = $interviewerId = $session->get('userID');
441 $this->set('interviewerId', $interviewerId);
442 if (!CRM_Utils_Array
::value('survey_interviewer_name', $this->_formValues
)) {
443 $this->_formValues
['survey_interviewer_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
450 //format multi-select group and contact types.
452 'group', 'contact_type') as $param) {
456 $paramValue = CRM_Utils_Array
::value($param, $this->_formValues
);
457 if ($paramValue && is_array($paramValue)) {
458 unset($this->_formValues
[$param]);
459 foreach ($paramValue as $key => $value) {
460 $this->_formValues
[$param][$value] = 1;
465 //apply filter of survey contact type for search.
466 $contactType = CRM_Campaign_BAO_Survey
::getSurveyContactType(CRM_Utils_Array
::value('campaign_survey_id', $this->_formValues
));
467 if ($contactType && in_array($this->_operation
, array(
468 'reserve', 'interview'))) {
469 $this->_formValues
['contact_type'][$contactType] = 1;
472 if ($this->_operation
== 'reserve') {
473 if (CRM_Utils_Array
::value('campaign_survey_id', $this->_formValues
)) {
474 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey',
475 $this->_formValues
['campaign_survey_id'],
479 //allow voter search in sub-part of given constituents,
480 //but make sure in case user does not select any group.
481 //get all associated campaign groups in where filter, CRM-7406
482 $groups = CRM_Utils_Array
::value('group', $this->_formValues
);
483 if ($campaignId && CRM_Utils_System
::isNull($groups)) {
484 $campGroups = CRM_Campaign_BAO_Campaign
::getCampaignGroups($campaignId);
485 foreach ($campGroups as $id => $title) $this->_formValues
['group'][$id] = 1;
488 //carry servey id w/ this.
489 $this->set('surveyId', $this->_formValues
['campaign_survey_id']);
490 unset($this->_formValues
['campaign_survey_id']);
492 unset($this->_formValues
['survey_interviewer_id']);
494 elseif ($this->_operation
== 'interview' ||
495 $this->_operation
== 'release'
497 //to conduct interview / release activity status should be scheduled.
498 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
499 if ($scheduledStatusId = array_search('Scheduled', $activityStatus)) {
500 $this->_formValues
['survey_status_id'] = $scheduledStatusId;
504 //pass voter search operation.
505 $this->_formValues
['campaign_search_voter_for'] = $this->_operation
;
508 function fixFormValues() {
509 // if this search has been forced
510 // then see if there are any get values, and if so over-ride the post values
511 // note that this means that GET over-rides POST :)
513 //since we have qfKey, no need to manipulate set defaults.
514 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', CRM_Core_DAO
::$_nullObject);
516 if (!$this->_force || CRM_Utils_Rule
::qfKey($qfKey)) {
521 $surveyId = CRM_Utils_Request
::retrieve('sid', 'Positive', CRM_Core_DAO
::$_nullObject);
524 $surveyId = CRM_Utils_Type
::escape($surveyId, 'Integer');
527 // use default survey id
528 $surveyId = key(CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE));
531 CRM_Core_Error
::fatal('Could not find valid Survey Id.');
533 $this->_formValues
['campaign_survey_id'] = $this->_formValues
['campaign_survey_id'] = $surveyId;
535 $session = CRM_Core_Session
::singleton();
536 $userId = $session->get('userID');
538 // get interviewer id
539 $cid = CRM_Utils_Request
::retrieve('cid', 'Positive',
540 CRM_Core_DAO
::$_nullObject, FALSE, $userId
542 //to force other contact as interviewer, user should be admin.
543 if ($cid != $userId &&
544 !CRM_Core_Permission
::check('administer CiviCampaign')
546 CRM_Utils_System
::permissionDenied();
547 CRM_Utils_System
::civiExit();
549 $this->_formValues
['survey_interviewer_id'] = $cid;
550 $this->_formValues
['survey_interviewer_name'] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
555 //get all in defaults.
556 $this->_defaults
= $this->_formValues
;
557 $this->_limit
= CRM_Utils_Request
::retrieve('limit', 'Positive', $this);
560 function voterClause() {
561 $params = array('campaign_search_voter_for' => $this->_operation
);
563 $clauseFields = array(
564 'surveyId' => 'campaign_survey_id',
565 'interviewerId' => 'survey_interviewer_id',
568 foreach ($clauseFields as $param => $key) {
569 $params[$key] = CRM_Utils_Array
::value($key, $this->_formValues
);
570 if (!$params[$key]) {
571 $params[$key] = $this->get($param);
576 $voterClause = CRM_Campaign_BAO_Query
::voterClause($params);
582 * Return a descriptive name for the page, used in wizard header
587 public function getTitle() {
588 return ts('Find Respondents');