Merge pull request #22484 from civicrm/5.46
[civicrm-core.git] / CRM / Admin / Form / Preferences / Contribute.php
CommitLineData
d75f2f47 1<?php
c73475ea
WA
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
c73475ea 5 | |
bc77d7c0
TO
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 |
c73475ea
WA
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
c73475ea 16 */
9d4da082
RK
17
18/**
ce064e4f 19 * This class generates form components for the display preferences.
9d4da082
RK
20 */
21class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences {
ce064e4f 22
1be9403d 23 /**
2f6c641a 24 * Our standards for settings are to have a setting per value with defined metadata.
25 *
26 * Unfortunately the 'contribution_invoice_settings' has been added in non-compliance.
27 * We use this array to hack-handle.
28 *
71bbdae1 29 * These are now stored as individual settings but this form still does weird & wonderful things.
30 *
31 * Note the 'real' settings on this form are added via metadata definition - ie
32 * 'settings_pages' => ['contribute' => ['weight' => 1]], in their metadata.
2f6c641a 33 *
34 * @var array
1be9403d 35 */
2f6c641a 36 protected $invoiceSettings = [];
d75f2f47 37
9d4da082 38 /**
eceb18cc 39 * Build the form object.
71bbdae1 40 *
41 * @throws \CiviCRM_API3_Exception
42 * @throws \CRM_Core_Exception
9d4da082 43 */
00be9182 44 public function buildQuickForm() {
2f6c641a 45 parent::buildQuickForm();
2f6c641a 46 $this->invoiceSettings = [
47 'invoice_prefix' => [
48 'html_type' => 'text',
49 'title' => ts('Invoice Prefix'),
50 'weight' => 1,
51 'description' => ts('Enter prefix to be display on PDF for invoice'),
52 ],
2f6c641a 53 'due_date' => [
54 'html_type' => 'text',
55 'title' => ts('Due Date'),
56 'weight' => 3,
57 ],
58 'due_date_period' => [
59 'html_type' => 'select',
60 'title' => ts('For transmission'),
61 'weight' => 4,
62 'description' => ts('Select the interval for due date.'),
63 'option_values' => [
64 'select' => ts('- select -'),
65 'days' => ts('Days'),
66 'months' => ts('Months'),
67 'years' => ts('Years'),
68 ],
69 ],
70 'notes' => [
71 'html_type' => 'wysiwyg',
72 'title' => ts('Notes or Standard Terms'),
73 'weight' => 5,
74 'description' => ts('Enter note or message to be displayed on PDF invoice or credit notes '),
75 'attributes' => ['rows' => 2, 'cols' => 40],
76 ],
77 'is_email_pdf' => [
78 'html_type' => 'checkbox',
79 'title' => ts('Automatically email invoice when user purchases online'),
80 'weight' => 6,
81 'description' => ts('Should a pdf invoice be emailed automatically?'),
82 ],
83 'tax_term' => [
84 'html_type' => 'text',
85 'title' => ts('Tax Term'),
86 'weight' => 7,
87 ],
88 'tax_display_settings' => [
89 'html_type' => 'select',
90 'title' => ts('Tax Display Settings'),
91 'weight' => 8,
92 'option_values' => [
6dabf459
ML
93 'Do_not_show' => ts('Do not show breakdown, only show total - i.e %1', [
94 1 => CRM_Utils_Money::format(120),
95 ]),
96 'Inclusive' => ts('Show [tax term] inclusive price - i.e. %1', [
97 1 => ts('%1 (includes [tax term] of %2)', [1 => CRM_Utils_Money::format(120), 2 => CRM_Utils_Money::format(20)]),
98 ]),
99 'Exclusive' => ts('Show [tax term] exclusive price - i.e. %1', [
100 1 => ts('%1 + %2 [tax term]', [1 => CRM_Utils_Money::format(120), 2 => CRM_Utils_Money::format(20)]),
101 ]),
2f6c641a 102 ],
103 ],
104 ];
105
106 // @todo this is a faux metadata approach - we should be honest & add them correctly or find a way to make this
107 // compatible with our settings standards.
108 foreach ($this->invoiceSettings as $fieldName => $fieldValue) {
109 switch ($fieldValue['html_type']) {
110 case 'text':
111 $this->addElement('text',
112 $fieldName,
113 $fieldValue['title'],
114 [
115 'maxlength' => 64,
116 'size' => 32,
117 ]
118 );
119 break;
120
121 case 'checkbox':
122 $this->add($fieldValue['html_type'],
123 $fieldName,
124 $fieldValue['title']
125 );
126 break;
127
128 case 'select':
129 $this->addElement('select',
130 $fieldName,
131 $fieldValue['title'],
132 $fieldValue['option_values'],
133 CRM_Utils_Array::value('attributes', $fieldValue)
134 );
135 break;
136
137 case 'wysiwyg':
138 $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']);
139 break;
e0d19757 140 }
e0d19757 141 }
2f6c641a 142
143 $this->assign('htmlFields', $this->invoiceSettings);
9d4da082 144 }
1be9403d 145
146 /**
c490a46a 147 * Set default values for the form.
1be9403d 148 *
ce064e4f 149 * default values are retrieved from the database
1be9403d 150 */
00be9182 151 public function setDefaultValues() {
2f6c641a 152 $defaults = parent::setDefaultValues();
153 $defaults = array_merge($defaults, Civi::settings()->get('contribution_invoice_settings'));
1be9403d 154 return $defaults;
155 }
156
157 /**
eceb18cc 158 * Process the form after the input has been submitted and validated.
1be9403d 159 */
160 public function postProcess() {
161 // store the submitted values in an array
162 $params = $this->controller->exportValues($this->_name);
2f6c641a 163 $invoiceParams = array_intersect_key($params, $this->invoiceSettings);
47279db3 164 // This is a hack - invoicing is it's own setting but it is being used from invoice params
165 // too. This means that saving from api will not have the desired core effect.
166 // but we should fix that elsewhere - ie. stop abusing the settings
167 // and fix the code repetition associated with invoicing
168 $invoiceParams['invoicing'] = CRM_Utils_Array::value('invoicing', $params, 0);
2f6c641a 169 Civi::settings()->set('contribution_invoice_settings', $invoiceParams);
170 parent::postProcess();
1be9403d 171 }
96025800 172
9d4da082 173}