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