/
Item.php
159 lines (122 loc) · 3.03 KB
/
Item.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* This file is part of the Mesour DataGrid (http://grid.mesour.com)
*
* Copyright (c) 2015-2016 Matouš Němec (http://mesour.com)
*
* For full licence and copyright please view the file licence.md in root of this project
*/
namespace Mesour\DataGrid\Extensions\SubItem\Items;
use Mesour;
/**
* @author Matouš Němec <matous.nemec@mesour.com>
*/
abstract class Item
{
use Mesour\SmartObject;
const DEFAULT_COUNT = 20;
/** @var Mesour\DataGrid\Extensions\SubItem\ISubItem */
protected $parent;
protected $callback;
protected $checkCallback;
protected $type;
protected $name;
protected $disabled = false;
protected $description;
protected $pageLimit;
protected $keys = [];
protected $permission = false;
protected $aliases = [];
public function __construct(Mesour\DataGrid\Extensions\SubItem\ISubItem $parent, $name, $description = null)
{
$this->parent = $parent;
$this->name = $name;
$this->pageLimit = $this->parent->getPageLimit();
$this->description = $description;
}
/**
* @return Mesour\DataGrid\SubItemGrid
*/
protected function getGrid()
{
return $this->getParent()->getParent();
}
public function setCallback($callback)
{
Mesour\Components\Utils\Helpers::checkCallback($callback);
$this->callback = $callback;
return $this;
}
public function setCheckCallback($callback)
{
Mesour\Components\Utils\Helpers::checkCallback($callback);
$this->checkCallback = $callback;
return $this;
}
public function check($rowData)
{
if ($this->checkCallback) {
Mesour\Components\Utils\Helpers::invokeArgs($this->checkCallback, [$rowData, $this]);
}
}
public function isDisabled()
{
return $this->disabled;
}
public function setDisabled($disabled = true)
{
$this->disabled = $disabled;
return $this;
}
public function setPermission($resource, $privilege)
{
$this->permission = [$this->parent->getUserRole(), $resource, $privilege];
return $this;
}
public function isAllowed()
{
return !$this->permission
|| Mesour\Components\Utils\Helpers::invokeArgs([$this->parent->getAuthorizator(), 'isAllowed'], $this->permission);
}
public function getName()
{
return $this->name;
}
/**
* @return Mesour\DataGrid\Column\SubItem
*/
protected function getParent()
{
return $this->parent;
}
public function setDescription($description)
{
return $this->description = $description;
}
public function getDescription()
{
return $this->description ? $this->description : $this->name;
}
public function addAlias($forKey, $alias)
{
$this->aliases[$forKey] = $alias;
}
public function getTranslatedKey($key)
{
return isset($this->aliases[$key]) ? $this->aliases[$key] : $key;
}
public function invoke(array $args = [], $name, $key)
{
return $this->callback ? Mesour\Components\Utils\Helpers::invokeArgs($this->callback, $args) : null;
}
public function hasKey($key)
{
return isset($this->parent[$this->name . $key]);
}
public function getKeys()
{
return $this->keys;
}
abstract public function render();
abstract public function reset();
}