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() {
24 $this->setSelectedChild('settings');
28 * Set default values for the form.
30 public function setDefaultValues() {
31 $defaults = parent
::setDefaultValues();
32 // @todo handle properly on parent.
34 $defaults['start_date'] = date('Y-m-d H:i:s');
35 unset($defaults['start_time']);
37 $soft_credit_types = CRM_Core_OptionGroup
::values('soft_credit_type', TRUE, FALSE, FALSE, NULL, 'name');
40 $title = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage',
44 $this->setTitle(ts('Title and Settings') . " ($title)");
46 foreach (['on_behalf', 'soft_credit'] as $module) {
47 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
48 $ufJoinDAO->module
= $module;
49 $ufJoinDAO->entity_id
= $this->_id
;
50 $ufJoinDAO->entity_table
= 'civicrm_contribution_page';
51 if ($ufJoinDAO->find(TRUE)) {
52 $jsonData = CRM_Contribute_BAO_ContributionPage
::formatModuleData($ufJoinDAO->module_data
, TRUE, $module);
53 if ($module == 'soft_credit') {
54 $defaults['honoree_profile'] = $ufJoinDAO->uf_group_id
;
55 $defaults = array_merge($defaults, $jsonData);
56 $defaults['honor_block_is_active'] = $ufJoinDAO->is_active
;
59 $defaults['onbehalf_profile_id'] = $ufJoinDAO->uf_group_id
;
60 $defaults = array_merge($defaults, $jsonData);
61 $defaults['is_organization'] = $ufJoinDAO->is_active
;
65 if ($module == 'soft_credit') {
66 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
67 $ufGroupDAO->name
= 'honoree_individual';
68 if ($ufGroupDAO->find(TRUE)) {
69 $defaults['honoree_profile'] = $ufGroupDAO->id
;
71 $defaults['soft_credit_types'] = [
72 CRM_Utils_Array
::value('in_honor_of', $soft_credit_types),
73 CRM_Utils_Array
::value('in_memory_of', $soft_credit_types),
77 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
78 $ufGroupDAO->name
= 'on_behalf_organization';
79 if ($ufGroupDAO->find(TRUE)) {
80 $defaults['onbehalf_profile_id'] = $ufGroupDAO->id
;
82 $defaults['for_organization'] = ts('I am contributing on behalf of an organization.');
83 $defaults['is_for_organization'] = 1;
89 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
90 $ufGroupDAO->name
= 'honoree_individual';
91 if ($ufGroupDAO->find(TRUE)) {
92 $defaults['honoree_profile'] = $ufGroupDAO->id
;
94 $defaults['soft_credit_types'] = [
95 CRM_Utils_Array
::value('in_honor_of', $soft_credit_types),
96 CRM_Utils_Array
::value('in_memory_of', $soft_credit_types),
104 * Build the form object.
106 public function buildQuickForm() {
108 $this->_first
= TRUE;
109 $attributes = CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_ContributionPage');
112 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, CRM_Core_Action
::ADD
);
113 $financialOptions = [
114 'options' => $financialTypes,
116 if (!CRM_Core_Permission
::check('administer CiviCRM Financial Types')) {
117 $financialOptions['context'] = 'search';
119 $this->addSelect('financial_type_id', $financialOptions, TRUE);
122 $this->add('text', 'title', ts('Title'), $attributes['title'], TRUE);
123 $this->addField('contribution_page_frontend_title', ['entity' => 'ContributionPage']);
125 //CRM-7362 --add campaigns.
126 CRM_Campaign_BAO_Campaign
::addCampaign($this, CRM_Utils_Array
::value('campaign_id', $this->_values
));
128 $this->add('wysiwyg', 'intro_text', ts('Introductory Message'), $attributes['intro_text']);
130 $this->add('wysiwyg', 'footer_text', ts('Footer Message'), $attributes['footer_text']);
132 //Register schema which will be used for OnBehalOf and HonorOf profile Selector
133 CRM_UF_Page_ProfileEditor
::registerSchemas(['OrganizationModel', 'HouseholdModel']);
135 // is on behalf of an organization ?
136 $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);"]);
138 //CRM-15787 - If applicable, register 'membership_1'
139 $member = CRM_Member_BAO_Membership
::getMembershipBlock($this->_id
);
140 $coreTypes = ['Contact', 'Organization'];
143 'entity_name' => ['contact_1'],
144 'entity_type' => 'OrganizationModel',
147 if ($member && $member['is_active']) {
148 $coreTypes[] = 'Membership';
150 'entity_name' => ['membership_1'],
151 'entity_type' => 'MembershipModel',
155 $allowCoreTypes = array_merge($coreTypes, CRM_Contact_BAO_ContactType
::subTypes('Organization'));
158 $this->addProfileSelector('onbehalf_profile_id', ts('Organization Profile'), $allowCoreTypes, $allowSubTypes, $entities);
160 $this->addRadio('is_for_organization', '', [1 => ts('Optional'), 2 => ts('Required')]);
161 $this->add('textarea', 'for_organization', ts('On behalf of Label'), ['rows' => 2, 'cols' => 50]);
163 // collect goal amount
164 $this->add('text', 'goal_amount', ts('Goal Amount'), ['size' => 8, 'maxlength' => 12]);
165 $this->addRule('goal_amount', ts('Please enter a valid money value (e.g. %1).', [1 => CRM_Utils_Money
::formatLocaleNumericRoundedForDefaultCurrency('99.99')]), 'money');
167 // is confirmation page enabled?
168 $this->addElement('checkbox', 'is_confirm_enabled', ts('Use a confirmation page?'));
170 // is this page shareable through social media ?
171 $this->addElement('checkbox', 'is_share', ts('Add footer region with Twitter, Facebook and LinkedIn share buttons and scripts?'));
173 // is this page active ?
174 $this->addElement('checkbox', 'is_active', ts('Is this Online Contribution Page Active?'));
176 // should the honor be enabled
177 $this->addElement('checkbox', 'honor_block_is_active', ts('Honoree Section Enabled'), NULL, ['onclick' => "showHonor()"]);
179 $this->add('text', 'honor_block_title', ts('Honoree Section Title'), ['maxlength' => 255, 'size' => 45]);
181 $this->add('textarea', 'honor_block_text', ts('Honoree Introductory Message'), ['rows' => 2, 'cols' => 50]);
183 $this->addSelect('soft_credit_types', [
184 'label' => ts('Honor Types'),
185 'entity' => 'ContributionSoft',
186 'field' => 'soft_credit_type_id',
193 'entity_name' => 'contact_1',
194 'entity_type' => 'IndividualModel',
198 $allowCoreTypes = array_merge([
203 ], CRM_Contact_BAO_ContactType
::subTypes('Individual'));
206 $this->addProfileSelector('honoree_profile', ts('Honoree Profile'), $allowCoreTypes, $allowSubTypes, $entities);
208 if (!empty($this->_submitValues
['honor_block_is_active'])) {
209 $this->addRule('soft_credit_types', ts('At least one value must be selected if Honor Section is active'), 'required');
210 $this->addRule('honoree_profile', ts('Please select a profile used for honoree'), 'required');
213 // add optional start and end dates
214 $this->add('datepicker', 'start_date', ts('Start Date'));
215 $this->add('datepicker', 'end_date', ts('End Date'));
217 $this->addFormRule(['CRM_Contribute_Form_ContributionPage_Settings', 'formRule'], $this);
219 parent
::buildQuickForm();
223 * Global validation rules for the form.
225 * @param array $values
226 * Posted values of the form.
232 * list of errors to be posted back to the form
234 public static function formRule($values, $files, $self) {
236 $contributionPageId = $self->_id
;
238 if (strstr($values['title'], '/')) {
239 $errors['title'] = ts("Please do not use '/' in Title");
242 // ensure on-behalf-of profile meets minimum requirements
243 if (!empty($values['is_organization'])) {
244 if (empty($values['onbehalf_profile_id'])) {
245 $errors['onbehalf_profile_id'] = ts('Please select a profile to collect organization information on this contribution page.');
248 $requiredProfileFields = ['organization_name', 'email'];
249 if (!CRM_Core_BAO_UFGroup
::checkValidProfile($values['onbehalf_profile_id'], $requiredProfileFields)) {
250 $errors['onbehalf_profile_id'] = ts('Profile does not contain the minimum required fields for an On Behalf Of Organization');
256 $start = CRM_Utils_Date
::processDate($values['start_date']);
257 $end = CRM_Utils_Date
::processDate($values['end_date']);
258 if (($end < $start) && ($end != 0)) {
259 $errors['end_date'] = ts('End date should be after Start date.');
262 if (!empty($self->_values
['payment_processor']) && $financialType = CRM_Contribute_BAO_Contribution
::validateFinancialType($values['financial_type_id'])) {
263 $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType;
266 //dont allow on behalf of save when
267 //pre or post profile consists of membership fields
268 if ($contributionPageId && !empty($values['is_organization'])) {
270 'module' => 'CiviContribute',
271 'entity_table' => 'civicrm_contribution_page',
272 'entity_id' => $contributionPageId,
275 list($contributionProfiles['custom_pre_id'],
276 $contributionProfiles['custom_post_id']
277 ) = CRM_Core_BAO_UFJoin
::getUFGroupIds($ufJoinParams);
279 $conProfileType = NULL;
280 if ($contributionProfiles['custom_pre_id']) {
281 $preProfileType = CRM_Core_BAO_UFField
::getProfileType($contributionProfiles['custom_pre_id']);
282 if ($preProfileType == 'Membership') {
283 $conProfileType = "'Includes Profile (top of page)'";
287 if ($contributionProfiles['custom_post_id']) {
288 $postProfileType = CRM_Core_BAO_UFField
::getProfileType($contributionProfiles['custom_post_id']);
289 if ($postProfileType == 'Membership') {
290 $conProfileType = empty($conProfileType) ?
"'Includes Profile (bottom of page)'" : "{$conProfileType} and 'Includes Profile (bottom of page)'";
293 if (!empty($conProfileType)) {
294 $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]);
303 public function postProcess() {
304 // get the submitted form values.
305 $params = $this->controller
->exportValues($this->_name
);
307 // we do this in case the user has hit the forward/back button
309 $params['id'] = $this->_id
;
312 $session = CRM_Core_Session
::singleton();
313 $params['created_id'] = $session->get('userID');
314 $params['created_date'] = date('YmdHis');
315 $config = CRM_Core_Config
::singleton();
316 $params['currency'] = $config->defaultCurrency
;
319 $params['is_confirm_enabled'] = CRM_Utils_Array
::value('is_confirm_enabled', $params, FALSE);
320 $params['is_share'] = CRM_Utils_Array
::value('is_share', $params, FALSE);
321 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, FALSE);
322 $params['is_credit_card_only'] = CRM_Utils_Array
::value('is_credit_card_only', $params, FALSE);
323 $params['honor_block_is_active'] = CRM_Utils_Array
::value('honor_block_is_active', $params, FALSE);
324 $params['is_for_organization'] = !empty($params['is_organization']) ? CRM_Utils_Array
::value('is_for_organization', $params, FALSE) : 0;
325 $params['goal_amount'] = CRM_Utils_Rule
::cleanMoney($params['goal_amount']);
327 if (!$params['honor_block_is_active']) {
328 $params['honor_block_title'] = NULL;
329 $params['honor_block_text'] = NULL;
332 $dao = CRM_Contribute_BAO_ContributionPage
::create($params);
335 'is_organization' => [
336 'module' => 'on_behalf',
337 'entity_table' => 'civicrm_contribution_page',
338 'entity_id' => $dao->id
,
340 'honor_block_is_active' => [
341 'module' => 'soft_credit',
342 'entity_table' => 'civicrm_contribution_page',
343 'entity_id' => $dao->id
,
347 foreach ($ufJoinParams as $index => $ufJoinParam) {
348 if (!empty($params[$index])) {
349 // Look for an existing entry
350 $ufJoinDAO = new CRM_Core_DAO_UFJoin();
351 $ufJoinDAO->module
= $ufJoinParam['module'];
352 $ufJoinDAO->entity_table
= 'civicrm_contribution_page';
353 $ufJoinDAO->entity_id
= $ufJoinParam['entity_id'];
354 $ufJoinDAO->find(TRUE);
356 if (!empty($ufJoinDAO->id
)) {
357 $ufJoinParam['id'] = $ufJoinDAO->id
;
360 $ufJoinParam['uf_group_id'] = $params[$index];
361 $ufJoinParam['weight'] = 1;
362 $ufJoinParam['is_active'] = 1;
363 if ($index == 'honor_block_is_active') {
364 $ufJoinParam['uf_group_id'] = $params['honoree_profile'];
365 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage
::formatModuleData($params, FALSE, 'soft_credit');
368 $ufJoinParam['uf_group_id'] = $params['onbehalf_profile_id'];
369 $ufJoinParam['module_data'] = CRM_Contribute_BAO_ContributionPage
::formatModuleData($params, FALSE, 'on_behalf');
371 CRM_Core_BAO_UFJoin
::create($ufJoinParam);
374 if ($index == 'honor_block_is_active') {
375 $params['honor_block_title'] = NULL;
376 $params['honor_block_text'] = NULL;
379 $params['for_organization'] = NULL;
382 //On subsequent honor_block_is_active uncheck, disable(don't delete)
383 //that particular honoree profile entry in UFjoin table, CRM-13981
384 $ufId = CRM_Core_BAO_UFJoin
::findJoinEntryId($ufJoinParam);
386 $ufJoinParam['uf_group_id'] = CRM_Core_BAO_UFJoin
::findUFGroupId($ufJoinParam);
387 $ufJoinParam['is_active'] = 0;
388 CRM_Core_BAO_UFJoin
::create($ufJoinParam);
393 $this->set('id', $dao->id
);
394 if ($this->_action
& CRM_Core_Action
::ADD
) {
395 $url = 'civicrm/admin/contribute/amount';
396 $urlParams = "action=update&reset=1&id={$dao->id}";
397 // special case for 'Save and Done' consistency.
398 if ($this->controller
->getButtonName('submit') == '_qf_Amount_upload_done') {
399 $url = 'civicrm/admin/contribute';
400 $urlParams = 'reset=1';
401 CRM_Core_Session
::setStatus(ts("'%1' information has been saved.",
402 [1 => $this->getTitle()]
403 ), ts('Saved'), 'success');
406 CRM_Utils_System
::redirect(CRM_Utils_System
::url($url, $urlParams));
408 parent
::endPostProcess();
412 * Return a descriptive name for the page, used in wizard header
416 public function getTitle() {
417 return ts('Title and Settings');