You are here

function views_bulk_operations_add_actions in Views Bulk Operations (VBO) 6.3

Same name and namespace in other branches
  1. 6 views_bulk_operations.module \views_bulk_operations_add_actions()

API function to add actions to a VBO.

File

./views_bulk_operations.module, line 1372
Allows operations to be performed on items selected in a view.

Code

function views_bulk_operations_add_actions($vid, $actions) {
  $view = views_get_view($vid);
  if (!is_object($view)) {
    _views_bulk_operations_report_error('Could not find view %vid.', array(
      '%vid' => $vid,
    ));
    return;
  }

  // Find the view display that has the VBO style.
  $found = FALSE;
  foreach (array_keys($view->display) as $display) {
    $display_options =& $view->display[$display]->display_options;
    if (isset($display_options['style_plugin']) && $display_options['style_plugin'] == 'bulk') {
      $found = TRUE;
      break;
    }
  }
  if (!$found) {
    _views_bulk_operations_report_error('Could not find a VBO display in view %vid.', array(
      '%vid' => $vid,
    ));
    return;
  }

  // Iterate on the desired actions.
  $selected_operations = $display_options['style_options']['selected_operations'];
  $ignored = array();
  if (!empty($actions)) {
    foreach ($actions as $action) {
      $modified = FALSE;
      if (is_numeric($action)) {

        // aid
        $action_object = db_fetch_object(db_query("SELECT * FROM {actions} WHERE aid = %d", $action));
        if (is_object($action_object)) {
          $parameters = unserialize($action_object->parameters);
          $key = $action_object->callback . (empty($parameters) ? '' : ':' . md5($action_object->parameters));
          if (isset($selected_operations[$key])) {

            // available for this view
            $display_options['style_options']['selected_operations'][$key] = $key;
            $modified = TRUE;
          }
        }
      }
      else {

        // callback or title
        if (isset($selected_operations[$action])) {

          // callback and available for this view
          $display_options['style_options']['selected_operations'][$action] = $action;
          $modified = TRUE;
        }
        else {

          // try the title
          $action_object = db_fetch_object(db_query("SELECT * FROM {actions} WHERE UPPER(description) = '%s'", strtoupper(db_escape_string($action))));
          if (is_object($action_object)) {
            $parameters = unserialize($action_object->parameters);
            $key = $action_object->callback . (empty($parameters) ? '' : ':' . md5($action_object->parameters));
            if (isset($selected_operations[$key])) {

              // available for this view
              $display_options['style_options']['selected_operations'][$key] = $key;
              $modified = TRUE;
            }
          }
        }
      }
      if (!$modified) {
        $ignored[] = $action;
      }
    }
  }

  // Save the view if anything was changed.
  if (count($actions) > count($ignored)) {
    $view
      ->save();
    views_object_cache_clear('view', $vid);
    $msg = t('View %vid was successfully modified.', array(
      '%vid' => $vid,
    ));
    if (!empty($ignored)) {
      $msg .= ' ' . t('The following actions were ignored: %ignored.', array(
        '%ignored' => implode(', ', $ignored),
      ));
    }
  }
  else {
    $msg = t('View %vid was not modified, because all actions were ignored.', array(
      '%vid' => $vid,
    ));
  }
  _views_bulk_operations_log($msg);
}