Skip to content

Commit 8398c7a

Browse files
author
yddmat
committed
action links
1 parent 0ead0a8 commit 8398c7a

File tree

4 files changed

+103
-29
lines changed

4 files changed

+103
-29
lines changed

src/AbstractDataTable.php

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace PHPDataTables;
33

4+
use PHPDataTables\Action\AbstractActionType;
5+
use PHPDataTables\Action\Link;
46
use Zend\Http\PhpEnvironment\Request;
57

68
/**
@@ -30,8 +32,11 @@ abstract class AbstractDataTable
3032
*/
3133
private $columns = [];
3234

35+
/**
36+
* @var AbstractActionType[]
37+
*/
3338
private $actions = [];
34-
39+
3540
/**
3641
* AbstractDataTable constructor.
3742
*
@@ -83,41 +88,26 @@ public function addColumn(array $spec)
8388

8489
public function addAction(array $spec)
8590
{
86-
if (! isset($spec['url']) || ! isset($spec['label'])) {
87-
throw new \Exception('Missing required options');
91+
if (! class_exists($spec['type'])) {
92+
throw new \Exception('Wrong type');
8893
}
8994

90-
$this->actions[] = $spec;
95+
$action = new $spec['type'];
96+
97+
if (! $action instanceof AbstractActionType) {
98+
throw new \Exception('Type must extend AbstractActionType');
99+
}
100+
101+
$action->setOptions($spec['options']);
102+
$this->actions[] = $action;
91103
}
92-
104+
93105
protected function injectActions(array &$items)
94106
{
95-
foreach ($this->actions as &$action) {
96-
foreach ($items as &$item) {
97-
$find = [];
98-
preg_match_all("/&[a-z1-9]*&/", $action['url'], $find);
99-
100-
foreach ($find[0] as $actionParamName) {
101-
$clearParamName = trim($actionParamName, "&");
102-
$paramExist = false;
103-
foreach ($this->columns as $column) {
104-
if ($clearParamName == $column->getJsName()) {
105-
$paramValue = $item[$clearParamName];
106-
$action['url'] = str_replace($actionParamName, $paramValue, $action['url']);
107-
$paramExist = true;
108-
}
109-
}
110-
111-
if (! $paramExist) {
112-
throw new \Exception("Url param doesnt exists");
113-
}
114-
}
115-
116-
$item['actions'][] = $action;
117-
}
107+
foreach ($this->actions as $action) {
108+
$action->injectData($items);
118109
}
119110
}
120-
121111
/**
122112
* Get column
123113
*
@@ -294,6 +284,14 @@ public function getAdapter(): Adapter\AdapterInterface
294284
return $this->adapter;
295285
}
296286

287+
/**
288+
* @return Action\AbstractActionType[]
289+
*/
290+
public function getActions(): array
291+
{
292+
return $this->actions;
293+
}
294+
297295
/**
298296
* @return string
299297
*/

src/Action/AbstractActionType.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace PHPDataTables\Action;
3+
4+
abstract class AbstractActionType
5+
{
6+
abstract public function setOptions(array $options);
7+
8+
abstract public function getOptions():array;
9+
10+
abstract public function injectData(array &$items);
11+
}

src/Action/Link.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace PHPDataTables\Action;
3+
4+
class Link extends AbstractActionType
5+
{
6+
/** @var array */
7+
protected $options;
8+
9+
public function injectData(array &$items)
10+
{
11+
$find = [];
12+
preg_match_all("/&[a-z1-9]*&/", $this->options['url'], $find);
13+
14+
foreach ($items as &$item) {
15+
$replacedUrl = $this->options['url'];
16+
foreach ($find[0] as $actionParamName) {
17+
$clearParamName = trim($actionParamName, "&");
18+
$paramExist = false;
19+
foreach ($item as $itemFieldName => $itemFieldValue) {
20+
if ($itemFieldName == $clearParamName) {
21+
$replacedUrl = str_replace(
22+
$actionParamName,
23+
$itemFieldValue,
24+
$replacedUrl
25+
);
26+
$paramExist = true;
27+
$this->options['ready_url'] = $replacedUrl;
28+
}
29+
}
30+
31+
if (! $paramExist) {
32+
throw new \Exception("Url param doesn\'t exists");
33+
}
34+
}
35+
36+
$actionsHTML = '';
37+
$actionsHTMLTemplate = '<a href="%s">%s</a>';
38+
$actionsHTML .= sprintf($actionsHTMLTemplate, $replacedUrl, $this->options['label']);
39+
40+
$item['actions'] .= $actionsHTML;
41+
}
42+
}
43+
44+
public function setOptions(array $spec)
45+
{
46+
if (! isset($spec['url']) || ! isset($spec['label'])) {
47+
throw new \Exception('Missing required options');
48+
}
49+
50+
$this->options = $spec;
51+
}
52+
53+
public function getOptions():array
54+
{
55+
return $this->options;
56+
}
57+
}

src/DataTableView.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function renderJs(): string
7070
return [ 'data' => $column->getJsName() ];
7171
}, $this->dataTable->getColumns());
7272

73+
if ($this->dataTable->getActions()) {
74+
$columns[] = ["data" => "actions"];
75+
}
76+
7377
return sprintf($template, $this->jsId, $this->serverSideUrl, Json::encode($columns));
7478
}
7579

@@ -115,6 +119,10 @@ private function buildRowString($cellTag): string
115119
$result .= sprintf('<%s>%s</%s>', $cellTag, $column->getLabel(), $cellTag);
116120
}
117121

122+
if ($this->dataTable->getActions()) {
123+
$result .= sprintf('<%s>%s</%s>', $cellTag, 'Действия', $cellTag);
124+
}
125+
118126
return $result;
119127
}
120128
}

0 commit comments

Comments
 (0)