| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui\UserAction; |
| 6: | |
| 7: | use Atk4\Core\HookTrait; |
| 8: | use Atk4\Data\Model; |
| 9: | use Atk4\Ui\Exception; |
| 10: | use Atk4\Ui\Js\JsBlock; |
| 11: | use Atk4\Ui\Js\JsFunction; |
| 12: | use Atk4\Ui\Js\JsToast; |
| 13: | use Atk4\Ui\Loader; |
| 14: | use Atk4\Ui\Modal; |
| 15: | use Atk4\Ui\View; |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | class ModalExecutor extends Modal implements JsExecutorInterface |
| 35: | { |
| 36: | use CommonExecutorTrait; |
| 37: | use HookTrait; |
| 38: | use StepExecutorTrait; |
| 39: | |
| 40: | public const HOOK_STEP = self::class . '@onStep'; |
| 41: | |
| 42: | #[\Override] |
| 43: | protected function init(): void |
| 44: | { |
| 45: | parent::init(); |
| 46: | |
| 47: | $this->initExecutor(); |
| 48: | } |
| 49: | |
| 50: | protected function initExecutor(): void {} |
| 51: | |
| 52: | #[\Override] |
| 53: | public function getAction(): Model\UserAction |
| 54: | { |
| 55: | return $this->action; |
| 56: | } |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | protected function afterActionInit(): void |
| 65: | { |
| 66: | $this->loader = Loader::addTo($this, ['ui' => $this->loaderUi, 'shim' => $this->loaderShim]); |
| 67: | $this->loader->loadEvent = false; |
| 68: | $this->loader->addClass('atk-hide-loading-content'); |
| 69: | $this->actionData = $this->loader->jsGetStoreData()['session']; |
| 70: | } |
| 71: | |
| 72: | #[\Override] |
| 73: | public function setAction(Model\UserAction $action) |
| 74: | { |
| 75: | $this->action = $action; |
| 76: | $this->afterActionInit(); |
| 77: | |
| 78: | |
| 79: | $this->steps = $this->getSteps(); |
| 80: | if ($this->steps !== []) { |
| 81: | $this->title ??= $action->getDescription(); |
| 82: | |
| 83: | |
| 84: | $this->step = $this->stickyGet('step') ?? $this->steps[0]; |
| 85: | } |
| 86: | |
| 87: | $this->actionInitialized = true; |
| 88: | |
| 89: | return $this; |
| 90: | } |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | #[\Override] |
| 96: | public function executeModelAction(): void |
| 97: | { |
| 98: | $this->action = $this->executeModelActionLoad($this->action); |
| 99: | |
| 100: | |
| 101: | $this->addButtonAction($this->createButtonBar()); |
| 102: | $this->jsSetButtonsState($this->loader, $this->step); |
| 103: | $this->runSteps(); |
| 104: | } |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | private function jsShowAndLoad(array $urlArgs): JsBlock |
| 110: | { |
| 111: | return new JsBlock([ |
| 112: | $this->jsShow(), |
| 113: | $this->js()->data('closeOnLoadingError', true), |
| 114: | $this->loader->jsLoad($urlArgs, [ |
| 115: | 'method' => 'POST', |
| 116: | 'onSuccess' => new JsFunction([], [$this->js()->removeData('closeOnLoadingError')]), |
| 117: | ]), |
| 118: | ]); |
| 119: | } |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | public function assignTrigger(View $view, array $urlArgs = [], string $when = 'click', string $selector = null): self |
| 131: | { |
| 132: | if (!$this->actionInitialized) { |
| 133: | throw new Exception('Action must be set prior to assign trigger'); |
| 134: | } |
| 135: | |
| 136: | if ($this->steps !== []) { |
| 137: | |
| 138: | $urlArgs['step'] = $this->step; |
| 139: | if ($this->action->enabled) { |
| 140: | $view->on($when, $selector, $this->jsShowAndLoad($urlArgs)); |
| 141: | } else { |
| 142: | $view->addClass('disabled'); |
| 143: | } |
| 144: | } |
| 145: | |
| 146: | return $this; |
| 147: | } |
| 148: | |
| 149: | #[\Override] |
| 150: | public function jsExecute(array $urlArgs = []): JsBlock |
| 151: | { |
| 152: | if (!$this->actionInitialized) { |
| 153: | throw new Exception('Action must be set prior to assign trigger'); |
| 154: | } |
| 155: | |
| 156: | $urlArgs['step'] = $this->step; |
| 157: | |
| 158: | return $this->jsShowAndLoad($urlArgs); |
| 159: | } |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | protected function jsGetExecute($obj, $id): JsBlock |
| 168: | { |
| 169: | $success = $this->jsSuccess instanceof \Closure |
| 170: | ? ($this->jsSuccess)($this, $this->action->getModel(), $id, $obj) |
| 171: | : $this->jsSuccess; |
| 172: | |
| 173: | return new JsBlock([ |
| 174: | $this->jsHide(), |
| 175: | JsBlock::fromHookResult($this->hook(BasicExecutor::HOOK_AFTER_EXECUTE, [$obj, $id]) |
| 176: | ?: ($success ?? new JsToast('Success' . (is_string($obj) ? (': ' . $obj) : '')))), |
| 177: | $this->loader->jsClearStoreData(true), |
| 178: | ]); |
| 179: | } |
| 180: | } |
| 181: | |