| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui; |
| 6: | |
| 7: | use Atk4\Ui\Exception\UnhandledCallbackExceptionError; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | class Callback extends AbstractView |
| 26: | { |
| 27: | public const URL_QUERY_TRIGGER_PREFIX = '__atk_cb_'; |
| 28: | public const URL_QUERY_TARGET = '__atk_cbtarget'; |
| 29: | |
| 30: | |
| 31: | protected $urlTrigger; |
| 32: | |
| 33: | |
| 34: | public $triggerOnReload = true; |
| 35: | |
| 36: | #[\Override] |
| 37: | public function add(AbstractView $object, array $args = []): AbstractView |
| 38: | { |
| 39: | throw new Exception('Callback cannot contain children'); |
| 40: | } |
| 41: | |
| 42: | #[\Override] |
| 43: | protected function init(): void |
| 44: | { |
| 45: | $this->getApp(); |
| 46: | |
| 47: | parent::init(); |
| 48: | |
| 49: | $this->setUrlTrigger($this->urlTrigger); |
| 50: | } |
| 51: | |
| 52: | public function setUrlTrigger(string $trigger = null): void |
| 53: | { |
| 54: | $this->urlTrigger = $trigger ?? $this->name; |
| 55: | |
| 56: | $this->getOwner()->stickyGet(self::URL_QUERY_TRIGGER_PREFIX . $this->urlTrigger); |
| 57: | } |
| 58: | |
| 59: | public function getUrlTrigger(): string |
| 60: | { |
| 61: | return $this->urlTrigger; |
| 62: | } |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | public function set($fx = null, $fxArgs = null) |
| 75: | { |
| 76: | if ($this->isTriggered() && $this->canTrigger()) { |
| 77: | try { |
| 78: | return $fx(...($fxArgs ?? [])); |
| 79: | } catch (\Exception $e) { |
| 80: | |
| 81: | |
| 82: | throw new UnhandledCallbackExceptionError('', 0, $e); |
| 83: | } |
| 84: | } |
| 85: | |
| 86: | return null; |
| 87: | } |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public function terminateJson(View $view): void |
| 93: | { |
| 94: | if ($this->canTerminate()) { |
| 95: | $this->getApp()->terminateJson($view); |
| 96: | } |
| 97: | } |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | public function isTriggered(): bool |
| 103: | { |
| 104: | return $this->getApp()->hasRequestQueryParam(self::URL_QUERY_TRIGGER_PREFIX . $this->urlTrigger); |
| 105: | } |
| 106: | |
| 107: | public function getTriggeredValue(): string |
| 108: | { |
| 109: | return $this->getApp()->tryGetRequestQueryParam(self::URL_QUERY_TRIGGER_PREFIX . $this->urlTrigger) ?? ''; |
| 110: | } |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | public function canTerminate(): bool |
| 116: | { |
| 117: | return $this->getApp()->hasRequestQueryParam(self::URL_QUERY_TARGET) && $this->getApp()->getRequestQueryParam(self::URL_QUERY_TARGET) === $this->urlTrigger; |
| 118: | } |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | public function canTrigger(): bool |
| 124: | { |
| 125: | return $this->triggerOnReload || !$this->getApp()->hasRequestQueryParam('__atk_reload'); |
| 126: | } |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | public function getJsUrl(string $value = 'ajax'): string |
| 134: | { |
| 135: | return $this->getOwner()->jsUrl($this->getUrlArguments($value)); |
| 136: | } |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | public function getUrl(string $value = 'callback'): string |
| 143: | { |
| 144: | return $this->getOwner()->url($this->getUrlArguments($value)); |
| 145: | } |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | private function getUrlArguments(string $value = null): array |
| 151: | { |
| 152: | return [ |
| 153: | self::URL_QUERY_TARGET => $this->urlTrigger, |
| 154: | self::URL_QUERY_TRIGGER_PREFIX . $this->urlTrigger => $value ?? ($this->isTriggered() ? $this->getTriggeredValue() : ''), |
| 155: | ]; |
| 156: | } |
| 157: | } |
| 158: | |