Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
df371444 | 19 | * This class generates form components for adding a petition. |
6a488035 | 20 | */ |
1c6e0372 | 21 | class CRM_Campaign_Form_Petition extends CRM_Core_Form { |
6a488035 | 22 | |
43427a24 | 23 | /** |
db55935b | 24 | * Making this public so we can reference it in the formRule |
43427a24 | 25 | * @var int |
43427a24 | 26 | */ |
ad980eb2 | 27 | public $_surveyId; |
43427a24 | 28 | |
14c67729 MWMC |
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 | ||
6a488035 | 45 | public function preProcess() { |
1c6e0372 AH |
46 | if (!CRM_Campaign_BAO_Campaign::accessCampaign()) { |
47 | CRM_Utils_System::permissionDenied(); | |
48 | } | |
49 | ||
edc80cda | 50 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
1c6e0372 AH |
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) { | |
483cfbc4 | 60 | $this->setTitle(ts('Edit Survey')); |
1c6e0372 AH |
61 | } |
62 | else { | |
483cfbc4 | 63 | $this->setTitle(ts('Delete Survey')); |
1c6e0372 AH |
64 | } |
65 | } | |
66 | ||
14c67729 MWMC |
67 | // Add custom data to form |
68 | CRM_Custom_Form_CustomData::addToForm($this); | |
1c6e0372 AH |
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'); | |
8ef12e64 | 75 | |
1c6e0372 | 76 | if (!is_array($this->_values)) { |
be2fb01f | 77 | $this->_values = []; |
1c6e0372 | 78 | if ($this->_surveyId) { |
be2fb01f | 79 | $params = ['id' => $this->_surveyId]; |
1c6e0372 AH |
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); | |
1c6e0372 | 87 | |
6a488035 TO |
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) { | |
483cfbc4 | 92 | $this->setTitle(ts('Edit Petition')); |
6a488035 TO |
93 | } |
94 | else { | |
483cfbc4 | 95 | $this->setTitle(ts('Delete Petition')); |
6a488035 TO |
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 | ||
be2fb01f | 103 | CRM_Utils_System::appendBreadCrumb([['title' => ts('Petition Dashboard'), 'url' => $url]]); |
6a488035 TO |
104 | } |
105 | ||
106 | /** | |
c490a46a | 107 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
108 | * the default values are retrieved from the database |
109 | * | |
a6c01b45 CW |
110 | * @return array |
111 | * array of default values | |
6a488035 | 112 | */ |
00be9182 | 113 | public function setDefaultValues() { |
1c6e0372 | 114 | $defaults = $this->_values; |
6a488035 | 115 | |
be2fb01f | 116 | $ufContactJoinParams = [ |
6a488035 TO |
117 | 'entity_table' => 'civicrm_survey', |
118 | 'entity_id' => $this->_surveyId, | |
119 | 'weight' => 2, | |
be2fb01f | 120 | ]; |
6a488035 | 121 | |
5a547074 | 122 | if ($ufContactGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufContactJoinParams)) { |
123 | $defaults['contact_profile_id'] = $ufContactGroupId; | |
124 | } | |
be2fb01f | 125 | $ufActivityJoinParams = [ |
5a547074 | 126 | 'entity_table' => 'civicrm_survey', |
127 | 'entity_id' => $this->_surveyId, | |
128 | 'weight' => 1, | |
be2fb01f | 129 | ]; |
5a547074 | 130 | |
131 | if ($ufActivityGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufActivityJoinParams)) { | |
132 | $defaults['profile_id'] = $ufActivityGroupId; | |
6a488035 | 133 | } |
8ef12e64 | 134 | |
6a488035 TO |
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 | ||
6a488035 TO |
147 | public function buildQuickForm() { |
148 | ||
149 | if ($this->_action & CRM_Core_Action::DELETE) { | |
424392c5 | 150 | $this->addButtons( |
be2fb01f CW |
151 | [ |
152 | [ | |
6a488035 TO |
153 | 'type' => 'next', |
154 | 'name' => ts('Delete'), | |
155 | 'isDefault' => TRUE, | |
be2fb01f CW |
156 | ], |
157 | [ | |
6a488035 TO |
158 | 'type' => 'cancel', |
159 | 'name' => ts('Cancel'), | |
be2fb01f CW |
160 | ], |
161 | ] | |
6a488035 TO |
162 | ); |
163 | return; | |
164 | } | |
165 | ||
6a488035 TO |
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 | ||
e3f27fc0 | 170 | $petitionTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Petition'); |
6a488035 TO |
171 | $this->addElement('hidden', 'activity_type_id', $petitionTypeID); |
172 | ||
173 | // script / instructions / description of petition purpose | |
5d51a2f9 | 174 | $this->add('wysiwyg', 'instructions', ts('Introduction'), $attributes['instructions']); |
6a488035 | 175 | |
f7b32533 | 176 | $this->addEntityRef('campaign_id', ts('Campaign'), [ |
af00ced5 | 177 | 'entity' => 'Campaign', |
f7b32533 | 178 | 'create' => TRUE, |
33ccf435 | 179 | 'select' => ['minimumInputLength' => 0], |
f7b32533 | 180 | ]); |
6a488035 | 181 | |
be2fb01f | 182 | $customContactProfiles = CRM_Core_BAO_UFGroup::getProfiles(['Individual']); |
6a488035 TO |
183 | // custom group id |
184 | $this->add('select', 'contact_profile_id', ts('Contact Profile'), | |
be2fb01f | 185 | [ |
389bcebf | 186 | '' => ts('- select -'), |
be2fb01f | 187 | ] + $customContactProfiles, TRUE |
6a488035 TO |
188 | ); |
189 | ||
be2fb01f | 190 | $customProfiles = CRM_Core_BAO_UFGroup::getProfiles(['Activity']); |
6a488035 TO |
191 | // custom group id |
192 | $this->add('select', 'profile_id', ts('Activity Profile'), | |
be2fb01f | 193 | [ |
21dfd5f5 | 194 | '' => ts('- select -'), |
be2fb01f | 195 | ] + $customProfiles |
6a488035 TO |
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')); | |
5d51a2f9 | 200 | $this->add('wysiwyg', 'thankyou_text', ts('Thank-you Message'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_text')); |
8ef12e64 | 201 | |
6a488035 TO |
202 | // bypass email confirmation? |
203 | $this->add('checkbox', 'bypass_confirm', ts('Bypass email confirmation')); | |
204 | ||
424392c5 | 205 | //is share through social media |
007a4455 | 206 | $this->addElement('checkbox', 'is_share', ts('Add footer region with Twitter, Facebook and LinkedIn share buttons and scripts?')); |
424392c5 | 207 | |
6a488035 TO |
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 | |
424392c5 | 215 | $this->addButtons( |
be2fb01f CW |
216 | [ |
217 | [ | |
6a488035 TO |
218 | 'type' => 'next', |
219 | 'name' => ts('Save'), | |
220 | 'isDefault' => TRUE, | |
be2fb01f CW |
221 | ], |
222 | [ | |
6a488035 TO |
223 | 'type' => 'next', |
224 | 'name' => ts('Save and New'), | |
225 | 'subName' => 'new', | |
be2fb01f CW |
226 | ], |
227 | [ | |
6a488035 TO |
228 | 'type' => 'cancel', |
229 | 'name' => ts('Cancel'), | |
be2fb01f CW |
230 | ], |
231 | ] | |
6a488035 TO |
232 | ); |
233 | ||
234 | // add a form rule to check default value | |
be2fb01f | 235 | $this->addFormRule(['CRM_Campaign_Form_Petition', 'formRule'], $this); |
ad980eb2 DG |
236 | } |
237 | ||
238 | /** | |
fe482240 | 239 | * Global validation rules for the form. |
645ee340 EM |
240 | * @param $fields |
241 | * @param $files | |
242 | * @param $form | |
243 | * @return array|bool | |
ad980eb2 | 244 | */ |
00be9182 | 245 | public static function formRule($fields, $files, $form) { |
be2fb01f | 246 | $errors = []; |
ad980eb2 DG |
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. | |
be2fb01f CW |
249 | $where = ['activity_type_id = %1', 'title = %2']; |
250 | $params = [ | |
251 | 1 => [$fields['activity_type_id'], 'Integer'], | |
252 | 2 => [$fields['title'], 'String'], | |
253 | ]; | |
d1401e86 | 254 | $uniqueRuleErrorMessage = ts('This title is already associated with the selected activity type. Please specify a unique title.'); |
ad980eb2 DG |
255 | |
256 | if (empty($fields['campaign_id'])) { | |
257 | $where[] = 'campaign_id IS NULL'; | |
d75f2f47 EM |
258 | } |
259 | else { | |
ad980eb2 | 260 | $where[] = 'campaign_id = %3'; |
be2fb01f | 261 | $params[3] = [$fields['campaign_id'], 'Integer']; |
d1401e86 | 262 | $uniqueRuleErrorMessage = ts('This title is already associated with the selected campaign and activity type. Please specify a unique title.'); |
ad980eb2 DG |
263 | } |
264 | ||
265 | // Exclude current Petition row if UPDATE. | |
266 | if ($form->_surveyId) { | |
267 | $where[] = 'id != %4'; | |
be2fb01f | 268 | $params[4] = [$form->_surveyId, 'Integer']; |
ad980eb2 | 269 | } |
d75f2f47 | 270 | |
ad980eb2 DG |
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) { | |
d1401e86 | 281 | $errors['title'] = $uniqueRuleErrorMessage; |
ad980eb2 DG |
282 | } |
283 | return empty($errors) ? TRUE : $errors; | |
6a488035 TO |
284 | } |
285 | ||
6a488035 TO |
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'); | |
424392c5 | 294 | $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE); |
6a488035 TO |
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 | ||
14c67729 | 316 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->getEntityId(), $this->getDefaultEntity()); |
98c54bff | 317 | |
6a488035 TO |
318 | $surveyId = CRM_Campaign_BAO_Survey::create($params); |
319 | ||
6a488035 | 320 | // also update the ProfileModule tables |
be2fb01f | 321 | $ufJoinParams = [ |
6a488035 TO |
322 | 'is_active' => 1, |
323 | 'module' => 'CiviCampaign', | |
324 | 'entity_table' => 'civicrm_survey', | |
325 | 'entity_id' => $surveyId->id, | |
be2fb01f | 326 | ]; |
6a488035 TO |
327 | |
328 | // first delete all past entries | |
329 | if ($this->_surveyId) { | |
330 | CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams); | |
331 | } | |
a7488080 | 332 | if (!empty($params['profile_id'])) { |
6a488035 TO |
333 | $ufJoinParams['weight'] = 1; |
334 | $ufJoinParams['uf_group_id'] = $params['profile_id']; | |
335 | CRM_Core_BAO_UFJoin::create($ufJoinParams); | |
336 | } | |
337 | ||
a7488080 | 338 | if (!empty($params['contact_profile_id'])) { |
6a488035 TO |
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 | } | |
96025800 | 357 | |
6a488035 | 358 | } |