| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\FormElement\TermInput; |
| 4 |
|
| 5 |
use ipl\Html\Attributes; |
| 6 |
use ipl\Html\BaseHtmlElement; |
| 7 |
use ipl\Html\HtmlElement; |
| 8 |
use ipl\Html\Text; |
| 9 |
use ipl\I18n\Translation; |
| 10 |
use ipl\Web\FormElement\TermInput; |
| 11 |
use ipl\Web\Widget\Icon; |
| 12 |
|
| 13 |
class TermContainer extends BaseHtmlElement |
| 14 |
{ |
| 15 |
use Translation; |
| 16 |
|
| 17 |
protected $tag = 'div'; |
| 18 |
|
| 19 |
protected $defaultAttributes = ['class' => 'terms']; |
| 20 |
|
| 21 |
/** @var TermInput */ |
| 22 |
protected $input; |
| 23 |
|
| 24 |
/** |
| 25 |
* Create a new TermContainer |
| 26 |
* |
| 27 |
* @param TermInput $input |
| 28 |
*/ |
| 29 |
public function __construct(TermInput $input) |
| 30 |
{ |
| 31 |
$this->input = $input; |
| 32 |
|
| 33 |
if ($input->getOrdered()) { |
| 34 |
$this->tag = 'ol'; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
protected function assemble() |
| 39 |
{ |
| 40 |
if ($this->input->getReadOnly()) { |
| 41 |
$removeLabel = $this->translate('Remove'); |
| 42 |
// bind remove translation to DOM, this allows the JS part to make use of it |
| 43 |
$this->setAttribute('data-remove-action-label', $removeLabel); |
| 44 |
} |
| 45 |
|
| 46 |
foreach ($this->input->getTerms() as $i => $term) { |
| 47 |
$value = $term->getLabel() ?: $term->getSearchValue(); |
| 48 |
|
| 49 |
$label = new HtmlElement( |
| 50 |
'label', |
| 51 |
Attributes::create([ |
| 52 |
'class' => $term->getClass(), |
| 53 |
'data-search' => $term->getSearchValue(), |
| 54 |
'data-label' => $value, |
| 55 |
'data-index' => $i |
| 56 |
]), |
| 57 |
new HtmlElement( |
| 58 |
'input', |
| 59 |
Attributes::create([ |
| 60 |
'type' => 'text', |
| 61 |
'value' => $value, |
| 62 |
'pattern' => $term->getPattern(), |
| 63 |
'data-invalid-msg' => $term->getMessage(), |
| 64 |
'readonly' => $this->input->getReadOnly() |
| 65 |
]) |
| 66 |
) |
| 67 |
); |
| 68 |
if ($this->input->getReadOnly()) { |
| 69 |
$label->addHtml( |
| 70 |
new HtmlElement( |
| 71 |
'div', |
| 72 |
Attributes::create(['class' => 'remove-action', 'title' => $removeLabel]), |
| 73 |
new Icon('trash'), |
| 74 |
new HtmlElement( |
| 75 |
'span', |
| 76 |
Attributes::create(['class' => 'remove-action-label']), |
| 77 |
new Text($removeLabel) |
| 78 |
) |
| 79 |
), |
| 80 |
new HtmlElement('span', Attributes::create(['class' => 'invalid-reason'])) |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
if ($this->tag === 'ol') { |
| 85 |
$this->addHtml(new HtmlElement( |
| 86 |
'li', |
| 87 |
null, |
| 88 |
$label, |
| 89 |
new Icon('bars', ['data-drag-initiator' => true]) |
| 90 |
)); |
| 91 |
} else { |
| 92 |
$this->addHtml($label); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |