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