| 1 |
<?php |
<?php |
|
|
|
| 2 |
/** |
/** |
| 3 |
* @file |
* @file |
| 4 |
* This module will allow you to add calculated fields to views tables |
* This module will allow you to add calculated fields to views tables |
| 113 |
$items['admin/settings/views_calc/settings'] = array( |
$items['admin/settings/views_calc/settings'] = array( |
| 114 |
'title' => t('Settings'), |
'title' => t('Settings'), |
| 115 |
'type' => MENU_LOCAL_TASK, |
'type' => MENU_LOCAL_TASK, |
| 116 |
'weight' => 10, |
'weight' => 6, |
| 117 |
'priority' => 1, |
'priority' => 1, |
| 118 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 119 |
'page arguments' => array('views_calc_settings_form'), |
'page arguments' => array('views_calc_settings_form'), |
| 120 |
'access arguments' => array('administer views calc'), |
'access arguments' => array('administer views calc'), |
| 121 |
); |
); |
| 122 |
|
$items['admin/settings/views_calc/export'] = array( |
| 123 |
|
'title' => 'Export fields', |
| 124 |
|
'page callback' => 'drupal_get_form', |
| 125 |
|
'page arguments' => array('views_calc_export_form'), |
| 126 |
|
'access arguments' => array('create views calc'), |
| 127 |
|
'type' => MENU_LOCAL_TASK, |
| 128 |
|
'weight' => 7, |
| 129 |
|
); |
| 130 |
|
$items['admin/settings/views_calc/import'] = array( |
| 131 |
|
'title' => 'Import fields', |
| 132 |
|
'page callback' => 'drupal_get_form', |
| 133 |
|
'page arguments' => array('views_calc_import_form'), |
| 134 |
|
'access arguments' => array('create views calc'), |
| 135 |
|
'type' => MENU_LOCAL_TASK, |
| 136 |
|
'weight' => 8, |
| 137 |
|
); |
| 138 |
|
|
| 139 |
return $items; |
return $items; |
| 140 |
} |
} |
| 141 |
|
|
| 278 |
if ($item['calc'] == '' || !is_numeric($delta)) { |
if ($item['calc'] == '' || !is_numeric($delta)) { |
| 279 |
// remove blank fields, don't save them |
// remove blank fields, don't save them |
| 280 |
unset($form_values[$delta]); |
unset($form_values[$delta]); |
| 281 |
} else { |
} |
| 282 |
|
else { |
| 283 |
// Remove all valid values from calc, if anything is left over, it is invalid. |
// Remove all valid values from calc, if anything is left over, it is invalid. |
| 284 |
|
|
| 285 |
// First, remove all field names. |
// First, remove all field names. |
| 411 |
$operators = array_filter(_views_calc_operators(), 'trim'); |
$operators = array_filter(_views_calc_operators(), 'trim'); |
| 412 |
$numbers = range(0, 9); |
$numbers = range(0, 9); |
| 413 |
return array_merge($operators, $numbers); |
return array_merge($operators, $numbers); |
| 414 |
|
} |
| 415 |
|
|
| 416 |
|
|
| 417 |
|
/** |
| 418 |
|
* Field export form. |
| 419 |
|
*/ |
| 420 |
|
function views_calc_export_form() { |
| 421 |
|
|
| 422 |
|
$fields = _views_calc_fields(); |
| 423 |
|
$string = ''; |
| 424 |
|
while ($field = db_fetch_array($fields)) { |
| 425 |
|
$string .= "\$fields[] = ". var_export((array) $field, TRUE) .";\n"; |
| 426 |
|
} |
| 427 |
|
|
| 428 |
|
$form['#prefix'] = t('This form will export Views Calc custom fields.'); |
| 429 |
|
$form['macro'] = array( |
| 430 |
|
'#type' => 'textarea', |
| 431 |
|
'#rows' => 20, |
| 432 |
|
'#title' => t('Export data'), |
| 433 |
|
'#default_value' => $string, |
| 434 |
|
'#description' => t('This is an export of the custom Views Calc fields. Paste this text into a Views Calc import box to import these fields into another installation. This will only work if the other installation uses the same base tables required by these fields.'), |
| 435 |
|
); |
| 436 |
|
return $form; |
| 437 |
|
} |
| 438 |
|
|
| 439 |
|
/** |
| 440 |
|
* Field import form. |
| 441 |
|
*/ |
| 442 |
|
function views_calc_import_form(&$form_state, $type_name = '') { |
| 443 |
|
$form['#prefix'] = t('This form will import Views Calc custom fields.'); |
| 444 |
|
$form['macro'] = array( |
| 445 |
|
'#type' => 'textarea', |
| 446 |
|
'#rows' => 20, |
| 447 |
|
'#title' => t('Import data'), |
| 448 |
|
'#required' => TRUE, |
| 449 |
|
'#description' => t('Paste the text created by a Views Calc export into this field.'), |
| 450 |
|
); |
| 451 |
|
$form['submit'] = array( |
| 452 |
|
'#type' => 'submit', |
| 453 |
|
'#value' => t('Import'), |
| 454 |
|
); |
| 455 |
|
// Read in a file if there is one and set it as the default macro value. |
| 456 |
|
if (isset($_REQUEST['macro_file']) && $file = file_get_contents($_REQUEST['macro_file'])) { |
| 457 |
|
$form['macro']['#default_value'] = $file; |
| 458 |
|
if (isset($_REQUEST['type_name'])) { |
| 459 |
|
$form['type_name']['#default_value'] = $_REQUEST['type_name']; |
| 460 |
|
} |
| 461 |
|
$form['#prefix'] .= '<p class="error">'. t('A file has been pre-loaded for import.') .'</p>'; |
| 462 |
|
} |
| 463 |
|
return $form; |
| 464 |
|
} |
| 465 |
|
|
| 466 |
|
/** |
| 467 |
|
* Submit handler for import form. |
| 468 |
|
*/ |
| 469 |
|
function views_calc_import_form_submit($form, &$form_state) { |
| 470 |
|
$form_values = $form_state['values']; |
| 471 |
|
$fields = NULL; |
| 472 |
|
|
| 473 |
|
// Use '@' to suppress errors about undefined constants in the macro. |
| 474 |
|
@eval($form_values['macro']); |
| 475 |
|
|
| 476 |
|
if (empty($fields) || !is_array($fields)) { |
| 477 |
|
return; |
| 478 |
|
} |
| 479 |
|
|
| 480 |
|
foreach ($fields as $delta => $field) { |
| 481 |
|
// Don't over-write existing fields, create new ones. |
| 482 |
|
$fields[$delta]['cid'] = NULL; |
| 483 |
|
} |
| 484 |
|
|
| 485 |
|
// Run the values thru drupal_execute() so they are properly validated. |
| 486 |
|
$form_state = array('values' => $fields); |
| 487 |
|
drupal_execute('views_calc_fields_form', $form_state); |
| 488 |
|
|
| 489 |
} |
} |