1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui\Table\Column;
6:
7: use Atk4\Data\Field;
8: use Atk4\Data\Model;
9: use Atk4\Ui\Button;
10: use Atk4\Ui\Form;
11: use Atk4\Ui\Js\Jquery;
12: use Atk4\Ui\Js\JsBlock;
13: use Atk4\Ui\Js\JsReload;
14: use Atk4\Ui\Popup;
15: use Atk4\Ui\View;
16:
17: /**
18: * Implement a filterPopup in a table column.
19: * The popup contains a form associate to a field type model
20: * and use session to store it's data.
21: */
22: class FilterPopup extends Popup
23: {
24: /** @var Form The form associate with this FilterPopup. */
25: public $form;
26:
27: /** The table field that need filtering. */
28: public Field $field;
29:
30: /** @var View|null The view associated with this filter popup that needs to be reloaded. */
31: public $reload;
32:
33: /**
34: * The Table Column triggering the popup.
35: * This is need to simulate click in order to properly
36: * close the popup window on "Clear".
37: *
38: * @var string
39: */
40: public $colTrigger;
41:
42: #[\Override]
43: protected function init(): void
44: {
45: parent::init();
46:
47: $this->setOption('delay', ['hide' => 1500]);
48: $this->setHoverable();
49:
50: $model = FilterModel::factoryType($this->getApp(), $this->field);
51: $model = $model->createEntity();
52:
53: $this->form = Form::addTo($this)->addClass('');
54: $this->form->buttonSave->addClass('');
55: $this->form->addGroup('Where ' . $this->field->getCaption() . ':');
56:
57: $this->form->buttonSave->set('Set');
58:
59: $this->form->setControlsDisplayRules($model->getFormDisplayRules());
60:
61: // load data associated with this popup
62: $filter = $model->recallData();
63: if ($filter !== null) {
64: $model->setMulti($filter);
65: }
66: $this->form->setModel($model);
67:
68: $this->form->onSubmit(function (Form $form) {
69: $form->model->save();
70:
71: return new jsReload($this->reload);
72: });
73:
74: Button::addTo($this->form, ['Clear', 'class.clear' => true])
75: ->on('click', function (Jquery $j) use ($model) {
76: $model->clearData();
77:
78: return new JsBlock([
79: $this->form->js()->form('reset'),
80: new JsReload($this->reload),
81: (new Jquery($this->colTrigger))->click(),
82: ]);
83: });
84: }
85:
86: public function isFilterOn(): bool
87: {
88: return $this->recallData() !== null;
89: }
90:
91: public function recallData(): ?array
92: {
93: return $this->form->model->recallData();
94: }
95:
96: /**
97: * Set filter condition base on the field Type model use in this FilterPopup.
98: *
99: * @return Model
100: */
101: public function setFilterCondition(Model $tableModel)
102: {
103: return $this->form->model->setConditionForModel($tableModel);
104: }
105: }
106: