Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISLANDORA-1392: New Islandora Solr Views mechanic #39

Open
wants to merge 4 commits into
base: 7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions handlers/islandora_solr_views_handler_argument_by_schema.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* @file
* Views base handler for arguments
*/

// @codingStandardsIgnoreStart
// This Whole file is ignored due to classes and methods are not camelcase and it
// being called all over the place. TODO bring up to coding standards
class islandora_solr_views_handler_argument_by_schema extends views_handler_argument {
/**
* Function query.
*/
function query($group_by = FALSE) {
if (!empty($this->argument) && !empty($this->options['solr_field'])) {
$value = islandora_solr_lesser_escape($this->argument);
$solr_field = $this->options['solr_field'];
module_load_include('inc', 'islandora_solr', 'includes/utilities');
$group = isset($this->options['group']) ? $this->options['group'] : FALSE;
if (is_array($value)) {
$values = array_filter($value);
// Ensure that some values have been selected.
if (!empty($values)) {
$this->query->add_filter($solr_field, '(' . implode('OR', $values) . ')', $group);
}
return;
}
if (!empty($value)) {
$this->query->add_filter($solr_field, $value, $group);
}
}
}

/**
* Define custom option for our solr field.
*/
function option_definition() {
$options = parent::option_definition();
$options['solr_field'] = array('default' => 'PID');
return $options;
}

/**
* Define form element for real solr field.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['solr_field'] = array(
'#prefix' => '<div class="clearfix">',
'#suffix' => '</div>',
'#title' => t('Solr field to use'),
'#description' => t("Select the Solr field to use"),
'#size' => 45,
'#type' => 'textfield',
'#autocomplete_path' => 'islandora_solr_views/autocomplete_luke/all',
'#default_value' => !empty($this->options['solr_field']) ? $this->options['solr_field'] : 'PID',
'#required' => TRUE,
'#weight' => -10,
);
}
}
// @codingStandardsIgnoreEnd
48 changes: 48 additions & 0 deletions handlers/islandora_solr_views_handler_field_by_schema.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* @file
* Views base handler for field.
*/

// @codingStandardsIgnoreStart
// This Whole file is ignored due to classes and methods are not camelcase and it
// being called all over the place. TODO bring up to coding standards

class islandora_solr_views_handler_field_by_schema extends islandora_solr_views_handler_field {


/**
* Define new options for additional solr field.
*/
function option_definition() {
$options = parent::option_definition();
$options['solr_field'] = array('default' => 'PID');
return $options;
}

/**
* Define form element for 'link to object' option and 'solr_field'.
*/
function options_form(&$form, &$form_state) {

$form['solr_field'] = array(
'#title' => t('Solr field to use'),
'#description' => t("Select the Solr field to use"),
'#size' => 45,
'#type' => 'textfield',
'#autocomplete_path' => 'islandora_solr_views/autocomplete_luke/displayable',
'#default_value' => !empty($this->options['solr_field']) ? $this->options['solr_field'] : 'PID',
'#required' => TRUE,
);

parent::options_form($form, $form_state);
}

function ui_name($short = FALSE) {
return $this->get_field(parent::ui_name($short)) . ' ' . $this->options['solr_field'];
}


}
// @codingStandardsIgnoreEnd
38 changes: 36 additions & 2 deletions handlers/islandora_solr_views_handler_field_date.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@
// being called all over the place. TODO bring up to coding standards
class islandora_solr_views_handler_field_date extends islandora_solr_views_handler_field {


/**
* Get value.
*
* @param type $values
* @param type $field
*
* @return type string
*/
function get_value($values, $field = NULL) {

$alias = isset($field) ? $this->aliases[$field] : $this->field_alias;

if (isset($values->{$alias})) {
if (is_array($values->{$alias})) {
$values->{$alias} = array_filter($values->{$alias}, 'trim');
return implode(", ", $values->{$alias});
}
else {
return $values->{$alias};
}
}
}
/**
* Define new option.
*/
Expand All @@ -20,6 +43,7 @@ class islandora_solr_views_handler_field_date extends islandora_solr_views_handl
$options = parent::option_definition();

// Set defaults.
$options['solr_field'] = array('default' => 'fgs_createdDate_dt');
$options['date_format'] = array('default' => 'small');
$options['custom_date_format'] = array('default' => '');

Expand All @@ -39,7 +63,15 @@ class islandora_solr_views_handler_field_date extends islandora_solr_views_handl
'@date' => format_date(REQUEST_TIME, $value['type']),
));
}

$form['solr_field'] = array(
'#title' => t('Solr field to use'),
'#description' => t("Select the Solr date field to use"),
'#size' => 45,
'#type' => 'textfield',
'#autocomplete_path' => 'islandora_solr_views/autocomplete_luke/date',
'#default_value' => !empty($this->options['solr_field']) ? $this->options['solr_field'] : 'fgs_createdDate_dt',
'#required' => TRUE,
);
$form['date_format'] = array(
'#type' => 'select',
'#title' => t('Date format'),
Expand Down Expand Up @@ -76,7 +108,9 @@ class islandora_solr_views_handler_field_date extends islandora_solr_views_handl

parent::options_form($form, $form_state);
}

function ui_name($short = FALSE) {
return $this->get_field(parent::ui_name($short)) . ' ' . $this->options['solr_field'];
}
/**
* Render field.
*/
Expand Down
1 change: 1 addition & 0 deletions handlers/islandora_solr_views_handler_filter.inc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class islandora_solr_views_handler_filter extends views_handler_filter {
* Provide default options for exposed filters.
*/
function expose_options() {
parent::expose_options();
$this->options['expose']['identifier'] = drupal_strtolower(preg_replace('/[^A-Za-z0-9]/', '_', $this->options['id']));
}

Expand Down
139 changes: 139 additions & 0 deletions handlers/islandora_solr_views_handler_filter_by_schema.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

/**
* @file
* Views filter handler base.
*/

// @codingStandardsIgnoreStart
// This Whole file is ignored due to classes and methods are not camelcase and it
// being called all over the place. TODO bring up to coding standards
class islandora_solr_views_handler_filter_by_schema extends islandora_solr_views_handler_filter {

function query() {
if (!empty($this->value) && !empty($this->options['solr_field'])) {
$value = $this->value;
module_load_include('inc', 'islandora_solr', 'includes/utilities');
// Only escape if 'value_type' is disabled to allow range queries
// and other non string ones.
if (!$this->options['value_type']) {
$value = islandora_solr_lesser_escape($value);
}
$exclude = isset($this->operator) && $this->operator === '!=';
if (is_array($value)) {
$values = array_filter($value);
// Ensure that some values have been selected.
if (!empty($values)) {
$this->query->add_filter($this->options['solr_field'], '(' . implode('OR', $values) . ')', $this->options['group'], $exclude);
}
return;
}
$this->query->add_filter($this->options['solr_field'], $value, $this->options['group'], $exclude);
}
}
/**
* Define new custom options.
*/
function option_definition() {
$options = parent::option_definition();
$options['solr_field'] = array('default' => 'PID');
$options['value_type'] = array('default' => 0);
// There is a bug in Views that makes this option required,
// even when not exposed.
$options['expose']['identifier'] = array('default' => 'solrfilter');

return $options;
}

function admin_summary() {
return check_plain((string) $this->options['solr_field']) . check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
}

/**
* Define custom form elements to match options.
*/
function options_form(&$form, &$form_state) {
// Add an option to allow non string filters like [* TO NOW]
$form['value_type'] = array(
'#type' => 'checkbox',
'#title' => t("Don't escape filter value for Solr"),
'#description' => t('Enable filter value to be passed without escaping. Useful for e.g [* TO NOW]'),
'#default_value' => !empty($this->options['value_type']) ? $this->options['value_type'] : 0,
'#weight' => 11,
);
$form['solr_field'] = array(
'#prefix' => '<div class="clearfix">',
'#suffix' => '</div>',
'#title' => t('Solr field to use'),
'#description' => t("Select the Solr field to use"),
'#size' => 45,
'#type' => 'textfield',
'#autocomplete_path' => 'islandora_solr_views/autocomplete_luke/all',
'#default_value' => !empty($this->options['solr_field']) ? $this->options['solr_field'] : 'PID',
'#required' => TRUE,
'#weight' => -10,
);
parent::options_form($form, $form_state);
// Modify the title for 'value'
$form['value']['#title'] = t("Filter value for Solr field");
$form['value']['#weigth'] = 10;
}

/**
* Don't allow exposure for not filtered values.
*/
function can_expose() {
return ($this->options['value_type'] == 0);
}

/**
* Validation handler, the parent:: fails on disabling 'expose'.
*/
function options_validate(&$form, &$form_state) {
$this->operator_validate($form, $form_state);
$this->value_validate($form, $form_state);
if (isset($form_state['values']['expose_button']['checkbox']['checkbox']) && ($form_state['values']['expose_button']['checkbox']['checkbox'] == 1) && !$this->is_a_group()) {
$this->expose_validate($form, $form_state);
}
if ($this->is_a_group()) {
$this->build_group_validate($form, $form_state);
}
}

/**
* Build strings from the operators() for 'select' options.
*/
function operator_options($which = 'title') {
$options = array();
foreach ($this->operators() as $id => $info) {
$options[$id] = $info[$which];
}

return $options;
}

function operator_values($values = 1) {
$options = array();
foreach ($this->operators() as $id => $info) {
if (isset($info['values']) && $info['values'] == $values) {
$options[] = $id;
}
}

return $options;
}

public function expose_form(&$form, &$form_state) {
parent::expose_form($form, $form_state);
if (empty($form['expose']['identifier']['#default_value'])) {
$form['expose']['identifier']['#default_value'] = $this->options['field'];
}
if (empty($form['expose']['label']['#default_value'])) {
$form['expose']['label']['#default_value'] = $this->definition['title'];
}
if (empty($form['ui_name']['#default_value'])) {
$form['ui_name']['#default_value'] = $this->definition['title'];
}
}
}
// @codingStandardsIgnoreEnd
58 changes: 58 additions & 0 deletions handlers/islandora_solr_views_handler_sort_by_schema.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @file
* Views base handler for sort.
*/

// @codingStandardsIgnoreStart
// This Whole file is ignored due to classes and methods are not camelcase and it
// being called all over the place. TODO bring up to coding standards

/**
* Class for sorting for a field.
*/
class islandora_solr_views_handler_sort_by_schema extends views_handler_sort {

/**
* Places the sort into the search parameters.
*/
public function query() {
$order = drupal_strtolower($this->options['order']);
$this->query->add_sort($this->options['solr_field'], $order);
}
/**
* Define new custom option.
*/
function option_definition() {
$options = parent::option_definition();
$options['solr_field'] = array('default' => 'PID');
return $options;
}

/**
* Custom admin summary for field.
*/
function admin_summary() {
$internal_summary = parent::admin_summary();
return check_plain((string) $this->options['solr_field']) . ' ' . check_plain(drupal_strtolower((string) $this->options['order'])) . ' ' . $internal_summary;
}

/**
* Define form element for 'solr_field'.
*/
function options_form(&$form, &$form_state) {
$form['solr_field'] = array(
'#title' => t('Solr field to use for sorting'),
'#description' => t("Select the Solr field to use for sorting"),
'#size' => 45,
'#type' => 'textfield',
'#autocomplete_path' => 'islandora_solr_views/autocomplete_luke/sortable',
'#default_value' => !empty($this->options['solr_field']) ? $this->options['solr_field'] : 'PID',
'#required' => TRUE,
);

parent::options_form($form, $form_state);
}
}
// @codingStandardsIgnoreEnd
Loading