function views_calc_fields_form_submit in Views Calc 5
Same name and namespace in other branches
- 6.3 views_calc.module \views_calc_fields_form_submit()
- 6 views_calc.module \views_calc_fields_form_submit()
- 7 views_calc.module \views_calc_fields_form_submit()
Save the views calc field settings
File
- ./
views_calc.module, line 325 - This module will allow you to add calculated fields to views tables and compute (SUM, COUNT, AVG, etc) columns of numeric data in a views table.
Code
function views_calc_fields_form_submit($form_id, $form_values) {
$edit = $form_values;
foreach ($edit as $delta => $value) {
if ($value['calc'] == '' || !is_numeric($delta)) {
// remove blank fields, don't save them
unset($form_values[$delta]);
}
else {
$tables = array();
$form_values[$delta]['label'] = $value['label'];
$form_values[$delta]['format'] = $value['format'];
$form_values[$delta]['custom'] = $value['custom'];
$form_values[$delta]['calc'] = $value['calc'];
// Substitute field names back into the calculation.
$matches = array();
foreach (_views_calc_substitutions() as $key => $value) {
$label_patterns[] = "`(^|[^\\\\\\\\])" . preg_quote($value) . "`";
$value_patterns[] = "`(^|[^\\\\\\\\])" . preg_quote($key) . "`";
$repl[] = '${1}' . $key;
}
$form_values[$delta]['calc'] = preg_replace($label_patterns, $repl, $form_values[$delta]['calc']);
// Extract the fields and table names from the calculation.
$tables = array();
$fields = array();
foreach ($value_patterns as $pattern) {
if (preg_match($pattern, $form_values[$delta]['calc'], $results)) {
$fields[] = trim($results[0]);
$tmp = explode('.', trim($results[0]));
if (trim($tmp[0])) {
$tables[trim($tmp[0])] = trim($tmp[0]);
}
}
}
$form_values[$delta]['tablelist'] = implode(',', $tables);
$form_values[$delta]['fieldlist'] = implode(',', $fields);
}
}
foreach ($form_values as $delta => $value) {
if ($value['cid'] == 0) {
$cid = db_next_id('views_calc_field');
$qrytext = "INSERT INTO {views_calc_fields} SET cid = {$cid}, ";
$where = '';
}
else {
$qrytext = "UPDATE {views_calc_fields} SET ";
$where = " WHERE cid = " . $value['cid'];
}
$qrytext = $qrytext . "label = '%s', format = '%s', custom = '%s', calc = '%s', tablelist = '%s', fieldlist = '%s' " . $where;
db_query($qrytext, 'ViewsCalc: ' . $value['label'], $value['format'], $value['custom'], $value['calc'], $value['tablelist'], $value['fieldlist']);
}
views_invalidate_cache();
drupal_set_message(t('Views Calc fields were updated.'));
}