views_handler_argument_string.inc

Same filename in other branches
  1. 7.x-3.x handlers/views_handler_argument_string.inc

File

handlers/views_handler_argument_string.inc

View source
<?php


/**
 * Basic argument handler to implement string arguments that may have length
 * limits.
 *
 * @ingroup views_argument_handlers
 */
class views_handler_argument_string extends views_handler_argument {
    function init(&$view, $options) {
        parent::init($view, $options);
        if (!empty($this->definition['many to one'])) {
            $this->helper = new views_many_to_one_helper($this);
            // Ensure defaults for these, during summaries and stuff:
            $this->operator = 'or';
            $this->value = array();
        }
    }
    function option_definition() {
        $options = parent::option_definition();
        $options['glossary'] = array(
            'default' => FALSE,
        );
        $options['ignorecase'] = array(
            'default' => FALSE,
        );
        $options['limit'] = array(
            'default' => 0,
        );
        $options['case'] = array(
            'default' => 'none',
        );
        $options['path_case'] = array(
            'default' => 'none',
        );
        $options['transform_dash'] = array(
            'default' => FALSE,
        );
        $options['break_phrase'] = array(
            'default' => FALSE,
        );
        if (!empty($this->definition['many to one'])) {
            $options['add_table'] = array(
                'default' => FALSE,
            );
            $options['require_value'] = array(
                'default' => FALSE,
            );
        }
        return $options;
    }
    function options_form(&$form, &$form_state) {
        parent::options_form($form, $form_state);
        $form['glossary'] = array(
            '#type' => 'checkbox',
            '#title' => t('Glossary mode'),
            '#description' => t('Glossary mode applies a limit to the number of characters used in the argument, which allows the summary view to act as a glossary.'),
            '#default_value' => $this->options['glossary'],
        );
        $form['ignorecase'] = array(
            '#type' => 'checkbox',
            '#title' => t('Ignore case'),
            '#description' => t('Ignore case allows for doing database searches without case sensitivity. MySQL already works in lower-case mode, so MySQL users should leave this unchecked to improve performance.'),
            '#default_value' => $this->options['ignorecase'],
        );
        $form['limit'] = array(
            '#type' => 'textfield',
            '#title' => t('Character limit'),
            '#description' => t('How many characters of the argument to filter against. If set to 1, all fields starting with the letter in the argument would be matched.'),
            '#default_value' => $this->options['limit'],
            '#process' => array(
                'views_process_dependency',
            ),
            '#dependency' => array(
                'edit-options-glossary' => array(
                    TRUE,
                ),
            ),
        );
        $form['case'] = array(
            '#type' => 'select',
            '#title' => t('Case'),
            '#description' => t('When printing the argument result, how to transform the case.'),
            '#options' => array(
                'none' => t('No transform'),
                'upper' => t('Upper case'),
                'lower' => t('Lower case'),
                'ucfirst' => t('Capitalize first letter'),
                'ucwords' => t('Capitalize each word'),
            ),
            '#default_value' => $this->options['case'],
        );
        $form['path_case'] = array(
            '#type' => 'select',
            '#title' => t('Case in path'),
            '#description' => t('When printing url paths, how to transform the case of the argument. Do not use this unless with Postgres as it uses case sensitive comparisons.'),
            '#options' => array(
                'none' => t('No transform'),
                'upper' => t('Upper case'),
                'lower' => t('Lower case'),
                'ucfirst' => t('Capitalize first letter'),
                'ucwords' => t('Capitalize each word'),
            ),
            '#default_value' => $this->options['path_case'],
        );
        $form['transform_dash'] = array(
            '#type' => 'checkbox',
            '#title' => t('Transform spaces to dashes in URL'),
            '#default_value' => $this->options['transform_dash'],
        );
        if (!empty($this->definition['many to one'])) {
            $form['add_table'] = array(
                '#type' => 'checkbox',
                '#title' => t('Allow multiple arguments to work together'),
                '#description' => t('If selected, multiple instances of this argument can work together, as though multiple terms were supplied to the same argument. This setting is not compatible with the "Reduce duplicates" setting.'),
                '#default_value' => !empty($this->options['add_table']),
            );
            $form['require_value'] = array(
                '#type' => 'checkbox',
                '#title' => t('Do not display items with no value in summary'),
                '#default_value' => !empty($this->options['require_value']),
            );
        }
        // allow + for or, , for and
        $form['break_phrase'] = array(
            '#type' => 'checkbox',
            '#title' => t('Allow multiple terms per argument'),
            '#description' => t('If selected, users can enter multiple arguments in the form of 1+2+3 or 1,2,3.'),
            '#default_value' => !empty($this->options['break_phrase']),
        );
    }
    
    /**
     * Build the summary query based on a string
     */
    function summary_query() {
        if (empty($this->definition['many to one'])) {
            $this->ensure_my_table();
        }
        else {
            $this->table_alias = $this->helper
                ->summary_join();
        }
        if (empty($this->options['glossary'])) {
            // Add the field.
            if (empty($this->options['ignorecase'])) {
                $this->base_alias = $this->query
                    ->add_field($this->table_alias, $this->real_field);
                $this->query
                    ->set_count_field($this->table_alias, $this->real_field);
            }
            else {
                $this->base_alias = $this->query
                    ->add_field($this->table_alias, 'LOWER(' . $this->real_field . ')');
                $this->query
                    ->set_count_field($this->table_alias, 'LOWER(' . $this->real_field . ')');
            }
        }
        else {
            // Add the field.
            $formula = $this->get_formula();
            if (empty($this->options['ignorecase'])) {
                $this->base_alias = $this->query
                    ->add_field(NULL, $formula, $this->field . '_truncated');
                $this->query
                    ->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
            }
            else {
                $this->base_alias = $this->query
                    ->add_field(NULL, 'LOWER(' . $formula . ')', $this->field . '_truncated');
                $this->query
                    ->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
            }
        }
        $this->summary_name_field();
        return $this->summary_basics(FALSE);
    }
    
    /**
     * Get the formula for this argument.
     *
     * $this->ensure_my_table() MUST have been called prior to this.
     */
    function get_formula() {
        return "SUBSTR({$this->table_alias}.{$this->real_field}, 1, " . intval($this->options['limit']) . ")";
    }
    
    /**
     * Build the query based upon the formula
     */
    function query() {
        $argument = $this->argument;
        if (!empty($this->options['transform_dash'])) {
            $argument = strtr($argument, '-', ' ');
        }
        if (!empty($this->options['break_phrase'])) {
            views_break_phrase_string($argument, $this);
        }
        else {
            $this->value = array(
                $argument,
            );
            $this->operator = 'or';
        }
        if (!empty($this->definition['many to one'])) {
            if (!empty($this->options['glossary'])) {
                $this->helper->formula = TRUE;
            }
            $this->helper
                ->ensure_my_table();
            $this->helper
                ->add_filter();
            return;
        }
        $this->ensure_my_table();
        if (empty($this->options['glossary'])) {
            $field = "{$this->table_alias}.{$this->real_field}";
        }
        else {
            $field = $this->get_formula();
        }
        if (count($this->value) > 1) {
            $operator = 'IN';
            $placeholders = '(' . implode(', ', array_fill(0, sizeof($this->value), "'%s'")) . ')';
            $argument = $this->value;
        }
        else {
            $placeholders = "'%s'";
            $operator = '=';
        }
        if (empty($this->options['ignorecase'])) {
            $this->query
                ->add_where(0, "{$field} {$operator} {$placeholders}", $argument);
        }
        else {
            $this->query
                ->add_where(0, "LOWER({$field}) {$operator} LOWER({$placeholders})", $argument);
        }
    }
    function summary_argument($data) {
        $value = $this->case_transform($data->{$this->base_alias}, 'path_case');
        if (!empty($this->options['transform_dash'])) {
            $value = strtr($value, ' ', '-');
        }
        return $value;
    }
    function case_transform($string, $option) {
        global $multibyte;
        switch ($this->options[$option]) {
            default:
                return $string;
            case 'upper':
                return drupal_strtoupper($string);
            case 'lower':
                return drupal_strtolower($string);
            case 'ucfirst':
                return drupal_strtoupper(drupal_substr($string, 0, 1)) . drupal_substr($string, 1);
            case 'ucwords':
                if ($multibyte == UNICODE_MULTIBYTE) {
                    return mb_convert_case($string, MB_CASE_TITLE);
                }
                else {
                    return ucwords($string);
                }
        }
    }
    function title() {
        $this->argument = $this->case_transform($this->argument, 'case');
        if (!empty($this->options['transform_dash'])) {
            $this->argument = strtr($this->argument, '-', ' ');
        }
        if (!empty($this->options['break_phrase'])) {
            views_break_phrase_string($this->argument, $this);
        }
        else {
            $this->value = array(
                $this->argument,
            );
            $this->operator = 'or';
        }
        if (empty($this->value)) {
            return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
        }
        if ($this->value === array(
            -1,
        )) {
            return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
        }
        return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
    }
    
    /**
     * Override for specific title lookups.
     */
    function title_query() {
        return drupal_map_assoc($this->value, 'check_plain');
    }
    function summary_name($data) {
        return $this->case_transform(parent::summary_name($data), 'case');
    }

}

Classes

Title Deprecated Summary
views_handler_argument_string Basic argument handler to implement string arguments that may have length limits.