(NFC) (dev/core#878) Simplify copyright header (CRM/*)
[civicrm-core.git] / CRM / Campaign / Form / Petition.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 adding a petition.
20 */
21 class CRM_Campaign_Form_Petition extends CRM_Core_Form {
22
23 /**
24 * Making this public so we can reference it in the formRule
25 * @var int
26 */
27 public $_surveyId;
28
29 /**
30 * Explicitly declare the entity api name.
31 */
32 public function getDefaultEntity() {
33 return 'Survey';
34 }
35
36 /**
37 * Get the entity id being edited.
38 *
39 * @return int|null
40 */
41 public function getEntityId() {
42 return $this->_surveyId;
43 }
44
45 public function preProcess() {
46 if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
47 CRM_Utils_System::permissionDenied();
48 }
49
50 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
51
52 $this->assign('context', $this->_context);
53
54 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
55
56 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) {
57 $this->_surveyId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
58
59 if ($this->_action & CRM_Core_Action::UPDATE) {
60 CRM_Utils_System::setTitle(ts('Edit Survey'));
61 }
62 else {
63 CRM_Utils_System::setTitle(ts('Delete Survey'));
64 }
65 }
66
67 // Add custom data to form
68 CRM_Custom_Form_CustomData::addToForm($this);
69
70 $session = CRM_Core_Session::singleton();
71 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
72 $session->pushUserContext($url);
73
74 $this->_values = $this->get('values');
75
76 if (!is_array($this->_values)) {
77 $this->_values = [];
78 if ($this->_surveyId) {
79 $params = ['id' => $this->_surveyId];
80 CRM_Campaign_BAO_Survey::retrieve($params, $this->_values);
81 }
82 $this->set('values', $this->_values);
83 }
84
85 $this->assign('action', $this->_action);
86 $this->assign('surveyId', $this->_surveyId);
87
88 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) {
89 $this->_surveyId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
90
91 if ($this->_action & CRM_Core_Action::UPDATE) {
92 CRM_Utils_System::setTitle(ts('Edit Petition'));
93 }
94 else {
95 CRM_Utils_System::setTitle(ts('Delete Petition'));
96 }
97 }
98
99 $session = CRM_Core_Session::singleton();
100 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition');
101 $session->pushUserContext($url);
102
103 CRM_Utils_System::appendBreadCrumb([['title' => ts('Petition Dashboard'), 'url' => $url]]);
104 }
105
106 /**
107 * Set default values for the form. Note that in edit/view mode
108 * the default values are retrieved from the database
109 *
110 * @return array
111 * array of default values
112 */
113 public function setDefaultValues() {
114 $defaults = $this->_values;
115
116 $ufContactJoinParams = [
117 'entity_table' => 'civicrm_survey',
118 'entity_id' => $this->_surveyId,
119 'weight' => 2,
120 ];
121
122 if ($ufContactGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufContactJoinParams)) {
123 $defaults['contact_profile_id'] = $ufContactGroupId;
124 }
125 $ufActivityJoinParams = [
126 'entity_table' => 'civicrm_survey',
127 'entity_id' => $this->_surveyId,
128 'weight' => 1,
129 ];
130
131 if ($ufActivityGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufActivityJoinParams)) {
132 $defaults['profile_id'] = $ufActivityGroupId;
133 }
134
135 if (!isset($defaults['is_active'])) {
136 $defaults['is_active'] = 1;
137 }
138
139 $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE);
140 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
141 $defaults['is_default'] = 1;
142 }
143
144 return $defaults;
145 }
146
147 public function buildQuickForm() {
148
149 if ($this->_action & CRM_Core_Action::DELETE) {
150 $this->addButtons(
151 [
152 [
153 'type' => 'next',
154 'name' => ts('Delete'),
155 'isDefault' => TRUE,
156 ],
157 [
158 'type' => 'cancel',
159 'name' => ts('Cancel'),
160 ],
161 ]
162 );
163 return;
164 }
165
166 $this->add('text', 'title', ts('Petition Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
167
168 $attributes = CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey');
169
170 $petitionTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Petition');
171 $this->addElement('hidden', 'activity_type_id', $petitionTypeID);
172
173 // script / instructions / description of petition purpose
174 $this->add('wysiwyg', 'instructions', ts('Introduction'), $attributes['instructions']);
175
176 $this->addEntityRef('campaign_id', ts('Campaign'), [
177 'entity' => 'Campaign',
178 'create' => TRUE,
179 'select' => ['minimumInputLength' => 0],
180 ]);
181
182 $customContactProfiles = CRM_Core_BAO_UFGroup::getProfiles(['Individual']);
183 // custom group id
184 $this->add('select', 'contact_profile_id', ts('Contact Profile'),
185 [
186 '' => ts('- select -'),
187 ] + $customContactProfiles, TRUE
188 );
189
190 $customProfiles = CRM_Core_BAO_UFGroup::getProfiles(['Activity']);
191 // custom group id
192 $this->add('select', 'profile_id', ts('Activity Profile'),
193 [
194 '' => ts('- select -'),
195 ] + $customProfiles
196 );
197
198 // thank you title and text (html allowed in text)
199 $this->add('text', 'thankyou_title', ts('Thank-you Page Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_title'));
200 $this->add('wysiwyg', 'thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_text'));
201
202 // bypass email confirmation?
203 $this->add('checkbox', 'bypass_confirm', ts('Bypass email confirmation'));
204
205 //is share through social media
206 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
207
208 // is active ?
209 $this->add('checkbox', 'is_active', ts('Is Active?'));
210
211 // is default ?
212 $this->add('checkbox', 'is_default', ts('Is Default?'));
213
214 // add buttons
215 $this->addButtons(
216 [
217 [
218 'type' => 'next',
219 'name' => ts('Save'),
220 'isDefault' => TRUE,
221 ],
222 [
223 'type' => 'next',
224 'name' => ts('Save and New'),
225 'subName' => 'new',
226 ],
227 [
228 'type' => 'cancel',
229 'name' => ts('Cancel'),
230 ],
231 ]
232 );
233
234 // add a form rule to check default value
235 $this->addFormRule(['CRM_Campaign_Form_Petition', 'formRule'], $this);
236 }
237
238 /**
239 * Global validation rules for the form.
240 * @param $fields
241 * @param $files
242 * @param $form
243 * @return array|bool
244 */
245 public static function formRule($fields, $files, $form) {
246 $errors = [];
247 // Petitions should be unique by: title, campaign ID (if assigned) and activity type ID
248 // NOTE: This class is called for both Petition create / update AND for Survey Results tab, but this rule is only for Petition.
249 $where = ['activity_type_id = %1', 'title = %2'];
250 $params = [
251 1 => [$fields['activity_type_id'], 'Integer'],
252 2 => [$fields['title'], 'String'],
253 ];
254 $uniqueRuleErrorMessage = ts('This title is already associated with the selected activity type. Please specify a unique title.');
255
256 if (empty($fields['campaign_id'])) {
257 $where[] = 'campaign_id IS NULL';
258 }
259 else {
260 $where[] = 'campaign_id = %3';
261 $params[3] = [$fields['campaign_id'], 'Integer'];
262 $uniqueRuleErrorMessage = ts('This title is already associated with the selected campaign and activity type. Please specify a unique title.');
263 }
264
265 // Exclude current Petition row if UPDATE.
266 if ($form->_surveyId) {
267 $where[] = 'id != %4';
268 $params[4] = [$form->_surveyId, 'Integer'];
269 }
270
271 $whereClause = implode(' AND ', $where);
272
273 $query = "
274 SELECT COUNT(*) AS row_count
275 FROM civicrm_survey
276 WHERE $whereClause
277 ";
278
279 $result = CRM_Core_DAO::singleValueQuery($query, $params);
280 if ($result >= 1) {
281 $errors['title'] = $uniqueRuleErrorMessage;
282 }
283 return empty($errors) ? TRUE : $errors;
284 }
285
286 public function postProcess() {
287 // store the submitted values in an array
288 $params = $this->controller->exportValues($this->_name);
289
290 $session = CRM_Core_Session::singleton();
291
292 $params['last_modified_id'] = $session->get('userID');
293 $params['last_modified_date'] = date('YmdHis');
294 $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE);
295
296 if ($this->_surveyId) {
297
298 if ($this->_action & CRM_Core_Action::DELETE) {
299 CRM_Campaign_BAO_Survey::del($this->_surveyId);
300 CRM_Core_Session::setStatus(ts(' Petition has been deleted.'), ts('Record Deleted'), 'success');
301 $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition'));
302 return;
303 }
304
305 $params['id'] = $this->_surveyId;
306 }
307 else {
308 $params['created_id'] = $session->get('userID');
309 $params['created_date'] = date('YmdHis');
310 }
311
312 $params['bypass_confirm'] = CRM_Utils_Array::value('bypass_confirm', $params, 0);
313 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
314 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0);
315
316 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->getEntityId(), $this->getDefaultEntity());
317
318 $surveyId = CRM_Campaign_BAO_Survey::create($params);
319
320 // also update the ProfileModule tables
321 $ufJoinParams = [
322 'is_active' => 1,
323 'module' => 'CiviCampaign',
324 'entity_table' => 'civicrm_survey',
325 'entity_id' => $surveyId->id,
326 ];
327
328 // first delete all past entries
329 if ($this->_surveyId) {
330 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
331 }
332 if (!empty($params['profile_id'])) {
333 $ufJoinParams['weight'] = 1;
334 $ufJoinParams['uf_group_id'] = $params['profile_id'];
335 CRM_Core_BAO_UFJoin::create($ufJoinParams);
336 }
337
338 if (!empty($params['contact_profile_id'])) {
339 $ufJoinParams['weight'] = 2;
340 $ufJoinParams['uf_group_id'] = $params['contact_profile_id'];
341 CRM_Core_BAO_UFJoin::create($ufJoinParams);
342 }
343
344 if (!is_a($surveyId, 'CRM_Core_Error')) {
345 CRM_Core_Session::setStatus(ts('Petition has been saved.'), ts('Saved'), 'success');
346 }
347
348 $buttonName = $this->controller->getButtonName();
349 if ($buttonName == $this->getButtonName('next', 'new')) {
350 CRM_Core_Session::setStatus(ts(' You can add another Petition.'), '', 'info');
351 $session->replaceUserContext(CRM_Utils_System::url('civicrm/petition/add', 'reset=1&action=add'));
352 }
353 else {
354 $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition'));
355 }
356 }
357
358 }