| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui\VueComponent; |
| 6: | |
| 7: | use Atk4\Data\Model; |
| 8: | use Atk4\Ui\View; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | class ItemSearch extends View |
| 14: | { |
| 15: | |
| 16: | public $reload; |
| 17: | |
| 18: | |
| 19: | public $q; |
| 20: | |
| 21: | |
| 22: | public $inputCss = 'ui input right icon transparent'; |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | public $inputTimeOut = 250; |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | public $context; |
| 39: | |
| 40: | |
| 41: | public $queryArg; |
| 42: | |
| 43: | public $defaultTemplate = 'item-search.html'; |
| 44: | |
| 45: | #[\Override] |
| 46: | protected function init(): void |
| 47: | { |
| 48: | parent::init(); |
| 49: | |
| 50: | if (!$this->queryArg) { |
| 51: | $this->queryArg = $this->name; |
| 52: | } |
| 53: | |
| 54: | if (!$this->q) { |
| 55: | $this->q = $this->getQuery(); |
| 56: | } |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | public function getQuery() |
| 63: | { |
| 64: | return $this->getApp()->tryGetRequestQueryParam($this->queryArg) ?? ''; |
| 65: | } |
| 66: | |
| 67: | public function setModelCondition(Model $model): void |
| 68: | { |
| 69: | $q = $this->getQuery(); |
| 70: | if ($q) { |
| 71: | $model->addCondition($model->titleField, 'like', '%' . $q . '%'); |
| 72: | } |
| 73: | } |
| 74: | |
| 75: | #[\Override] |
| 76: | protected function renderView(): void |
| 77: | { |
| 78: | $this->class = []; |
| 79: | parent::renderView(); |
| 80: | |
| 81: | |
| 82: | |
| 83: | if ($this->reload instanceof View) { |
| 84: | $reloadId = $this->reload->name; |
| 85: | } else { |
| 86: | $reloadId = $this->reload; |
| 87: | } |
| 88: | |
| 89: | $this->vue('AtkItemSearch', [ |
| 90: | 'reload' => $reloadId, |
| 91: | 'queryArg' => $this->queryArg, |
| 92: | 'url' => $this->reload->jsUrl(), |
| 93: | 'q' => $this->q, |
| 94: | 'context' => $this->context, |
| 95: | 'options' => [ |
| 96: | 'inputTimeOut' => $this->inputTimeOut, |
| 97: | 'inputCss' => $this->inputCss, |
| 98: | ], |
| 99: | ]); |
| 100: | } |
| 101: | } |
| 102: | |