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/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 @@ +getConfig('disable'))) { return '\'' . $htmlLink . '\''; } - return 'function(value) { - let disable = ' . $this->getConfig('disable') . ' - if (disable(value, obj)) { - return value; - } - - return \'' . $htmlLink . '\'; - }(' . $this->getConfig('value') . ')'; + return Text::insert($this->conditionalLinkScript, [ + 'disable' => $this->getConfig('disable'), + 'htmlLink' => $htmlLink, + 'valueObj' => $this->getConfig('value'), + ]); } } 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 = '';