You are here

function views_bulk_operations_add_actions in Views Bulk Operations (VBO) 6

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

API function to add actions to a VBO.

File

./views_bulk_operations.module, line 80
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)) {
    watchdog('vbo', 'Could not find view %vid.', array(
      '%vid' => $vid,
    ), WATCHDOG_ERROR);
    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) {
    watchdog('vbo', 'Could not find a VBO display in view %vid.', array(
      '%vid' => $vid,
    ), WATCHDOG_ERROR);
    return;
  }

  // Iterate on the desired actions.
  $operations = $display_options['style_options']['operations'];
  $ignored = $added = 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($operations[$key])) {

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

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

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

          // try the title
          $action_object = db_fetch_object(db_query("SELECT * FROM {actions} WHERE description LIKE '%s'", 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($operations[$key])) {

              // available for this view
              $display_options['style_options']['operations'][$key]['selected'] = TRUE;
              $modified = TRUE;
            }
          }
        }
      }
      if ($modified) {
        $added[] = $action;
      }
      else {
        $ignored[] = $action;
      }
    }
  }

  // Save the view if anything was changed.
  if (!empty($added)) {
    $view
      ->save();
    views_object_cache_clear('view', $vid);
    if (empty($ignored)) {
      watchdog('vbo', 'View %vid was successfully modified. The following actions were added: %added.', array(
        '%vid' => $vid,
        '%added' => implode(', ', $added),
      ), WATCHDOG_INFO);
    }
    else {
      watchdog('vbo', 'View %vid was modified. The following actions were added: %added. The following actions were ignored: %ignored.', array(
        '%vid' => $vid,
        '%added' => implode(', ', $added),
        '%ignored' => implode(', ', $ignored),
      ), WATCHDOG_WARNING);
    }
  }
  else {
    watchdog('vbo', 'View %vid was NOT modified. The following actions were ignored: %ignored.', array(
      '%vid' => $vid,
      '%ignored' => implode(', ', $ignored),
    ), WATCHDOG_ERROR);
  }
}