APIv4 - Add Address::getCoordinates action
[civicrm-core.git] / CRM / Campaign / Form / Survey / Main.php
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 * This class generates form components for processing a survey.
20 */
21 class CRM_Campaign_Form_Survey_Main extends CRM_Campaign_Form_Survey {
22
23 /**
24 * values
25 *
26 * @var array
27 *
28 */
29
30
31 public $_values;
32
33 /**
34 * Context.
35 *
36 * @var string
37 */
38 protected $_context;
39
40 public function preProcess() {
41 parent::preProcess();
42
43 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
44
45 $this->assign('context', $this->_context);
46
47 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
48
49 if ($this->_action & CRM_Core_Action::UPDATE) {
50 $this->setTitle(ts('Configure Survey') . ' - ' . $this->_surveyTitle);
51 }
52
53 // Add custom data to form
54 CRM_Custom_Form_CustomData::addToForm($this);
55
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]]);
59 }
60
61 $this->_values = $this->get('values');
62 if (!is_array($this->_values)) {
63 $this->_values = [];
64 if ($this->_surveyId) {
65 $params = ['id' => $this->_surveyId];
66 CRM_Campaign_BAO_Survey::retrieve($params, $this->_values);
67 }
68 $this->set('values', $this->_values);
69 }
70
71 $this->assign('action', $this->_action);
72 $this->assign('surveyId', $this->_surveyId);
73 }
74
75 /**
76 * Set default values for the form. Note that in edit/view mode
77 * the default values are retrieved from the database
78 *
79 * @return array
80 * array of default values
81 */
82 public function setDefaultValues() {
83
84 $defaults = $this->_values;
85
86 if ($this->_surveyId) {
87
88 if (!empty($defaults['result_id']) && !empty($defaults['recontact_interval'])) {
89
90 $resultId = $defaults['result_id'];
91 $recontactInterval = CRM_Utils_String::unserialize($defaults['recontact_interval']);
92
93 unset($defaults['recontact_interval']);
94 $defaults['option_group_id'] = $resultId;
95 }
96 }
97
98 if (!isset($defaults['is_active'])) {
99 $defaults['is_active'] = 1;
100 }
101
102 $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE);
103 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
104 $defaults['is_default'] = 1;
105 }
106
107 return $defaults;
108 }
109
110 /**
111 * Build the form object.
112 */
113 public function buildQuickForm() {
114 $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
115
116 // Activity Type id
117 $this->addSelect('activity_type_id', ['option_url' => 'civicrm/admin/campaign/surveyType'], TRUE);
118
119 $this->addEntityRef('campaign_id', ts('Campaign'), [
120 'entity' => 'Campaign',
121 'create' => TRUE,
122 'select' => ['minimumInputLength' => 0],
123 ]);
124
125 // script / instructions
126 $this->add('wysiwyg', 'instructions', ts('Instructions for interviewers'), ['rows' => 5, 'cols' => 40]);
127
128 // release frequency
129 $this->add('number', 'release_frequency', ts('Release Frequency'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency'));
130
131 $this->addRule('release_frequency', ts('Release Frequency interval should be a positive number.'), 'positiveInteger');
132
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');
136
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');
140
141 // is active ?
142 $this->add('checkbox', 'is_active', ts('Active?'));
143
144 // is default ?
145 $this->add('checkbox', 'is_default', ts('Default?'));
146
147 parent::buildQuickForm();
148 }
149
150 /**
151 * Process the form.
152 */
153 public function postProcess() {
154 // store the submitted values in an array
155 $params = $this->controller->exportValues($this->_name);
156
157 $session = CRM_Core_Session::singleton();
158
159 $params['last_modified_id'] = $session->get('userID');
160 $params['last_modified_date'] = date('YmdHis');
161
162 if ($this->_surveyId) {
163 $params['id'] = $this->_surveyId;
164 }
165 else {
166 $params['created_id'] = $session->get('userID');
167 $params['created_date'] = date('YmdHis');
168 }
169
170 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
171 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0);
172
173 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->getEntityId(), $this->getDefaultEntity());
174
175 $survey = CRM_Campaign_BAO_Survey::create($params);
176 $this->_surveyId = $survey->id;
177
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,
181 [
182 1 => [
183 $this->_values['result_id'],
184 'Positive',
185 ],
186 ]
187 );
188 // delete option group if no any survey is using it.
189 if (!$countSurvey) {
190 CRM_Core_BAO_OptionGroup::del($this->_values['result_id']);
191 }
192 }
193
194 parent::endPostProcess();
195 }
196
197 }