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