| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui\Form\Control; |
| 6: | |
| 7: | use Atk4\Core\Factory; |
| 8: | use Atk4\Core\HookTrait; |
| 9: | use Atk4\Data\Model; |
| 10: | use Atk4\Ui\Button; |
| 11: | use Atk4\Ui\CallbackLater; |
| 12: | use Atk4\Ui\Form; |
| 13: | use Atk4\Ui\Js\Jquery; |
| 14: | use Atk4\Ui\Js\JsBlock; |
| 15: | use Atk4\Ui\Js\JsExpression; |
| 16: | use Atk4\Ui\Js\JsExpressionable; |
| 17: | use Atk4\Ui\Js\JsFunction; |
| 18: | use Atk4\Ui\Js\JsModal; |
| 19: | use Atk4\Ui\Js\JsToast; |
| 20: | use Atk4\Ui\VirtualPage; |
| 21: | |
| 22: | class Lookup extends Input |
| 23: | { |
| 24: | use HookTrait; |
| 25: | |
| 26: | public $defaultTemplate = 'form/control/lookup.html'; |
| 27: | |
| 28: | public string $inputType = 'hidden'; |
| 29: | |
| 30: | |
| 31: | public $values = []; |
| 32: | |
| 33: | |
| 34: | public $callback; |
| 35: | |
| 36: | |
| 37: | public $empty = "\u{00a0}"; |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | public $search; |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | public $dependency; |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public $plus = false; |
| 70: | |
| 71: | |
| 72: | public $limit = 100; |
| 73: | |
| 74: | |
| 75: | public $idField; |
| 76: | |
| 77: | |
| 78: | public $titleField; |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | public $apiConfig = ['cache' => false]; |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | public $settings = []; |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | public $renderRowFunction; |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | public $multiple = false; |
| 128: | |
| 129: | #[\Override] |
| 130: | protected function init(): void |
| 131: | { |
| 132: | parent::init(); |
| 133: | |
| 134: | $this->template->set([ |
| 135: | 'placeholder' => $this->placeholder, |
| 136: | ]); |
| 137: | |
| 138: | $this->initQuickNewRecord(); |
| 139: | |
| 140: | $this->callback = CallbackLater::addTo($this); |
| 141: | $this->callback->set(function () { |
| 142: | $this->outputApiResponse(); |
| 143: | }); |
| 144: | } |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | protected function jsDropdown($when = false, $action = null): JsExpressionable |
| 153: | { |
| 154: | return $this->js($when, $action, 'div.ui.dropdown:has(> #' . $this->name . '_input)'); |
| 155: | } |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | protected function getCallbackUrl(): string |
| 161: | { |
| 162: | return $this->callback->getJsUrl(); |
| 163: | } |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | public function outputApiResponse(): void |
| 171: | { |
| 172: | $this->getApp()->terminateJson([ |
| 173: | 'success' => true, |
| 174: | 'results' => $this->getData(), |
| 175: | ]); |
| 176: | } |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | public function getData($limit = true): array |
| 186: | { |
| 187: | $this->applyLimit($limit); |
| 188: | |
| 189: | $this->applySearchConditions(); |
| 190: | |
| 191: | $this->applyDependencyConditions(); |
| 192: | |
| 193: | $data = []; |
| 194: | foreach ($this->model as $row) { |
| 195: | $data[] = $this->renderRow($row); |
| 196: | } |
| 197: | |
| 198: | if (!$this->multiple && $this->empty) { |
| 199: | array_unshift($data, ['value' => '', 'title' => $this->empty]); |
| 200: | } |
| 201: | |
| 202: | return $data; |
| 203: | } |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | public function renderRow(Model $row): array |
| 211: | { |
| 212: | if ($this->renderRowFunction !== null) { |
| 213: | return ($this->renderRowFunction)($this, $row); |
| 214: | } |
| 215: | |
| 216: | return $this->defaultRenderRow($row); |
| 217: | } |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | public function defaultRenderRow(Model $row, $key = null) |
| 227: | { |
| 228: | $idField = $this->idField ?? $row->idField; |
| 229: | $titleField = $this->titleField ?? $row->titleField; |
| 230: | |
| 231: | return ['value' => $row->get($idField), 'title' => $row->get($titleField)]; |
| 232: | } |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | protected function initQuickNewRecord(): void |
| 238: | { |
| 239: | if (!$this->plus) { |
| 240: | return; |
| 241: | } |
| 242: | |
| 243: | if ($this->plus === true) { |
| 244: | $this->plus = 'Add New'; |
| 245: | } |
| 246: | |
| 247: | if (is_string($this->plus)) { |
| 248: | $this->plus = ['button' => $this->plus]; |
| 249: | } |
| 250: | |
| 251: | $buttonSeed = $this->plus['button'] ?? []; |
| 252: | if (is_string($buttonSeed)) { |
| 253: | $buttonSeed = ['content' => $buttonSeed]; |
| 254: | } |
| 255: | |
| 256: | $defaultSeed = [Button::class, 'class.disabled' => $this->disabled || $this->readOnly]; |
| 257: | $this->action = Factory::factory(array_merge($defaultSeed, $buttonSeed)); |
| 258: | |
| 259: | $vp = VirtualPage::addTo($this->form ?? $this->getOwner()); |
| 260: | $vp->set(function (VirtualPage $p) { |
| 261: | $form = Form::addTo($p); |
| 262: | |
| 263: | $entity = $this->model->createEntity(); |
| 264: | $form->setModel($entity, $this->plus['fields'] ?? null); |
| 265: | |
| 266: | $form->onSubmit(function (Form $form) { |
| 267: | $msg = $form->model->getUserAction('add')->execute(); |
| 268: | |
| 269: | $res = new JsBlock(); |
| 270: | if (is_string($msg)) { |
| 271: | $res->addStatement(new JsToast($msg)); |
| 272: | } |
| 273: | $res->addStatement((new Jquery())->closest('.atk-modal')->modal('hide')); |
| 274: | |
| 275: | $row = $this->renderRow($form->model); |
| 276: | $chain = $this->jsDropdown(); |
| 277: | $chain->dropdown('set value', $row['value'])->dropdown('set text', $row['title']); |
| 278: | $res->addStatement($chain); |
| 279: | |
| 280: | return $res; |
| 281: | }); |
| 282: | }); |
| 283: | |
| 284: | $caption = $this->plus['caption'] ?? 'Add New ' . $this->model->getModelCaption(); |
| 285: | $this->action->on('click', new JsModal($caption, $vp)); |
| 286: | } |
| 287: | |
| 288: | |
| 289: | |
| 290: | |
| 291: | |
| 292: | |
| 293: | protected function applyLimit($limit = true): void |
| 294: | { |
| 295: | if ($limit !== false) { |
| 296: | $this->model->setLimit($limit === true ? $this->limit : $limit); |
| 297: | } |
| 298: | } |
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: | protected function applySearchConditions(): void |
| 304: | { |
| 305: | $query = $this->getApp()->tryGetRequestQueryParam('q') ?? ''; |
| 306: | if ($query === '') { |
| 307: | return; |
| 308: | } |
| 309: | |
| 310: | if ($this->search instanceof \Closure) { |
| 311: | ($this->search)($this->model, $query); |
| 312: | } elseif (is_array($this->search)) { |
| 313: | $scope = Model\Scope::createOr(); |
| 314: | foreach ($this->search as $field) { |
| 315: | $scope->addCondition($field, 'like', '%' . $query . '%'); |
| 316: | } |
| 317: | $this->model->addCondition($scope); |
| 318: | } else { |
| 319: | $titleField = $this->titleField ?? $this->model->titleField; |
| 320: | |
| 321: | $this->model->addCondition($titleField, 'like', '%' . $query . '%'); |
| 322: | } |
| 323: | } |
| 324: | |
| 325: | |
| 326: | |
| 327: | |
| 328: | protected function applyDependencyConditions(): void |
| 329: | { |
| 330: | if (!$this->dependency instanceof \Closure) { |
| 331: | return; |
| 332: | } |
| 333: | |
| 334: | $data = []; |
| 335: | if ($this->getApp()->hasRequestQueryParam('form')) { |
| 336: | parse_str($this->getApp()->getRequestQueryParam('form'), $data); |
| 337: | } elseif ($this->form) { |
| 338: | $data = $this->form->model->get(); |
| 339: | } else { |
| 340: | return; |
| 341: | } |
| 342: | |
| 343: | ($this->dependency)($this->model, $data); |
| 344: | } |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: | |
| 350: | |
| 351: | protected function initDropdown($chain): void |
| 352: | { |
| 353: | $settings = array_merge([ |
| 354: | 'fields' => ['name' => 'title'], |
| 355: | 'apiSettings' => array_merge(['url' => $this->getCallbackUrl() . '&q={query}'], $this->apiConfig), |
| 356: | ], $this->settings); |
| 357: | |
| 358: | $chain->dropdown($settings); |
| 359: | } |
| 360: | |
| 361: | #[\Override] |
| 362: | protected function renderView(): void |
| 363: | { |
| 364: | if ($this->multiple) { |
| 365: | $this->template->dangerouslySetHtml('multipleClass', 'multiple'); |
| 366: | } |
| 367: | |
| 368: | if ($this->disabled) { |
| 369: | $this->template->set('disabledClass', 'disabled'); |
| 370: | $this->template->dangerouslySetHtml('disabled', 'disabled="disabled"'); |
| 371: | } elseif ($this->readOnly) { |
| 372: | $this->template->set('disabledClass', 'read-only'); |
| 373: | $this->template->dangerouslySetHtml('disabled', 'readonly="readonly"'); |
| 374: | |
| 375: | $this->settings['apiSettings'] = null; |
| 376: | $this->settings['onShow'] = new JsFunction([], [new JsExpression('return false')]); |
| 377: | } |
| 378: | |
| 379: | if ($this->dependency) { |
| 380: | $this->apiConfig['data'] = array_merge([ |
| 381: | 'form' => new JsFunction([], [new JsExpression('return []', [$this->form->formElement->js()->serialize()])]), |
| 382: | ], $this->apiConfig['data'] ?? []); |
| 383: | } |
| 384: | |
| 385: | $chain = $this->jsDropdown(); |
| 386: | |
| 387: | $this->initDropdown($chain); |
| 388: | |
| 389: | if ($this->entityField && $this->entityField->get()) { |
| 390: | $idField = $this->idField ?? $this->model->idField; |
| 391: | |
| 392: | $this->model = $this->model->loadBy($idField, $this->entityField->get()); |
| 393: | |
| 394: | $row = $this->renderRow($this->model); |
| 395: | $chain->dropdown('set value', $row['value'])->dropdown('set text', $row['title']); |
| 396: | } |
| 397: | |
| 398: | $this->js(true, $chain); |
| 399: | |
| 400: | parent::renderView(); |
| 401: | } |
| 402: | } |
| 403: | |