3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for processing a survey.
21 class CRM_Campaign_Form_Survey_Main
extends CRM_Campaign_Form_Survey
{
40 public function preProcess() {
43 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
45 $this->assign('context', $this->_context
);
47 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
49 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
50 CRM_Utils_System
::setTitle(ts('Configure Survey') . ' - ' . $this->_surveyTitle
);
53 // Add custom data to form
54 CRM_Custom_Form_CustomData
::addToForm($this);
56 if ($this->_name
!= 'Petition') {
57 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
58 CRM_Utils_System
::appendBreadCrumb([['title' => ts('Survey Dashboard'), 'url' => $url]]);
61 $this->_values
= $this->get('values');
62 if (!is_array($this->_values
)) {
64 if ($this->_surveyId
) {
65 $params = ['id' => $this->_surveyId
];
66 CRM_Campaign_BAO_Survey
::retrieve($params, $this->_values
);
68 $this->set('values', $this->_values
);
71 $this->assign('action', $this->_action
);
72 $this->assign('surveyId', $this->_surveyId
);
76 * Set default values for the form. Note that in edit/view mode
77 * the default values are retrieved from the database
80 * array of default values
82 public function setDefaultValues() {
84 $defaults = $this->_values
;
86 if ($this->_surveyId
) {
88 if (!empty($defaults['result_id']) && !empty($defaults['recontact_interval'])) {
90 $resultId = $defaults['result_id'];
91 $recontactInterval = CRM_Utils_String
::unserialize($defaults['recontact_interval']);
93 unset($defaults['recontact_interval']);
94 $defaults['option_group_id'] = $resultId;
98 if (!isset($defaults['is_active'])) {
99 $defaults['is_active'] = 1;
102 $defaultSurveys = CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE);
103 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
104 $defaults['is_default'] = 1;
111 * Build the form object.
113 public function buildQuickForm() {
114 $this->add('text', 'title', ts('Title'), CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
117 $this->addSelect('activity_type_id', ['option_url' => 'civicrm/admin/campaign/surveyType'], TRUE);
119 $this->addEntityRef('campaign_id', ts('Campaign'), [
120 'entity' => 'Campaign',
122 'select' => ['minimumInputLength' => 0],
125 // script / instructions
126 $this->add('wysiwyg', 'instructions', ts('Instructions for interviewers'), ['rows' => 5, 'cols' => 40]);
129 $this->add('number', 'release_frequency', ts('Release Frequency'), CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency'));
131 $this->addRule('release_frequency', ts('Release Frequency interval should be a positive number.'), 'positiveInteger');
133 // max reserved contacts at a time
134 $this->add('number', 'default_number_of_contacts', ts('Maximum reserved at one time'), CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'default_number_of_contacts'));
135 $this->addRule('default_number_of_contacts', ts('Maximum reserved at one time should be a positive number'), 'positiveInteger');
137 // total reserved per interviewer
138 $this->add('number', 'max_number_of_contacts', ts('Total reserved per interviewer'), CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'max_number_of_contacts'));
139 $this->addRule('max_number_of_contacts', ts('Total reserved contacts should be a positive number'), 'positiveInteger');
142 $this->add('checkbox', 'is_active', ts('Active?'));
145 $this->add('checkbox', 'is_default', ts('Default?'));
147 parent
::buildQuickForm();
153 public function postProcess() {
154 // store the submitted values in an array
155 $params = $this->controller
->exportValues($this->_name
);
157 $session = CRM_Core_Session
::singleton();
159 $params['last_modified_id'] = $session->get('userID');
160 $params['last_modified_date'] = date('YmdHis');
162 if ($this->_surveyId
) {
163 $params['id'] = $this->_surveyId
;
166 $params['created_id'] = $session->get('userID');
167 $params['created_date'] = date('YmdHis');
170 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, 0);
171 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, 0);
173 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params, $this->getEntityId(), $this->getDefaultEntity());
175 $survey = CRM_Campaign_BAO_Survey
::create($params);
176 $this->_surveyId
= $survey->id
;
178 if (!empty($this->_values
['result_id'])) {
179 $query = "SELECT COUNT(*) FROM civicrm_survey WHERE result_id = %1";
180 $countSurvey = (int) CRM_Core_DAO
::singleValueQuery($query,
183 $this->_values
['result_id'],
188 // delete option group if no any survey is using it.
190 CRM_Core_BAO_OptionGroup
::del($this->_values
['result_id']);
194 parent
::endPostProcess();