You are here

function drush_views_list in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 6.3 drush/views.drush.inc \drush_views_list()

Callback function for views-list command.

File

drush/views.drush.inc, line 280
Drush integration for Views.

Code

function drush_views_list() {

  // Initialize stuff.
  $rows = array();
  $disabled_views = array();
  $enabled_views = array();
  $overridden = 0;
  $indb = 0;
  $incode = 0;
  $disabled = 0;
  $total = 0;
  $views = views_get_all_views();

  // Get the --name option.
  // @todo Take into account the case off a comma-separated list of names.
  $name = drush_get_option_list('name');
  $with_name = !empty($name) ? TRUE : FALSE;

  // Get the --tags option.
  $tags = drush_get_option_list('tags');
  $with_tags = !empty($tags) ? TRUE : FALSE;

  // Get the --status option. Store user input appart to reuse it after.
  $status_opt = drush_get_option_list('status');

  // Use the same logic than $view->disabled.
  if (in_array('disabled', $status_opt)) {
    $status = TRUE;
    $with_status = TRUE;
  }
  elseif (in_array('enabled', $status_opt)) {
    $status = FALSE;
    $with_status = TRUE;
  }
  else {
    $status = NULL;

    // Wrong or empty --status option.
    $with_status = FALSE;
  }

  // Get the --type option.
  $type = drush_get_option_list('type');

  // Use the same logic than $view->type.
  $with_type = FALSE;
  if (in_array('normal', $type) || in_array('default', $type) || in_array('overridden', $type)) {
    $with_type = TRUE;
  }

  // Set the table headers.
  $header = array(
    dt('Machine name'),
    dt('Description'),
    dt('Type'),
    dt('Status'),
    dt('Tag'),
  );

  // Setup a row for each view.
  foreach ($views as $id => $view) {

    // If options were specified, check that first mismatch push the loop to
    // the next view.
    if ($with_tags && !in_array($view->tag, $tags)) {
      continue;
    }
    if ($with_status && !$view->disabled == $status) {
      continue;
    }
    if ($with_type && strtolower($view->type) !== $type[0]) {
      continue;
    }
    if ($with_name && !stristr($view->name, $name[0])) {
      continue;
    }
    $row = array();

    // Each row entry should be in the same order as the header.
    $row[] = $view->name;
    $row[] = $view->description;
    $row[] = $view->type;
    $row[] = $view->disabled ? dt('Disabled') : dt('Enabled');
    $row[] = $view->tag;

    // Place the row in the appropiate array so we can have disabled views at
    // the bottom.
    if ($view->disabled) {
      $disabled_views[] = $row;
    }
    else {
      $enabled_views[] = $row;
    }
    unset($row);

    // Gather some statistics.
    switch ($view->type) {
      case dt('Normal'):
        $indb++;
        break;
      case dt('Overridden'):
        $overridden++;
        break;
      case dt('Default'):
        $incode++;
        break;
    }
    $total++;
  }
  $disabled = count($disabled_views);

  // Sort alphabeticaly.
  asort($disabled_views);
  asort($enabled_views);

  // If options were used.
  $summary = "";
  if ($with_name || $with_tags || $with_status || $with_type) {
    $summary = "Views";
    if ($with_name) {
      $summary .= " named {$name[0]}";
    }
    if ($with_tags) {
      $tags = implode(" or ", $tags);
      $summary .= " tagged {$tags}";
    }
    if ($with_status) {
      $status_opt = implode("", $status_opt);
      $summary .= " which status is '{$status_opt}'";
    }
    if ($with_type) {
      $type = ucfirst($type[0]);
      $summary .= " of type '{$type}'";
    }
  }
  if (!empty($summary)) {
    drush_print($summary . "\n");
  }

  // Print all rows as a table.
  if ($total > 0) {
    $rows = array_merge($enabled_views, $disabled_views);

    // Put the headers as first row.
    array_unshift($rows, $header);
    drush_print_table($rows, TRUE);
  }

  // Print the statistics messages.
  drush_print(dt("A total of @total views were found in this Drupal installation:", array(
    '@total' => $total,
  )));
  drush_print(dt("  @indb views reside only in the database", array(
    '@indb' => $indb,
  )));
  drush_print(dt("  @over views are overridden", array(
    '@over' => $overridden,
  )));
  drush_print(dt("  @incode views are in their default state", array(
    '@incode' => $incode,
  )));
  drush_print(dt("  @dis views are disabled\n", array(
    '@dis' => $disabled,
  )));
}