| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\Control\SearchBar; |
| 4 |
|
| 5 |
use ipl\Html\Attributes; |
| 6 |
use ipl\Html\BaseHtmlElement; |
| 7 |
use ipl\Html\HtmlElement; |
| 8 |
use ipl\Stdlib\Filter; |
| 9 |
use ipl\Web\Filter\QueryString; |
| 10 |
use ipl\Web\Widget\Icon; |
| 11 |
|
| 12 |
class Terms extends BaseHtmlElement |
| 13 |
{ |
| 14 |
protected $tag = 'div'; |
| 15 |
|
| 16 |
protected $defaultAttributes = ['class' => 'terms']; |
| 17 |
|
| 18 |
/** @var callable|Filter\Rule */ |
| 19 |
protected $filter; |
| 20 |
|
| 21 |
/** @var array */ |
| 22 |
protected $changes; |
| 23 |
|
| 24 |
/** @var int */ |
| 25 |
private $changeIndexCorrection = 0; |
| 26 |
|
| 27 |
/** @var int */ |
| 28 |
private $currentIndex = 0; |
| 29 |
|
| 30 |
public function setFilter($filter) |
| 31 |
{ |
| 32 |
$this->filter = $filter; |
| 33 |
|
| 34 |
return $this; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Apply term changes |
| 39 |
* |
| 40 |
* @param array $changes |
| 41 |
* |
| 42 |
* @return $this |
| 43 |
*/ |
| 44 |
public function applyChanges(array $changes) |
| 45 |
{ |
| 46 |
$this->changes = $changes; |
| 47 |
|
| 48 |
return $this; |
| 49 |
} |
| 50 |
|
| 51 |
protected function assemble() |
| 52 |
{ |
| 53 |
$filter = $this->filter; |
| 54 |
if (is_callable($filter)) { |
| 55 |
$filter = $filter(); |
| 56 |
} |
| 57 |
|
| 58 |
if ($filter === null) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if ($filter instanceof Filter\Chain) { |
| 63 |
if ($filter->isEmpty()) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
if ($filter instanceof Filter\None) { |
| 68 |
$this->assembleChain($filter, $this, $filter->count() > 1); |
| 69 |
} else { |
| 70 |
$this->assembleConditions($filter, $this); |
| 71 |
} |
| 72 |
} else { |
| 73 |
/** @var Filter\Condition $filter */ |
| 74 |
$this->assembleCondition($filter, $this); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
protected function assembleConditions(Filter\Chain $filters, BaseHtmlElement $where) |
| 79 |
{ |
| 80 |
foreach ($filters as $i => $filter) { |
| 81 |
if ($i > 0) { |
| 82 |
$logicalOperator = QueryString::getRuleSymbol($filters); |
| 83 |
$this->assembleTerm([ |
| 84 |
'class' => 'logical-operator', |
| 85 |
'type' => 'logical_operator', |
| 86 |
'search' => $logicalOperator, |
| 87 |
'label' => $logicalOperator |
| 88 |
], $where); |
| 89 |
} |
| 90 |
|
| 91 |
if ($filter instanceof Filter\Chain) { |
| 92 |
$this->assembleChain($filter, $where, $filter->count() > 1); |
| 93 |
} else { |
| 94 |
/** @var Filter\Condition $filter */ |
| 95 |
$this->assembleCondition($filter, $where); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
protected function assembleChain(Filter\Chain $chain, BaseHtmlElement $where, $wrap = false) |
| 101 |
{ |
| 102 |
if ($wrap) { |
| 103 |
$group = new HtmlElement( |
| 104 |
'div', |
| 105 |
Attributes::create(['class' => 'filter-chain', 'data-group-type' => 'chain']) |
| 106 |
); |
| 107 |
} else { |
| 108 |
$group = $where; |
| 109 |
} |
| 110 |
|
| 111 |
if ($chain instanceof Filter\None) { |
| 112 |
$this->assembleTerm([ |
| 113 |
'class' => 'logical-operator', |
| 114 |
'type' => 'negation_operator', |
| 115 |
'search' => '!', |
| 116 |
'label' => '!' |
| 117 |
], $where); |
| 118 |
} |
| 119 |
|
| 120 |
if ($wrap) { |
| 121 |
$opening = $this->assembleTerm([ |
| 122 |
'class' => 'grouping-operator-open', |
| 123 |
'type' => 'grouping_operator', |
| 124 |
'search' => '(', |
| 125 |
'label' => '(' |
| 126 |
], $group); |
| 127 |
} |
| 128 |
|
| 129 |
$this->assembleConditions($chain, $group); |
| 130 |
|
| 131 |
if ($wrap) { |
| 132 |
$closing = $this->assembleTerm([ |
| 133 |
'class' => 'grouping-operator-close', |
| 134 |
'type' => 'grouping_operator', |
| 135 |
'search' => ')', |
| 136 |
'label' => ')' |
| 137 |
], $group); |
| 138 |
|
| 139 |
$opening->addAttributes([ |
| 140 |
'data-counterpart' => $closing->getAttributes()->get('data-index')->getValue() |
| 141 |
]); |
| 142 |
$closing->addAttributes([ |
| 143 |
'data-counterpart' => $opening->getAttributes()->get('data-index')->getValue() |
| 144 |
]); |
| 145 |
|
| 146 |
$where->addHtml($group); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
protected function assembleCondition(Filter\Condition $filter, BaseHtmlElement $where) |
| 151 |
{ |
| 152 |
$column = $filter->getColumn(); |
| 153 |
$operator = QueryString::getRuleSymbol($filter); |
| 154 |
$value = $filter->getValue(); |
| 155 |
$columnLabel = $filter->metaData()->get('columnLabel', $column); |
| 156 |
|
| 157 |
$group = new HtmlElement( |
| 158 |
'div', |
| 159 |
Attributes::create(['class' => 'filter-condition', 'data-group-type' => 'condition']), |
| 160 |
new HtmlElement('button', Attributes::create(['type' => 'button']), new Icon('trash')) |
| 161 |
); |
| 162 |
|
| 163 |
$columnData = [ |
| 164 |
'class' => 'column', |
| 165 |
'type' => 'column', |
| 166 |
'search' => rawurlencode($column), |
| 167 |
'label' => $columnLabel, |
| 168 |
'title' => $column |
| 169 |
]; |
| 170 |
if ($filter->metaData()->has('invalidColumnPattern')) { |
| 171 |
$columnData['pattern'] = $filter->metaData()->get('invalidColumnPattern'); |
| 172 |
if ($filter->metaData()->has('invalidColumnMessage')) { |
| 173 |
$columnData['invalidMsg'] = $filter->metaData()->get('invalidColumnMessage'); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
$this->assembleTerm($columnData, $group); |
| 178 |
|
| 179 |
if ($value !== true) { |
| 180 |
$operatorData = [ |
| 181 |
'class' => 'operator', |
| 182 |
'type' => 'operator', |
| 183 |
'search' => $operator, |
| 184 |
'label' => $operator |
| 185 |
]; |
| 186 |
if ($filter->metaData()->has('invalidOperatorPattern')) { |
| 187 |
$operatorData['pattern'] = $filter->metaData()->get('invalidOperatorPattern'); |
| 188 |
if ($filter->metaData()->has('invalidOperatorMessage')) { |
| 189 |
$operatorData['invalidMsg'] = $filter->metaData()->get('invalidOperatorMessage'); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
$this->assembleTerm($operatorData, $group); |
| 194 |
|
| 195 |
if (! empty($value) || ! is_string($value) || ctype_digit($value)) { |
| 196 |
$valueData = [ |
| 197 |
'class' => 'value', |
| 198 |
'type' => 'value', |
| 199 |
'search' => rawurlencode($value), |
| 200 |
'label' => $value |
| 201 |
]; |
| 202 |
if ($filter->metaData()->has('invalidValuePattern')) { |
| 203 |
$valueData['pattern'] = $filter->metaData()->get('invalidValuePattern'); |
| 204 |
if ($filter->metaData()->has('invalidValueMessage')) { |
| 205 |
$valueData['invalidMsg'] = $filter->metaData()->get('invalidValueMessage'); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
$this->assembleTerm($valueData, $group); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
$where->addHtml($group); |
| 214 |
} |
| 215 |
|
| 216 |
protected function assembleTerm(array $data, BaseHtmlElement $where) |
| 217 |
{ |
| 218 |
if (isset($this->changes[$this->currentIndex - $this->changeIndexCorrection])) { |
| 219 |
$change = $this->changes[$this->currentIndex - $this->changeIndexCorrection]; |
| 220 |
if ($change['type'] !== $data['type']) { |
| 221 |
// This can happen because the user didn't insert parentheses but the parser did |
| 222 |
$this->changeIndexCorrection++; |
| 223 |
} else { |
| 224 |
$data = array_merge($data, $change); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
$term = new HtmlElement('label', Attributes::create([ |
| 229 |
'class' => $data['class'], |
| 230 |
'data-index' => $this->currentIndex++, |
| 231 |
'data-type' => $data['type'], |
| 232 |
'data-search' => $data['search'], |
| 233 |
'data-label' => $data['label'] |
| 234 |
]), new HtmlElement('input', Attributes::create([ |
| 235 |
'type' => 'text', |
| 236 |
'value' => $data['label'] |
| 237 |
]))); |
| 238 |
|
| 239 |
if (isset($data['title'])) { |
| 240 |
$term->setAttribute('title', $data['title']); |
| 241 |
} |
| 242 |
|
| 243 |
if (isset($data['pattern'])) { |
| 244 |
$term->getFirst('input')->setAttribute('pattern', $data['pattern']); |
| 245 |
|
| 246 |
if (isset($data['invalidMsg'])) { |
| 247 |
$term->getFirst('input')->setAttribute('data-invalid-msg', $data['invalidMsg']); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
$where->addHtml($term); |
| 252 |
|
| 253 |
return $term; |
| 254 |
} |
| 255 |
} |