Commit 7c7920f7 authored by amateescu's avatar amateescu Committed by tim.plunkett

Issue #1636864 by amateescu: Convert View object to PSR-0.

parent 92447b83
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\views\View;
/**
* Relationship handler that allows a groupwise maximum of the linked in table.
......@@ -170,8 +171,7 @@ function options_submit(&$form, &$form_state) {
* We use this to obtain our subquery SQL.
*/
function get_temporary_view() {
views_include('view');
$view = new view();
$view = new View();
$view->vid = 'new'; // @todo: what's this?
$view->base_table = $this->definition['base'];
$view->add_display('default');
......
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Database\Database;
use Drupal\views\View;
/**
* Create an array of Views admin CSS for adding or attaching.
......@@ -1997,7 +1998,6 @@ function views_ui_import_page($form, &$form_state) {
*/
function views_ui_import_validate($form, &$form_state) {
$view = '';
views_include('view');
// Be forgiving if someone pastes views code that starts with '<?php'.
if (substr($form_state['values']['view'], 0, 5) == '<?php') {
$form_state['values']['view'] = substr($form_state['values']['view'], 5);
......@@ -2071,7 +2071,7 @@ function views_ui_import_validate($form, &$form_state) {
}
}
foreach (views_object_types() as $type => $info) {
foreach (View::views_object_types() as $type => $info) {
$handlers = $display->handler->get_handlers($type);
if ($handlers) {
foreach ($handlers as $id => $handler) {
......@@ -2202,7 +2202,7 @@ function views_ui_edit_form_get_bucket($type, $view, $display) {
$build = array(
'#theme_wrappers' => array('views_ui_display_tab_bucket'),
);
$types = views_object_types();
$types = View::views_object_types();
$build['#overridden'] = FALSE;
$build['#defaulted'] = FALSE;
......@@ -3342,7 +3342,7 @@ function views_ui_config_type_form($form, &$form_state) {
$display_id = $form_state['display_id'];
$type = $form_state['type'];
$types = views_object_types();
$types = View::views_object_types();
if (!$view->set_display($display_id)) {
views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
}
......@@ -3374,7 +3374,7 @@ function views_ui_config_type_form($form, &$form_state) {
* Submit handler for type configuration form
*/
function views_ui_config_type_form_submit($form, &$form_state) {
$types = views_object_types();
$types = View::views_object_types();
$display = &$form_state['view']->display[$form_state['display_id']];
// Store in cache
......@@ -3389,7 +3389,7 @@ function views_ui_rearrange_form($form, &$form_state) {
$display_id = $form_state['display_id'];
$type = $form_state['type'];
$types = views_object_types();
$types = View::views_object_types();
if (!$view->set_display($display_id)) {
views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
}
......@@ -3549,7 +3549,7 @@ function theme_views_ui_expose_filter_form($variables) {
* Submit handler for rearranging form
*/
function views_ui_rearrange_form_submit($form, &$form_state) {
$types = views_object_types();
$types = View::views_object_types();
$display = &$form_state['view']->display[$form_state['display_id']];
$old_fields = $display->handler->get_option($types[$form_state['type']]['plural']);
......@@ -3585,7 +3585,7 @@ function views_ui_rearrange_filter_form($form, &$form_state) {
$display_id = $form_state['display_id'];
$type = $form_state['type'];
$types = views_object_types();
$types = View::views_object_types();
if (!$view->set_display($display_id)) {
views_ajax_render(t('Invalid display id @display', array('@display' => $display_id)));
}
......@@ -3851,7 +3851,7 @@ function theme_views_ui_rearrange_filter_form(&$vars) {
* Submit handler for rearranging form
*/
function views_ui_rearrange_filter_form_submit($form, &$form_state) {
$types = views_object_types();
$types = View::views_object_types();
$display = &$form_state['view']->display[$form_state['display_id']];
$remember_groups = array();
......@@ -3976,7 +3976,7 @@ function views_ui_add_item_form($form, &$form_state) {
}
$display = &$view->display[$display_id];
$types = views_object_types();
$types = View::views_object_types();
$ltitle = $types[$type]['ltitle'];
$section = $types[$type]['plural'];
......@@ -4094,7 +4094,7 @@ function views_ui_add_item_form($form, &$form_state) {
*/
function views_ui_add_item_form_submit($form, &$form_state) {
$type = $form_state['type'];
$types = views_object_types();
$types = View::views_object_types();
$section = $types[$type]['plural'];
// Handle the override select.
......@@ -4182,7 +4182,7 @@ function views_ui_config_item_form($form, &$form_state) {
$form['markup'] = array('#markup' => t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
}
else {
$types = views_object_types();
$types = View::views_object_types();
// If this item can come from the default display, show a dropdown
// that lets the user choose which display the changes should apply to.
......@@ -4302,7 +4302,7 @@ function views_ui_config_item_form_submit_temporary($form, &$form_state) {
// Run it through the handler's submit function.
$form_state['handler']->options_submit($form['options'], $form_state);
$item = $form_state['handler']->options;
$types = views_object_types();
$types = View::views_object_types();
// For footer/header $handler_type is area but $type is footer/header.
// For all other handle types it's the same.
......@@ -4357,7 +4357,7 @@ function views_ui_config_item_form_submit($form, &$form_state) {
// Run it through the handler's submit function.
$form_state['handler']->options_submit($form['options'], $form_state);
$item = $form_state['handler']->options;
$types = views_object_types();
$types = View::views_object_types();
// For footer/header $handler_type is area but $type is footer/header.
// For all other handle types it's the same.
......@@ -4434,7 +4434,7 @@ function views_ui_config_item_group_form($type, &$form_state) {
}
else {
$handler->init($view, $item);
$types = views_object_types();
$types = View::views_object_types();
$form['#title'] = t('Configure group settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->ui_name()));
......@@ -4535,7 +4535,7 @@ function views_ui_config_item_extra_form($form, &$form_state) {
}
else {
$handler->init($view, $item);
$types = views_object_types();
$types = View::views_object_types();
$form['#title'] = t('Configure extra settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->ui_name()));
......@@ -4606,7 +4606,7 @@ function views_ui_config_style_form($form, &$form_state) {
}
else {
$handler->init($view, $item);
$types = views_object_types();
$types = View::views_object_types();
$form['#title'] = t('Configure summary style for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->ui_name()));
......@@ -5016,7 +5016,6 @@ function views_ui_edit_display_form_change_theme($form, &$form_state) {
function views_ui_autocomplete_tag($string = '') {
$matches = array();
// get matches from default views:
views_include('view');
$views = views_get_all_views();
foreach ($views as $view) {
if (!empty($view->tag) && strpos($view->tag, $string) === 0) {
......@@ -5371,7 +5370,7 @@ function views_ui_field_list() {
foreach ($views as $view) {
foreach ($view->display as $display_id => $display) {
if ($view->set_display($display_id)) {
foreach (views_object_types() as $type => $info) {
foreach (View::views_object_types() as $type => $info) {
foreach ($view->get_items($type, $display_id) as $item) {
$data = views_fetch_data($item['table']);
if (isset($data[$item['field']]) && isset($data[$item['field']][$type])
......
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Database\Database;
use Drupal\views\View;
/**
* Instantiate and construct a new handler
......@@ -213,7 +214,7 @@ function init(&$view, &$options) {
$options['field'] = $this->actual_field;
}
$types = views_object_types();
$types = View::views_object_types();
$plural = $this->handler_type;
if (isset($types[$this->handler_type]['plural'])) {
$plural = $types[$this->handler_type]['plural'];
......@@ -430,7 +431,7 @@ function use_group_by() {
function groupby_form(&$form, &$form_state) {
$view = &$form_state['view'];
$display_id = $form_state['display_id'];
$types = views_object_types();
$types = View::views_object_types();
$type = $form_state['type'];
$id = $form_state['id'];
......
......@@ -23,11 +23,6 @@ public static function getInfo() {
public function setUp() {
parent::setUp('views_ui');
module_enable(array('views_ui'));
// @TODO Figure out why it's required to clear the cache here.
views_module_include('views_default', TRUE);
views_get_all_views(TRUE);
menu_router_rebuild();
// Add an admin user will full rights;
$this->admin = $this->drupalCreateUser(array('administer views'));
......
......@@ -8,7 +8,7 @@
namespace Drupal\views\Tests;
use ViewsSqlTest;
use view;
use Drupal\views\View;
/**
* Tests Views argument validators.
......@@ -47,7 +47,7 @@ function testArgumentValidateNumeric() {
function view_test_argument_validate_php($string) {
$code = 'return $argument == \''. $string .'\';';
$view = new view;
$view = new View();
$view->name = 'view_argument_validate_numeric';
$view->description = '';
$view->tag = '';
......@@ -79,7 +79,7 @@ function view_test_argument_validate_php($string) {
}
function view_argument_validate_numeric() {
$view = new view;
$view = new View();
$view->name = 'view_argument_validate_numeric';
$view->description = '';
$view->tag = '';
......
......@@ -8,8 +8,7 @@
namespace Drupal\views\Tests\Handler;
use ViewsSqlTest;
use view;
use Drupal\views\View;
/**
* Tests the core views_handler_argument_string handler.
......@@ -60,10 +59,10 @@ function testGlossary() {
* Provide a test view for testGlossary.
*
* @see testGlossary
* @return view
* @return Drupal\views\View
*/
function viewGlossary() {
$view = new view();
$view = new View();
$view->name = 'test_glossary';
$view->description = '';
$view->tag = 'default';
......
......@@ -8,8 +8,7 @@
namespace Drupal\views\Tests\Handler;
use ViewsSqlTest;
use view;
use Drupal\views\View;
/**
* Tests the core views_handler_filter_date handler.
......@@ -157,7 +156,7 @@ function testUiValidation() {
}
function views_test_between() {
$view = new view;
$view = new View();
$view->name = 'test_filter_date_between';
$view->description = '';
$view->tag = '';
......
......@@ -8,7 +8,7 @@
namespace Drupal\views\Tests\Style;
use ViewsSqlTest;
use view;
use Drupal\views\View;
/**
* Tests jump menu style functionality.
......@@ -63,7 +63,7 @@ function testDuplicatePaths() {
}
function getJumpMenuView() {
$view = new view;
$view = new View();
$view->name = 'test_jump_menu';
$view->description = '';
$view->tag = 'default';
......
......@@ -134,17 +134,16 @@ function testEditUi() {
$edit = array(
'views_ui_display_embed' => TRUE,
);
$this->drupalPost('admin/structure/views/settings', $edit, t('Save configuration'));
$this->drupalPost('admin/structure/views/add', $view, t('Continue & edit'));
$this->assertFieldById('edit-displays-top-add-display-embed');
$edit = array(
'views_ui_display_embed' => FALSE,
);
$this->drupalPost('admin/structure/views/settings', $edit, t('Save configuration'));
views_invalidate_cache();
$this->drupalPost('admin/structure/views/add', $view, t('Continue & edit'));
$this->assertNoFieldById('edit-displays-top-add-display-embed');
//
}
}
<?php
/**
* @file
* Definition of Drupal\views\ViewsDbObject.
*/
namespace Drupal\views;
/**
* Base class for views' database objects.
*/
class ViewsDbObject {
public $db_table;
/**
* Initialize this object, setting values from schema defaults.
*
* @param $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.
*/
function init($init = TRUE) {
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 = '';
}
}
}
}
/**
* Write the row to the database.
*
* @param $update
* If true this will be an UPDATE query. Otherwise it will be an INSERT.
*/
function save_row($update = NULL) {
$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;
}
}
}
/**
* 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.
*
* @param $data
* An object from db_fetch_object. It should contain all of the fields
* that are in the schema.
*/
function load_row($data) {
$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);
}
}
/**
* Export a loaded row, such as an argument, field or the view itself to PHP code.
*
* @param $identifier
* The variable to assign the PHP code for this object to.
* @param $indent
* An optional indentation for prettifying nested code.
*/
function export_row($identifier = NULL, $indent = '') {
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($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;
}
/**
* Add a new display handler to the view, automatically creating an id.
*
* @param $type
* The plugin type from the views plugin data. Defaults to 'page'.
* @param $title
* The title of the display; optional, may be filled in from default.
* @param $id
* The id to use.
* @return
* The key to the display in $view->display, so that the new display
* can be easily located.
*/
function add_display($type = 'page', $title = NULL, $id = NULL) {
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 ViewsDisplay();
$display->options($type, $id, $title);
// Add the new display object to the view.
$this->display[$id] = $display;
return $id;
}
/**
* Generate a display id of a certain plugin type.
*
* @param $type
* Which plugin should be used for the new display id.
*/
function generate_display_id($type) {
// '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;
}
/**
* Generates a unique ID for an item.
*
* These items are typically fields, filters, sort criteria, or arguments.
*
* @param $requested_id
* The requested ID for the item.
* @param $existing_items
* An array of existing items, keyed by their IDs.
*
* @return
* 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.
*/
public static function generate_item_id($requested_id, $existing_items) {
$count = 0;
$id = $requested_id;
while (!empty($existing_items[$id])) {
$id = $requested_id . '_' . ++$count;
}
return $id;
}