class ViewsUiBaseViewsWizard implements ViewsWizardInterface {
protected $base_table;
protected $entity_type;
protected $entity_info = array();
protected $validated_views = array();
protected $plugin = array();
protected $filter_defaults = array(
'id' => NULL,
'expose' => array('operator' => FALSE),
'group' => 1,
);
function __construct($plugin) {
$this->base_table = $plugin['base_table'];
$default = $this->filter_defaults;
if (isset($plugin['filters'])) {
foreach ($plugin['filters'] as $name => $info) {
$default['id'] = $name;
$plugin['filters'][$name] = $info + $default;
}
}
$this->plugin = $plugin;
$entities = entity_get_info();
foreach ($entities as $entity_type => $entity_info) {
if (isset($entity_info['base table']) && $this->base_table == $entity_info['base table']) {
$this->entity_info = $entity_info;
$this->entity_type = $entity_type;
}
}
}
function build_form($form, &$form_state) {
$style_options = views_fetch_plugin_names('style', 'normal', array($this->base_table));
$feed_row_options = views_fetch_plugin_names('row', 'feed', array($this->base_table));
$path_prefix = url(NULL, array('absolute' => TRUE)) . (config_get('system.core', 'clean_url') ? '' : '?q=');
$this->build_filters($form, $form_state);
$this->build_sorts($form, $form_state);
$form['displays']['page'] = array(
'#type' => 'fieldset',
'#attributes' => array(
'class' => array(
'views-attachment',
'edit-page-style-wrapper',
),
),
'#tree' => TRUE,
);
$form['displays']['page']['create'] = array(
'#title' => t('Create a page'),
'#type' => 'checkbox',
'#attributes' => array('class' => array('strong')),
'#default_value' => (bool) config_get('views_ui.settings', 'wizard_default_display.page'),
'#id' => 'edit-page-create',
);
$form['displays']['page']['options'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('options-set', 'form-wrapper'),),
'#states' => array(
'visible' => array(
':input[name="page[create]"]' => array('checked' => TRUE),
),
),
'#prefix' => '<div><div class="edit-page-style-inner-wrapper">',
'#suffix' => '</div></div>',
'#parents' => array('page'),
);
$form['displays']['page']['options']['title'] = array(
'#title' => t('Page title'),
'#type' => 'textfield',
);
$form['displays']['page']['options']['path'] = array(
'#title' => t('Path'),
'#type' => 'textfield',
'#field_prefix' => $path_prefix,
);
$form['displays']['page']['options']['style'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
);
$form['displays']['page']['options']['style']['style_plugin'] = array(
'#title' => t('Display format'),
'#help_topic' => 'style',
'#type' => 'select',
'#options' => $style_options,
);
$style_form = &$form['displays']['page']['options']['style'];
$style_form['style_plugin']['#default_value'] = views_ui_get_selected($form_state, array('page', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'page', 'options'));
$this->build_form_style($form, $form_state, 'page');
$form['displays']['page']['options']['items_per_page'] = array(
'#title' => t('Items to display'),
'#type' => 'number',
'#default_value' => '10',
'#size' => 5,
'#min' => 0,
'#step' => 1,
);
$form['displays']['page']['options']['pager'] = array(
'#title' => t('Use a pager'),
'#type' => 'checkbox',
'#default_value' => TRUE,
);
$form['displays']['page']['options']['link'] = array(
'#title' => t('Create a menu link'),
'#type' => 'checkbox',
'#id' => 'edit-page-link',
);
$form['displays']['page']['options']['link_properties'] = array(
'#type' => 'container',
'#states' => array(
'visible' => array(
':input[name="page[link]"]' => array('checked' => TRUE),
),
),
'#prefix' => '<div id="edit-page-link-properties-wrapper">',
'#suffix' => '</div>',
);
if (module_exists('menu')) {
$menu_options = menu_get_menus();
}
else {
$menu_options = menu_list_system_menus();
foreach ($menu_options as $name => $title) {
$menu_options[$name] = t($title);
}
}
$form['displays']['page']['options']['link_properties']['menu_name'] = array(
'#title' => t('Menu'),
'#type' => 'select',
'#options' => $menu_options,
);
if (array_key_exists('main-menu', $menu_options)) {
$form['displays']['page']['options']['link_properties']['menu_name']['#default_value'] = array('main-menu');
}
$form['displays']['page']['options']['link_properties']['title'] = array(
'#title' => t('Link text'),
'#type' => 'textfield',
);
if ($feed_row_options) {
$form['displays']['page']['options']['feed'] = array(
'#title' => t('Include an RSS feed'),
'#type' => 'checkbox',
'#id' => 'edit-page-feed',
);
$form['displays']['page']['options']['feed_properties'] = array(
'#type' => 'container',
'#states' => array(
'visible' => array(
':input[name="page[feed]"]' => array('checked' => TRUE),
),
),
'#prefix' => '<div id="edit-page-feed-properties-wrapper">',
'#suffix' => '</div>',
);
$form['displays']['page']['options']['feed_properties']['path'] = array(
'#title' => t('Feed path'),
'#type' => 'textfield',
'#field_prefix' => $path_prefix,
);
$form['displays']['page']['options']['feed_properties']['row_plugin'] = array(
'#title' => t('Feed row style'),
'#type' => 'select',
'#options' => $feed_row_options,
'#default_value' => key($feed_row_options),
'#access' => (count($feed_row_options) > 1),
'#states' => array(
'visible' => array(
':input[name="page[feed]"]' => array('checked' => TRUE),
),
),
'#prefix' => '<div id="edit-page-feed-properties-row-plugin-wrapper">',
'#suffix' => '</div>',
);
}
$form['displays']['block'] = array(
'#type' => 'fieldset',
'#attributes' => array(
'class' => array(
'views-attachment',
'edit-block-style-wrapper',
),
),
'#tree' => TRUE,
);
$form['displays']['block']['create'] = array(
'#title' => t('Create a block'),
'#type' => 'checkbox',
'#attributes' => array('class' => array('strong')),
'#default_value' => (bool) config_get('views_ui.settings', 'wizard_default_display.block'),
'#id' => 'edit-block-create',
);
$form['displays']['block']['options'] = array(
'#type' => 'container',
'#attributes' => array('class' => array('options-set', 'form-wrapper'),),
'#states' => array(
'visible' => array(
':input[name="block[create]"]' => array('checked' => TRUE),
),
),
'#prefix' => '<div class="edit-block-style-inner-wrapper">',
'#suffix' => '</div>',
'#parents' => array('block'),
);
$form['displays']['block']['options']['title'] = array(
'#title' => t('Block title'),
'#type' => 'textfield',
);
$form['displays']['block']['options']['style'] = array(
'#type' => 'fieldset',
'#attributes' => array('class' => array('container-inline', 'fieldset-no-legend')),
);
$form['displays']['block']['options']['style']['style_plugin'] = array(
'#title' => t('Display format'),
'#help_topic' => 'style',
'#type' => 'select',
'#options' => $style_options,
);
$style_form = &$form['displays']['block']['options']['style'];
$style_form['style_plugin']['#default_value'] = views_ui_get_selected($form_state, array('block', 'style', 'style_plugin'), 'default', $style_form['style_plugin']);
views_ui_add_ajax_trigger($style_form, 'style_plugin', array('displays', 'block', 'options'));
$this->build_form_style($form, $form_state, 'block');
$form['displays']['block']['options']['items_per_page'] = array(
'#title' => t('Items to display'),
'#type' => 'number',
'#default_value' => '5',
'#size' => 5,
'#min' => 0,
'#step' => 1,
);
$form['displays']['block']['options']['pager'] = array(
'#title' => t('Use a pager'),
'#type' => 'checkbox',
'#default_value' => FALSE,
);
return $form;
}
protected function build_form_style(&$form, &$form_state, $type) {
$style_form = &$form['displays'][$type]['options']['style'];
$style = $style_form['style_plugin']['#default_value'];
$style_plugin = views_get_plugin('style', $style);
if (isset($style_plugin) && $style_plugin->uses_row_plugin()) {
$options = $this->row_style_options($type);
$style_form['row_plugin'] = array(
'#type' => 'select',
'#title' => t('of'),
'#options' => $options,
'#access' => count($options) > 1,
);
$block_with_linked_titles_available = ($type == 'block' && isset($options['titles_linked']));
$default_value = $block_with_linked_titles_available ? 'titles_linked' : key($options);
$style_form['row_plugin']['#default_value'] = views_ui_get_selected($form_state, array($type, 'style', 'row_plugin'), $default_value, $style_form['row_plugin']);
views_ui_add_ajax_trigger($style_form, 'row_plugin', array('displays', $type, 'options', 'style', 'row_options'));
$style_form['row_options'] = array(
'#theme_wrappers' => array('container'),
'#attributes' => array(),
);
}
elseif ($style_plugin->uses_fields()) {
$style_form['row_plugin'] = array('#markup' => '<span>' . t('of fields') . '</span>');
}
}
protected function row_style_options($type) {
$data = views_fetch_data($this->base_table);
$options = views_fetch_plugin_names('row', 'normal', array($this->base_table));
return $options;
}
protected function build_filters(&$form, &$form_state) {
$fields = views_fetch_fields($this->base_table, 'filter');
$entity_info = $this->entity_info;
if (isset($entity_info['bundle keys']) && isset($entity_info['bundles'])) {
$options = array('all' => t('All'));
foreach ($entity_info['bundles'] as $type => $bundle) {
$options[$type] = $bundle['label'];
}
$form['displays']['show']['type'] = array(
'#type' => 'select',
'#title' => t('of type'),
'#options' => $options,
);
$selected_bundle = views_ui_get_selected($form_state, array('show', 'type'), 'all', $form['displays']['show']['type']);
$form['displays']['show']['type']['#default_value'] = $selected_bundle;
views_ui_add_ajax_trigger($form['displays']['show'], 'type', array('displays'));
}
if (isset($fields['taxonomy_index.tid'])) {
if (!empty($entity_info['fieldable'])) {
$bundles = array_keys($entity_info['bundles']);
if (isset($selected_bundle) && in_array($selected_bundle, $bundles)) {
$bundles = array($selected_bundle);
}
$tag_fields = array();
foreach ($bundles as $bundle) {
foreach (field_info_instances($this->entity_type, $bundle) as $instance) {
if ($instance['widget']['type'] == 'taxonomy_autocomplete') {
$tag_fields[] = $instance['field_name'];
}
}
}
$tag_fields = array_unique($tag_fields);
if (!empty($tag_fields)) {
if (in_array('field_tags', $tag_fields)) {
$tag_field_name = 'field_tags';
}
else {
$tag_field_name = reset($tag_fields);
}
$form['displays']['show']['tagged_with'] = array(
'#type' => 'textfield',
'#title' => t('tagged with'),
'#autocomplete_path' => 'taxonomy/autocomplete/' . $tag_field_name,
'#size' => 30,
'#maxlength' => 1024,
'#field_name' => $tag_field_name,
'#element_validate' => array('views_ui_taxonomy_autocomplete_validate'),
);
}
}
}
}
protected function build_sorts(&$form, &$form_state) {
$sorts = array(
'none' => t('Unsorted'),
);
if (!empty($this->plugin['created_column'])) {
$sorts += array(
$this->plugin['created_column'] . ':DESC' => t('Newest first'),
$this->plugin['created_column'] . ':ASC' => t('Oldest first'),
);
}
if (isset($this->plugin['available_sorts'])) {
$sorts += $this->plugin['available_sorts'];
}
if (!empty($sorts)) {
$form['displays']['show']['sort'] = array(
'#type' => 'select',
'#title' => t('sorted by'),
'#options' => $sorts,
'#default_value' => isset($this->plugin['created_column']) ? $this->plugin['created_column'] . ':DESC' : 'none',
);
}
}
protected function instantiate_view($form, &$form_state) {
$view = views_new_view();
$view->name = $form_state['values']['name'];
$view->human_name = $form_state['values']['human_name'];
$view->description = $form_state['values']['description'];
$view->tag = '';
$view->core = BACKDROP_VERSION;
$view->base_table = $this->base_table;
$display_options = $this->build_display_options($form, $form_state);
$this->alter_display_options($display_options, $form, $form_state);
$this->add_displays($view, $display_options, $form, $form_state);
return $view;
}
protected function build_display_options($form, $form_state) {
$display_options['default'] = $this->default_display_options($form, $form_state);
$display_options['default'] += array(
'filters' => array(),
'sorts' => array(),
);
$display_options['default']['filters'] += $this->default_display_filters($form, $form_state);
$display_options['default']['sorts'] += $this->default_display_sorts($form, $form_state);
if (!empty($form_state['values']['page']['create'])) {
$display_options['page'] = $this->page_display_options($form, $form_state);
if (!empty($form_state['values']['page']['feed'])) {
$display_options['feed'] = $this->page_feed_display_options($form, $form_state);
}
}
if (!empty($form_state['values']['block']['create'])) {
$display_options['block'] = $this->block_display_options($form, $form_state);
}
return $display_options;
}
protected function alter_display_options(&$display_options, $form, $form_state) {
if (isset($this->plugin['path_field'])) {
$path_field = $this->plugin['path_field'];
$path_fields_added = FALSE;
foreach ($display_options as $display_type => $options) {
if (!empty($options['style_plugin']) && $options['style_plugin'] == 'jump_menu') {
if (!$path_fields_added) {
$path_fields = !empty($this->plugin['path_fields_supplemental']) ? $this->plugin['path_fields_supplemental'] : array();
$path_fields[] = &$path_field;
foreach ($path_fields as &$field) {
$field['id'] = view::generate_item_id($field['id'], $display_options['default']['fields']);
$display_options['default']['fields'][$field['id']] = $field;
}
$path_fields_added = TRUE;
}
$display_options[$display_type]['style_options']['path'] = $path_field['id'];
}
}
}
foreach ($display_options as &$options) {
if ($options['style_plugin'] == 'table') {
foreach ($display_options['default']['fields'] as &$field) {
unset($field['label']);
}
}
}
}
protected function add_displays($view, $display_options, $form, $form_state) {
$default_display = $view->new_display('default', 'Default', 'default');
foreach ($display_options['default'] as $option => $value) {
$default_display->set_option($option, $value);
}
if (isset($display_options['page'])) {
$display = $view->new_display('page', 'Page', 'page');
$this->set_default_options($display_options['page'], $display, $default_display);
if (isset($display_options['feed'])) {
$display = $view->new_display('feed', 'Feed', 'feed');
$this->set_override_options($display_options['feed'], $display, $default_display);
}
}
if (isset($display_options['block'])) {
$display = $view->new_display('block', 'Block', 'block');
if (!isset($display_options['page'])) {
$this->set_default_options($display_options['block'], $display, $default_display);
}
else {
$this->set_override_options($display_options['block'], $display, $default_display);
}
}
}
protected function default_display_options($form, $form_state) {
$display_options = array();
$display_options['access']['type'] = 'none';
$display_options['cache']['type'] = 'none';
$display_options['query']['type'] = 'views_query';
$display_options['exposed_form']['type'] = 'basic';
$display_options['pager']['type'] = 'full';
$display_options['style_plugin'] = 'default';
$display_options['row_plugin'] = 'fields';
$data = views_fetch_data($this->base_table);
if (isset($data['table']['base']['defaults']['field'])) {
$field = $data['table']['base']['defaults']['field'];
}
else {
foreach ($data as $field => $field_data) {
if (isset($field_data['field']['handler'])) {
break;
}
}
}
$display_options['fields'][$field] = array(
'table' => $this->base_table,
'field' => $field,
'id' => $field,
);
return $display_options;
}
protected function default_display_filters($form, $form_state) {
$filters = array();
if (isset($this->plugin['filters'])) {
foreach ($this->plugin['filters'] as $name => $info) {
$filters[$name] = $info;
}
}
$filters = array_merge($filters, $this->default_display_filters_user($form, $form_state));
return $filters;
}
protected function default_display_filters_user($form, $form_state) {
$filters = array();
if (!empty($form_state['values']['show']['type']) && $form_state['values']['show']['type'] != 'all') {
$entity_key = $this->entity_info['entity keys']['bundle'];
$fields = views_fetch_fields($this->base_table, 'filter');
if (isset($fields[$this->base_table . '.' . $entity_key])) {
$table = $this->base_table;
}
else {
foreach ($fields as $field_name => $value) {
if ($pos = strpos($field_name, '.' . $entity_key)) {
$table = substr($field_name, 0, $pos);
break;
}
}
}
$table_data = views_fetch_data($table);
$handler = $table_data[$entity_key]['filter']['handler'];
if ($handler == 'views_handler_filter_in_operator' || is_subclass_of($handler, 'views_handler_filter_in_operator')) {
$value = backdrop_map_assoc(array($form_state['values']['show']['type']));
}
else {
$value = $form_state['values']['show']['type'];
}
$filters[$entity_key] = array(
'id' => $entity_key,
'table' => $table,
'field' => $entity_key,
'value' => $value,
);
}
if (!empty($form_state['values']['show']['tagged_with']['tids'])) {
$filters['tid'] = array(
'id' => 'tid',
'table' => 'taxonomy_index',
'field' => 'tid',
'value' => $form_state['values']['show']['tagged_with']['tids'],
'vocabulary' => $form_state['values']['show']['tagged_with']['vocabulary'],
);
if (count($form_state['values']['show']['tagged_with']['tids']) > 1) {
$filters['tid']['operator'] = 'and';
sort($filters['tid']['value']);
}
}
return $filters;
}
protected function default_display_sorts($form, $form_state) {
$sorts = array();
if (isset($this->plugin['sorts'])) {
foreach ($this->plugin['sorts'] as $name => $info) {
$sorts[$name] = $info;
}
}
$sorts = array_merge($sorts, $this->default_display_sorts_user($form, $form_state));
return $sorts;
}
protected function default_display_sorts_user($form, $form_state) {
$sorts = array();
if (!empty($form_state['values']['show']['sort']) && $form_state['values']['show']['sort'] != 'none') {
list($column, $sort) = explode(':', $form_state['values']['show']['sort']);
$column = explode('-', $column);
if (count($column) > 1) {
$table = $column[0];
$column = $column[1];
}
else {
$table = $this->base_table;
$column = $column[0];
}
$sorts[$column] = array(
'id' => $column,
'table' => $table,
'field' => $column,
'order' => $sort,
);
}
return $sorts;
}
protected function page_display_options($form, $form_state) {
$display_options = array();
$page = $form_state['values']['page'];
$display_options['title'] = $page['title'];
$display_options['path'] = $page['path'];
$display_options['style_plugin'] = $page['style']['style_plugin'];
$display_options['row_plugin'] = isset($page['style']['row_plugin']) ? $page['style']['row_plugin'] : 'fields';
if (empty($page['items_per_page'])) {
$display_options['pager']['type'] = 'none';
}
elseif (!empty($page['pager'])) {
$display_options['pager']['type'] = 'full';
}
else {
$display_options['pager']['type'] = 'some';
}
$display_options['pager']['options']['items_per_page'] = $page['items_per_page'];
if (!empty($page['link'])) {
$display_options['menu']['type'] = 'normal';
$display_options['menu']['title'] = $page['link_properties']['title'];
$display_options['menu']['name'] = $page['link_properties']['menu_name'];
}
return $display_options;
}
protected function block_display_options($form, $form_state) {
$display_options = array();
$block = $form_state['values']['block'];
$display_options['title'] = $block['title'];
$display_options['style_plugin'] = $block['style']['style_plugin'];
$display_options['row_plugin'] = isset($block['style']['row_plugin']) ? $block['style']['row_plugin'] : 'fields';
$display_options['pager']['type'] = $block['pager'] ? 'full' : (empty($block['items_per_page']) ? 'none' : 'some');
$display_options['pager']['options']['items_per_page'] = $block['items_per_page'];
return $display_options;
}
protected function page_feed_display_options($form, $form_state) {
$display_options = array();
$display_options['pager']['type'] = 'some';
$display_options['style_plugin'] = 'rss';
$display_options['row_plugin'] = $form_state['values']['page']['feed_properties']['row_plugin'];
$display_options['path'] = $form_state['values']['page']['feed_properties']['path'];
$display_options['title'] = $form_state['values']['page']['title'];
$display_options['displays'] = array(
'default' => 'default',
'page' => 'page',
);
return $display_options;
}
protected function set_default_options($options, $display, $default_display) {
foreach ($options as $option => $value) {
$default_value = $default_display->get_option($option);
if (isset($default_value)) {
$default_display->set_option($option, $value);
}
else {
$display->set_option($option, $value);
}
}
}
protected function set_override_options($options, $display, $default_display) {
foreach ($options as $option => $value) {
$default_value = $default_display->get_option($option);
if (!isset($default_value)) {
$display->set_option($option, $value);
}
elseif ($default_value !== $value) {
$display->override_option($option, $value);
}
}
}
protected function retrieve_validated_view($form, $form_state, $unset = TRUE) {
$key = hash('sha256', serialize($form_state['values']));
$view = (isset($this->validated_views[$key]) ? $this->validated_views[$key] : NULL);
if ($unset) {
unset($this->validated_views[$key]);
}
return $view;
}
protected function set_validated_view($form, $form_state, $view) {
$key = hash('sha256', serialize($form_state['values']));
$this->validated_views[$key] = $view;
}
function validate($form, &$form_state) {
$view = $this->instantiate_view($form, $form_state);
$errors = $view->validate();
if (!is_array($errors) || empty($errors)) {
$this->set_validated_view($form, $form_state, $view);
return array();
}
return $errors;
}
function create_view($form, &$form_state) {
$view = $this->retrieve_validated_view($form, $form_state);
if (empty($view)) {
throw new ViewsWizardException(t('Attempted to create_view with values that have not been validated'));
}
return $view;
}
}