You are here

function drush_views_list in Views (for Drupal 7) 6.3

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

Callback function for views-list command.

File

drush/views.drush.inc, line 228
Drush integration of views.

Code

function drush_views_list() {

  // Initialize stuf
  $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,
  )));
}