1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace App\Model;
13:
14: use Nette,
15: Nette\Object,
16: App\Lib\Tree,
17: App\Lib\Statics\Cons;
18:
19: 20: 21:
22: class Resources extends Object {
23:
24: 25: 26: 27:
28: private $resources = array();
29:
30: 31: 32: 33:
34: private $source;
35:
36: 37: 38: 39:
40: public function __construct(Database $database) {
41: $this->source = (new Tree(Cons::TABLE_ACL . '_' . Cons::TABLE_RESOURCES, $database))
42: ->data
43: ->where(Cons::COLUMN_PARENT . ' NOT', NULL);
44: foreach ($this->source as $activeRow) {
45: $parent = $activeRow->ref(Cons::COLUMN_PARENT)[Cons::COLUMN_PATH];
46: $this->addResource($activeRow[Cons::COLUMN_PATH], ($parent === NULL || $parent === '')? NULL : $parent);
47: }
48: }
49:
50: 51: 52: 53: 54:
55: public function setResource($resource, $parent = NULL, $parentName = NULL){
56: $data = array(Cons::COLUMN_NAME => $resource);
57: if ($parent !== NULL){
58: $parent = $this->source->findById($parent);
59: $data[Cons::COLUMN_PARENT] = $parent[Cons::COLUMN_ID];
60: $parentName = $parent[Cons::COLUMN_PATH];
61: }
62: $this->addResource($resource, $parentName);
63: $this->source->insert($data);
64: }
65:
66: 67: 68: 69: 70:
71: public function hasResource($resource) {
72: $this->checkResource($resource, FALSE);
73: return isset($this->resources[$resource]);
74: }
75:
76: 77: 78: 79:
80: public function getResources() {
81: return $this->resources;
82: }
83:
84: 85: 86: 87: 88: 89: 90: 91:
92: private function addResource($resource, $parent = NULL) {
93: $this->checkResource($resource, FALSE);
94: if (isset($this->resources[$resource])) {
95: throw new Nette\InvalidStateException("Zdroj '$resource' již v seznamu existuje.");
96: }
97: if ($parent !== NULL) {
98: $this->checkResource($parent);
99: $this->resources[$parent]['childs'][] = $resource;
100: }
101: $this->resources[$resource] = array('parent' => $parent,'childs' => array());
102: return $this;
103: }
104:
105: 106: 107: 108: 109: 110: 111:
112: private function checkResource($resource, $need = TRUE) {
113: if (!is_string($resource) || $resource === '') {
114: throw new Nette\InvalidArgumentException('Zdroj musí být řetězec a nesmí být prázdný.');
115: } elseif ($need && !isset($this->resources[$resource])) {
116: throw new Nette\InvalidStateException("Zdroj '$resource' neexistuje.");
117: }
118: }
119: }