00001 <?php
00002
00009 class views_handler_argument_string extends views_handler_argument {
00010 function init(&$view, &$options) {
00011 parent::init($view, $options);
00012 if (!empty($this->definition['many to one'])) {
00013 $this->helper = new views_many_to_one_helper($this);
00014
00015
00016 $this->operator = 'or';
00017 $this->value = array();
00018 }
00019 }
00020
00021 function option_definition() {
00022 $options = parent::option_definition();
00023
00024 $options['glossary'] = array('default' => FALSE);
00025 $options['limit'] = array('default' => 0);
00026 $options['case'] = array('default' => 'none');
00027 $options['path_case'] = array('default' => 'none');
00028 $options['transform_dash'] = array('default' => FALSE);
00029 $options['break_phrase'] = array('default' => FALSE);
00030
00031 if (!empty($this->definition['many to one'])) {
00032 $options['add_table'] = array('default' => FALSE);
00033 $options['require_value'] = array('default' => FALSE);
00034 }
00035
00036 return $options;
00037 }
00038
00039 function options_form(&$form, &$form_state) {
00040 parent::options_form($form, $form_state);
00041
00042 $form['glossary'] = array(
00043 '#type' => 'checkbox',
00044 '#title' => t('Glossary mode'),
00045 '#description' => t('Glossary mode applies a limit to the number of characters used in the filter value, which allows the summary view to act as a glossary.'),
00046 '#default_value' => $this->options['glossary'],
00047 '#fieldset' => 'more',
00048 );
00049
00050 $form['limit'] = array(
00051 '#type' => 'textfield',
00052 '#title' => t('Character limit'),
00053 '#description' => t('How many characters of the filter value to filter against. If set to 1, all fields starting with the first letter in the filter value would be matched.'),
00054 '#default_value' => $this->options['limit'],
00055 '#dependency' => array('edit-options-glossary' => array(TRUE)),
00056 '#fieldset' => 'more',
00057 );
00058
00059 $form['case'] = array(
00060 '#type' => 'select',
00061 '#title' => t('Case'),
00062 '#description' => t('When printing the title and summary, how to transform the case of the filter value.'),
00063 '#options' => array(
00064 'none' => t('No transform'),
00065 'upper' => t('Upper case'),
00066 'lower' => t('Lower case'),
00067 'ucfirst' => t('Capitalize first letter'),
00068 'ucwords' => t('Capitalize each word'),
00069 ),
00070 '#default_value' => $this->options['case'],
00071 '#fieldset' => 'more',
00072 );
00073
00074 $form['path_case'] = array(
00075 '#type' => 'select',
00076 '#title' => t('Case in path'),
00077 '#description' => t('When printing url paths, how to transform the case of the filter value. Do not use this unless with Postgres as it uses case sensitive comparisons.'),
00078 '#options' => array(
00079 'none' => t('No transform'),
00080 'upper' => t('Upper case'),
00081 'lower' => t('Lower case'),
00082 'ucfirst' => t('Capitalize first letter'),
00083 'ucwords' => t('Capitalize each word'),
00084 ),
00085 '#default_value' => $this->options['path_case'],
00086 '#fieldset' => 'more',
00087 );
00088
00089 $form['transform_dash'] = array(
00090 '#type' => 'checkbox',
00091 '#title' => t('Transform spaces to dashes in URL'),
00092 '#default_value' => $this->options['transform_dash'],
00093 '#fieldset' => 'more',
00094 );
00095
00096 if (!empty($this->definition['many to one'])) {
00097 $form['add_table'] = array(
00098 '#type' => 'checkbox',
00099 '#title' => t('Allow multiple filter values to work together'),
00100 '#description' => t('If selected, multiple instances of this filter can work together, as though multiple values were supplied to the same filter. This setting is not compatible with the "Reduce duplicates" setting.'),
00101 '#default_value' => !empty($this->options['add_table']),
00102 '#fieldset' => 'more',
00103 );
00104
00105 $form['require_value'] = array(
00106 '#type' => 'checkbox',
00107 '#title' => t('Do not display items with no value in summary'),
00108 '#default_value' => !empty($this->options['require_value']),
00109 '#fieldset' => 'more',
00110 );
00111 }
00112
00113
00114 $form['break_phrase'] = array(
00115 '#type' => 'checkbox',
00116 '#title' => t('Allow multiple values'),
00117 '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
00118 '#default_value' => !empty($this->options['break_phrase']),
00119 '#fieldset' => 'more',
00120 );
00121 }
00122
00126 function summary_query() {
00127 $this->placeholder_length = $this->placeholder();
00128 if (empty($this->definition['many to one'])) {
00129 $this->ensure_my_table();
00130 }
00131 else {
00132 $this->table_alias = $this->helper->summary_join();
00133 }
00134
00135 if (empty($this->options['glossary'])) {
00136
00137 $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
00138 $this->query->set_count_field($this->table_alias, $this->real_field);
00139 }
00140 else {
00141
00142 $formula = $this->get_formula();
00143 $params = array(
00144 'placeholders' => array(
00145 $this->placeholder_length => intval($this->options['limit']),
00146 ),
00147 );
00148
00149 $this->base_alias = $this->query->add_field(NULL, $formula, $this->field . '_truncated', $params);
00150 $this->query->set_count_field(NULL, $formula, $this->field, $this->field . '_truncated');
00151 }
00152
00153 $this->summary_name_field();
00154 return $this->summary_basics(FALSE);
00155 }
00156
00162 function get_formula() {
00163 return "SUBSTRING($this->table_alias.$this->real_field, 1, $this->placeholder_length)";
00164 }
00165
00169 function query($group_by = FALSE) {
00170 $argument = $this->argument;
00171 if (!empty($this->options['transform_dash'])) {
00172 $argument = strtr($argument, '-', ' ');
00173 }
00174 $this->placeholder_length = $this->placeholder();
00175
00176 if (!empty($this->options['break_phrase'])) {
00177 views_break_phrase_string($argument, $this);
00178 }
00179 else {
00180 $this->value = array($argument);
00181 $this->operator = 'or';
00182 }
00183
00184 if (!empty($this->definition['many to one'])) {
00185 if (!empty($this->options['glossary'])) {
00186 $this->helper->formula = TRUE;
00187 }
00188 $this->helper->ensure_my_table();
00189
00190 $this->helper->placeholders = array($this->placeholder_length => intval($this->options['limit']));
00191 $this->helper->add_filter();
00192 return;
00193 }
00194
00195 $this->ensure_my_table();
00196 $formula = FALSE;
00197 if (empty($this->options['glossary'])) {
00198 $field = "$this->table_alias.$this->real_field";
00199 }
00200 else {
00201 $formula = TRUE;
00202 $field = $this->get_formula();
00203 }
00204
00205 if (count($this->value) > 1) {
00206 $operator = 'IN';
00207 $argument = $this->value;
00208 }
00209 else {
00210 $operator = '=';
00211 }
00212
00213 if ($formula) {
00214 $placeholder = $this->placeholder();
00215 $placeholder_length = $this->placeholder_length;
00216 if ($operator == 'IN') {
00217 $field .= " IN($placeholder)";
00218 }
00219 else {
00220 $field .= ' = ' . $placeholder;
00221 }
00222 $placeholders = array(
00223 $placeholder_length => intval($this->options['limit']),
00224 $placeholder => $argument,
00225 );
00226 $this->query->add_where_expression(0, $field, $placeholders);
00227 }
00228 else {
00229 $this->query->add_where(0, $field, $argument, $operator);
00230 }
00231 }
00232
00233 function summary_argument($data) {
00234 $value = $this->case_transform($data->{$this->base_alias}, 'path_case');
00235 if (!empty($this->options['transform_dash'])) {
00236 $value = strtr($value, ' ', '-');
00237 }
00238 return $value;
00239 }
00240
00241 function get_sort_name() {
00242 return t('Alphabetical', array(), array('context' => 'Sort order'));
00243 }
00244
00245 function title() {
00246 $this->argument = $this->case_transform($this->argument, $this->options['case']);
00247 if (!empty($this->options['transform_dash'])) {
00248 $this->argument = strtr($this->argument, '-', ' ');
00249 }
00250
00251 if (!empty($this->options['break_phrase'])) {
00252 views_break_phrase_string($this->argument, $this);
00253 }
00254 else {
00255 $this->value = array($this->argument);
00256 $this->operator = 'or';
00257 }
00258
00259 if (empty($this->value)) {
00260 return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
00261 }
00262
00263 if ($this->value === array(-1)) {
00264 return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
00265 }
00266
00267 return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
00268 }
00269
00273 function title_query() {
00274 return drupal_map_assoc($this->value, 'check_plain');
00275 }
00276
00277 function summary_name($data) {
00278 return $this->case_transform(parent::summary_name($data), $this->options['case']);
00279 }
00280
00281 }