webform_conditional.install in Webform Conditional (Same Page Conditionals) 7.2
Same filename and directory in other branches
Install, update and uninstall functions for the webform_conditional module.
File
webform_conditional.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the webform_conditional module.
*
*/
/**
* @file
* Used for module updates
*
* @author Ted Bowman <ted@tedbow.com>
*/
function webform_conditional_update_6000() {
$sql = "SELECT name, form_key, type, extra,cid,nid FROM {webform_component}";
$result = db_query($sql);
if ($result) {
while ($row = db_fetch_array($result)) {
$extra = unserialize($row['extra']);
$extra['webform_conditional_field_key'] = trim($extra['webform_conditional_field_key']);
if (!empty($extra['webform_conditional_field_key'])) {
$row['extra'] = $extra;
$rows[] = $row;
}
}
if ($rows) {
//keep track of which dependent components coould not be update because keys are not unique
$cannot_update = array();
foreach ($rows as $row) {
if (empty($cannot_update[$row['nid']][$row['extra']['webform_conditional_field_key']])) {
$sql = "SELECT count(cid) FROM {webform_component} WHERE nid = %d and form_key = '%s'";
$count = db_query("SELECT count(cid) FROM {webform_component} WHERE nid = :nid and form_key = :form_key", array(
':nid' => $row['nid'],
':form_key' => $row['extra']['webform_conditional_field_key'],
))
->fetchField();
if ($count == 1) {
//can only update if key appears once in webform node
$sql = "SELECT cid FROM {webform_component} WHERE nid = %d and form_key = '%s'";
//get cid to replace key
$dependet_cid = db_query("SELECT cid FROM {webform_component} WHERE nid = :nid and form_key = :form_key", array(
':nid' => $row['nid'],
':form_key' => $row['extra']['webform_conditional_field_key'],
))
->fetchField();
//no longer storing key
unset($row['extra']['webform_conditional_field_key']);
$row['extra']['webform_conditional_cid'] = $dependet_cid;
$row['extra']['webform_conditional_field_value'] = str_replace("|", "\n", $row['extra']['webform_conditional_field_value']);
$row['extra']['webform_conditional_operator '] = "=";
$extra = serialize($row['extra']);
//can't use update_sql here becauce of serialized data??? is that right?
// Update the SQL database
$and = db_and()
->condition('nid', $row['nid'])
->condition('cid', $row['cid']);
$success = db_update('webform_component')
->fields(array(
'extra' => $extra,
))
->condition($and)
->execute();
$affected = db_affected_rows();
if (!$success || $affected != 1) {
watchdog("wfc_conditional", "Webform_conditional could not update. Could not update webform_component table.", NULL, WATCHDOG_ERROR);
$ret['#abort'] = array(
'success' => FALSE,
);
return $ret;
}
}
else {
$cannot_update[$row['nid']][$row['extra']['webform_conditional_field_key']][] = $row['form_key'];
}
}
}
}
if (!empty($cannot_update)) {
$error_msg = "For Webform conditional the following problems were found during the update:<br /><ul>";
foreach ($cannot_update as $nid => $conditional_keys) {
$error_msg .= "<li>In Node {$nid} the following dependent field keys were not unique:<ul>";
foreach ($conditional_keys as $conditional_key => $keys) {
$error_msg .= "<li>{$conditional_key} used by these components: " . implode(", ", $keys) . "<li>";
}
$error_msg .= "</ul></li>";
}
$error_msg .= "</ul>";
drupal_set_message(check_plain($error_msg), "warning");
}
}
// hook_update_N() no longer returns a $ret array. Instead, return
// nothing or a translated string indicating the update ran successfully.
// See http://drupal.org/node/224333#update_sql.
return t('TODO Add a descriptive string here to show in the UI.');
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function webform_conditional_update_6001() {
$ret = array();
$sql = "SELECT nid, cid,extra FROM {webform_component}";
$result = db_query($sql);
if ($result) {
while ($row = db_fetch_array($result)) {
$extra = unserialize($row['extra']);
if (!empty($extra['webform_conditional_mandatory'])) {
$update_components[$row['nid']][] = $row['cid'];
}
}
if ($update_components) {
foreach ($update_components as $nid => $cids) {
// TODO update_sql has been removed. Use the database API for any schema or data changes.
$ret[] = array();
}
}
}
// hook_update_N() no longer returns a $ret array. Instead, return
// nothing or a translated string indicating the update ran successfully.
// See http://drupal.org/node/224333#update_sql.
return t('TODO Add a descriptive string here to show in the UI.');
}
/**
* Convert per-component conditionals to new more flexible conditional system used in Webform 4.x.
*
* This upgrade makes this module no longer neccesary.
*/
function webform_conditional_update_7201(&$sandbox) {
// Set up the initial batch process.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['last_nid_processed'] = -1;
$sandbox['converted_count'] = 0;
$sandbox['max'] = db_select('webform')
->countQuery()
->execute()
->fetchField();
}
$limit = variable_get('webform_update_batch_size', 100);
$webforms = db_select('webform', 'w')
->fields('w')
->condition('nid', $sandbox['last_nid_processed'], '>')
->orderBy('nid', 'ASC')
->range(0, $limit)
->execute()
->fetchAllAssoc('nid', PDO::FETCH_ASSOC);
foreach ($webforms as $nid => $webform) {
// Update tokens in component configurations.
$result = db_select('webform_component', 'wc', array(
'fetch' => PDO::FETCH_ASSOC,
))
->fields('wc')
->condition('wc.nid', $nid)
->execute();
$rgid = NULL;
foreach ($result as $component) {
// For each component, check if it has conditional properties that need
// to be removed and/or migrated. Because these properties may be in any
// order, copy the original extra array for comparison.
$component['extra'] = unserialize($component['extra']);
$original_extra = $component['extra'];
// Remove conditional properties if present.
if (isset($component['extra']['webform_conditional_cid'])) {
unset($component['extra']['webform_conditional_cid']);
}
if (isset($component['extra']['webform_conditional_operator'])) {
unset($component['extra']['webform_conditional_operator']);
}
if (isset($component['extra']['webform_conditional_field_value'])) {
unset($component['extra']['webform_conditional_field_value']);
// If the component has conditional values specified, that indicates
// that this component was conditionally shown. Convert it to a new
// conditional with multiple rules if needed.
if (strlen(trim($original_extra['webform_conditional_field_value'])) && !empty($original_extra['webform_conditional_operator']) && !empty($original_extra['webform_conditional_cid'])) {
$conditional_values = explode("\n", $original_extra['webform_conditional_field_value']);
if ($rgid === NULL) {
// Webform update function webform_update_7403 may have already added records to for this nid
$existing_query = db_select('webform_conditional', 'wc');
$existing_query
->addField('wc', 'rgid');
$existing_query
->condition('nid', $nid);
$existing_query
->orderBy('rgid', 'DESC');
$existing_query
->range(0, 1);
$rgid = $existing_query
->execute()
->fetchField();
if (empty($rgid)) {
$rgid = 0;
}
else {
$rgid++;
}
}
$rules = array();
$rule = array(
'nid' => $nid,
'rgid' => $rgid,
'rid' => NULL,
'source_type' => 'component',
'source' => $original_extra['webform_conditional_cid'],
'operator' => 'equal',
'value' => NULL,
);
foreach ($conditional_values as $value) {
$value = trim($value);
if ($value) {
$new_rule = $rule;
$new_rule['rid'] = count($rules);
$new_rule['value'] = $value;
$rules[] = $new_rule;
}
}
if (count($rules)) {
$conditional = array(
'nid' => $nid,
'rgid' => $rgid,
'andor' => 'or',
'action' => $original_extra['webform_conditional_operator'] === '=' ? 'show' : 'hide',
'target_type' => 'component',
'target' => $component['cid'],
'weight' => 0,
);
drupal_write_record('webform_conditional', $conditional);
foreach ($rules as $rule) {
drupal_write_record('webform_conditional_rules', $rule);
}
$sandbox['converted_count']++;
$rgid++;
}
}
}
// Update the component with the conditional properties removed.
if ($component['extra'] != $original_extra) {
$component['extra'] = serialize($component['extra']);
drupal_write_record('webform_component', $component, array(
'nid',
'cid',
));
}
}
}
// If less than limit was processed, the update process is finished.
if (count($webforms) < $limit || $sandbox['progress'] == $sandbox['max']) {
$finished = TRUE;
}
// If there's no max value then there's nothing to update and we're finished.
if (empty($sandbox['max']) || isset($finished)) {
return t('@count webforms using same page conditionals updated to the new conditional system. You may now uninstall the Webform Conditional Module.', array(
'@count' => $sandbox['converted_count'],
));
}
else {
// Indicate our current progress to the batch update system.
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}
}
Functions
Name | Description |
---|---|
webform_conditional_update_6000 | @file Used for module updates |
webform_conditional_update_6001 | @todo Please document this function. |
webform_conditional_update_7201 | Convert per-component conditionals to new more flexible conditional system used in Webform 4.x. |