forked from avr/webform_commerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebform_commerce.component.inc
326 lines (283 loc) · 10 KB
/
webform_commerce.component.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* @file
* Webform module pay component.
*/
/**
* Implements _webform_defaults_component().
*/
function _webform_defaults_commerce() {
return array(
'name' => '',
'form_key' => NULL,
'pid' => 0,
'weight' => 0,
'value' => '',
'extra' => array(
'pricing' => array(
'type' => 'ref',
'product_to_add' => '',
'amount' => '',
),
'price_components' => array(),
'status' => 'unpaid',
),
);
}
/**
* Implements _webform_edit_component().
*/
function _webform_edit_commerce($component) {
$form = array();
$form['extra']['description'] = array(
'#type' => 'value',
'#value' => '',
);
$node = node_load($component['nid']);
$form['extra']['pricing'] = array(
'#type' => 'fieldset',
'#title' => t('Pricing and cost'),
'#description' => t('Establish the cost of purchasing this webform.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['extra']['pricing']['type'] = array(
'#type' => 'select',
'#options' => array('ref' => 'Reference an existing product', 'new' => 'Create a new product'),
'#default_value' => $component['extra']['pricing']['type'],
);
$ref_product = array(
'visible' => array(
'#edit-extra-pricing-type' => array('value' => 'ref'),
),
);
$add_product = array(
'visible' => array(
'#edit-extra-pricing-type' => array('value' => 'new'),
),
'required' => array(
'#edit-extra-pricing-type' => array('value' => 'new'),
),
);
if ($product = commerce_product_load_by_sku($component['extra']['pricing']['product_to_add'])) {
$amount = commerce_product_calculate_sell_price($product);
$currency_code = $amount['currency_code'];
}
$display_amount = isset($amount) ? $amount : '0';
$form['extra']['pricing']['product_to_add'] = array(
'#type' => 'textfield',
'#title' => t('Product to place in the order'),
'#description' => t('This product will be added to the cart when the user clicks the "Submit" button on the form.'),
'#default_value' => $component['extra']['pricing']['product_to_add'],
// Here we're using Commerce Product's autocomplete path in an unusual way
// just to get a product for the form.
'#autocomplete_path' => 'commerce_product/autocomplete/any/any/any',
'#required' => TRUE,
'#ajax' => array(
'callback' => '_webform_edit_commerce_update_pricing',
'wrapper' => 'amount',
),
'#states' => $ref_product,
);
$form['extra']['pricing']['amount'] = array(
'#type' => 'item',
'#title' => t('Current base price'),
'#prefix' => '<div id="amount">',
'#suffix' => '</div>',
'#markup' => '<span class="cost">' . commerce_currency_format($display_amount['amount'], 'USD') . '</span>',
'#states' => $ref_product,
);
$types = commerce_product_types();
$form['extra']['pricing']['product_type'] = array(
'#type' => 'select',
'#title' => t('Type of product to be created'),
'#description' => t('New product types can be created !link.', array('!link' => l('here', 'admin/commerce/products/types'))),
'#options' => drupal_map_assoc(array_keys($types)),
'#states' => $add_product,
);
$form['extra']['pricing']['product_title'] = array(
'#type' => 'textfield',
'#title' => t('Product title'),
'#default_value' => t('Webform product'),
'#states' => $add_product,
);
$form['extra']['pricing']['product_sku'] = array(
'#type' => 'textfield',
'#title' => t('Product sku'),
'#default_value' => '',
'#description' => t('Unique id for this product'),
'#states' => $add_product,
);
$form['extra']['pricing']['product_price'] = array(
'#title' => t('Product price'),
'#type' => 'textfield',
'#description' => t('A price in decimal format, without a currency symbol'),
'#default_value' => '100.00',
'#states' => $add_product,
);
$price_components = webform_component_list($node, 'price', FALSE, FALSE);
$form['extra']['price_components'] = array(
'#type' => 'select',
'#title' => t('Price components'),
'#options' => $price_components,
'#default_value' => $component['extra']['price_components'],
'#multiple' => TRUE,
'#size' => 10,
'#description' => t('Select the components that contain price values. These can be either "select" or "textfield" items. Each of these components will be added as a line item in the user\'s cart.'),
'#process' => array('webform_component_select'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#all_checkbox' => FALSE,
);
return $form;
}
function _webform_edit_commerce_update_pricing(&$form, $form_state) {
$commands = array();
// Adjust the display of the main "Total"
$product = commerce_product_load_by_sku($form_state['input']['extra']['pricing']['product_to_add']);
$wrapper = entity_metadata_wrapper('commerce_product', $product);
$amount = $wrapper->commerce_price->amount->value();
$currency_code = 'USD';
$commands[] = ajax_command_html("#amount .cost", commerce_currency_format($amount, $currency_code));
return array('#type' => 'ajax', '#commands' => $commands);
}
function webform_commerce_form_webform_component_edit_form_alter(&$form, &$form_state, $form_id) {
// This is required in order to return the cost of the refrenced product by ajax.
form_load_include($form_state, 'inc', 'webform', 'includes/webform.components');
// Extra submit handler in order to create product if necessary
// $form['#submit'][] = '_webform_edit_commerce_submit';
// $form['extra']['pricing']['product_to_add']['#default_value'] = $form_state['values']['extra']['pricing']['product_to_add'];
}
/**
* Render a Webform component to be part of a form.
*
* @param $component
* A Webform component array.
* @param $value
* If editing an existing submission or resuming a draft, this will contain
* an array of values to be shown instead of the default in the component
* configuration. This value will always be an array, keyed numerically for
* each value saved in this field.
* @param $filter
* Whether or not to filter the contents of descriptions and values when
* rendering the component. Values need to be unfiltered to be editable by
* Form Builder.
*
* @see _webform_client_form_add_component()
*/
/**
* Implements _webform_render_component().
*/
function _webform_render_commerce($component, $value = NULL, $filter = TRUE) {
// This is a merely a placeholder element, since pay forms need to be located
// at a specific location in the form. This will be populated with the actual
// form in webform_pay_prerender().
$element = array(
'#value' => $value,
'#weight' => $component['weight'],
'#webform_component' => $component,
'#theme_wrapper' => array('webform_element'),
);
return $element;
}
/**
* Implements _webform_submit_component().
*/
function _webform_submit_commerce($component, $value) {
// $value is set in the the custom form validate from webform_commerce.module
$value = array_intersect_key($value, drupal_map_assoc(array(
'total',
'status',
)));
return $value;
}
/**
* Implements _webform_display_component().
*/
/**
* Display the result of a submission for a component.
*
* The output of this function will be displayed under the "Results" tab then
* "Submissions". This should output the saved data in some reasonable manner.
*
* @param $component
* A Webform component array.
* @param $value
* An array of information containing the submission result, directly
* correlating to the webform_submitted_data database table schema.
* @param $format
* Either 'html' or 'text'. Defines the format that the content should be
* returned as. Make sure that returned content is run through check_plain()
* or other filtering functions when returning HTML.
* @return
* A renderable element containing at the very least these properties:
* - #title
* - #weight
* - #component
* - #format
* - #value
* Webform also uses #theme_wrappers to output the end result to the user,
* which will properly format the label and content for use within an e-mail
* (such as wrapping the text) or as HTML (ensuring consistent output).
*/
function _webform_display_commerce($component, $value, $format) {
$element = array(
'#title' => $component['name'],
'#weight' => $component['weight'],
'#format' => $format,
// '#theme' => 'webform_display_address',
'#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
'#sorted' => TRUE,
'#webform_component' => $component,
'#value' => $value,
'status' => array(
'#type' => 'item',
'#title' => t('Status'),
'#markup' => $value['status'],
),
'amount' => array(
'#type' => 'item',
'#title' => t('Amount'),
'#markup' => commerce_currency_format($value['total'], 'USD'),
),
);
return $element;
}
/**
* Implements _webform_table_component().
*/
function _webform_table_commerce($component, $value) {
$output = '';
$output .= "Status: " . ucfirst($value['status']) . "<br />";
$output .= "Amount: " . commerce_currency_format($value['total'], 'USD');
return $output;
}
/**
* Implements _webform_analysis_component().
*/
function _webform_analysis_commerce($component, $sids = array(), $single = FALSE) {
$query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
->fields('wsd', array('no', 'data'))
->condition('nid', $component['nid'])
->condition('cid', $component['cid']);
if (count($sids)) {
$query->condition('sid', $sids, 'IN');
}
$submissions = 0;
$paid = 0;
$total = 0;
$result = $query->execute();
foreach ($result as $data) {
if ($data['data'] == 'unpaid') {
$paid++;
}
if ($data['no'] == 'total') {
$total += $data['data'];
}
$submissions++;
}
$rows[0] = array(t('Submissions'), ($submissions));
$rows[1] = array(t('Paid'), $paid);
$rows[2] = array(t('Total'), commerce_currency_format($total, 'USD'));
return $rows;
}