View source
<?php
namespace Drupal\google_analytics_reports\Plugin\views\filter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class GoogleAnalyticsNumeric extends GoogleAnalyticsBase {
use StringTranslationTrait;
public function defineOptions() {
$options = parent::defineOptions();
$options['value'] = [
'contains' => [
'min' => [
'default' => '',
],
'max' => [
'default' => '',
],
'value' => [
'default' => '',
],
],
];
return $options;
}
public function operators() {
$operators = [
'<' => [
'title' => $this
->t('Is less than'),
'method' => 'opSimple',
'short' => $this
->t('<'),
'values' => 1,
],
'<=' => [
'title' => $this
->t('Is less than or equal to'),
'method' => 'opSimple',
'short' => $this
->t('<='),
'values' => 1,
],
'==' => [
'title' => $this
->t('Is equal to'),
'method' => 'opSimple',
'short' => $this
->t('=='),
'values' => 1,
],
'!=' => [
'title' => $this
->t('Is not equal to'),
'method' => 'opSimple',
'short' => $this
->t('!='),
'values' => 1,
],
'>=' => [
'title' => $this
->t('Is greater than or equal to'),
'method' => 'opSimple',
'short' => $this
->t('>='),
'values' => 1,
],
'>' => [
'title' => $this
->t('Is greater than'),
'method' => 'opSimple',
'short' => $this
->t('>'),
'values' => 1,
],
];
return $operators;
}
public function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
$values = $form_state
->getValues();
$form['value']['#tree'] = TRUE;
$which = 'all';
if (!empty($form['operator'])) {
$source = $form['operator']['#type'] == 'radios' ? 'radio:options[operator]' : 'edit-options-operator';
}
if (!empty($values['exposed'])) {
$identifier = $this->options['expose']['identifier'];
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
$which = in_array($this->operator, $this
->operatorValues(2)) ? 'minmax' : 'value';
}
else {
$source = 'edit-' . Html::getUniqueId($this->options['expose']['operator_id']);
}
}
if ($which == 'all') {
$form['value']['value'] = [
'#type' => 'textfield',
'#title' => empty($values['exposed']) ? $this
->t('Value') : '',
'#size' => 30,
'#default_value' => $this->value['value'],
'#dependency' => [
$source => $this
->operatorValues(1),
],
];
if (!empty($values['exposed']) && !isset($values['input'][$identifier]['value'])) {
$values['input'][$identifier]['value'] = $this->value['value'];
}
}
elseif ($which == 'value') {
$form['value'] = [
'#type' => 'textfield',
'#title' => empty($values['exposed']) ? $this
->t('Value') : '',
'#size' => 30,
'#default_value' => $this->value['value'],
];
if (!empty($values['exposed']) && !isset($values['input'][$identifier])) {
$values['input'][$identifier] = $this->value['value'];
}
}
if ($which == 'all' || $which == 'minmax') {
$form['value']['min'] = [
'#type' => 'textfield',
'#title' => empty($values['exposed']) ? $this
->t('Min') : '',
'#size' => 30,
'#default_value' => $this->value['min'],
];
$form['value']['max'] = [
'#type' => 'textfield',
'#title' => empty($values['exposed']) ? $this
->t('And max') : $this
->t('And'),
'#size' => 30,
'#default_value' => $this->value['max'],
];
if ($which == 'all') {
$dependency = [
'#dependency' => [
$source => $this
->operatorValues(2),
],
];
$form['value']['min'] += $dependency;
$form['value']['max'] += $dependency;
}
if (!empty($values['exposed']) && !isset($values['input'][$identifier]['min'])) {
$values['input'][$identifier]['min'] = $this->value['min'];
}
if (!empty($values['exposed']) && !isset($values['input'][$identifier]['max'])) {
$values['input'][$identifier]['max'] = $this->value['max'];
}
if (!isset($form['value'])) {
$form['value'] = [
'#type' => 'value',
'#value' => NULL,
];
}
}
}
protected function opSimple($field) {
$this->query
->addWhere($this->options['group'], $field, $this->value['value'], $this->operator);
}
public function adminSummary() {
if (!empty($this->options['exposed'])) {
return $this
->t('exposed');
}
$options = $this
->operatorOptions('short');
$output = Html::escape($options[$this->operator]);
if (in_array($this->operator, $this
->operatorValues(2))) {
$output .= ' ' . $this
->t('@min and @max', [
'@min' => $this->value['min'],
'@max' => $this->value['max'],
]);
}
elseif (in_array($this->operator, $this
->operatorValues(1))) {
$output .= ' ' . Html::escape($this->value['value']);
}
return $output;
}
public function acceptExposedInput($input) {
if (empty($this->options['exposed'])) {
return TRUE;
}
if (!empty($this->options['expose']['identifier'])) {
$value =& $input[$this->options['expose']['identifier']];
if (!is_array($value)) {
$value = [
'value' => $value,
];
}
}
$rc = parent::acceptExposedInput($input);
if (empty($this->options['expose']['required'])) {
$info = $this
->operators();
if (!empty($info[$this->operator]['values'])) {
switch ($info[$this->operator]['values']) {
case 1:
if ($value['value'] === '') {
return FALSE;
}
break;
case 2:
if ($value['min'] === '' && $value['max'] === '') {
return FALSE;
}
break;
}
}
}
return $rc;
}
public function operatorValues($values = 1) {
$options = [];
foreach ($this
->operators() as $id => $info) {
if ($info['values'] == $values) {
$options[] = $id;
}
}
return $options;
}
}