Public Member Functions | Static Public Member Functions | Public Attributes

views_db_object Class Reference
[Objects that represent a View or part of a view.]

Base class for views' database objects. More...

Inheritance diagram for views_db_object:
view views_display

List of all members.

Public Member Functions

 init ($init=TRUE)
 Initialize this object, setting values from schema defaults.
 save_row ($update=NULL)
 Write the row to the database.
 load_row ($data)
 Load the object with a row from the database.
 export_row ($identifier=NULL, $indent= '')
 Export a loaded row, such as an argument, field or the view itself to PHP code.
 add_display ($type= 'page', $title=NULL, $id=NULL)
 Add a new display handler to the view, automatically creating an id.
 generate_display_id ($type)
 Generate a display id of a certain plugin type.
new_display ($type= 'page', $title=NULL, $id=NULL)
 Create a new display and a display handler for it.
 add_item ($display_id, $type, $table, $field, $options=array(), $id=NULL)
 Add an item with a handler to the view.
 get_items ($type, $display_id=NULL)
 Get an array of items for the current display.
 get_item ($display_id, $type, $id)
 Get the configuration of an item (field/sort/filter/etc) on a given display.
 set_item ($display_id, $type, $id, $item)
 Set the configuration of an item (field/sort/filter/etc) on a given display.
 set_item_option ($display_id, $type, $id, $option, $value)
 Set an option on an item.

Static Public Member Functions

static generate_item_id ($requested_id, $existing_items)
 Generates a unique ID for an item.

Public Attributes

 $db_table

Detailed Description

Base class for views' database objects.

Definition at line 2016 of file view.inc.


Member Function Documentation

views_db_object::add_display ( type = 'page',
title = NULL,
id = NULL 
)

Add a new display handler to the view, automatically creating an id.

Parameters:
$type The plugin type from the views plugin data. Defaults to 'page'.
$title The title of the display; optional, may be filled in from default.
$id The id to use.
Returns:
The key to the display in $view->display, so that the new display can be easily located.

Definition at line 2186 of file view.inc.

References generate_display_id(), and views_fetch_plugin_data().

Referenced by new_display().

                                                                  {
    if (empty($type)) {
      return FALSE;
    }

    $plugin = views_fetch_plugin_data('display', $type);
    if (empty($plugin)) {
      $plugin['title'] = t('Broken');
    }


    if (empty($id)) {
      $id = $this->generate_display_id($type);
      if ($id !== 'default') {
        preg_match("/[0-9]+/", $id, $count);
        $count = $count[0];
      }
      else {
        $count = '';
      }

      if (empty($title)) {
        if ($count > 1) {
          $title = $plugin['title'] . ' ' . $count;
        }
        else {
          $title = $plugin['title'];
        }
      }
    }

    // Create the new display object
    $display = new views_display;
    $display->options($type, $id, $title);

    // Add the new display object to the view.
    $this->display[$id] = $display;
    return $id;
  }

views_db_object::add_item ( display_id,
type,
table,
field,
options = array(),
id = NULL 
)

Add an item with a handler to the view.

These items may be fields, filters, sort criteria, or arguments.

Definition at line 2314 of file view.inc.

References generate_item_id(), views_get_handler(), and views_object_types().

Referenced by view::fix_missing_relationships().

                                                                                        {
    $types = views_object_types();
    $this->set_display($display_id);

    $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);

    if (empty($id)) {
      $id = $this->generate_item_id($field, $fields);
    }

    $new_item = array(
      'id' => $id,
      'table' => $table,
      'field' => $field,
    ) + $options;

    if (!empty($types[$type]['type'])) {
      $handler_type = $types[$type]['type'];
    }
    else {
      $handler_type = $type;
    }

    $handler = views_get_handler($table, $field, $handler_type);

    $fields[$id] = $new_item;
    $this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);

    return $id;
  }

views_db_object::export_row ( identifier = NULL,
indent = '' 
)

Export a loaded row, such as an argument, field or the view itself to PHP code.

Parameters:
$identifier The variable to assign the PHP code for this object to.
$indent An optional indentation for prettifying nested code.

Definition at line 2131 of file view.inc.

Referenced by view::export().

                                                        {
    ctools_include('export');

    if (!$identifier) {
      $identifier = $this->db_table;
    }
    $schema = drupal_get_schema($this->db_table);

    $output = $indent . '$' . $identifier . ' = new ' . get_class($this) . ";\n";
    // Go through our schema and build correlations.
    foreach ($schema['fields'] as $field => $info) {
      if (!empty($info['no export'])) {
        continue;
      }
      if (!isset($this->$field)) {
        if (isset($info['default'])) {
          $this->$field = $info['default'];
        }
        else {
          $this->$field = '';
        }

        // serialized defaults must be set as serialized.
        if (isset($info['serialize'])) {
          $this->$field = unserialize(db_decode_blob($this->$field));
        }
      }
      $value = $this->$field;
      if ($info['type'] == 'int') {
        if (isset($info['size']) && $info['size'] == 'tiny') {
          $value = (bool) $value;
        }
        else {
          $value = (int) $value;
        }
      }

      $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
    }
    return $output;
  }

views_db_object::generate_display_id ( type  ) 

Generate a display id of a certain plugin type.

Parameters:
$type Which plugin should be used for the new display id.

Definition at line 2232 of file view.inc.

Referenced by add_display().

                                      {
    // 'default' is singular and is unique, so just go with 'default'
    // for it. For all others, start counting.
    if ($type == 'default') {
      return 'default';
    }
    // Initial id.
    $id = $type . '_1';
    $count = 1;

    // Loop through IDs based upon our style plugin name until
    // we find one that is unused.
    while (!empty($this->display[$id])) {
      $id = $type . '_' . ++$count;
    }

    return $id;
  }

static views_db_object::generate_item_id ( requested_id,
existing_items 
) [static]

Generates a unique ID for an item.

These items are typically fields, filters, sort criteria, or arguments.

Parameters:
$requested_id The requested ID for the item.
$existing_items An array of existing items, keyed by their IDs.
Returns:
A unique ID. This will be equal to $requested_id if no item with that ID already exists. Otherwise, it will be appended with an integer to make it unique, e.g. "{$requested_id}_1", "{$requested_id}_2", etc.

Definition at line 2266 of file view.inc.

Referenced by add_item(), and ViewsUiBaseViewsWizard::alter_display_options().

                                                                          {
    $count = 0;
    $id = $requested_id;
    while (!empty($existing_items[$id])) {
      $id = $requested_id . '_' . ++$count;
    }
    return $id;
  }

views_db_object::get_item ( display_id,
type,
id 
)

Get the configuration of an item (field/sort/filter/etc) on a given display.

Definition at line 2364 of file view.inc.

References views_object_types().

Referenced by set_item_option().

                                             {
    // Get info about the types so we can get the right data.
    $types = views_object_types();
    // Initialize the display
    $this->set_display($display_id);

    // Get the existing configuration
    $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);

    return isset($fields[$id]) ? $fields[$id] : NULL;
  }

views_db_object::get_items ( type,
display_id = NULL 
)

Get an array of items for the current display.

Definition at line 2348 of file view.inc.

References views_object_types().

                                                {
    $this->set_display($display_id);

    if (!isset($display_id)) {
      $display_id = $this->current_display;
    }

    // Get info about the types so we can get the right data.
    $types = views_object_types();
    return $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  }

views_db_object::init ( init = TRUE  ) 

Initialize this object, setting values from schema defaults.

Parameters:
$init If an array, this is a set of values from db_fetch_object to load. Otherwse, if TRUE values will be filled in from schema defaults.

Definition at line 2027 of file view.inc.

References load_row().

Referenced by view::__construct().

                              {
    if (is_array($init)) {
      return $this->load_row($init);
    }

    if (!$init) {
      return;
    }

    $schema = drupal_get_schema($this->db_table);

    if (!$schema) {
      return;
    }

    // Go through our schema and build correlations.
    foreach ($schema['fields'] as $field => $info) {
      if ($info['type'] == 'serial') {
        $this->$field = NULL;
      }
      if (!isset($this->$field)) {
        if (!empty($info['serialize']) && isset($info['serialized default'])) {
          $this->$field = unserialize($info['serialized default']);
        }
        elseif (isset($info['default'])) {
          $this->$field = $info['default'];
        }
        else {
          $this->$field = '';
        }
      }
    }
  }

views_db_object::load_row ( data  ) 

Load the object with a row from the database.

This method is separate from the constructor in order to give us more flexibility in terms of how the view object is built in different contexts.

Parameters:
$data An object from db_fetch_object. It should contain all of the fields that are in the schema.

Definition at line 2114 of file view.inc.

Referenced by init().

                           {
    $schema = drupal_get_schema($this->db_table);

    // Go through our schema and build correlations.
    foreach ($schema['fields'] as $field => $info) {
      $this->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field);
    }
  }

& views_db_object::new_display ( type = 'page',
title = NULL,
id = NULL 
)

Create a new display and a display handler for it.

Parameters:
$type The plugin type from the views plugin data. Defaults to 'page'.
$title The title of the display; optional, may be filled in from default.
$id The id to use.
Returns:
views_plugin_display A reference to the new handler object.

Definition at line 2286 of file view.inc.

References add_display(), and views_get_plugin().

                                                                   {
    $id = $this->add_display($type, $title, $id);

    // Create a handler
    $this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
    if (empty($this->display[$id]->handler)) {
      // provide a 'default' handler as an emergency. This won't work well but
      // it will keep things from crashing.
      $this->display[$id]->handler = views_get_plugin('display', 'default');
    }

    if (!empty($this->display[$id]->handler)) {
      // Initialize the new display handler with data.
      $this->display[$id]->handler->init($this, $this->display[$id]);
      // If this is NOT the default display handler, let it know which is
      if ($id != 'default') {
        $this->display[$id]->handler->default_display = &$this->display['default']->handler;
      }
    }

    return $this->display[$id]->handler;
  }

views_db_object::save_row ( update = NULL  ) 

Write the row to the database.

Parameters:
$update If true this will be an UPDATE query. Otherwise it will be an INSERT.

Definition at line 2067 of file view.inc.

Referenced by view::save().

                                    {
    $fields = $defs = $values = $serials = array();
    $schema = drupal_get_schema($this->db_table);

    // Go through our schema and build correlations.
    foreach ($schema['fields'] as $field => $info) {
      // special case -- skip serial types if we are updating.
      if ($info['type'] == 'serial') {
        $serials[] = $field;
        continue;
      }
      elseif ($info['type'] == 'int') {
        $this->$field = (int) $this->$field;
      }
      $fields[$field] = empty($info['serialize']) ? $this->$field : serialize($this->$field);
    }
    if (!$update) {
      $query = db_insert($this->db_table);
    }
    else {
      $query = db_update($this->db_table)
        ->condition($update, $this->$update);
    }
    $return = $query
      ->fields($fields)
      ->execute();

    if ($serials && !$update) {
      // get last insert ids and fill them in.
      // Well, one ID.
      foreach ($serials as $field) {
        $this->$field = $return;
      }
    }
  }

views_db_object::set_item ( display_id,
type,
id,
item 
)

Set the configuration of an item (field/sort/filter/etc) on a given display.

Pass in NULL for the $item to remove an item.

Definition at line 2382 of file view.inc.

References views_object_types().

Referenced by set_item_option().

                                                    {
    // Get info about the types so we can get the right data.
    $types = views_object_types();
    // Initialize the display
    $this->set_display($display_id);

    // Get the existing configuration
    $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
    if (isset($item)) {
      $fields[$id] = $item;
    }
    else {
      unset($fields[$id]);
    }

    // Store.
    $this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);
  }

views_db_object::set_item_option ( display_id,
type,
id,
option,
value 
)

Set an option on an item.

Use this only if you have just 1 or 2 options to set; if you have many, consider getting the item, adding the options and doing set_item yourself.

Definition at line 2408 of file view.inc.

References get_item(), and set_item().

                                                                     {
    $item = $this->get_item($display_id, $type, $id);
    $item[$option] = $value;
    $this->set_item($display_id, $type, $id, $item);
  }


The documentation for this class was generated from the following file: