Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for adding a petition | |
38 | * | |
39 | */ | |
43427a24 | 40 | |
1c6e0372 | 41 | class CRM_Campaign_Form_Petition extends CRM_Core_Form { |
6a488035 | 42 | |
43427a24 | 43 | /** |
db55935b | 44 | * Making this public so we can reference it in the formRule |
43427a24 | 45 | * @var int |
43427a24 | 46 | */ |
ad980eb2 | 47 | public $_surveyId; |
43427a24 | 48 | |
6a488035 | 49 | public function preProcess() { |
1c6e0372 AH |
50 | if (!CRM_Campaign_BAO_Campaign::accessCampaign()) { |
51 | CRM_Utils_System::permissionDenied(); | |
52 | } | |
53 | ||
54 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
55 | ||
56 | $this->assign('context', $this->_context); | |
57 | ||
58 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this); | |
59 | ||
60 | if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) { | |
61 | $this->_surveyId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); | |
62 | ||
63 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
64 | CRM_Utils_System::setTitle(ts('Edit Survey')); | |
65 | } | |
66 | else { | |
67 | CRM_Utils_System::setTitle(ts('Delete Survey')); | |
68 | } | |
69 | } | |
70 | ||
71 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
72 | $this->assign('cdType', FALSE); | |
73 | if ($this->_cdType) { | |
74 | $this->assign('cdType', TRUE); | |
75 | return CRM_Custom_Form_CustomData::preProcess($this); | |
76 | } | |
77 | ||
78 | // when custom data is included in this page | |
a7488080 | 79 | if (!empty($_POST['hidden_custom'])) { |
1c6e0372 AH |
80 | CRM_Custom_Form_CustomData::preProcess($this); |
81 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
82 | } | |
83 | ||
84 | $session = CRM_Core_Session::singleton(); | |
85 | $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey'); | |
86 | $session->pushUserContext($url); | |
87 | ||
88 | $this->_values = $this->get('values'); | |
8ef12e64 | 89 | |
1c6e0372 AH |
90 | if (!is_array($this->_values)) { |
91 | $this->_values = array(); | |
92 | if ($this->_surveyId) { | |
93 | $params = array('id' => $this->_surveyId); | |
94 | CRM_Campaign_BAO_Survey::retrieve($params, $this->_values); | |
95 | } | |
96 | $this->set('values', $this->_values); | |
97 | } | |
98 | ||
99 | $this->assign('action', $this->_action); | |
100 | $this->assign('surveyId', $this->_surveyId); | |
101 | // for custom data | |
102 | $this->assign('entityID', $this->_surveyId); | |
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 | * | |
126 | * @param null | |
127 | * | |
128 | * @return array array of default values | |
6a488035 | 129 | */ |
00be9182 | 130 | public function setDefaultValues() { |
1c6e0372 | 131 | $defaults = $this->_values; |
6a488035 | 132 | |
5a547074 | 133 | $ufContactJoinParams = array( |
6a488035 TO |
134 | 'entity_table' => 'civicrm_survey', |
135 | 'entity_id' => $this->_surveyId, | |
136 | 'weight' => 2, | |
137 | ); | |
138 | ||
5a547074 | 139 | if ($ufContactGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufContactJoinParams)) { |
140 | $defaults['contact_profile_id'] = $ufContactGroupId; | |
141 | } | |
142 | $ufActivityJoinParams = array( | |
143 | 'entity_table' => 'civicrm_survey', | |
144 | 'entity_id' => $this->_surveyId, | |
145 | 'weight' => 1, | |
146 | ); | |
147 | ||
148 | if ($ufActivityGroupId = CRM_Core_BAO_UFJoin::findUFGroupId($ufActivityJoinParams)) { | |
149 | $defaults['profile_id'] = $ufActivityGroupId; | |
6a488035 | 150 | } |
8ef12e64 | 151 | |
6a488035 TO |
152 | if (!isset($defaults['is_active'])) { |
153 | $defaults['is_active'] = 1; | |
154 | } | |
155 | ||
156 | $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE); | |
157 | if (!isset($defaults['is_default']) && empty($defaultSurveys)) { | |
158 | $defaults['is_default'] = 1; | |
159 | } | |
160 | ||
161 | return $defaults; | |
162 | } | |
163 | ||
164 | ||
165 | public function buildQuickForm() { | |
166 | ||
167 | if ($this->_action & CRM_Core_Action::DELETE) { | |
424392c5 | 168 | $this->addButtons( |
169 | array( | |
6a488035 TO |
170 | array( |
171 | 'type' => 'next', | |
172 | 'name' => ts('Delete'), | |
173 | 'isDefault' => TRUE, | |
174 | ), | |
175 | array( | |
176 | 'type' => 'cancel', | |
177 | 'name' => ts('Cancel'), | |
178 | ), | |
179 | ) | |
180 | ); | |
181 | return; | |
182 | } | |
183 | ||
6a488035 TO |
184 | $this->add('text', 'title', ts('Petition Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE); |
185 | ||
186 | $attributes = CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey'); | |
187 | ||
188 | $petitionTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'petition', 'name'); | |
189 | $this->addElement('hidden', 'activity_type_id', $petitionTypeID); | |
190 | ||
191 | // script / instructions / description of petition purpose | |
192 | $this->addWysiwyg('instructions', ts('Introduction'), $attributes['instructions']); | |
193 | ||
194 | // Campaign id | |
195 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('campaign_id', $this->_values)); | |
196 | $this->add('select', 'campaign_id', ts('Campaign'), array('' => ts('- select -')) + $campaigns); | |
197 | ||
198 | $customContactProfiles = CRM_Core_BAO_UFGroup::getProfiles(array('Individual')); | |
199 | // custom group id | |
200 | $this->add('select', 'contact_profile_id', ts('Contact Profile'), | |
201 | array( | |
202 | '' => ts('- select -')) + $customContactProfiles, TRUE | |
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( | |
424392c5 | 209 | '' => ts('- select -') |
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')); | |
215 | $this->addWysiwyg('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 | /** | |
100fef9d | 254 | * Global validation rules for the form |
ad980eb2 DG |
255 | * |
256 | */ | |
00be9182 | 257 | public static function formRule($fields, $files, $form) { |
ad980eb2 DG |
258 | $errors = array(); |
259 | // Petitions should be unique by: title, campaign ID (if assigned) and activity type ID | |
260 | // NOTE: This class is called for both Petition create / update AND for Survey Results tab, but this rule is only for Petition. | |
261 | $where = array('activity_type_id = %1', 'title = %2'); | |
262 | $params = array( | |
263 | 1 => array($fields['activity_type_id'], 'Integer'), | |
264 | 2 => array($fields['title'], 'String'), | |
265 | ); | |
d1401e86 | 266 | $uniqueRuleErrorMessage = ts('This title is already associated with the selected activity type. Please specify a unique title.'); |
ad980eb2 DG |
267 | |
268 | if (empty($fields['campaign_id'])) { | |
269 | $where[] = 'campaign_id IS NULL'; | |
d75f2f47 EM |
270 | } |
271 | else { | |
ad980eb2 | 272 | $where[] = 'campaign_id = %3'; |
d75f2f47 | 273 | $params[3] = array($fields['campaign_id'], 'Integer'); |
d1401e86 | 274 | $uniqueRuleErrorMessage = ts('This title is already associated with the selected campaign and activity type. Please specify a unique title.'); |
ad980eb2 DG |
275 | } |
276 | ||
277 | // Exclude current Petition row if UPDATE. | |
278 | if ($form->_surveyId) { | |
279 | $where[] = 'id != %4'; | |
d75f2f47 | 280 | $params[4] = array($form->_surveyId, 'Integer'); |
ad980eb2 | 281 | } |
d75f2f47 | 282 | |
ad980eb2 DG |
283 | $whereClause = implode(' AND ', $where); |
284 | ||
285 | $query = " | |
286 | SELECT COUNT(*) AS row_count | |
287 | FROM civicrm_survey | |
288 | WHERE $whereClause | |
289 | "; | |
290 | ||
291 | $result = CRM_Core_DAO::singleValueQuery($query, $params); | |
292 | if ($result >= 1) { | |
d1401e86 | 293 | $errors['title'] = $uniqueRuleErrorMessage; |
ad980eb2 DG |
294 | } |
295 | return empty($errors) ? TRUE : $errors; | |
6a488035 TO |
296 | } |
297 | ||
298 | ||
299 | public function postProcess() { | |
300 | // store the submitted values in an array | |
301 | $params = $this->controller->exportValues($this->_name); | |
302 | ||
303 | $session = CRM_Core_Session::singleton(); | |
304 | ||
305 | $params['last_modified_id'] = $session->get('userID'); | |
306 | $params['last_modified_date'] = date('YmdHis'); | |
424392c5 | 307 | $params['is_share'] = CRM_Utils_Array::value('is_share', $params, FALSE); |
6a488035 TO |
308 | |
309 | if ($this->_surveyId) { | |
310 | ||
311 | if ($this->_action & CRM_Core_Action::DELETE) { | |
312 | CRM_Campaign_BAO_Survey::del($this->_surveyId); | |
313 | CRM_Core_Session::setStatus(ts(' Petition has been deleted.'), ts('Record Deleted'), 'success'); | |
314 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition')); | |
315 | return; | |
316 | } | |
317 | ||
318 | $params['id'] = $this->_surveyId; | |
319 | } | |
320 | else { | |
321 | $params['created_id'] = $session->get('userID'); | |
322 | $params['created_date'] = date('YmdHis'); | |
323 | } | |
324 | ||
325 | $params['bypass_confirm'] = CRM_Utils_Array::value('bypass_confirm', $params, 0); | |
326 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0); | |
327 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0); | |
328 | ||
329 | $surveyId = CRM_Campaign_BAO_Survey::create($params); | |
330 | ||
6a488035 TO |
331 | // also update the ProfileModule tables |
332 | $ufJoinParams = array( | |
333 | 'is_active' => 1, | |
334 | 'module' => 'CiviCampaign', | |
335 | 'entity_table' => 'civicrm_survey', | |
336 | 'entity_id' => $surveyId->id, | |
337 | ); | |
338 | ||
339 | // first delete all past entries | |
340 | if ($this->_surveyId) { | |
341 | CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams); | |
342 | } | |
a7488080 | 343 | if (!empty($params['profile_id'])) { |
6a488035 TO |
344 | $ufJoinParams['weight'] = 1; |
345 | $ufJoinParams['uf_group_id'] = $params['profile_id']; | |
346 | CRM_Core_BAO_UFJoin::create($ufJoinParams); | |
347 | } | |
348 | ||
a7488080 | 349 | if (!empty($params['contact_profile_id'])) { |
6a488035 TO |
350 | $ufJoinParams['weight'] = 2; |
351 | $ufJoinParams['uf_group_id'] = $params['contact_profile_id']; | |
352 | CRM_Core_BAO_UFJoin::create($ufJoinParams); | |
353 | } | |
354 | ||
355 | if (!is_a($surveyId, 'CRM_Core_Error')) { | |
356 | CRM_Core_Session::setStatus(ts('Petition has been saved.'), ts('Saved'), 'success'); | |
357 | } | |
358 | ||
359 | $buttonName = $this->controller->getButtonName(); | |
360 | if ($buttonName == $this->getButtonName('next', 'new')) { | |
361 | CRM_Core_Session::setStatus(ts(' You can add another Petition.'), '', 'info'); | |
362 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/petition/add', 'reset=1&action=add')); | |
363 | } | |
364 | else { | |
365 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=petition')); | |
366 | } | |
367 | } | |
368 | } |