From f62e91d1ca363ca18283ccb4cc5e6375f22f5d77 Mon Sep 17 00:00:00 2001 From: Alberto Rodriguez Date: Wed, 7 Jun 2023 16:56:14 -0400 Subject: [PATCH 1/2] refact: links formatters logic --- src/Datatable/Datatable.php | 32 +++---- src/View/Formatter/Link/AbstractLink.php | 59 ------------ .../Link => LinkFormatter}/Link.php | 9 +- src/View/LinkFormatter/LinkInterface.php | 14 +++ src/View/LinkFormatter/LinkTrait.php | 90 +++++++++++++++++++ .../Link => LinkFormatter}/PostLink.php | 9 +- 6 files changed, 130 insertions(+), 83 deletions(-) delete mode 100644 src/View/Formatter/Link/AbstractLink.php rename src/View/{Formatter/Link => LinkFormatter}/Link.php (86%) create mode 100644 src/View/LinkFormatter/LinkInterface.php create mode 100644 src/View/LinkFormatter/LinkTrait.php rename src/View/{Formatter/Link => LinkFormatter}/PostLink.php (96%) diff --git a/src/Datatable/Datatable.php b/src/Datatable/Datatable.php index c6c334e..17a62c3 100644 --- a/src/Datatable/Datatable.php +++ b/src/Datatable/Datatable.php @@ -9,6 +9,7 @@ use Cake\Utility\Text; use CakeDC\Datatables\Datatables; use CakeDC\Datatables\Exception\MissConfiguredException; +use CakeDC\Datatables\View\LinkFormatter\LinkInterface; use Exception; use InvalidArgumentException; @@ -506,7 +507,7 @@ public function setGetDataUrl($defaultUrl = null) }; GET_DATA; } else { - if ($csrfToken !== null){ + if ($csrfToken !== null) { $headers = "headers: { 'X-CSRF-Token': '{$csrfToken}' },"; } else { $headers = ""; @@ -570,9 +571,9 @@ protected function processColumnRenderCallbacks() if ($key['width'] ?? null) { $output .= "\nwidth: '{$key['width']}',"; } - if ($key['className'] ?? null) { - $output .= "\nclassName: '{$key['className']}',"; - } + if ($key['className'] ?? null) { + $output .= "\nclassName: '{$key['className']}',"; + } } $output .= '}'; @@ -594,7 +595,7 @@ protected function processActionLinkList(array $sourceLinks): array { $links = []; foreach ($sourceLinks as $link) { - $links[] = $this->processActionLink($link); + $links[] = $this->processActionLink($link)->render(); } return $links; @@ -604,34 +605,29 @@ protected function processActionLinkList(array $sourceLinks): array * Format link with specified options from links array. * * @param array $link - * @return string + * @return LinkInterface */ - protected function processActionLink(array $link): string + protected function processActionLink(array $link): LinkInterface { switch ($link['type'] ?? null) { case Datatables::LINK_TYPE_DELETE: case Datatables::LINK_TYPE_PUT: case Datatables::LINK_TYPE_POST: - $output = new \CakeDC\Datatables\View\Formatter\Link\PostLink($this->Helper, $link); + $output = new \CakeDC\Datatables\View\LinkFormatter\PostLink($this->Helper, $link); break; case Datatables::LINK_TYPE_CUSTOM: - if (!class_exists($link['formatter'] ?? null)) { - throw new \OutOfBoundsException("Please specify a custom formatter"); - } - $output = new $link['formatter']($this->Helper, $link); - - if (!method_exists($output, 'link')) { - throw new \OutOfBoundsException("Method link is not found in class"); + if (!class_exists($link['linkFormatter'] ?? null)) { + throw new \OutOfBoundsException("Please specify a custom linkFormatter"); } - + $output = new $link['linkFormatter']($this->Helper, $link); break; case Datatables::LINK_TYPE_GET: default: - $output = new \CakeDC\Datatables\View\Formatter\Link\Link($this->Helper, $link); + $output = new \CakeDC\Datatables\View\LinkFormatter\Link($this->Helper, $link); break; } - return $output->link(); + return $output; } /** diff --git a/src/View/Formatter/Link/AbstractLink.php b/src/View/Formatter/Link/AbstractLink.php deleted file mode 100644 index e9d8f44..0000000 --- a/src/View/Formatter/Link/AbstractLink.php +++ /dev/null @@ -1,59 +0,0 @@ -_helper = $helper; - $this->setConfig($config); - $this->initialize($config); - - if (empty($this->getConfig('url'))) { - throw new Exception("url option cannot be empty"); - } - } - - /** - * @param array $config - * @return void - */ - public function initialize(array $config = []): void - { - } - - /** - * @return string - */ - public function link(): string - { - return ''; - } - - protected function conditionalLink(string $htmlLink) - { - if (empty($this->getConfig('disable'))) { - return '\'' . $htmlLink . '\''; - } - - return 'function(value) { - let disable = ' . $this->getConfig('disable') . ' - if (disable(value, obj)) { - return value; - } - - return \'' . $htmlLink . '\'; - }(' . $this->getConfig('value') . ')'; - } -} diff --git a/src/View/Formatter/Link/Link.php b/src/View/LinkFormatter/Link.php similarity index 86% rename from src/View/Formatter/Link/Link.php rename to src/View/LinkFormatter/Link.php index eb2671e..c5a131c 100644 --- a/src/View/Formatter/Link/Link.php +++ b/src/View/LinkFormatter/Link.php @@ -2,19 +2,22 @@ declare(strict_types=1); -namespace CakeDC\Datatables\View\Formatter\Link; +namespace CakeDC\Datatables\View\LinkFormatter; use Cake\Utility\Text; use CakeDC\Datatables\Datatables; -class Link extends AbstractLink +class Link implements LinkInterface { + use LinkTrait; + protected $_defaultConfig = [ 'template' => ':content', 'url' => null, 'value' => null, 'label' => null, 'disable' => null, + 'disableValue' => '', 'type' => Datatables::LINK_TYPE_GET, 'confirm' => false, 'target' => '_self', @@ -23,7 +26,7 @@ class Link extends AbstractLink /** * @return string */ - public function link(): string + public function render(): string { $urlExtraValue = ''; diff --git a/src/View/LinkFormatter/LinkInterface.php b/src/View/LinkFormatter/LinkInterface.php new file mode 100644 index 0000000..6288d0f --- /dev/null +++ b/src/View/LinkFormatter/LinkInterface.php @@ -0,0 +1,14 @@ +_helper = $helper; + $this->setConfig($config); + $this->initialize($config); + + if (empty($this->getConfig('url'))) { + throw new Exception("url option cannot be empty"); + } + } + + /** + * @param array $config + * @return void + */ + public function initialize(array $config = []): void + { + } + + /** + * @return string + */ + public function render(): string + { + return ''; + } + + protected $conditionalLinkScript = <<getConfig('disable'))) { + return '\'' . $htmlLink . '\''; + } + + return Text::insert($this->conditionalLinkScript, [ + 'disable' => $this->getConfig('disable'), + 'htmlLink' => $htmlLink, + 'valueObj' => $this->getConfig('value'), + ]); + } + + /* + protected $displayCondition = <<getConfig('displayCondition', true)) { + return true; + } + + return Text::insert($this->conditionalLinkScript, [ + 'disable' => $this->getConfig('displayCondition'), + ]); + } + */ + +} diff --git a/src/View/Formatter/Link/PostLink.php b/src/View/LinkFormatter/PostLink.php similarity index 96% rename from src/View/Formatter/Link/PostLink.php rename to src/View/LinkFormatter/PostLink.php index 78692b6..3c9886d 100644 --- a/src/View/Formatter/Link/PostLink.php +++ b/src/View/LinkFormatter/PostLink.php @@ -2,19 +2,22 @@ declare(strict_types=1); -namespace CakeDC\Datatables\View\Formatter\Link; +namespace CakeDC\Datatables\View\LinkFormatter; use Cake\Utility\Text; use CakeDC\Datatables\Datatables; -class PostLink extends AbstractLink +class PostLink implements LinkInterface { + use LinkTrait; + protected $_defaultConfig = [ 'template' => ':content', 'url' => null, 'value' => null, 'label' => null, 'disable' => null, + 'disableValue' => '', 'type' => Datatables::LINK_TYPE_POST, 'target' => '_self', 'confirm' => false, @@ -24,7 +27,7 @@ class PostLink extends AbstractLink /** * @return string */ - public function link(): string + public function render(): string { $urlExtraValue = ''; From 7667c19abf0e16f1488e31c204887a9323120afb Mon Sep 17 00:00:00 2001 From: Alberto Rodriguez Date: Fri, 9 Jun 2023 12:41:00 -0400 Subject: [PATCH 2/2] fix: debug code --- src/View/LinkFormatter/LinkTrait.php | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/View/LinkFormatter/LinkTrait.php b/src/View/LinkFormatter/LinkTrait.php index b924876..6258dd9 100644 --- a/src/View/LinkFormatter/LinkTrait.php +++ b/src/View/LinkFormatter/LinkTrait.php @@ -66,25 +66,4 @@ protected function conditionalLink(string $htmlLink) 'valueObj' => $this->getConfig('value'), ]); } - - /* - protected $displayCondition = <<getConfig('displayCondition', true)) { - return true; - } - - return Text::insert($this->conditionalLinkScript, [ - 'disable' => $this->getConfig('displayCondition'), - ]); - } - */ - }