1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* Add a new Price formatter that allow more options on how
|
5
|
* to display prices.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_theme().
|
10
|
*/
|
11
|
function commerce_price_flexyformatter_theme() {
|
12
|
return array(
|
13
|
'commerce_price_flexyformatter_selected_amount' => array(
|
14
|
'variables' => array(),
|
15
|
),
|
16
|
);
|
17
|
}
|
18
|
|
19
|
/**
|
20
|
* Implements hook_field_formatter_info().
|
21
|
*/
|
22
|
function commerce_price_flexyformatter_field_formatter_info() {
|
23
|
return array(
|
24
|
'commerce_price_flexyformatter_selected_amount' => array(
|
25
|
'label' => t('Formatted amount with selected components'),
|
26
|
'field types' => array('commerce_price'),
|
27
|
'settings' => array(
|
28
|
'base' => array('selection' => array('base_price' => 'base_price'), 'weight' => 0, 'calc' => 'default'),
|
29
|
'reference1' => array('class' => 'flexy-striked', 'weight' => -10, 'calc' => 'default'),
|
30
|
'reference2' => array('weight' => 10, 'calc' => 'default'),
|
31
|
'raw' => array(),
|
32
|
// need this to load all price components, hoping will not be changed
|
33
|
'calculation' => 'calculated_sell_price',
|
34
|
),
|
35
|
),
|
36
|
);
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Implements hook_field_formatter_settings_form().
|
41
|
*/
|
42
|
function commerce_price_flexyformatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
|
43
|
$settings = $instance['display'][$view_mode]['settings'];
|
44
|
|
45
|
$element = array();
|
46
|
$defaults = array();
|
47
|
$reference1_defaults = $reference2_defaults = array();
|
48
|
$methods = array('default' => 'actual values',
|
49
|
'reference1' => 'calculated on reference1 selected components',
|
50
|
'reference2' => 'calculated on reference2 selected components');
|
51
|
// Get all the price component types
|
52
|
$options = commerce_price_component_titles();
|
53
|
|
54
|
foreach ($settings['base']['selection'] as $key => $value) {
|
55
|
if ($value != '0') { $defaults[] = $key; }
|
56
|
}
|
57
|
foreach ($settings['reference1']['selection'] as $key => $value) {
|
58
|
if ($value != '0') { $reference1_defaults[] = $key; }
|
59
|
}
|
60
|
foreach ($settings['reference2']['selection'] as $key => $value) {
|
61
|
if ($value != '0') { $reference2_defaults[] = $key; }
|
62
|
}
|
63
|
|
64
|
$element['base']['selection'] = array(
|
65
|
'#type' => 'checkboxes',
|
66
|
'#options' => $options,
|
67
|
'#default_value' => $defaults,
|
68
|
);
|
69
|
$element['base']['label'] = array(
|
70
|
'#type' => 'textfield',
|
71
|
'#title' => t('Label'),
|
72
|
'#default_value' => $settings['base']['label'],
|
73
|
'#size' => 20,
|
74
|
);
|
75
|
$element['base']['class'] = array(
|
76
|
'#type' => 'textfield',
|
77
|
'#title' => t('Custom class'),
|
78
|
'#default_value' => $settings['base']['class'],
|
79
|
'#size' => 20,
|
80
|
);
|
81
|
$element['base']['calc'] = array(
|
82
|
'#type' => 'radios',
|
83
|
'#options' => $methods,
|
84
|
'#title' => t('Tax calculation method'),
|
85
|
'#default_value' => $settings['base']['calc'],
|
86
|
);
|
87
|
$element['base']['weight'] = array(
|
88
|
'#type' => 'textfield',
|
89
|
'#title' => t('Weight'),
|
90
|
'#default_value' => $settings['base']['weight'],
|
91
|
'#size' => 6,
|
92
|
);
|
93
|
$element['reference1'] = array (
|
94
|
'#type' => 'fieldset',
|
95
|
'#title' => t('1st reference amount'),
|
96
|
'#collapsible' => TRUE,
|
97
|
'#collapsed' => ($settings['reference1']['enabled']? FALSE : TRUE),
|
98
|
);
|
99
|
$element['reference1']['enabled'] = array(
|
100
|
'#title' => t('Enable'),
|
101
|
'#type' => 'checkbox',
|
102
|
'#default_value' => $settings['reference1']['enabled'],
|
103
|
);
|
104
|
$element['reference1']['selection'] = array(
|
105
|
'#type' => 'checkboxes',
|
106
|
'#options' => $options,
|
107
|
'#default_value' => $reference1_defaults,
|
108
|
);
|
109
|
$element['reference1']['label'] = array(
|
110
|
'#type' => 'textfield',
|
111
|
'#title' => t('Label'),
|
112
|
'#default_value' => $settings['reference1']['label'],
|
113
|
'#size' => 20,
|
114
|
);
|
115
|
$element['reference1']['class'] = array(
|
116
|
'#type' => 'textfield',
|
117
|
'#title' => t('Custom class'),
|
118
|
'#default_value' => $settings['reference1']['class'],
|
119
|
'#size' => 20,
|
120
|
);
|
121
|
$element['reference1']['calc'] = array(
|
122
|
'#type' => 'radios',
|
123
|
'#options' => $methods,
|
124
|
'#title' => t('Tax calculation method'),
|
125
|
'#default_value' => $settings['reference1']['calc'],
|
126
|
);
|
127
|
$element['reference1']['weight'] = array(
|
128
|
'#type' => 'textfield',
|
129
|
'#title' => t('Weight'),
|
130
|
'#default_value' => $settings['reference1']['weight'],
|
131
|
'#size' => 6,
|
132
|
);
|
133
|
$element['reference2'] = array (
|
134
|
'#type' => 'fieldset',
|
135
|
'#title' => t('2nd reference amount'),
|
136
|
'#collapsible' => TRUE,
|
137
|
'#collapsed' => ($settings['reference2']['enabled']? FALSE : TRUE),
|
138
|
);
|
139
|
$element['reference2']['enabled'] = array(
|
140
|
'#title' => t('Enable'),
|
141
|
'#type' => 'checkbox',
|
142
|
'#default_value' => $settings['reference2']['enabled'],
|
143
|
);
|
144
|
$element['reference2']['selection'] = array(
|
145
|
'#type' => 'checkboxes',
|
146
|
'#options' => $options,
|
147
|
'#default_value' => $reference2_defaults,
|
148
|
);
|
149
|
$element['reference2']['label'] = array(
|
150
|
'#type' => 'textfield',
|
151
|
'#title' => t('Label'),
|
152
|
'#default_value' => $settings['reference2']['label'],
|
153
|
'#size' => 20,
|
154
|
);
|
155
|
$element['reference2']['class'] = array(
|
156
|
'#type' => 'textfield',
|
157
|
'#title' => t('Custom class'),
|
158
|
'#default_value' => $settings['reference2']['class'],
|
159
|
'#size' => 20,
|
160
|
);
|
161
|
$element['reference2']['calc'] = array(
|
162
|
'#type' => 'radios',
|
163
|
'#options' => $methods,
|
164
|
'#title' => t('Tax calculation method'),
|
165
|
'#default_value' => $settings['reference2']['calc'],
|
166
|
);
|
167
|
$element['reference2']['weight'] = array(
|
168
|
'#type' => 'textfield',
|
169
|
'#title' => t('Weight'),
|
170
|
'#default_value' => $settings['reference2']['weight'],
|
171
|
'#size' => 6,
|
172
|
);
|
173
|
$element['raw'] = array(
|
174
|
'#type' => 'checkbox',
|
175
|
'#title' => t('Use unformatted amount'),
|
176
|
'#default_value' => $settings['raw'],
|
177
|
);
|
178
|
|
179
|
return $element;
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Implements hook_field_formatter_settings_summary().
|
184
|
*/
|
185
|
function commerce_price_flexyformatter_field_formatter_settings_summary($field, $instance, $view_mode) {
|
186
|
$display = $instance['display'][$view_mode];
|
187
|
$settings = $display['settings'];
|
188
|
|
189
|
$summary = array();
|
190
|
$types = commerce_price_component_titles();
|
191
|
$methods = array('default' => 'actual values',
|
192
|
'reference1' => 'calculated on reference1 selected components',
|
193
|
'reference2' => 'calculated on reference2 selected components');
|
194
|
$components['list'] = '';
|
195
|
$components['count'] = 0;
|
196
|
foreach($settings['base']['selection'] as $key => $value) {
|
197
|
if ($value != '0') {
|
198
|
$components['count']++;
|
199
|
if ($components['count'] > 1) {$components['list'] .= ', ';}
|
200
|
$components['list'] .= $types[$key];
|
201
|
}
|
202
|
}
|
203
|
if ($components['count'] == 0) {
|
204
|
$summary[] = t('No components selected');
|
205
|
}
|
206
|
else {
|
207
|
$description = ($settings['base']['label']?$settings['base']['label'].' - selected ':'Selected ') .
|
208
|
format_plural($components['count'], 'component is !list', 'components are !list', array('!list' => $components['list'])) .
|
209
|
' - ' . t('weight: ') . $settings['base']['weight'] . ' - ' . t('tax ') . $methods[$settings['base']['calc']] .'.';
|
210
|
$summary[] = $description;
|
211
|
if ($settings['reference1']['enabled']) {
|
212
|
$reference['list'] = '';
|
213
|
$reference['count'] = 0;
|
214
|
foreach($settings['reference1']['selection'] as $key => $value) {
|
215
|
if ($value != '0') {
|
216
|
$reference['count']++;
|
217
|
if ($reference['count'] > 1) {$reference['list'] .= ', ';}
|
218
|
$reference['list'] .= $types[$key];
|
219
|
}
|
220
|
}
|
221
|
$description = ($settings['reference1']['label']?$settings['reference1']['label'].' - selected ':'1st reference amount enabled with selected ') .
|
222
|
format_plural($reference['count'], 'component is !list', 'components are !list', array('!list' => $reference['list'])) .
|
223
|
' - ' . t('weight: ') . $settings['reference1']['weight'] . ' - ' . t('tax ') .
|
224
|
$methods[$settings['reference1']['calc']] .'.';
|
225
|
$summary[] = $description;
|
226
|
}
|
227
|
else {
|
228
|
$summary[] = t('1st reference amount not enabled.');
|
229
|
}
|
230
|
if ($settings['reference2']['enabled']) {
|
231
|
$reference['list'] = '';
|
232
|
$reference['count'] = 0;
|
233
|
foreach($settings['reference2']['selection'] as $key => $value) {
|
234
|
if ($value != '0') {
|
235
|
$reference['count']++;
|
236
|
if ($reference['count'] > 1) {$reference['list'] .= ', ';}
|
237
|
$reference['list'] .= $types[$key];
|
238
|
}
|
239
|
}
|
240
|
$description = ($settings['reference2']['label']?$settings['reference2']['label'].' - selected ':'2nd reference amount enabled with selected ') .
|
241
|
format_plural($reference['count'], 'component is !list', 'components are !list', array('!list' => $reference['list'])) .
|
242
|
' - ' . t('weight: ') . $settings['reference2']['weight'] . ' - ' . t('tax ') .
|
243
|
$methods[$settings['reference2']['calc']] .'.';
|
244
|
$summary[] = $description;
|
245
|
}
|
246
|
else {
|
247
|
$summary[] = t('2nd reference amount not enabled.');
|
248
|
}
|
249
|
}
|
250
|
if (isset($settings['raw']) && $settings['raw']) $summary[] = t('Using unformatted amounts.');
|
251
|
return implode('<br />', $summary);
|
252
|
}
|
253
|
/**
|
254
|
* Implements hook_field_formatter_prepare_view().
|
255
|
*/
|
256
|
function commerce_price_flexyformatter_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
|
257
|
// why I need to repeat all that ???
|
258
|
|
259
|
// Allow other modules to prepare the item values prior to formatting.
|
260
|
foreach(module_implements('commerce_price_field_formatter_prepare_view') as $module) {
|
261
|
$function = $module . '_commerce_price_field_formatter_prepare_view';
|
262
|
$function($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
|
263
|
}
|
264
|
}
|
265
|
|
266
|
/**
|
267
|
* Implements hook_field_formatter_view().
|
268
|
*/
|
269
|
function commerce_price_flexyformatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
|
270
|
$element = array();
|
271
|
// Loop through each price value in this field.
|
272
|
foreach ($items as $delta => $item) {
|
273
|
// Do not render a price if the amount is NULL (i.e. non-zero empty value).
|
274
|
if (is_null($item['amount'])) {
|
275
|
// TODO: Consider if we should render as N/A or something indicating a
|
276
|
// price was not available as opposed to just leaving a blank.
|
277
|
continue;
|
278
|
}
|
279
|
// Theme the display of the price based on the display type.
|
280
|
switch ($display['type']) {
|
281
|
|
282
|
case 'commerce_price_flexyformatter_selected_amount':
|
283
|
// Build an array of component display titles and their prices.
|
284
|
$tax_base = array('reference1' => 0, 'reference2' => 0);
|
285
|
$amount = $reference1_amount = $reference2_amount = 0;
|
286
|
|
287
|
// calc tax base
|
288
|
foreach ($item['data']['components'] as $key => $component) {
|
289
|
if (!isset($component['price']['data']['tax_rate'])) {
|
290
|
if (isset($display['settings']['reference1']['selection'][$component['name']])
|
291
|
&& $display['settings']['reference1']['selection'][$component['name']] != '0') {
|
292
|
$tax_base['reference1'] += $component['price']['amount'];
|
293
|
}
|
294
|
if (isset($display['settings']['reference2']['selection'][$component['name']])
|
295
|
&& $display['settings']['reference2']['selection'][$component['name']] != '0') {
|
296
|
$tax_base['reference2'] += $component['price']['amount'];
|
297
|
}
|
298
|
}
|
299
|
}
|
300
|
$amount = commerce_price_flexyformatter_amount_calc($item['data']['components'],$display['settings']['base'],$tax_base);
|
301
|
if (isset($display['settings']['reference1']['enabled']) && $display['settings']['reference1']['enabled']) {
|
302
|
$reference1_amount = commerce_price_flexyformatter_amount_calc($item['data']['components'],$display['settings']['reference1'],$tax_base);
|
303
|
}
|
304
|
if (isset($display['settings']['reference2']['enabled']) && $display['settings']['reference2']['enabled']) {
|
305
|
$reference2_amount = commerce_price_flexyformatter_amount_calc($item['data']['components'],$display['settings']['reference2'],$tax_base);
|
306
|
}
|
307
|
|
308
|
$element[$delta] = array(
|
309
|
'#markup' => theme('commerce_price_flexyformatter_selected_amount',
|
310
|
array(
|
311
|
'base' => array(
|
312
|
'amount' => $amount,
|
313
|
'label' => (isset($display['settings']['base']['label'])?$display['settings']['base']['label']:null),
|
314
|
'class' => (isset($display['settings']['base']['class'])?$display['settings']['base']['class']:null),
|
315
|
'weight' => (isset($display['settings']['base']['weight'])?$display['settings']['base']['weight']:null),
|
316
|
),
|
317
|
'reference1' => array(
|
318
|
'amount' => (isset($reference1_amount)?$reference1_amount:null),
|
319
|
'label' => (isset($display['settings']['reference1']['label'])?$display['settings']['reference1']['label']:null),
|
320
|
'class' => (isset($display['settings']['reference1']['class'])?$display['settings']['reference1']['class']:null),
|
321
|
'weight' => (isset($display['settings']['reference1']['weight'])?$display['settings']['reference1']['weight']:null),
|
322
|
'enabled' => (isset($display['settings']['reference1']['enabled'])?$display['settings']['reference1']['enabled']:0),
|
323
|
),
|
324
|
'reference2' => array(
|
325
|
'amount' => (isset($reference2_amount)?$reference2_amount:null),
|
326
|
'label' => (isset($display['settings']['reference2']['label'])?$display['settings']['reference2']['label']:null),
|
327
|
'class' => (isset($display['settings']['reference2']['class'])?$display['settings']['reference2']['class']:null),
|
328
|
'weight' => (isset($display['settings']['reference2']['weight'])?$display['settings']['reference2']['weight']:null),
|
329
|
'enabled' => (isset($display['settings']['reference2']['enabled'])?$display['settings']['reference2']['enabled']:0),
|
330
|
),
|
331
|
'raw' =>(isset($display['settings']['raw'])?$display['settings']['raw']:false),
|
332
|
'currency_code' => $item['currency_code'],
|
333
|
)
|
334
|
),
|
335
|
);
|
336
|
break;
|
337
|
}
|
338
|
}
|
339
|
return $element;
|
340
|
}
|
341
|
|
342
|
/**
|
343
|
* Themes flexyformatted price.
|
344
|
*
|
345
|
* @param $variables
|
346
|
*/
|
347
|
function theme_commerce_price_flexyformatter_selected_amount($variables) {
|
348
|
$output = '';
|
349
|
uasort($variables, 'drupal_sort_weight');
|
350
|
drupal_add_css(drupal_get_path('module', 'commerce_price_flexyformatter') . '/commerce_price_flexyformatter.css');
|
351
|
foreach ($variables as $key => $variable) {
|
352
|
if (isset($variable['weight']) && is_numeric($variable['weight']) && ($key == 'base' || $variable['enabled'])) {
|
353
|
$output .= '<span class="flexy-item' . ($variable['class']?' ' . $variable['class']:'') . '"><span class="flexy-title">' . $variable['label'] . '</span><span class="flexy-amount">' . ($variables['raw'] == false ? commerce_currency_format($variable['amount'], $variables['currency_code']): commerce_price_flexyformatter_raw_format($variable['amount'], $variables['currency_code'])) . '</span></span>';
|
354
|
}
|
355
|
}
|
356
|
|
357
|
return $output;
|
358
|
}
|
359
|
/**
|
360
|
* Calculate raw
|
361
|
*/
|
362
|
function commerce_price_flexyformatter_raw_format($amount, $currency_code){
|
363
|
$currency = commerce_currency_load($currency_code);
|
364
|
$amount = commerce_currency_amount_to_decimal($amount, $currency_code);
|
365
|
return number_format($amount, $currency['decimals'], $currency['decimal_separator'], '');
|
366
|
}
|
367
|
/**
|
368
|
* Calculate amount to be displayed.
|
369
|
*
|
370
|
* @param array $components
|
371
|
* @param array $settings
|
372
|
* @param array $tax_base
|
373
|
*/
|
374
|
|
375
|
function commerce_price_flexyformatter_amount_calc($components,$settings,$tax_base) {
|
376
|
$amount = 0;
|
377
|
foreach ($components as $key => $component) {
|
378
|
if (isset($settings['selection'][$component['name']]) && $settings['selection'][$component['name']] != '0') {
|
379
|
if (!isset($component['price']['data']['tax_rate'])
|
380
|
or (isset($settings['calc'])
|
381
|
and $settings['calc'] == 'default')) {
|
382
|
$amount += $component['price']['amount'];
|
383
|
} else {
|
384
|
$amount += $component['price']['data']['tax_rate']['rate'] * $tax_base[$settings['calc']];
|
385
|
}
|
386
|
}
|
387
|
}
|
388
|
return $amount;
|
389
|
}
|