You are here

function rb_numbers_action_views_sum in Rules Bonus Pack 6

Action for 'rb_numbers_action_views_sum'.

File

./rb_numbers.module, line 238
Functions for extending number management with Rules.

Code

function rb_numbers_action_views_sum($settings) {

  // Get necessary metadata about the view.
  $views_settings = explode('|', $settings['view']);
  $view_name = $views_settings[0];
  $display_name = $views_settings[1];
  $view_arguments = explode("\r", $settings['args']);

  // Build and execute the view.
  $view = views_get_view($view_name);
  $view
    ->set_display($display_name);
  $view
    ->set_arguments($view_arguments);
  $view
    ->set_items_per_page(0);
  $view
    ->execute();

  // Get the sum!
  $sum = 0;
  foreach ($settings['view_fields'] as $field) {

    // Only sum over the selected fields.
    if ($field) {

      // Assign the alias to a variable, for code readability. (We need this
      // alias, since the view results are stored by alias.)
      $alias =& $view->display[$display_name]->handler->handlers['field'][$field]->field_alias;
      foreach ($view->result as &$row) {
        $sum += $row->{$alias};
      }

      // Destroy the $row variable left after doing a foreach-by-reference.
      unset($row);
    }
  }

  // Return the sum.
  return array(
    'sum' => $sum,
  );
}