/[drupal]/contributions/modules/ecommerce/ec_charge/modules/system.inc
ViewVC logotype

Contents of /contributions/modules/ecommerce/ec_charge/modules/system.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1.2.15 - (show annotations) (download) (as text)
Mon Apr 27 01:56:15 2009 UTC (7 months ago) by gordon
Branch: DRUPAL-6--4
CVS Tags: DRUPAL-6--4-0-RC9, DRUPAL-6--4-0-RC8, DRUPAL-6--4-0-RC7, DRUPAL-6--4-0-RC6, DRUPAL-6--4-0-RC15, DRUPAL-6--4-0-RC14, DRUPAL-6--4-0-RC11, DRUPAL-6--4-0-RC10, DRUPAL-6--4-0-RC13, DRUPAL-6--4-0-RC12
Changes since 1.1.2.14: +8 -2 lines
File MIME type: text/x-php
* Fix up calculation so that it will not throw errors when the calculation is stuffed
1 <?php
2 // $Id: system.inc,v 1.1.2.14 2009/04/21 12:24:16 gordon Exp $
3
4 /**
5 * @file
6 * Provide system intergration for charges to allow low level actions to be
7 * done one changes
8 */
9
10 /**
11 * Implementation of hook_ec_charge_filter_info().
12 */
13 function system_ec_charge_filter_info() {
14 return array(
15 'cookie' => array(
16 'name' => t('Cookie*'),
17 'description' => t('Validate that a cookie exists and it is equal to a defined value'),
18 'module' => 'system_cookie',
19 'file' => 'system.inc',
20 'core' => TRUE,
21 ),
22 'session' => array(
23 'name' => t('Session*'),
24 'description' => t('Validate that a session variable exists and it is equal to a defined value'),
25 'module' => 'system_session',
26 'file' => 'system.inc',
27 'core' => TRUE,
28 ),
29 );
30 }
31
32 /**
33 * Implementation of hook_ec_charge_calculation_info().
34 */
35 function system_ec_charge_calculation_info() {
36 return array(
37 'calc' => array(
38 'name' => t('Calculation'),
39 'description' => t('Provide a method to allow the calculation of the charge.'),
40 'module' => 'system_calc',
41 'file' => 'system.inc',
42 'core' => TRUE,
43 ),
44 );
45 }
46
47 function system_ec_charge_modification_info() {
48 return array(
49 'lowest' => array(
50 'name' => t('Lowest Value'),
51 'description' => t('based up all the transactions that have been calculated, this will choose the lowest charge and only return that one.'),
52 'module' => 'system_lowest',
53 'file' => 'system.inc',
54 'core' => TRUE,
55 ),
56 'highest' => array(
57 'name' => t('Highest Value'),
58 'description' => t('based up all the transactions that have been calculated, this will choose the highest charge and only return that one.'),
59 'module' => 'system_highest',
60 'file' => 'system.inc',
61 'core' => TRUE,
62 ),
63 'displayonly' => array(
64 'name' => t('Display only'),
65 'description' => t('Change the transactions to only be display only'),
66 'module' => 'system_displayonly',
67 'file' => 'system.inc',
68 'core' => TRUE,
69 ),
70 'title' => array(
71 'name' => t('Title'),
72 'description' => t('Change the transactions titles which will display on the transaction'),
73 'module' => 'system_title',
74 'file' => 'system.inc',
75 'core' => TRUE,
76 ),
77 'clearprice' => array(
78 'name' => t('Clear Price'),
79 'description' => t('This is mainly used for display only values so that you can add a message with a calculation.'),
80 'module' => 'system_clearprice',
81 'file' => 'system.inc',
82 'core' => TRUE,
83 ),
84 'additional' => array(
85 'name' => t('Move title to additional'),
86 'description' => t('Use this to move the additional text section of the invoice'),
87 'module' => 'system_additional',
88 'file' => 'system.inc',
89 'core' => TRUE,
90 ),
91 );
92 }
93
94 function system_calc_calculation_form(&$form_state, $settings) {
95 $form = array();
96 $default = array('calc' => '');
97 $settings+= $default;
98
99 $form['calc'] = array(
100 '#type' => 'textfield',
101 '#title' => t('Calculation'),
102 '#default_value' => $settings['calc'],
103 );
104
105 $chg = $form_state['storage']['chg'];
106 $variables = ec_charge_get_variables($chg, $chg['type'] ? 'node' : 'txn', NULL);
107 $descriptions = ec_charge_get_variables_description($chg, $chg['type'] ? 'node' : 'txn', NULL, $variables);
108
109 $form['replacements'] = array(
110 '#type' => 'fieldset',
111 '#title' => t('Replacement Tokens'),
112 '#collapsible' => TRUE,
113 '#collapsed' => TRUE,
114 array(
115 '#value' => theme('ec_charge_variable_description', $descriptions),
116 ),
117 '#weight' => 5,
118 );
119
120 return $form;
121 }
122
123 function system_calc_calculation_settings() {
124 return array('calc');
125 }
126
127 function system_calc_calculation_process($type, $settings, $variables, $object) {
128 $vars = array();
129 if (!empty($variables)) {
130 $keys = array_map('_system_calc_map_variables', array_keys($variables));
131 $vars = array_combine($keys, $variables);
132 }
133
134 $calc = strtr($settings['calc'], $vars);
135
136 $price = @eval('return '. $calc .';');
137
138 if (!is_scalar($price)) {
139 $price = 0;
140 drupal_set_message(t('Calculation for %name as failed', array('%name' => $settings['name'])), 'error');
141 watchdog('ec_charge', 'Calculation for %name has failed', array('%name' => $settings['name']), WATCHDOG_ERROR);
142 }
143
144 $misc = array(
145 'description' => $settings['name'],
146 'price' => $price,
147 );
148
149 return $misc;
150 }
151
152 function _system_calc_map_variables($a) {
153 return '['. $a .']';
154 }
155
156 function system_lowest_modification_process($type, $settings, $misc, $object) {
157 $lowest = reset($misc);
158
159 foreach ($misc as $item) {
160 if (abs($lowest['price']) > abs($item['price'])) {
161 $lowest = $item;
162 }
163 }
164
165 return array($lowest);
166 }
167
168 function system_highest_modification_process($type, $settings, $misc, $object) {
169 $highest = reset($misc);
170
171 foreach ($misc as $item) {
172 if (abs($highest['price']) < abs($item['price'])) {
173 $highest = $item;
174 }
175 }
176
177 return array($highest);
178 }
179
180 function system_displayonly_modification_process($type, $settings, $misc, $object) {
181 if (is_array($misc)) {
182 return array_map('_system_displayonly_map', $misc);
183 }
184 }
185
186 function _system_displayonly_map($a) {
187 $a['displayonly'] = 1;
188 return $a;
189 }
190
191 function system_title_modification_form(&$form_state, $settings) {
192 $form = array();
193 $settings+= array('title' => '');
194
195 $form['title'] = array(
196 '#type' => 'textfield',
197 '#title' => t('Title'),
198 '#default_value' => $settings['title'],
199 '#size' => 60,
200 '#maxlength' => 255,
201 '#required' => TRUE,
202 );
203
204 return $form;
205 }
206
207 function system_title_modification_settings() {
208 return array('title');
209 }
210
211 function system_title_modification_process($type, $settings, $misc, $object) {
212 if (is_array($misc)) {
213 _system_title_map('init', $settings);
214 return array_map('_system_title_map', $misc);
215 }
216 }
217
218 function _system_title_map($a, $params = NULL) {
219 static $settings;
220
221 if ($a == 'init') {
222 $settings = $params;
223 return;
224 }
225
226 $keys = array_map('_system_calc_map_variables', array_keys($a));
227 $params = array_combine($keys, $a);
228
229 $params['[price]'] = format_currency($a['price']);
230
231 $a['description'] = strtr($settings['title'], $params);
232
233 return $a;
234 }
235
236 function system_clearprice_modification_process($type, $settings, $misc, $object) {
237 if (is_array($misc)) {
238 return array_map('_system_clearprice_map', $misc);
239 }
240 }
241
242 function _system_clearprice_map($a) {
243 unset($a['price']);
244 return $a;
245 }
246
247 function system_additional_modification_form(&$form_state, $settings) {
248 $form = array();
249 $settings+= array('divid' => '', 'class' => '', 'title' => '');
250
251 $form['divid'] = array(
252 '#type' => 'textfield',
253 '#title' => t('Id'),
254 '#default_value' => $settings['divid'],
255 '#size' => 60,
256 '#maxlength' => 255,
257 );
258
259 $form['class'] = array(
260 '#type' => 'textfield',
261 '#title' => t('Class'),
262 '#default_value' => $settings['class'],
263 '#size' => 60,
264 '#maxlength' => 255,
265 );
266
267 $form['title'] = array(
268 '#type' => 'textfield',
269 '#title' => t('Title'),
270 '#default_value' => $settings['title'],
271 '#size' => 60,
272 '#maxlength' => 255,
273 '#required' => TRUE,
274 );
275
276 return $form;
277 }
278
279 function system_additional_modification_settings() {
280 return array('divid', 'class', 'title');
281 }
282
283 function system_additional_modification_process($type, $settings, $misc, &$object) {
284 $$type =& $object;
285
286 if (is_array($misc)) {
287 _system_title_map('init', $settings);
288 $misc = array_map('_system_title_map', $misc);
289
290 $attributes = array();
291 foreach (array('divid' => 'id', 'class' => 'class') as $id => $field) {
292 if (isset($settings[$id])) {
293 $attributes[$field] = $settings[$id];
294 }
295 }
296
297 foreach ($misc as $item) {
298 $txn->additional['ec_charge_'. $settings['chgid']] = array(
299 '#prefix' => '<div'. drupal_attributes($attributes) .'>',
300 '#value' => $item['description'],
301 '#suffix' => '</div>',
302 );
303 }
304
305 return array();
306 }
307 }
308

  ViewVC Help
Powered by ViewVC 1.1.2