3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class generates form components for adding a petition
40 class CRM_Campaign_Form_Petition
extends CRM_Core_Form
{
42 public function preProcess() {
43 if (!CRM_Campaign_BAO_Campaign
::accessCampaign()) {
44 CRM_Utils_System
::permissionDenied();
47 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
49 $this->assign('context', $this->_context
);
51 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
53 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::DELETE
)) {
54 $this->_surveyId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, TRUE);
56 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
57 CRM_Utils_System
::setTitle(ts('Edit Survey'));
60 CRM_Utils_System
::setTitle(ts('Delete Survey'));
64 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
65 $this->assign('cdType', FALSE);
67 $this->assign('cdType', TRUE);
68 return CRM_Custom_Form_CustomData
::preProcess($this);
71 // when custom data is included in this page
72 if (CRM_Utils_Array
::value('hidden_custom', $_POST)) {
73 CRM_Custom_Form_CustomData
::preProcess($this);
74 CRM_Custom_Form_CustomData
::buildQuickForm($this);
77 $session = CRM_Core_Session
::singleton();
78 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
79 $session->pushUserContext($url);
81 $this->_values
= $this->get('values');
83 if (!is_array($this->_values
)) {
84 $this->_values
= array();
85 if ($this->_surveyId
) {
86 $params = array('id' => $this->_surveyId
);
87 CRM_Campaign_BAO_Survey
::retrieve($params, $this->_values
);
89 $this->set('values', $this->_values
);
92 $this->assign('action', $this->_action
);
93 $this->assign('surveyId', $this->_surveyId
);
95 $this->assign('entityID', $this->_surveyId
);
97 if ($this->_action
& (CRM_Core_Action
::UPDATE | CRM_Core_Action
::DELETE
)) {
98 $this->_surveyId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, TRUE);
100 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
101 CRM_Utils_System
::setTitle(ts('Edit Petition'));
104 CRM_Utils_System
::setTitle(ts('Delete Petition'));
108 $session = CRM_Core_Session
::singleton();
109 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=petition');
110 $session->pushUserContext($url);
112 CRM_Utils_System
::appendBreadCrumb(array(array('title' => ts('Petition Dashboard'), 'url' => $url)));
116 * This function sets the default values for the form. Note that in edit/view mode
117 * the default values are retrieved from the database
121 * @return array array of default values
124 function setDefaultValues() {
125 $defaults = $this->_values
;
127 $ufJoinParams = array(
128 'entity_table' => 'civicrm_survey',
129 'entity_id' => $this->_surveyId
,
133 if ($ufGroupId = CRM_Core_BAO_UFJoin
::findUFGroupId($ufJoinParams)) {
134 $defaults['contact_profile_id'] = $ufGroupId;
137 if (!isset($defaults['is_active'])) {
138 $defaults['is_active'] = 1;
141 $defaultSurveys = CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE);
142 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
143 $defaults['is_default'] = 1;
150 public function buildQuickForm() {
152 if ($this->_action
& CRM_Core_Action
::DELETE
) {
154 $this->addButtons(array(
157 'name' => ts('Delete'),
162 'name' => ts('Cancel'),
170 $this->add('text', 'title', ts('Petition Title'), CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
172 $attributes = CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey');
174 $petitionTypeID = CRM_Core_OptionGroup
::getValue('activity_type', 'petition', 'name');
175 $this->addElement('hidden', 'activity_type_id', $petitionTypeID);
177 // script / instructions / description of petition purpose
178 $this->addWysiwyg('instructions', ts('Introduction'), $attributes['instructions']);
181 $campaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(CRM_Utils_Array
::value('campaign_id', $this->_values
));
182 $this->add('select', 'campaign_id', ts('Campaign'), array('' => ts('- select -')) +
$campaigns);
184 $customContactProfiles = CRM_Core_BAO_UFGroup
::getProfiles(array('Individual'));
186 $this->add('select', 'contact_profile_id', ts('Contact Profile'),
188 '' => ts('- select -')) +
$customContactProfiles, TRUE
191 $customProfiles = CRM_Core_BAO_UFGroup
::getProfiles(array('Activity'));
193 $this->add('select', 'profile_id', ts('Activity Profile'),
195 '' => ts('- select -')) +
$customProfiles
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->addWysiwyg('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'));
206 $this->add('checkbox', 'is_active', ts('Is Active?'));
209 $this->add('checkbox', 'is_default', ts('Is Default?'));
212 $this->addButtons(array(
215 'name' => ts('Save'),
220 'name' => ts('Save and New'),
225 'name' => ts('Cancel'),
230 // add a form rule to check default value
231 $this->addFormRule(array('CRM_Campaign_Form_Survey_Results', 'formRule'), $this);
235 public function postProcess() {
236 // store the submitted values in an array
237 $params = $this->controller
->exportValues($this->_name
);
239 $session = CRM_Core_Session
::singleton();
241 $params['last_modified_id'] = $session->get('userID');
242 $params['last_modified_date'] = date('YmdHis');
244 if ($this->_surveyId
) {
246 if ($this->_action
& CRM_Core_Action
::DELETE
) {
247 CRM_Campaign_BAO_Survey
::del($this->_surveyId
);
248 CRM_Core_Session
::setStatus(ts(' Petition has been deleted.'), ts('Record Deleted'), 'success');
249 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=petition'));
253 $params['id'] = $this->_surveyId
;
256 $params['created_id'] = $session->get('userID');
257 $params['created_date'] = date('YmdHis');
260 $params['bypass_confirm'] = CRM_Utils_Array
::value('bypass_confirm', $params, 0);
261 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, 0);
262 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, 0);
264 $surveyId = CRM_Campaign_BAO_Survey
::create($params);
267 // also update the ProfileModule tables
268 $ufJoinParams = array(
270 'module' => 'CiviCampaign',
271 'entity_table' => 'civicrm_survey',
272 'entity_id' => $surveyId->id
,
275 // first delete all past entries
276 if ($this->_surveyId
) {
277 CRM_Core_BAO_UFJoin
::deleteAll($ufJoinParams);
279 if (CRM_Utils_Array
::value('profile_id', $params)) {
280 $ufJoinParams['weight'] = 1;
281 $ufJoinParams['uf_group_id'] = $params['profile_id'];
282 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
285 if (CRM_Utils_Array
::value('contact_profile_id', $params)) {
286 $ufJoinParams['weight'] = 2;
287 $ufJoinParams['uf_group_id'] = $params['contact_profile_id'];
288 CRM_Core_BAO_UFJoin
::create($ufJoinParams);
291 if (!is_a($surveyId, 'CRM_Core_Error')) {
292 CRM_Core_Session
::setStatus(ts('Petition has been saved.'), ts('Saved'), 'success');
295 $buttonName = $this->controller
->getButtonName();
296 if ($buttonName == $this->getButtonName('next', 'new')) {
297 CRM_Core_Session
::setStatus(ts(' You can add another Petition.'), '', 'info');
298 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/petition/add', 'reset=1&action=add'));
301 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=petition'));