uc_webform_pane.module in Ubercart Webform Checkout Pane 6.3
Same filename and directory in other branches
Ubercart Webform Checkout Pane.
@note Webform data will be stored in database during checkout, even when order is not complete.
File
uc_webform_pane.moduleView source
<?php
/**
* @file uc_webform_pane.module
*
* Ubercart Webform Checkout Pane.
*
* @note Webform data will be stored in database during checkout, even when order is not complete.
*/
/**
* Implementation of hook_theme().
*/
function uc_webform_pane_theme() {
$theme = array(
'uc_webform_pane_order_view' => array(
'arguments' => array(
'data' => NULL,
),
),
);
return $theme;
}
/**
* Implementation of hook_form_alter().
*/
function uc_webform_pane_form_alter(&$form, &$form_state, $form_id) {
global $user;
// Make sure the webform form supports files.
if ($form_id == 'uc_cart_checkout_form' || $form_id == 'uc_order_edit_form') {
$form['#attributes']['enctype'] = 'multipart/form-data';
}
// Support for Webform Validation module
if ($form_id == 'uc_cart_checkout_form' && function_exists('webform_validation_validate')) {
$form['#validate'][] = 'uc_webform_pane_wv_validate';
}
// Add a config option to webform nodes to enable them as checkout panes.
if ($form_id == 'webform_configure_form') {
$form['advanced']['uc_webform_pane'] = array(
'#type' => 'checkbox',
'#return_value' => 1,
'#default_value' => uc_webform_pane_nodestatus($form['nid']['#value']),
'#title' => t('Generate a checkout pane'),
'#description' => t('Allow this form to be used as a Ubercart checkout pane.'),
'#weight' => -15,
'#access' => user_access('administer store'),
);
$form['#submit'][] = 'uc_webform_pane_webform_configure_form_submit';
}
// Add access validation to all UC checkout pane webforms.
if (preg_match('|webform_.*_form|', $form_id)) {
$nid = arg(1);
if (uc_webform_pane_nodestatus($nid)) {
$form['uc_access'] = array(
'#type' => 'hidden',
'#value' => user_access('administer store', $user) ? '1' : '0',
);
$form['#validate'][] = 'uc_webform_pane_access_validate';
}
}
// Support for Webform Conditional module
if (function_exists('_webform_condtional_component_is_conditional') && strstr($form_id, 'webform_client_form_') && $form['#node']->in_checkout) {
$nid = $form['details']['nid']['#value'];
foreach (element_children($form['submitted']) as $key) {
// Add an adapted after_build function
$field = $form['submitted'][$key]['#webform_component'];
if ($field['type'] != 'fieldset' && _webform_condtional_component_is_conditional($field, $nid)) {
$form['submitted'][$key]['#after_build'][] = 'uc_webform_pane_webform_conditional_element_after_build';
}
}
}
}
/**
* Send the values of each Webform Pane through to Webform Validation checkers.
*/
function uc_webform_pane_wv_validate($form, &$form_state) {
$panes = $form_state['values']['panes'];
if (!$panes) {
return;
}
foreach ($panes as $paneid => $values) {
if (substr($paneid, 0, 11) == 'webform_nid') {
$nid = substr($paneid, 11);
$pane_values = array();
$pane_values['op'] = t('Submit');
$pane_values['submitted'] = $values[0];
$pane_values['details']['nid'] = $nid;
$pane_state = array();
$pane_state['values'] = $pane_values;
webform_validation_validate($form, $pane_state);
}
}
}
/**
* Make sure that the user trying to modify the UC checkout pane enabled webform has adequate Ubercart permissions.
*/
function uc_webform_pane_access_validate($form, &$form_state) {
if ($form_state['values']['uc_access'] === '0') {
form_set_error('uc_access', t('This webform is used as an Ubercart checkout pane and cannot be accessed outside of Ubercart and/or without the appropriate Ubercart permissions.'));
}
}
/**
* Mark the webform as a potential checkout pane.
*/
function uc_webform_pane_webform_configure_form_submit($form, &$form_state) {
if ($form_state['values']['uc_webform_pane']) {
if (!db_result(db_query("SELECT nid FROM {uc_webform_pane} WHERE nid = %d", $form_state['values']['nid']))) {
db_query("INSERT INTO {uc_webform_pane} (nid) VALUES (%d)", $form_state['values']['nid']);
}
}
else {
db_query("DELETE FROM {uc_webform_pane} WHERE nid = %d", $form_state['values']['nid']);
}
}
/**
* Implementation of hook_nodeapi().
*/
function uc_webform_pane_nodeapi(&$node, $op, $a3 = NULL, $a4 = NUL) {
if (in_array($node->type, webform_variable_get('webform_node_types'))) {
switch ($op) {
case 'load':
$node->webform['uc_webform_pane'] = uc_webform_pane_nodestatus($node->nid);
break;
case 'delete':
db_query("DELETE FROM {uc_webform_pane} WHERE nid = %d", $node->nid);
break;
}
}
}
/**
* Check if a webform node should be selectable as a checkout pane.
*/
function uc_webform_pane_nodestatus($nid) {
$found = db_result(db_query("SELECT nid FROM {uc_webform_pane} WHERE nid = %d", $nid));
return $found ? TRUE : FALSE;
}
/**
* Implementation of hook_token_list().
*/
function uc_webform_pane_token_list($type = 'all') {
$tokens = array();
if ($type == 'order' || $type == 'ubercart' || $type == 'all') {
$webforms = _uc_webform_pane_get_nodes();
foreach ($webforms as $webform) {
$nid = $webform->nid;
$node = node_load($nid);
foreach ($node->webform['components'] as $field) {
if ($field['type'] == 'markup') {
continue;
}
$tokens['order']['uc-webform-' . $nid . '-' . str_replace('_', '-', $field['form_key'])] = check_plain($node->title) . ': ' . check_plain($field['name']);
}
}
}
return $tokens;
}
/**
* Implementation of hook_token_values().
*/
function uc_webform_pane_token_values($type, $object = NULL) {
$values = array();
switch ($type) {
case 'order':
$webforms = _uc_webform_pane_get_nodes();
foreach ($webforms as $webform) {
$nid = $webform->nid;
$node = node_load($nid);
$data = get_object_vars(_uc_webform_pane_get_data($nid, $object->order_id));
foreach ($node->webform['components'] as $field) {
if ($field['type'] == 'markup') {
continue;
}
$field_data = isset($data['data'][$field['cid']]['value']) ? $data['data'][$field['cid']]['value'] : NULL;
$field_element = webform_component_invoke($field['type'], 'display', $field, $field_data);
$submission_output = theme($field_element['#theme'], $field_element);
$values['uc-webform-' . $nid . '-' . str_replace('_', '-', $field['form_key'])] = $submission_output;
}
}
break;
}
return $values;
}
/**
* Implementation of hook_checkout_pane().
*/
function uc_webform_pane_checkout_pane() {
$webforms = _uc_webform_pane_get_nodes();
$panes = array();
foreach ($webforms as $webform) {
$nid = $webform->nid;
$funcname = '_uc_webform_pane_' . $nid;
// Dynamically create callback function
if (!function_exists($funcname)) {
eval('function ' . $funcname . '($op, &$arg1, $arg2) { return uc_webform_pane_checkout_pane_callback(' . $nid . ',$op, $arg1, $arg2); } ');
}
$node = node_load($nid);
$panes[] = array(
'id' => 'webform_nid' . $nid,
'callback' => $funcname,
'title' => check_plain($node->title),
'desc' => $node->body,
'weight' => 0,
);
}
return $panes;
}
/**
* Checkout Pane callback for dynamic callback function.
*
* @link http://www.ubercart.org/docs/developer/245/checkout.
* @param $arg1 is the current order object if it exists.
* @param $arg2 is the contents of the array of the submitted form information for that pane.
*/
function uc_webform_pane_checkout_pane_callback($nid, $op, &$arg1, $arg2) {
switch ($op) {
case 'view':
$form = _uc_webform_pane_edit_form($nid, $arg1->order_id, 'checkout');
$node = node_load($nid);
return array(
'contents' => array(
$form,
),
'description' => $node->body,
);
break;
case 'process':
_uc_webform_pane_save($nid, $arg1->order_id, $arg2[0]);
// save to database
return TRUE;
break;
case 'review':
$data = _uc_webform_pane_render($nid, $arg1->order_id);
if ($data) {
return $data;
}
break;
default:
break;
}
}
/**
* Implementation of drupal_alter('checkout_pane', $panes).
*/
function uc_webform_pane_checkout_pane_alter(&$panes) {
// Validate all the webform panes individually during the checkout process
foreach ($panes as $pid => $pane) {
if (substr($pane['id'], 0, 11) == 'webform_nid') {
$nid = substr($pane['id'], 11);
$node = node_load($nid);
// Check if this page is cached or not
$cached = $user->uid == 0 && (variable_get('cache', 0) || function_exists('drupal_page_is_cacheable') && drupal_page_is_cacheable() === FALSE);
// Check if the webform user submission limit has already been reached
if ($node->webform['submit_limit'] != -1) {
// -1: Submissions are never throttled.
module_load_include('inc', 'webform', 'includes/webform.submissions');
// Disable the form if the limit is exceeded and page cache is not active
if (($user_limit_exceeded = _webform_submission_user_limit_check($node)) && !$cached) {
$panes[$pid]['enabled'] = FALSE;
}
}
// Check if the webform total submission limit has already been reached
if ($node->webform['total_submit_limit'] != -1) {
// -1: Submissions are never throttled.
module_load_include('inc', 'webform', 'includes/webform.submissions');
// Disable the form if the limit is exceeded and page cache is not active
if (($total_limit_exceeded = _webform_submission_total_limit_check($node)) && !$cached) {
$panes[$pid]['enabled'] = FALSE;
}
}
}
}
}
/**
* Implementation of hook_uc_checkout_complete().
*/
function uc_webform_pane_uc_checkout_complete($order, $account) {
// Assign the Webform pane submissions to the newly created user
if (isset($order->data['new_user'])) {
$webforms = _uc_webform_pane_get_nodes();
foreach ($webforms as $webform) {
$nid = $webform->nid;
// Get the sid for webform submission in the current order
$sid = _uc_webform_pane_get_sid($nid, $order->order_id);
if ($sid) {
db_query("UPDATE {webform_submissions} SET uid = %d WHERE sid = %d", $order->uid, $sid);
}
}
}
}
/**
* Implementation of hook_order_pane().
*/
function uc_webform_pane_order_pane() {
$webforms = _uc_webform_pane_get_nodes();
$panes = array();
foreach ($webforms as $webform) {
$nid = $webform->nid;
$funcname = '_uc_webform_order_pane_' . $nid;
// Dynamically create callback function
if (!function_exists($funcname)) {
eval('function ' . $funcname . '($op, $arg1) { return uc_webform_pane_order_pane_callback(' . $nid . ',$op, $arg1); } ');
}
$node = node_load($nid);
$panes[] = array(
'id' => 'webform_nid' . $nid,
'callback' => $funcname,
'title' => check_plain($node->title),
'desc' => $node->body,
'class' => 'abs-left',
'weight' => 7,
'show' => array(
'view',
'customer',
'edit',
),
);
}
return $panes;
}
/**
* Order Pane callback for dynamic order callback function.
*/
function uc_webform_pane_order_pane_callback($nid, $op, $arg1) {
switch ($op) {
case 'view':
case 'customer':
$data = _uc_webform_pane_render($nid, $arg1->order_id);
if ($data) {
return theme('uc_webform_pane_order_view', $data);
}
break;
case 'edit-form':
$webform = _uc_webform_pane_edit_form($nid, $arg1->order_id, 'order');
$node = node_load($nid);
$form['webform_nid' . $nid] = array(
'#type' => 'fieldset',
'#title' => t('Modify @title information', array(
'@title' => $node->title,
)),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
foreach ($webform as $field_id => $field_settings) {
$form['webform_nid' . $nid][$field_id] = $field_settings;
}
return $form;
break;
case 'edit-theme':
drupal_add_css(drupal_get_path('module', 'uc_webform_pane') . '/uc_webform_pane.css');
$output = '<div id="webform-' . $nid . '-select"></div><table class="uc-webform-edit-table">';
foreach (element_children($arg1['webform_nid' . $nid]) as $fname) {
if (in_array($arg1['webform_nid' . $nid][$fname]['#type'], array(
'value',
'markup',
))) {
continue;
}
else {
if ($arg1['webform_nid' . $nid][$fname]['#type'] == 'hidden') {
$output .= drupal_render($arg1['webform_nid' . $nid][$fname]);
}
else {
$field = $arg1['webform_nid' . $nid][$fname];
$title = $field['#title'];
// Process file widgets
if (isset($field['file'])) {
$title = $field['file']['#title'];
$field['file']['#title'] = NULL;
}
// Process select_or_other widgets
if (isset($field['select'])) {
$title = $field['select']['#title'];
$field['select']['#title'] = NULL;
}
$field['#title'] = NULL;
$output .= '<tr><td class="uwet-label">' . check_plain($title) . ':</td><td>' . drupal_render($field) . '</td></tr>';
}
}
}
$output .= '</table>';
return $output;
break;
case 'edit-process':
_uc_webform_pane_save($nid, $arg1['order_id'], $arg1['webform_nid' . $nid]);
break;
}
}
/**
* Implementation of hook_order().
*/
function uc_webform_pane_order($op, &$arg1, $arg2) {
switch ($op) {
// Delete from the database.
case 'delete':
include_once drupal_get_path('module', 'webform') . "/includes/webform.submissions.inc";
$uc_wf_subs = db_query("SELECT nid, sid FROM {uc_webform_pane_submissions} WHERE order_id = %d", $arg1->order_id);
while ($uc_wf_sub = db_fetch_object($uc_wf_subs)) {
$node = node_load($uc_wf_sub->nid);
$submission = webform_get_submission($uc_wf_sub->nid, $uc_wf_sub->sid);
webform_submission_delete($node, $submission);
}
db_query("DELETE FROM {uc_webform_pane_submissions} WHERE order_id = %d", $arg1->order_id);
break;
}
}
/**
* Save pane values as webform submission through Webform submit handler.
*/
function _uc_webform_pane_save($nid, $order_id, $values) {
// Don't bother doing all this if the checkout form didn't validate in the first place
if (form_get_errors()) {
return;
}
$node = node_load($nid);
if ($sid = _uc_webform_pane_get_sid($nid, $order_id)) {
$form_values['details']['sid'] = $sid;
}
$form_values['op'] = t('Submit');
$form_values['submitted'] = $values;
$form_values['details']['nid'] = $nid;
$form_state = array();
$form_state['values'] = $form_values;
// Perform post processing by components.
_webform_client_form_submit_process($node, $form_state['values']['submitted']);
// Flatten trees within the submission.
$form_state['values']['submitted_tree'] = $form_state['values']['submitted'];
$form_state['values']['submitted'] = _webform_client_form_submit_flatten($node, $form_state['values']['submitted']);
// Do some cleaning up before our fake submit
foreach ($form_state['values']['submitted'] as $key => $value) {
// Clean up anything that is not a Webform component
if (!is_numeric($key)) {
unset($form_state['values']['submitted'][$key]);
}
// Clean up empty values as these won't be saved anyway
if (is_array($value) && sizeof($value) == 0) {
unset($form_state['values']['submitted'][$key]);
}
}
// If the values list ends up empty, all required fields must be empty - get out
if (!$form_state['values']['submitted']) {
return;
}
// Set a flag indicating processing should continue and be saved.
$form_state['webform_completed'] = TRUE;
webform_client_form_submit(array(
'#node' => $node,
), $form_state);
if (!$sid) {
$sid = $form_state['values']['details']['sid'];
db_query("INSERT INTO {uc_webform_pane_submissions} (nid, sid, order_id) VALUES (%d, %d, %d)", $nid, $sid, $order_id);
}
}
/**
* Return webform submission id.
*/
function _uc_webform_pane_get_sid($nid, $order_id) {
return db_result(db_query("SELECT sid FROM {uc_webform_pane_submissions} WHERE nid = %d AND order_id = %d", $nid, $order_id));
}
/**
* Return webform submission data.
*/
function _uc_webform_pane_get_data($nid, $order_id) {
if ($sid = _uc_webform_pane_get_sid($nid, $order_id)) {
include_once drupal_get_path('module', 'webform') . "/includes/webform.submissions.inc";
return webform_get_submission($nid, $sid);
}
$empty = new stdClass();
return $empty;
}
/**
* Return list of webform nodes.
*/
function _uc_webform_pane_get_nodes() {
$q = db_query(db_rewrite_sql("\n SELECT n.nid FROM {node} n NATURAL JOIN {uc_webform_pane} \n WHERE n.type IN ('" . implode(webform_variable_get('webform_node_types'), "', '") . "') AND n.status = 1 \n ORDER BY n.title DESC\n "));
$webforms = array();
while ($webform = db_fetch_object($q)) {
$webforms[] = $webform;
}
return $webforms;
}
/**
* Render webform submission data.
*/
function _uc_webform_pane_render($nid, $order_id) {
$data = get_object_vars(_uc_webform_pane_get_data($nid, $order_id));
$output = array();
if ($data) {
$node = node_load($nid);
foreach ($node->webform['components'] as $field) {
if (!in_array($field['type'], array(
'hidden',
'markup',
'fieldset',
))) {
$field_data = isset($data['data'][$field['cid']]['value']) ? $data['data'][$field['cid']]['value'] : NULL;
$field_element = webform_component_invoke($field['type'], 'display', $field, $field_data);
$submission_output = theme($field_element['#theme'], $field_element);
if ($submission_output !== NULL && trim($submission_output)) {
$output[] = array(
'title' => check_plain($field['name']),
'data' => $submission_output,
);
}
}
}
}
return $output;
}
/**
* Theme webform submission data on the order view page.
*/
function theme_uc_webform_pane_order_view($data) {
$output = '<table>';
foreach ($data as $row) {
$border = '';
if (isset($row['border'])) {
$border = ' class="row-border-' . $row['border'] . '"';
}
$output .= '<tr valign="top"' . $border . '><td class="title-col" ' . 'nowrap>' . $row['title'] . ':</td><td class="data-col">' . $row['data'] . '</td></tr>';
}
$output .= '</table>';
return $output;
}
/**
* Generate a form to edit webform submission data.
*/
function _uc_webform_pane_edit_form($nid, $order_id, $pane_type) {
drupal_add_css(drupal_get_path('module', 'webform') . '/css/webform.css');
drupal_add_js(drupal_get_path('module', 'webform') . '/js/webform.js');
// Load the node and make sure it's a Webform
$node = node_load($nid);
if (!$node->webform['components']) {
return array();
}
$submission = _uc_webform_pane_get_data($nid, $order_id);
// Mark the node as a checkout pane
$node->in_checkout = TRUE;
// Load the Webform
$form_state = array();
$form = drupal_retrieve_form('webform_client_form_' . $node->nid, $form_state, $node, $submission);
drupal_prepare_form('webform_client_form_' . $node->nid, $form, $form_state);
// Remove some things that get in the way
foreach (array(
'details',
'form_id',
'form_token',
'actions',
'#parents',
'#theme',
'#tree',
'#id',
'#type',
) as $key) {
unset($form[$key]);
}
// Wrap the form in same id's as it would be as a real Webform
$form['#prefix'] = '<div id="webform-client-form-' . $nid . '">';
$form['#suffix'] = '</div>';
foreach ($form['submitted'] as $key => $field) {
$form[$key] = $field;
}
unset($form['submitted']);
// Prepare form fields for processing using UCWP
_uc_webform_pane_edit_form_prepare($form, $pane_type);
return $form;
}
/**
* Prepare form fields for processing using UCWP.
*/
function _uc_webform_pane_edit_form_prepare(&$form, $pane_type) {
foreach (element_children($form) as $k) {
$form[$k]["#validated"] = FALSE;
if ($pane_type == 'order') {
$form[$k]["#required"] = FALSE;
}
if ($form[$k]["#type"] == 'fieldset') {
_uc_webform_pane_edit_form_prepare($form[$k], $pane_type);
}
}
}
/**
* Implementation of drupal_alter('webform_conditional_css_ids', $css_ids, $form).
*/
function uc_webform_pane_webform_conditional_css_ids_alter($css_ids, $form) {
$nid = $form['#node']->nid;
// If this is a checkout pane, adjust the CSS ids of Webform elements
if ($form['#node']->in_checkout) {
foreach ($css_ids as $id => $css_id) {
$css_ids[$id] = str_replace('webform-component-', 'webform-component-panes--webform-nid' . $nid . '--0--', $css_id);
}
}
}
/**
* An adapted version of Webform Conditional's webform_conditional_element_after_build().
*/
function uc_webform_pane_webform_conditional_element_after_build($form_element, &$form_state) {
if (!empty($form_element['#post'])) {
// Load the Webform node
$nid = $form_element['#webform_component']['nid'];
$node = node_load($nid);
// Create a form_state array of the appropriate type
$pane_state = array();
$pane_state['webform']['component_tree']['children'] = $node->webform['components'];
$pane_state['values']['submitted'] = $form_element['#post']['panes']['webform_nid' . $nid][0];
// Fetch submitted values
$submitted_values = _webform_conditional_get_submitted_values($pane_state);
// Load Webform components
$components = _webform_conditional_get_all_components($form_element['#webform_component']['nid'], $submitted_values);
$was_hidden = _webform_conditional_was_hidden($form_element['#webform_component']['cid'], $components);
// If the element was hidden, set is as not required
if ($was_hidden) {
$form_element['#required'] = FALSE;
$form_element['#validated'] = TRUE;
_webform_condtional_clear_element_values($form_element, $form_state);
}
}
return $form_element;
}
Functions
Name![]() |
Description |
---|---|
theme_uc_webform_pane_order_view | Theme webform submission data on the order view page. |
uc_webform_pane_access_validate | Make sure that the user trying to modify the UC checkout pane enabled webform has adequate Ubercart permissions. |
uc_webform_pane_checkout_pane | Implementation of hook_checkout_pane(). |
uc_webform_pane_checkout_pane_alter | Implementation of drupal_alter('checkout_pane', $panes). |
uc_webform_pane_checkout_pane_callback | Checkout Pane callback for dynamic callback function. |
uc_webform_pane_form_alter | Implementation of hook_form_alter(). |
uc_webform_pane_nodeapi | Implementation of hook_nodeapi(). |
uc_webform_pane_nodestatus | Check if a webform node should be selectable as a checkout pane. |
uc_webform_pane_order | Implementation of hook_order(). |
uc_webform_pane_order_pane | Implementation of hook_order_pane(). |
uc_webform_pane_order_pane_callback | Order Pane callback for dynamic order callback function. |
uc_webform_pane_theme | Implementation of hook_theme(). |
uc_webform_pane_token_list | Implementation of hook_token_list(). |
uc_webform_pane_token_values | Implementation of hook_token_values(). |
uc_webform_pane_uc_checkout_complete | Implementation of hook_uc_checkout_complete(). |
uc_webform_pane_webform_conditional_css_ids_alter | Implementation of drupal_alter('webform_conditional_css_ids', $css_ids, $form). |
uc_webform_pane_webform_conditional_element_after_build | An adapted version of Webform Conditional's webform_conditional_element_after_build(). |
uc_webform_pane_webform_configure_form_submit | Mark the webform as a potential checkout pane. |
uc_webform_pane_wv_validate | Send the values of each Webform Pane through to Webform Validation checkers. |
_uc_webform_pane_edit_form | Generate a form to edit webform submission data. |
_uc_webform_pane_edit_form_prepare | Prepare form fields for processing using UCWP. |
_uc_webform_pane_get_data | Return webform submission data. |
_uc_webform_pane_get_nodes | Return list of webform nodes. |
_uc_webform_pane_get_sid | Return webform submission id. |
_uc_webform_pane_render | Render webform submission data. |
_uc_webform_pane_save | Save pane values as webform submission through Webform submit handler. |