Merge pull request #13014 from eileenmcnaughton/strlen
[civicrm-core.git] / CRM / Admin / Form / Preferences / Contribute.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * This class generates form components for the display preferences.
36 */
37 class CRM_Admin_Form_Preferences_Contribute extends CRM_Admin_Form_Preferences {
38 protected $_settings = array(
39 'cvv_backoffice_required' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
40 'update_contribution_on_membership_type_change' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
41 'acl_financial_type' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
42 'always_post_to_accounts_receivable' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
43 'deferred_revenue_enabled' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
44 'default_invoice_page' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
45 'invoicing' => CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
46 );
47
48 /**
49 * Our standards for settings are to have a setting per value with defined metadata.
50 *
51 * Unfortunately the 'contribution_invoice_settings' has been added in non-compliance.
52 * We use this array to hack-handle.
53 *
54 * I think the best way forwards would be to covert to multiple individual settings.
55 *
56 * @var array
57 */
58 protected $invoiceSettings = [];
59
60 /**
61 * Build the form object.
62 */
63 public function buildQuickForm() {
64 parent::buildQuickForm();
65 $config = CRM_Core_Config::singleton();
66 $this->invoiceSettings = [
67 'invoice_prefix' => [
68 'html_type' => 'text',
69 'title' => ts('Invoice Prefix'),
70 'weight' => 1,
71 'description' => ts('Enter prefix to be display on PDF for invoice'),
72 ],
73 'credit_notes_prefix' => [
74 'html_type' => 'text',
75 'title' => ts('Credit Notes Prefix'),
76 'weight' => 2,
77 'description' => ts('Enter prefix to be display on PDF for credit notes.'),
78 ],
79 'due_date' => [
80 'html_type' => 'text',
81 'title' => ts('Due Date'),
82 'weight' => 3,
83 ],
84 'due_date_period' => [
85 'html_type' => 'select',
86 'title' => ts('For transmission'),
87 'weight' => 4,
88 'description' => ts('Select the interval for due date.'),
89 'option_values' => [
90 'select' => ts('- select -'),
91 'days' => ts('Days'),
92 'months' => ts('Months'),
93 'years' => ts('Years'),
94 ],
95 ],
96 'notes' => [
97 'html_type' => 'wysiwyg',
98 'title' => ts('Notes or Standard Terms'),
99 'weight' => 5,
100 'description' => ts('Enter note or message to be displayed on PDF invoice or credit notes '),
101 'attributes' => ['rows' => 2, 'cols' => 40],
102 ],
103 'is_email_pdf' => [
104 'html_type' => 'checkbox',
105 'title' => ts('Automatically email invoice when user purchases online'),
106 'weight' => 6,
107 'description' => ts('Should a pdf invoice be emailed automatically?'),
108 ],
109 'tax_term' => [
110 'html_type' => 'text',
111 'title' => ts('Tax Term'),
112 'weight' => 7,
113 ],
114 'tax_display_settings' => [
115 'html_type' => 'select',
116 'title' => ts('Tax Display Settings'),
117 'weight' => 8,
118 'option_values' => [
119 'Do_not_show' => ts('Do not show breakdown, only show total -i.e ' .
120 $config->defaultCurrencySymbol . '120.00'),
121 'Inclusive' => ts('Show [tax term] inclusive price - i.e. ' .
122 $config->defaultCurrencySymbol .
123 '120.00 (includes [tax term] of ' .
124 $config->defaultCurrencySymbol . '20.00)'),
125 'Exclusive' => ts('Show [tax term] exclusive price - i.e. ' .
126 $config->defaultCurrencySymbol . '100.00 + ' .
127 $config->defaultCurrencySymbol . '20.00 [tax term]'),
128 ],
129 ],
130 ];
131
132 // @todo this is a faux metadata approach - we should be honest & add them correctly or find a way to make this
133 // compatible with our settings standards.
134 foreach ($this->invoiceSettings as $fieldName => $fieldValue) {
135 switch ($fieldValue['html_type']) {
136 case 'text':
137 $this->addElement('text',
138 $fieldName,
139 $fieldValue['title'],
140 [
141 'maxlength' => 64,
142 'size' => 32,
143 ]
144 );
145 break;
146
147 case 'checkbox':
148 $this->add($fieldValue['html_type'],
149 $fieldName,
150 $fieldValue['title']
151 );
152 break;
153
154 case 'select':
155 $this->addElement('select',
156 $fieldName,
157 $fieldValue['title'],
158 $fieldValue['option_values'],
159 CRM_Utils_Array::value('attributes', $fieldValue)
160 );
161 break;
162
163 case 'wysiwyg':
164 $this->add('wysiwyg', $fieldName, $fieldValue['title'], $fieldValue['attributes']);
165 break;
166 }
167 }
168
169 $this->assign('htmlFields', $this->invoiceSettings);
170 }
171
172 /**
173 * Set default values for the form.
174 *
175 * default values are retrieved from the database
176 */
177 public function setDefaultValues() {
178 $defaults = parent::setDefaultValues();
179 $defaults = array_merge($defaults, Civi::settings()->get('contribution_invoice_settings'));
180 return $defaults;
181 }
182
183 /**
184 * Process the form after the input has been submitted and validated.
185 */
186 public function postProcess() {
187 // store the submitted values in an array
188 $params = $this->controller->exportValues($this->_name);
189 $invoiceParams = array_intersect_key($params, $this->invoiceSettings);
190 Civi::settings()->set('contribution_invoice_settings', $invoiceParams);
191 parent::postProcess();
192
193 // @todo - all this should be handled by defining an on change action in the metadata.
194 // to set default value for 'Invoices / Credit Notes' checkbox on display preferences
195 $values = CRM_Core_BAO_Setting::getItem("CiviCRM Preferences");
196 $optionValues = CRM_Core_OptionGroup::values('user_dashboard_options', FALSE, FALSE, FALSE, NULL, 'name');
197 $setKey = array_search('Invoices / Credit Notes', $optionValues);
198
199 if (isset($params['invoicing'])) {
200 $value = array($setKey => $optionValues[$setKey]);
201 $setInvoice = CRM_Core_DAO::VALUE_SEPARATOR .
202 implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value)) .
203 CRM_Core_DAO::VALUE_SEPARATOR;
204 Civi::settings()->set('user_dashboard_options', $values['user_dashboard_options'] . $setInvoice);
205 }
206 else {
207 $setting = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($values['user_dashboard_options'], 1, -1));
208 $invoiceKey = array_search($setKey, $setting);
209 if ($invoiceKey !== FALSE) {
210 unset($setting[$invoiceKey]);
211 }
212 $settingName = CRM_Core_DAO::VALUE_SEPARATOR .
213 implode(CRM_Core_DAO::VALUE_SEPARATOR, array_values($setting)) .
214 CRM_Core_DAO::VALUE_SEPARATOR;
215 Civi::settings()->set('user_dashboard_options', $settingName);
216 }
217 }
218
219 }