Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/Datatable/Datatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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 .= '}';

Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<a href=":href" target=":target">:content</a>',
'url' => null,
'value' => null,
'label' => null,
'disable' => null,
'disableValue' => '',
'type' => Datatables::LINK_TYPE_GET,
'confirm' => false,
'target' => '_self',
Expand All @@ -23,7 +26,7 @@ class Link extends AbstractLink
/**
* @return string
*/
public function link(): string
public function render(): string
{
$urlExtraValue = '';

Expand Down
14 changes: 14 additions & 0 deletions src/View/LinkFormatter/LinkInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace CakeDC\Datatables\View\LinkFormatter;

use Cake\View\Helper;

interface LinkInterface
{
public function __construct(Helper $helper, array $config = []);
public function initialize(array $config = []): void;
public function render(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

declare(strict_types=1);

namespace CakeDC\Datatables\View\Formatter\Link;
namespace CakeDC\Datatables\View\LinkFormatter;

use Cake\Core\InstanceConfigTrait;
use Cake\Utility\Text;
use Cake\View\Helper;
use Exception;

class AbstractLink
trait LinkTrait
{
use InstanceConfigTrait;

Expand Down Expand Up @@ -36,24 +37,33 @@ public function initialize(array $config = []): void
/**
* @return string
*/
public function link(): string
public function render(): string
{
return '';
}

protected $conditionalLinkScript = <<<CONDITIONAL_LINK_SCRIPT
function (value) {
const disable = :disable
if (disable(value, obj)) {
return value ?? "";
}

return ':htmlLink';
}(:valueObj)
CONDITIONAL_LINK_SCRIPT;


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') . ')';
return Text::insert($this->conditionalLinkScript, [
'disable' => $this->getConfig('disable'),
'htmlLink' => $htmlLink,
'valueObj' => $this->getConfig('value'),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<a href=":href" target=":target" onclick=":onclick">:content</a>',
'url' => null,
'value' => null,
'label' => null,
'disable' => null,
'disableValue' => '',
'type' => Datatables::LINK_TYPE_POST,
'target' => '_self',
'confirm' => false,
Expand All @@ -24,7 +27,7 @@ class PostLink extends AbstractLink
/**
* @return string
*/
public function link(): string
public function render(): string
{
$urlExtraValue = '';

Expand Down