1 |
85ad3d82
|
Assos Assos
|
<?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 |
|
|
'selection' => array('base_price' => 'base_price'),
|
29 |
|
|
'reference' => array(),
|
30 |
|
|
// need this to load all price components, hoping will not be changed
|
31 |
|
|
'calculation' => 'calculated_sell_price',
|
32 |
|
|
),
|
33 |
|
|
),
|
34 |
|
|
);
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
/**
|
38 |
|
|
* Implements hook_field_formatter_settings_form().
|
39 |
|
|
*/
|
40 |
|
|
function commerce_price_flexyformatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
|
41 |
|
|
$settings = $instance['display'][$view_mode]['settings'];
|
42 |
|
|
|
43 |
|
|
$element = array();
|
44 |
|
|
$defaults = array();
|
45 |
|
|
$reference_defaults = array();
|
46 |
|
|
|
47 |
|
|
// Get all the price component types
|
48 |
|
|
$options = commerce_price_component_titles();
|
49 |
|
|
|
50 |
|
|
foreach ($settings['selection'] as $key => $value) {
|
51 |
|
|
if ($value != '0') { $defaults[] = $key; }
|
52 |
|
|
}
|
53 |
|
|
foreach ($settings['reference']['selection'] as $key => $value) {
|
54 |
|
|
if ($value != '0') { $reference_defaults[] = $key; }
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
$element['selection'] = array(
|
58 |
|
|
'#type' => 'checkboxes',
|
59 |
|
|
'#options' => $options,
|
60 |
|
|
'#default_value' => $defaults,
|
61 |
|
|
);
|
62 |
|
|
$element['reference'] = array (
|
63 |
|
|
'#type' => 'fieldset',
|
64 |
|
|
'#title' => t('Reference amount'),
|
65 |
|
|
'#collapsible' => TRUE,
|
66 |
|
|
'#collapsed' => ($settings['reference']['enabled']? FALSE : TRUE),
|
67 |
|
|
);
|
68 |
|
|
$element['reference']['enabled'] = array(
|
69 |
|
|
'#title' => t('Enable'),
|
70 |
|
|
'#type' => 'checkbox',
|
71 |
|
|
'#default_value' => $settings['reference']['enabled'],
|
72 |
|
|
);
|
73 |
|
|
$element['reference']['selection'] = array(
|
74 |
|
|
'#type' => 'checkboxes',
|
75 |
|
|
'#options' => $options,
|
76 |
|
|
'#default_value' => $reference_defaults,
|
77 |
|
|
);
|
78 |
|
|
|
79 |
|
|
return $element;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Implements hook_field_formatter_settings_summary().
|
84 |
|
|
*/
|
85 |
|
|
function commerce_price_flexyformatter_field_formatter_settings_summary($field, $instance, $view_mode) {
|
86 |
|
|
$display = $instance['display'][$view_mode];
|
87 |
|
|
$settings = $display['settings'];
|
88 |
|
|
|
89 |
|
|
$summary = array();
|
90 |
|
|
$types = commerce_price_component_titles();
|
91 |
|
|
$components['list'] = '';
|
92 |
|
|
$components['count'] = 0;
|
93 |
|
|
foreach($settings['selection'] as $key => $value) {
|
94 |
|
|
if ($value != '0') {
|
95 |
|
|
$components['count']++;
|
96 |
|
|
if ($components['count'] > 1) {$components['list'] .= ', ';}
|
97 |
|
|
$components['list'] .= $types[$key];
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
if ($components['count'] == 0) {
|
101 |
|
|
$summary[] = t('No components selected');
|
102 |
|
|
}
|
103 |
|
|
else {
|
104 |
|
|
$summary[] = format_plural($components['count'], 'Selected component is !list.', 'Selected components are !list.', array('!list' => $components['list']));
|
105 |
|
|
if ($settings['reference']['enabled']) {
|
106 |
|
|
$reference['list'] = '';
|
107 |
|
|
$reference['count'] = 0;
|
108 |
|
|
foreach($settings['reference']['selection'] as $key => $value) {
|
109 |
|
|
if ($value != '0') {
|
110 |
|
|
$reference['count']++;
|
111 |
|
|
if ($reference['count'] > 1) {$reference['list'] .= ', ';}
|
112 |
|
|
$reference['list'] .= $types[$key];
|
113 |
|
|
}
|
114 |
|
|
}
|
115 |
|
|
$summary[] = t('Reference amount enabled with !list.', array('!list' => $reference['list']));
|
116 |
|
|
}
|
117 |
|
|
else {
|
118 |
|
|
$summary[] = t('Reference amount not enabled.');
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
return implode('<br />', $summary);
|
123 |
|
|
}
|
124 |
|
|
/**
|
125 |
|
|
* Implements hook_field_formatter_prepare_view().
|
126 |
|
|
*/
|
127 |
|
|
function commerce_price_flexyformatter_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
|
128 |
|
|
// why I need to repeat all that ???
|
129 |
|
|
|
130 |
|
|
// Allow other modules to prepare the item values prior to formatting.
|
131 |
|
|
foreach(module_implements('commerce_price_field_formatter_prepare_view') as $module) {
|
132 |
|
|
$function = $module . '_commerce_price_field_formatter_prepare_view';
|
133 |
|
|
$function($entity_type, $entities, $field, $instances, $langcode, $items, $displays);
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
/**
|
138 |
|
|
* Implements hook_field_formatter_view().
|
139 |
|
|
*/
|
140 |
|
|
function commerce_price_flexyformatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
|
141 |
|
|
$element = array();
|
142 |
|
|
|
143 |
|
|
// Loop through each price value in this field.
|
144 |
|
|
foreach ($items as $delta => $item) {
|
145 |
|
|
// Do not render a price if the amount is NULL (i.e. non-zero empty value).
|
146 |
|
|
if (is_null($item['amount'])) {
|
147 |
|
|
// TODO: Consider if we should render as N/A or something indicating a
|
148 |
|
|
// price was not available as opposed to just leaving a blank.
|
149 |
|
|
continue;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
// Theme the display of the price based on the display type.
|
153 |
|
|
switch ($display['type']) {
|
154 |
|
|
|
155 |
|
|
case 'commerce_price_flexyformatter_selected_amount':
|
156 |
|
|
// Build an array of component display titles and their prices.
|
157 |
|
|
$components = array();
|
158 |
|
|
$amount = $reference_amount = 0;
|
159 |
|
|
$difference = $reference_difference = 0;
|
160 |
|
|
foreach ($item['data']['components'] as $key => $component) {
|
161 |
|
|
if ($display['settings']['selection'][$component['name']] != '0') {
|
162 |
|
|
$amount += $component['price']['amount'];
|
163 |
|
|
if (isset($component['price']['data']['tax_rate'])) {
|
164 |
|
|
$amount -= $component['price']['data']['tax_rate']['rate'] * $difference;
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
else {
|
168 |
|
|
$difference += $component['price']['amount'];
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
// Check if reference amount is ebabled
|
172 |
|
|
if ($display['settings']['reference']['enabled']) {
|
173 |
|
|
$components = array();
|
174 |
|
|
// reset($item['data']['components']);
|
175 |
|
|
foreach ($item['data']['components'] as $key => $component) {
|
176 |
|
|
if ($display['settings']['reference']['selection'][$component['name']] != '0') {
|
177 |
|
|
$reference_amount += $component['price']['amount'];
|
178 |
|
|
if (isset($component['price']['data']['tax_rate'])) {
|
179 |
|
|
$reference_amount -= $component['price']['data']['tax_rate']['rate'] * $reference_difference;
|
180 |
|
|
}
|
181 |
|
|
}
|
182 |
|
|
else {
|
183 |
|
|
$reference_difference += $component['price']['amount'];
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
$element[$delta] = array(
|
188 |
|
|
'#markup' => theme('commerce_price_flexyformatter_selected_amount',
|
189 |
|
|
array('amount' => $amount,
|
190 |
|
|
'reference_enabled' => $display['settings']['reference']['enabled'],
|
191 |
|
|
'reference_amount' => $reference_amount,
|
192 |
|
|
'currency_code' => $item['currency_code'],
|
193 |
|
|
)
|
194 |
|
|
),
|
195 |
|
|
);
|
196 |
|
|
break;
|
197 |
|
|
}
|
198 |
|
|
}
|
199 |
|
|
return $element;
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
/**
|
203 |
|
|
* Themes flexyformatted price.
|
204 |
|
|
*
|
205 |
|
|
* @param $variables
|
206 |
|
|
*/
|
207 |
|
|
function theme_commerce_price_flexyformatter_selected_amount($variables) {
|
208 |
|
|
if ($variables['reference_enabled'] && ($variables['amount'] != $variables['reference_amount'])) {
|
209 |
|
|
// Add the CSS styling.
|
210 |
|
|
drupal_add_css(drupal_get_path('module', 'commerce_price_flexyformatter') . '/commerce_price_flexyformatter.css');
|
211 |
|
|
$output = '<span class="flexy-reference">' . commerce_currency_format($variables['reference_amount'], $variables['currency_code']) . '</span> ';
|
212 |
|
|
$output .= '<span class="flexy-amount has-reference">' . commerce_currency_format($variables['amount'], $variables['currency_code']) . '</span>';
|
213 |
|
|
}
|
214 |
|
|
else {
|
215 |
|
|
$output = '<span class="flexy-amount has-no-reference">' . commerce_currency_format($variables['amount'], $variables['currency_code']) . '</span>';
|
216 |
|
|
}
|
217 |
|
|
return $output;
|
218 |
|
|
} |