You are here

function _views_save_view in Views (for Drupal 7) 5

Save a view to the database.

1 call to _views_save_view()
views_edit_view_submit in ./views_ui.module
Handle submit buttons on a view form.

File

./views.module, line 1000

Code

function _views_save_view($view) {
  _views_check_arrays($view);
  $view->is_cacheable = _views_is_cacheable($view);
  $view->access = implode(', ', $view->access);
  $view->changed = time();
  $fields = _views_view_fields();
  if ($view->vid) {

    // update
    // Prepare the query:
    foreach ($view as $key => $value) {
      if (array_key_exists($key, $fields)) {
        $q[] = db_escape_string($key) . " = {$fields[$key]}";
        $v[] = $value;
      }
    }

    // Update the view in the database:
    db_query("UPDATE {view_view} SET " . implode(', ', $q) . " WHERE vid = '{$view->vid}'", $v);
    db_query("DELETE from {view_sort} WHERE vid='{$view->vid}'");
    db_query("DELETE from {view_argument} WHERE vid='{$view->vid}'");
    db_query("DELETE from {view_tablefield} WHERE vid='{$view->vid}'");
    db_query("DELETE from {view_filter} WHERE vid='{$view->vid}'");
    db_query("DELETE from {view_exposed_filter} WHERE vid='{$view->vid}'");
    cache_clear_all('views_query:' . $view->name, 'cache_views');
  }
  else {

    // insert
    // This method really saves on typos, and makes it a lot easier to add fields
    // later on.
    $view->vid = db_next_id('{view_view}_vid');

    // Prepare the query:
    foreach ($view as $key => $value) {
      if (array_key_exists((string) $key, $fields)) {
        $k[] = db_escape_string($key);
        $v[] = $value;
        $s[] = $fields[$key];
      }
    }
    db_query("INSERT INTO {view_view} (" . implode(", ", $k) . ") VALUES (" . implode(", ", $s) . ")", $v);
  }
  foreach ($view->sort as $i => $sort) {
    db_query("INSERT INTO {view_sort} (vid, position, field, sortorder, options) VALUES (%d, %d, '%s', '%s', '%s')", $view->vid, $i, $sort['field'], $sort['sortorder'], $sort['options']);
  }
  foreach ($view->argument as $i => $arg) {
    db_query("INSERT INTO {view_argument} (vid, type, argdefault, title, options, position, wildcard, wildcard_substitution) VALUES (%d, '%s', %d, '%s', '%s', %d, '%s', '%s')", $view->vid, $arg['type'], $arg['argdefault'], $arg['title'], $arg['options'], $i, $arg['wildcard'], $arg['wildcard_substitution']);
  }
  foreach ($view->field as $i => $arg) {
    db_query("INSERT INTO {view_tablefield} (vid, tablename, field, label, handler, sortable, defaultsort, options, position) VALUES (%d, '%s', '%s', '%s', '%s', %d, '%s', '%s', %d)", $view->vid, $arg['tablename'], $arg['field'], $arg['label'], $arg['handler'], $arg['sortable'], $arg['defaultsort'], $arg['options'], $i);
  }
  foreach ($view->filter as $i => $arg) {
    if (is_array($arg['value'])) {
      $arg['value'] = implode(',', $arg['value']);
    }
    db_query("INSERT INTO {view_filter} (vid, tablename, field, value, operator, options, position) VALUES (%d, '%s', '%s', '%s', '%s', '%s', %d)", $view->vid, $arg['tablename'], $arg['field'], $arg['value'], $arg['operator'], $arg['options'], $i);
  }
  foreach ($view->exposed_filter as $i => $arg) {
    db_query("INSERT INTO {view_exposed_filter} (vid, field, label, optional, is_default, single, operator, position) VALUES (%d, '%s', '%s', %d, %d, %d, %d, %d)", $view->vid, $arg['field'], $arg['label'], $arg['optional'], $arg['is_default'], $arg['single'], $arg['operator'], $i);
  }
  cache_clear_all('views_urls', 'cache_views');
  cache_clear_all();

  // clear the page cache as well.
  return $view->vid;
}