3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for adding a petition.
21 class CRM_Campaign_Form_Petition
extends CRM_Core_Form
{
24 * Making this public so we can reference it in the formRule
30 * Explicitly declare the entity api name.
32 public function getDefaultEntity() {
37 * Get the entity id being edited.
41 public function getEntityId() {
42 return $this->_surveyId
;
45 public function preProcess() {
46 if (!CRM_Campaign_BAO_Campaign
::accessCampaign()) {
47 CRM_Utils_System
::permissionDenied();
50 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
52 $this->assign('context', $this->_context
);
54 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
56 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::DELETE
)) {
57 $this->_surveyId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, TRUE);
59 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
60 CRM_Utils_System
::setTitle(ts('Edit Survey'));
63 CRM_Utils_System
::setTitle(ts('Delete Survey'));
67 // Add custom data to form
68 CRM_Custom_Form_CustomData
::addToForm($this);
70 $session = CRM_Core_Session
::singleton();
71 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
72 $session->pushUserContext($url);
74 $this->_values
= $this->get('values');
76 if (!is_array($this->_values
)) {
78 if ($this->_surveyId
) {
79 $params = ['id' => $this->_surveyId
];
80 CRM_Campaign_BAO_Survey
::retrieve($params, $this->_values
);
82 $this->set('values', $this->_values
);
85 $this->assign('action', $this->_action
);
86 $this->assign('surveyId', $this->_surveyId
);
88 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::DELETE
)) {
89 $this->_surveyId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, TRUE);
91 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
92 CRM_Utils_System
::setTitle(ts('Edit Petition'));
95 CRM_Utils_System
::setTitle(ts('Delete Petition'));
99 $session = CRM_Core_Session
::singleton();
100 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=petition');
101 $session->pushUserContext($url);
103 CRM_Utils_System
::appendBreadCrumb([['title' => ts('Petition Dashboard'), 'url' => $url]]);
107 * Set default values for the form. Note that in edit/view mode
108 * the default values are retrieved from the database
111 * array of default values
113 public function setDefaultValues() {
114 $defaults = $this->_values
;
116 $ufContactJoinParams = [
117 'entity_table' => 'civicrm_survey',
118 'entity_id' => $this->_surveyId
,
122 if ($ufContactGroupId = CRM_Core_BAO_UFJoin
::findUFGroupId($ufContactJoinParams)) {
123 $defaults['contact_profile_id'] = $ufContactGroupId;
125 $ufActivityJoinParams = [
126 'entity_table' => 'civicrm_survey',
127 'entity_id' => $this->_surveyId
,
131 if ($ufActivityGroupId = CRM_Core_BAO_UFJoin
::findUFGroupId($ufActivityJoinParams)) {
132 $defaults['profile_id'] = $ufActivityGroupId;
135 if (!isset($defaults['is_active'])) {
136 $defaults['is_active'] = 1;
139 $defaultSurveys = CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE);
140 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
141 $defaults['is_default'] = 1;
147 public function buildQuickForm() {
149 if ($this->_action
& CRM_Core_Action
::DELETE
) {
154 'name' => ts('Delete'),
159 'name' => ts('Cancel'),
166 $this->add('text', 'title', ts('Petition Title'), CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
168 $attributes = CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey');
170 $petitionTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Petition');
171 $this->addElement('hidden', 'activity_type_id', $petitionTypeID);
173 // script / instructions / description of petition purpose
174 $this->add('wysiwyg', 'instructions', ts('Introduction'), $attributes['instructions']);
176 $this->addEntityRef('campaign_id', ts('Campaign'), [
177 'entity' => 'Campaign',
179 'select' => ['minimumInputLength' => 0],
182 $customContactProfiles = CRM_Core_BAO_UFGroup
::getProfiles(['Individual']);
184 $this->add('select', 'contact_profile_id', ts('Contact Profile'),
186 '' => ts('- select -'),
187 ] +
$customContactProfiles, TRUE
190 $customProfiles = CRM_Core_BAO_UFGroup
::getProfiles(['Activity']);
192 $this->add('select', 'profile_id', ts('Activity Profile'),
194 '' => ts('- select -'),
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'));
200 $this->add('wysiwyg', 'thankyou_text', ts('Thank-you Message'), CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'thankyou_text'));
202 // bypass email confirmation?
203 $this->add('checkbox', 'bypass_confirm', ts('Bypass email confirmation'));
205 //is share through social media
206 $this->addElement('checkbox', 'is_share', ts('Add footer region with Twitter, Facebook and LinkedIn share buttons and scripts?'));
209 $this->add('checkbox', 'is_active', ts('Is Active?'));
212 $this->add('checkbox', 'is_default', ts('Is Default?'));
219 'name' => ts('Save'),
224 'name' => ts('Save and New'),
229 'name' => ts('Cancel'),
234 // add a form rule to check default value
235 $this->addFormRule(['CRM_Campaign_Form_Petition', 'formRule'], $this);
239 * Global validation rules for the form.
245 public static function formRule($fields, $files, $form) {
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.
249 $where = ['activity_type_id = %1', 'title = %2'];
251 1 => [$fields['activity_type_id'], 'Integer'],
252 2 => [$fields['title'], 'String'],
254 $uniqueRuleErrorMessage = ts('This title is already associated with the selected activity type. Please specify a unique title.');
256 if (empty($fields['campaign_id'])) {
257 $where[] = 'campaign_id IS NULL';
260 $where[] = 'campaign_id = %3';
261 $params[3] = [$fields['campaign_id'], 'Integer'];
262 $uniqueRuleErrorMessage = ts('This title is already associated with the selected campaign and activity type. Please specify a unique title.');
265 // Exclude current Petition row if UPDATE.
266 if ($form->_surveyId
) {
267 $where[] = 'id != %4';
268 $params[4] = [$form->_surveyId
, 'Integer'];
271 $whereClause = implode(' AND ', $where);
274 SELECT COUNT(*) AS row_count
279 $result = CRM_Core_DAO
::singleValueQuery($query, $params);
281 $errors['title'] = $uniqueRuleErrorMessage;
283 return empty($errors) ?
TRUE : $errors;
286 public function postProcess() {
287 // store the submitted values in an array
288 $params = $this->controller
->exportValues($this->_name
);
290 $session = CRM_Core_Session
::singleton();
292 $params['last_modified_id'] = $session->get('userID');
293 $params['last_modified_date'] = date('YmdHis');
294 $params['is_share'] = CRM_Utils_Array
::value('is_share', $params, FALSE);
296 if ($this->_surveyId
) {
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'));
305 $params['id'] = $this->_surveyId
;
308 $params['created_id'] = $session->get('userID');
309 $params['created_date'] = date('YmdHis');
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);
316 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params, $this->getEntityId(), $this->getDefaultEntity());
318 $surveyId = CRM_Campaign_BAO_Survey
::create($params);
320 // also update the ProfileModule tables
323 'module' => 'CiviCampaign',
324 'entity_table' => 'civicrm_survey',
325 'entity_id' => $surveyId->id
,
328 // first delete all past entries
329 if ($this->_surveyId
) {
330 CRM_Core_BAO_UFJoin
::deleteAll($ufJoinParams);
332 if (!empty($params['profile_id'])) {
333 $ufJoinParams['weight'] = 1;
334 $ufJoinParams['uf_group_id'] = $params['profile_id'];
335 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
338 if (!empty($params['contact_profile_id'])) {
339 $ufJoinParams['weight'] = 2;
340 $ufJoinParams['uf_group_id'] = $params['contact_profile_id'];
341 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
344 if (!is_a($surveyId, 'CRM_Core_Error')) {
345 CRM_Core_Session
::setStatus(ts('Petition has been saved.'), ts('Saved'), 'success');
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'));
354 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=petition'));