2, 'path' => drupal_get_path('module', 'views_calc'), ); } function views_calc_theme() { $path = drupal_get_path('module', 'views_calc'); return array( 'views_calc_ui_table' => array( 'arguments' => array('form' => NULL), 'file' => 'theme.inc', ), ); } /** * Implementation of hook_help(). */ function views_calc_help($section, $arg) { switch ($section) { case 'admin/settings/views_calc': case 'admin/settings/views_calc/fields': return t('
Set up calculation fields. Calculation fields will be displayed in the views fields list and can be added to any view.
'); case 'admin/settings/views_calc/settings': return t('Put one operator on each line. To avoid the possibility of SQL injection, calculation text will only allow these values, numbers, and field names. Make sure this list includes any text other than field names that should be allowed in the calculation fields.'); case 'admin/help#views_calc': return t('The query operation to be performed, using numbers, field substitutions, and '. implode(' ', _views_calc_operators()) .'.
'), ); $form['group'][$i]['format'] = array( '#type' => 'select', '#title' => t('Format'), '#default_value' => $field['format'], '#options' => drupal_map_assoc(array_keys(_views_calc_format_options())), '#description' => t('The format of the result of this calculation.'), ); $form['group'][$i]['custom'] = array( '#type' => 'textfield', '#title' => t('Custom function'), '#default_value' => $field['custom'], '#description' => t('The function to call for a custom format.'), ); return $form; } /** * Validate the views calc settings */ function views_calc_fields_form_validate($form, &$form_state) { $form_values = $form_state['values']; $edit = $form_values; foreach ($edit as $delta => $item) { if ($item['calc'] == '' || !is_numeric($delta)) { // remove blank fields, don't save them unset($form_values[$delta]); } else { // Remove all valid values from calc, if anything is left over, it is invalid. // First, remove all field names. $repl = array(); $patterns = array(); foreach (_views_calc_substitutions() as $key => $value) { $key = trim($value); $count = strlen($value); $replace = preg_quote($value); $patterns[] = "`(^|[^\\\\\\\\])". $replace ."`"; $repl[] = '${1}'; } $remaining = trim(preg_replace($patterns, $repl, $item['calc'])); // Next, remove functions and numbers. $repl = array(); $patterns = array(); foreach (_views_calc_replacements() as $value) { $patterns[] = "`(^|[^\\\\\\\\])". preg_quote(trim($value)) ."`"; $repl[] = '${1}'; } $remaining = trim(preg_replace($patterns, $repl, $remaining)); if (!empty($remaining)) { form_set_error($form_values[$delta]['calc'], t('The values %remaining in %field are not allowed.', array('%remaining' => $remaining, '%field' => $item['label']))); } } } } /** * Save the views calc field settings */ function views_calc_fields_form_submit($form, &$form_state) { $form_values = $form_state['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) { drupal_write_record('views_calc_fields', $value); } else { drupal_write_record('views_calc_fields', $value, array('cid')); } } views_invalidate_cache(); drupal_set_message(t('Views Calc fields were updated.')); } /** * Wrapper function to make sure this function will always work. */ function views_calc_views_fetch_fields($base, $type) { if (!module_exists('views')) { return array(); } require_once('./'. drupal_get_path('module', 'views') .'/includes/admin.inc'); return views_fetch_fields($base, $type); } /** * Field substitutions for calculations. */ function _views_calc_substitutions($base = 'node') { $fields = views_calc_views_fetch_fields($base, 'field'); $substitutions['node.nid'] = '%Node.nid'; $substitutions['node.uid'] = '%Node.uid'; foreach ($fields as $key => $field) { // For now, omit calculated fields from available fields list. // Doing caculations on calculated fields will require some // complex additional logic, especially if they are nested // several levels deep. if (substr($key, 0, 4) != '.cid') { $substitutions[$key] = '%'. str_replace(' ', '', $key); } } return $substitutions; } /** * Views calc fields result object */ function _views_calc_fields() { return db_query("SELECT * FROM {views_calc_fields}"); } /** * An array of allowable calculation values. */ function _views_calc_replacements() { $operators = array_filter(_views_calc_operators(), 'trim'); $numbers = range(0, 9); return array_merge($operators, $numbers); }