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
17 class CRM_Contribute_Form_ContributionPage_Settings
extends CRM_Contribute_Form_ContributionPage
{
20 * Set variables up before form is built.
22 public function preProcess() {
27 * Set default values for the form.
29 public function setDefaultValues() {
30 $defaults = parent
::setDefaultValues();
31 // @todo handle properly on parent.
33 $defaults['start_date'] = date('Y-m-d H:i:s');
34 unset($defaults['start_time']);
36 $soft_credit_types = CRM_Core_OptionGroup
::values('soft_credit_type', TRUE, FALSE, FALSE, NULL, 'name');
39 $title = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage',
43 CRM_Utils_System
::setTitle(ts('Title and Settings') . " ($title)");
45 foreach (['on_behalf', 'soft_credit'] as $module) {
46 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
47 $ufJoinDAO->module
= $module;
48 $ufJoinDAO->entity_id
= $this->_id
;
49 $ufJoinDAO->entity_table
= 'civicrm_contribution_page';
50 if ($ufJoinDAO->find(TRUE)) {
51 $jsonData = CRM_Contribute_BAO_ContributionPage
::formatModuleData($ufJoinDAO->module_data
, TRUE, $module);
52 if ($module == 'soft_credit') {
53 $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id
;
54 $defaults = array_merge($defaults, $jsonData);
55 $defaults['honor_block_is_active'] = $ufJoinDAO->is_active
;
58 $defaults['onbehalf_profile_id'] = $ufJoinDAO->uf_group_id
;
59 $defaults = array_merge($defaults, $jsonData);
60 $defaults['is_organization'] = $ufJoinDAO->is_active
;
64 if ($module == 'soft_credit') {
65 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
66 $ufGroupDAO->name
= 'honoree_individual';
67 if ($ufGroupDAO->find(TRUE)) {
68 $defaults['honoree_profile'] = $ufGroupDAO->id
;
70 $defaults['soft_credit_types'] = [
71 CRM_Utils_Array
::value('in_honor_of', $soft_credit_types),
72 CRM_Utils_Array
::value('in_memory_of', $soft_credit_types),
76 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
77 $ufGroupDAO->name
= 'on_behalf_organization';
78 if ($ufGroupDAO->find(TRUE)) {
79 $defaults['onbehalf_profile_id'] = $ufGroupDAO->id
;
81 $defaults['for_organization'] = ts('I am contributing on behalf of an organization.');
82 $defaults['is_for_organization'] = 1;
88 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
89 $ufGroupDAO->name
= 'honoree_individual';
90 if ($ufGroupDAO->find(TRUE)) {
91 $defaults['honoree_profile'] = $ufGroupDAO->id
;
93 $defaults['soft_credit_types'] = [
94 CRM_Utils_Array
::value('in_honor_of', $soft_credit_types),
95 CRM_Utils_Array
::value('in_memory_of', $soft_credit_types),
103 * Build the form object.
105 public function buildQuickForm() {
107 $this->_first
= TRUE;
108 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_ContributionPage');
111 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::ADD
);
112 $financialOptions = [
113 'options' => $financialTypes,
115 if (!CRM_Core_Permission
::check('administer CiviCRM Financial Types')) {
116 $financialOptions['context'] = 'search';
118 $this->addSelect('financial_type_id', $financialOptions, TRUE);
121 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
122 $this->addField('contribution_page_frontend_title', ['entity' => 'ContributionPage']);
124 //CRM-7362 --add campaigns.
125 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
127 $this->add('wysiwyg', 'intro_text', ts('Introductory Message'), $attributes['intro_text']);
129 $this->add('wysiwyg', 'footer_text', ts('Footer Message'), $attributes['footer_text']);
131 //Register schema which will be used for OnBehalOf and HonorOf profile Selector
132 CRM_UF_Page_ProfileEditor
::registerSchemas(['OrganizationModel', 'HouseholdModel']);
134 // is on behalf of an organization ?
135 $this->addElement('checkbox', 'is_organization', ts('Allow individuals to contribute and / or signup for membership on behalf of an organization?'), NULL, ['onclick' => "showHideByValue('is_organization',true,'for_org_text','table-row','radio',false);showHideByValue('is_organization',true,'for_org_option','table-row','radio',false);"]);
137 //CRM-15787 - If applicable, register 'membership_1'
138 $member = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
139 $coreTypes = ['Contact', 'Organization'];
142 'entity_name' => ['contact_1'],
143 'entity_type' => 'OrganizationModel',
146 if ($member && $member['is_active']) {
147 $coreTypes[] = 'Membership';
149 'entity_name' => ['membership_1'],
150 'entity_type' => 'MembershipModel',
154 $allowCoreTypes = array_merge($coreTypes, CRM_Contact_BAO_ContactType
::subTypes('Organization'));
157 $this->addProfileSelector('onbehalf_profile_id', ts('Organization Profile'), $allowCoreTypes, $allowSubTypes, $entities);
160 $options[] = $this->createElement('radio', NULL, NULL, ts('Optional'), 1);
161 $options[] = $this->createElement('radio', NULL, NULL, ts('Required'), 2);
162 $this->addGroup($options, 'is_for_organization', '');
163 $this->add('textarea', 'for_organization', ts('On behalf of Label'), ['rows' => 2, 'cols' => 50]);
165 // collect goal amount
166 $this->add('text', 'goal_amount', ts('Goal Amount'), ['size' => 8, 'maxlength' => 12]);
167 $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money
::format('99.99', ' ')]), 'money');
169 // is confirmation page enabled?
170 $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?'));
172 // is this page shareable through social media ?
173 $this->addElement('checkbox', 'is_share', ts('Allow sharing through social media?'));
175 // is this page active ?
176 $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?'));
178 // should the honor be enabled
179 $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, ['onclick' => "showHonor()"]);
181 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), ['maxlength' => 255, 'size' => 45]);
183 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), ['rows' => 2, 'cols' => 50]);
185 $this->addSelect('soft_credit_types', [
186 'label' => ts('Honor Types'),
187 'entity' => 'ContributionSoft',
188 'field' => 'soft_credit_type_id',
195 'entity_name' => 'contact_1',
196 'entity_type' => 'IndividualModel',
200 $allowCoreTypes = array_merge([
205 ], CRM_Contact_BAO_ContactType
::subTypes('Individual'));
208 $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
210 if (!empty($this->_submitValues
['honor_block_is_active'])) {
211 $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required');
212 $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required');
215 // add optional start and end dates
216 $this->add('datepicker', 'start_date', ts('Start Date'));
217 $this->add('datepicker', 'end_date', ts('End Date'));
219 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Settings', 'formRule'], $this);
221 parent
::buildQuickForm();
225 * Global validation rules for the form.
227 * @param array $values
228 * Posted values of the form.
234 * list of errors to be posted back to the form
236 public static function formRule($values, $files, $self) {
238 $contributionPageId = $self->_id
;
240 if (strstr($values['title'], '/')) {
241 $errors['title'] = ts("Please do not use '/' in Title");
244 // ensure on-behalf-of profile meets minimum requirements
245 if (!empty($values['is_organization'])) {
246 if (empty($values['onbehalf_profile_id'])) {
247 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
250 $requiredProfileFields = ['organization_name', 'email'];
251 if (!CRM_Core_BAO_UFGroup
::checkValidProfile($values['onbehalf_profile_id'], $requiredProfileFields)) {
252 $errors['onbehalf_profile_id'] = ts('Profile does not contain the minimum required fields for an On Behalf Of Organization');
258 $start = CRM_Utils_Date
::processDate($values['start_date']);
259 $end = CRM_Utils_Date
::processDate($values['end_date']);
260 if (($end < $start) && ($end != 0)) {
261 $errors['end_date'] = ts('End date should be after Start date.');
264 if (!empty($self->_values
['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution
::validateFinancialType($values['financial_type_id'])) {
265 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
268 //dont allow on behalf of save when
269 //pre or post profile consists of membership fields
270 if ($contributionPageId && !empty($values['is_organization'])) {
272 'module' => 'CiviContribute',
273 'entity_table' => 'civicrm_contribution_page',
274 'entity_id' => $contributionPageId,
277 list($contributionProfiles['custom_pre_id'],
278 $contributionProfiles['custom_post_id']
279 ) = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
281 $conProfileType = NULL;
282 if ($contributionProfiles['custom_pre_id']) {
283 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($contributionProfiles['custom_pre_id']);
284 if ($preProfileType == 'Membership') {
285 $conProfileType = "'Includes Profile (top of page)'";
289 if ($contributionProfiles['custom_post_id']) {
290 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($contributionProfiles['custom_post_id']);
291 if ($postProfileType == 'Membership') {
292 $conProfileType = empty($conProfileType) ?
"'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'";
295 if (!empty($conProfileType)) {
296 $errors['is_organization'] = ts("You should move the membership related fields configured in %1 to the 'On Behalf' profile for this Contribution Page", [1 => $conProfileType]);
305 public function postProcess() {
306 // get the submitted form values.
307 $params = $this->controller
->exportValues($this->_name
);
309 // we do this in case the user has hit the forward/back button
311 $params['id'] = $this->_id
;
314 $session = CRM_Core_Session
::singleton();
315 $params['created_id'] = $session->get('userID');
316 $params['created_date'] = date('YmdHis');
317 $config = CRM_Core_Config
::singleton();
318 $params['currency'] = $config->defaultCurrency
;
321 $params['is_confirm_enabled'] = CRM_Utils_Array
::value('is_confirm_enabled', $params, FALSE);
322 $params['is_share'] = CRM_Utils_Array
::value('is_share', $params, FALSE);
323 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
324 $params['is_credit_card_only'] = CRM_Utils_Array
::value('is_credit_card_only', $params, FALSE);
325 $params['honor_block_is_active'] = CRM_Utils_Array
::value('honor_block_is_active', $params, FALSE);
326 $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array
::value('is_for_organization', $params, FALSE) : 0;
327 $params['goal_amount'] = CRM_Utils_Rule
::cleanMoney($params['goal_amount']);
329 if (!$params['honor_block_is_active']) {
330 $params['honor_block_title'] = NULL;
331 $params['honor_block_text'] = NULL;
334 $dao = CRM_Contribute_BAO_ContributionPage
::create($params);
337 'is_organization' => [
338 'module' => 'on_behalf',
339 'entity_table' => 'civicrm_contribution_page',
340 'entity_id' => $dao->id
,
342 'honor_block_is_active' => [
343 'module' => 'soft_credit',
344 'entity_table' => 'civicrm_contribution_page',
345 'entity_id' => $dao->id
,
349 foreach ($ufJoinParams as $index => $ufJoinParam) {
350 if (!empty($params[$index])) {
351 // first delete all past entries
352 CRM_Core_BAO_UFJoin
::deleteAll($ufJoinParam);
353 $ufJoinParam['uf_group_id'] = $params[$index];
354 $ufJoinParam['weight'] = 1;
355 $ufJoinParam['is_active'] = 1;
356 if ($index == 'honor_block_is_active') {
357 $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
358 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage
::formatModuleData($params, FALSE, 'soft_credit');
361 $ufJoinParam['uf_group_id'] = $params['onbehalf_profile_id'];
362 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage
::formatModuleData($params, FALSE, 'on_behalf');
364 CRM_Core_BAO_UFJoin
::create($ufJoinParam);
367 if ($index == 'honor_block_is_active') {
368 $params['honor_block_title'] = NULL;
369 $params['honor_block_text'] = NULL;
372 $params['for_organization'] = NULL;
375 //On subsequent honor_block_is_active uncheck, disable(don't delete)
376 //that particular honoree profile entry in UFjoin table, CRM-13981
377 $ufId = CRM_Core_BAO_UFJoin
::findJoinEntryId($ufJoinParam);
379 $ufJoinParam['uf_group_id'] = CRM_Core_BAO_UFJoin
::findUFGroupId($ufJoinParam);
380 $ufJoinParam['is_active'] = 0;
381 CRM_Core_BAO_UFJoin
::create($ufJoinParam);
386 $this->set('id', $dao->id
);
387 if ($this->_action
& CRM_Core_Action
::ADD
) {
388 $url = 'civicrm/admin/contribute/amount';
389 $urlParams = "action=update&reset=1&id={$dao->id}";
390 // special case for 'Save and Done' consistency.
391 if ($this->controller
->getButtonName('submit') == '_qf_Amount_upload_done') {
392 $url = 'civicrm/admin/contribute';
393 $urlParams = 'reset=1';
394 CRM_Core_Session
::setStatus(ts("'%1' information has been saved.",
395 [1 => $this->getTitle()]
396 ), ts('Saved'), 'success');
399 CRM_Utils_System
::redirect(CRM_Utils_System
::url($url, $urlParams));
401 parent
::endPostProcess();
405 * Return a descriptive name for the page, used in wizard header
409 public function getTitle() {
410 return ts('Title and Settings');