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