1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace App\Model;
13:
14: use Nette,
15: Nette\Object,
16: Nette\Security\User,
17: App\Model\Database,
18: App\Model\Jidash,
19: App\Lib\Structure,
20: App\Lib\Table,
21: App\Lib\Tree,
22: App\Lib\Statics\Cons;
23:
24: 25: 26:
27: class Action extends Object {
28:
29:
30: const STRUCT = 'action',
31: DATA = 'data';
32:
33:
34: private $database;
35:
36:
37: private $user;
38:
39:
40: private $data;
41:
42:
43: private $action;
44:
45:
46: private $structure;
47:
48:
49: private $params;
50:
51:
52: private $state;
53:
54:
55: private $jidash;
56:
57:
58: private $child;
59:
60: 61: 62: 63:
64: public function __construct(Database $database, User $user, Jidash $jidash) {
65: $this->database = $database;
66: $this->user = $user;
67: $this->jidash = $jidash;
68: $this->state = FALSE;
69:
70: }
71:
72: 73: 74: 75:
76: public function setParams($params) {
77: if (!$this->state) {
78: $this->params = $params;
79: $this->state = TRUE;
80: }
81: }
82:
83: 84: 85: 86: 87:
88: public function getState() {
89: if ($this->state === TRUE) {
90: return $this->state;
91: } else {
92: throw new Nette\InvalidStateException('Nebyly nastaveny parametry akce');
93: }
94: }
95:
96: 97: 98: 99:
100: public function getParent() {
101: $parent = (!$this->lazy(self::STRUCT)) ? NULL : $this->action[Cons::COLUMN_PARENT];
102: return $parent;
103: }
104:
105: 106: 107: 108:
109: public function getRelevancestruct() {
110: if ($this->lazy(self::STRUCT)) {
111: return new Structure($this->action->ref(Cons::COLUMN_PARENT)->ref(Cons::COLUMN_TABLE));
112: }
113: }
114:
115: 116: 117: 118:
119: public function getRelevance() {
120: $child = $this->getChild();
121: return $this->data->related($child[Cons::COLUMN_TABLE][Cons::COLUMN_OBJECT]);
122: }
123:
124: 125: 126: 127:
128: public function getChild() {
129: if ($this->child === NULL) {
130: $this->child = ($this->getParent()) ? (new Table(Cons::TABLE_SOURCE, $this->database))->findById($this->getParent()) : NULL;
131: }
132: return $this->child;
133: }
134:
135: 136: 137: 138:
139: public function getSource() {
140: return (!$this->lazy(self::STRUCT)) ? NULL : $this->action[Cons::COLUMN_ID];
141: }
142:
143: 144: 145: 146:
147: public function getName() {
148: return (!$this->lazy(self::STRUCT)) ? NULL : $this->action[Cons::COLUMN_NAME];
149: }
150:
151: 152: 153: 154:
155: public function getTable() {
156: return (!$this->lazy(self::STRUCT)) ? NULL : $this->structure;
157: }
158:
159: 160: 161: 162:
163: public function getData() {
164: return (!$this->lazy(self::DATA)) ? NULL : $this->data;
165: }
166:
167: 168: 169: 170:
171: public function getCreate() {
172: return $this->acl(Cons::CREATE);
173: }
174:
175: 176: 177: 178: 179:
180: public function delete($row) {
181: return ($this->acl(Cons::DELETE)) ? $this->data->delete($row[Cons::COLUMN_ID]) : FALSE;
182: }
183:
184: 185: 186: 187: 188: 189:
190: public function update($data, $id) {
191: $table = $this->getData();
192: if ($id) {
193: if ($this->acl(Cons::WRITE)) {
194: $result = $table->update($this->jidash->write($data, $this->user->id));
195: return ($result == TRUE)? : -1;
196: } else {
197: return FALSE;
198: }
199: } else {
200: return ($this->acl(Cons::CREATE)) ?
201: $table->insert($this->jidash->create($data, $this->user->id)) :
202: FALSE;
203: }
204: }
205:
206: 207: 208: 209: 210:
211: public function getFields() {
212: return $this->getTable();
213: }
214:
215: 216: 217: 218:
219: public function getUser() {
220: return $this->user;
221: }
222:
223: 224: 225: 226: 227: 228:
229: public function getRelation($table, $column) {
230: $name = strtolower($table);
231: if ($name === 'stock_source_id' || $name === 'stock_target_id') {
232:
233: $name = 'stock_id';
234: }
235: if ($name === 'parent_id') {
236: $name = $this->getTable()->object;
237: }
238: $prepared = rtrim($name, '_id');
239: return \App\Lib\Statics\Vars::fetch((new Table($prepared, $this->database))->data, $column);
240:
241: }
242:
243: 244: 245: 246:
247: public function getJidash() {
248: return $this->jidash->show($this->getdata());
249: }
250:
251: 252: 253:
254: private function setAction() {
255: $this->action = (new Table(Cons::TABLE_SOURCE, $this->database))->findById($this->params['source']);
256: if ($this->action[Cons::COLUMN_TABLE] !== NULL) {
257: $this->structure = new Structure($this->action->ref(Cons::COLUMN_TABLE));
258: }
259:
260: }
261:
262: 263: 264: 265: 266:
267: private function acl($permision, $field = NULL) {
268: $resource = Cons::RES_TABLES . ' > ' . $this->getTable()->object;
269: if ($field !== NULL) {
270: $resource .= ' > ' . $field;
271: }
272: $privilege = $this->user->isAllowed($resource, $permision);
273: return $privilege;
274: }
275:
276: 277: 278: 279: 280: 281:
282: private function setData() {
283: if ($this->acl(Cons::READ)) {
284: $data = $this->data($this->structure->type, $this->structure->object);
285: if (isset($this->params['id'])) {
286: $this->data = ($this->acl(Cons::READ)) ? $data->findById($this->params['id']) : array();
287: } else {
288: $this->data = $data;
289: $this->setFilter();
290: }
291: } else {
292: throw new Nette\Application\ForbiddenRequestException;
293: }
294: }
295:
296: 297: 298: 299: 300: 301:
302: private function data($type, $object) {
303: return ($type === 'tree') ?
304: new Tree($object, $this->database) :
305: new Table($object, $this->database);
306: }
307:
308: 309: 310: 311: 312: 313:
314: private function lazy($object = NULL) {
315: if ($this->getState()) {
316: if ($object === self::DATA) {
317: ($this->action !== NULL) ? : $this->setAction();
318: ($this->data !== NULL) ? : $this->setData();
319: } else if ($object === self::STRUCT) {
320: ($this->action !== NULL)? : $this->setAction();
321: } else {
322: throw new Nette\InvalidArgumentException("Byl zadán špatný zdroj: $object");
323: }
324: return TRUE;
325: }
326: }
327:
328: 329: 330:
331: private function aclFields() {
332: foreach ($this->getTable() as $field) {
333: if (!$this->acl($field[Cons::COLUMN_FIELD], Cons::WRITE) && !$this->acl($field[Cons::COLUMN_FIELD], Cons::CREATE)) {
334: ($this->acl($field[Cons::COLUMN_FIELD], Cons::READ)) ? : $this->structure->disableField($field);
335: }
336: }
337: }
338:
339: 340: 341:
342: private function setFilter() {
343: if ($this->action[Cons::COLUMN_FILTER] !== NULL && $this->action[Cons::COLUMN_FILTER] !== '') {
344: $values = explode(',', $this->action[Cons::COLUMN_FILTER]);
345: $condition = $values[0];
346: unset($values[0]);
347: foreach ($values as $value) {
348: ($value !== '%user%') ? : $value = $this->user->id;
349: }
350: if (count($values) === 2) {
351: $this->data->data->where($condition, $values[1], $values[2]);
352: }else {
353: $this->data->data->where($condition, $values[1]);
354: }
355:
356: }
357: }
358:
359: }
360: