1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace App\Model;
13:
14: use Nette\Object,
15: Nette\Database\Table\Selection,
16: App\Model\Database,
17: App\Lib\Tree,
18: App\Lib\Statics\Cons;
19:
20: 21: 22:
23: class Menu extends Object{
24:
25:
26: private $tree;
27:
28:
29: private $user;
30:
31: 32: 33: 34:
35: public function __construct(Database $database, \Nette\Security\User $user) {
36: $this->tree = new Tree(Cons::TABLE_MENU, $database);
37: $this->user = $user;
38: $this->checkMenuIntegrity();
39: }
40:
41: 42: 43: 44:
45: public function setPrivacy($user){
46: $this->user = $user;
47: }
48:
49: 50: 51: 52:
53: public function getFull() {
54: return $this->menu();
55: }
56:
57: 58: 59: 60: 61: 62: 63: 64:
65: public function checkResources($aclControler,$acl){
66: $state = true;
67: foreach ($this->tree->data as $activeRow){
68: if (!$acl->hasResource(Cons::RES_MENU. ' > '.$activeRow[Cons::COLUMN_PATH])){
69: $state = $aclControler->addResource(Cons::TABLE_MENU,$activeRow[Cons::COLUMN_PATH]) && $state;
70: }
71: }
72: return $state;
73: }
74:
75: 76: 77: 78:
79: private function menu(){
80: return $this->acl($this->tree->data);
81: }
82:
83: 84: 85: 86:
87: private function acl($selection){
88: foreach ($selection as $activeRow){
89: if (!$this->user->isAllowed(Cons::RES_MENU.' > '.$activeRow[Cons::COLUMN_PATH], Cons::READ)){
90: $selection->offsetUnset($activeRow[Cons::COLUMN_ID]);
91: }
92: }
93: return $selection;
94: }
95:
96: 97: 98: 99:
100: private function checkMenuIntegrity() {
101: $selection = $this->tree->copy
102: ->where('NOT '.Cons::TABLE_SOURCE.'_'.Cons::COLUMN_ID, NULL)
103: ->where(Cons::COLUMN_LINK, NULL);
104: ($selection->count() === 0)? : $this->repair($selection);
105: }
106:
107: 108: 109: 110:
111: private function repair(Selection $selection) {
112: foreach ($selection as $activeRow) {
113: $link = ":" . $activeRow[Cons::TABLE_SOURCE][Cons::TABLE_PRESENTER][Cons::TABLE_MODULE][Cons::COLUMN_DIR] .
114: ":" . $activeRow[Cons::TABLE_SOURCE][Cons::TABLE_PRESENTER][Cons::COLUMN_NAME] .
115: ":" . $activeRow[Cons::TABLE_SOURCE][Cons::COLUMN_ACTION];
116: $this->tree->update($activeRow->id, array(Cons::COLUMN_LINK => $link));
117: }
118: }
119: }
120: