Merge pull request #6560 from kurund/CRM-16719-fix
[civicrm-core.git] / CRM / Admin / Form / Preferences / Contribute.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 );
41
42 /**
43 * Process the form submission.
44 */
45 public function preProcess() {
46 $config = CRM_Core_Config::singleton();
47 CRM_Utils_System::setTitle(ts('CiviContribute Component Settings'));
48 $this->_varNames = array(
49 CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME => array(
50 'invoice_prefix' => array(
51 'html_type' => 'text',
52 'title' => ts('Invoice Prefix'),
53 'weight' => 1,
54 'description' => ts('Enter prefix to be display on PDF for invoice'),
55 ),
56 'credit_notes_prefix' => array(
57 'html_type' => 'text',
58 'title' => ts('Credit Notes Prefix'),
59 'weight' => 2,
60 'description' => ts('Enter prefix to be display on PDF for credit notes.'),
61 ),
62 'due_date' => array(
63 'html_type' => 'text',
64 'title' => ts('Due Date'),
65 'weight' => 3,
66 ),
67 'due_date_period' => array(
68 'html_type' => 'select',
69 'title' => ts('For transmission'),
70 'weight' => 4,
71 'description' => ts('Select the interval for due date.'),
72 'option_values' => array(
73 'select' => ts('- select -'),
74 'days' => ts('Days'),
75 'months' => ts('Months'),
76 'years' => ts('Years'),
77 ),
78 ),
79 'notes' => array(
80 'html_type' => 'wysiwyg',
81 'title' => ts('Notes or Standard Terms'),
82 'weight' => 5,
83 'description' => ts('Enter note or message to be displayed on PDF invoice or credit notes '),
84 'attributes' => array('rows' => 2, 'cols' => 40),
85 ),
86 'is_email_pdf' => array(
87 'html_type' => 'checkbox',
88 'title' => ts('Automatically email invoice when user purchases online'),
89 'weight' => 6,
90 ),
91 'tax_term' => array(
92 'html_type' => 'text',
93 'title' => ts('Tax Term'),
94 'weight' => 7,
95 ),
96 'tax_display_settings' => array(
97 'html_type' => 'select',
98 'title' => ts('Tax Display Settings'),
99 'weight' => 8,
100 'option_values' => array(
101 'Do_not_show' => ts('Do not show breakdown, only show total -i.e ' .
102 $config->defaultCurrencySymbol . '120.00'),
103 'Inclusive' => ts('Show [tax term] inclusive price - i.e. ' .
104 $config->defaultCurrencySymbol .
105 '120.00 (includes [tax term] of ' .
106 $config->defaultCurrencySymbol . '20.00)'),
107 'Exclusive' => ts('Show [tax term] exclusive price - i.e. ' .
108 $config->defaultCurrencySymbol . '100.00 + ' .
109 $config->defaultCurrencySymbol . '20.00 [tax term]'),
110 ),
111 ),
112 ),
113 );
114 parent::preProcess();
115 }
116
117 /**
118 * Build the form object.
119 */
120 public function buildQuickForm() {
121 //CRM-16691: Changes made related to settings of 'CVV'.
122 foreach ($this->_settings as $setting => $group) {
123 $settingMetaData = civicrm_api3('setting', 'getfields', array('name' => $setting));
124 $props = $settingMetaData['values'][$setting];
125 if (isset($props['quick_form_type'])) {
126 $add = 'add' . $props['quick_form_type'];
127 if ($add == 'addElement') {
128 $this->$add(
129 $props['html_type'],
130 $setting,
131 ts($props['title']),
132 CRM_Utils_Array::value($props['html_type'] == 'select' ? 'option_values' : 'html_attributes', $props, array()),
133 $props['html_type'] == 'select' ? CRM_Utils_Array::value('html_attributes', $props) : NULL
134 );
135 }
136 else {
137 $this->$add($setting, ts($props['title']));
138 }
139 }
140 $this->assign("{$setting}_description", ts($props['description']));
141 }
142 $this->add('checkbox', 'invoicing', ts('Enable Tax and Invoicing'));
143 parent::buildQuickForm();
144 }
145
146 /**
147 * Set default values for the form.
148 *
149 * default values are retrieved from the database
150 */
151 public function setDefaultValues() {
152 $defaults = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
153 //CRM-16691: Changes made related to settings of 'CVV'.
154 foreach ($this->_settings as $setting => $group) {
155 $settingMetaData = civicrm_api3('setting', 'getfields', array('name' => $setting));
156 $defaults[$setting] = civicrm_api3('setting', 'getvalue',
157 array(
158 'name' => $setting,
159 'group' => $group,
160 'default_value' => CRM_Utils_Array::value('default', $settingMetaData['values'][$setting]),
161 )
162 );
163 }
164 return $defaults;
165 }
166
167 /**
168 * Process the form after the input has been submitted and validated.
169 */
170 public function postProcess() {
171 // store the submitted values in an array
172 $params = $this->controller->exportValues($this->_name);
173 unset($params['qfKey']);
174 unset($params['entryURL']);
175 $setInvoiceSettings = CRM_Core_BAO_Setting::setItem($params, CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
176
177 // to set default value for 'Invoices / Credit Notes' checkbox on display preferences
178 $values = CRM_Core_BAO_Setting::getItem("CiviCRM Preferences");
179 $optionValues = CRM_Core_OptionGroup::values('user_dashboard_options', FALSE, FALSE, FALSE, NULL, 'name');
180 $setKey = array_search('Invoices / Credit Notes', $optionValues);
181
182 if (isset($params['invoicing'])) {
183 $value = array($setKey => $optionValues[$setKey]);
184 $setInvoice = CRM_Core_DAO::VALUE_SEPARATOR .
185 implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value)) .
186 CRM_Core_DAO::VALUE_SEPARATOR;
187 CRM_Core_BAO_Setting::setItem($values['user_dashboard_options'] .
188 $setInvoice, 'CiviCRM Preferences', 'user_dashboard_options');
189 }
190 else {
191 $setting = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($values['user_dashboard_options'], 1, -1));
192 $invoiceKey = array_search($setKey, $setting);
193 unset($setting[$invoiceKey]);
194 $settingName = CRM_Core_DAO::VALUE_SEPARATOR .
195 implode(CRM_Core_DAO::VALUE_SEPARATOR, array_values($setting)) .
196 CRM_Core_DAO::VALUE_SEPARATOR;
197 CRM_Core_BAO_Setting::setItem($settingName, 'CiviCRM Preferences', 'user_dashboard_options');
198 }
199 //CRM-16691: Changes made related to settings of 'CVV'.
200 $settings = array_intersect_key($params, $this->_settings);
201 $result = civicrm_api3('setting', 'create', $settings);
202 CRM_Core_Session::setStatus(ts('Your changes have been saved.'), ts('Changes Saved'), "success");
203 }
204
205 }