Overview

Namespaces

  • App
    • Components
    • Interfaces
    • Lib
      • Files
      • Forms
        • Controls
      • Html
      • Repair
      • Statics
    • Model
    • Modules
      • BaseModule
        • Presenters
      • PartnersModule
        • Presenters
      • ProductModule
        • Presenters
      • SaleModule
        • Model
        • Presenters
      • SettingsModule
        • Model
        • Presenters
      • StoreModule
        • Model
        • Presenters
    • Presenters
    • Router
  • PHP

Classes

  • DateTime
  • SplFileInfo

Interfaces

  • ArrayAccess
  • Countable
  • Exception
  • Iterator
  • Traversable
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Download
  1: <?php
  2: 
  3: /**
  4:  * Project sberp
  5:  *
  6:  * @file Menu 
  7:  * @author Jaromír Polášek
  8:  * @version 0.6.1 ACL
  9:  * Encoding UTF-8
 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:  * Čte Navigaci z databáze
 22:  */
 23: class Menu extends Object{    
 24:     
 25:     /** @var App\Lib\Tree */
 26:     private $tree;
 27:     
 28:     /** @var Nette\Security\User */
 29:     private $user;        
 30:     
 31:     /**
 32:      * Konstruktor - Kontroluje integritu menu
 33:      * @param \App\Model\Database $database
 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:      * Očekává uživatele, pokud null, menu bude prázdné 
 43:      * @param Nette/SecurityUser $user
 44:      */
 45:     public function setPrivacy($user){
 46:     $this->user = $user;
 47:     }
 48:     
 49:     /**
 50:      * Vrací setříděný strom menu
 51:      * @return Nette\Database\Selection
 52:      */
 53:     public function getFull() {
 54:     return $this->menu();
 55:     }
 56:     
 57:     /**
 58:      * Zkontroluje, zda jsou v resources všechny položky menu, pokud ne, tak doplní chybějící. 
 59:      * Volá se při instalaci modulu, nebo pokud je potřeba opravit menu. pokud přidání některého Resource 
 60:      * selže metoda vrátí false
 61:      * @param App\Model\Acl $aclControler
 62:      * @param Nette\Security\Permission $acl
 63:      * @return boolean
 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:      * Vrací odfiltrované menu
 77:      * @return array
 78:      */
 79:     private function menu(){
 80:     return $this->acl($this->tree->data);
 81:     }
 82:     
 83:     /**
 84:      * Odfiltruje položky, ke kterým nemá uživatel přístup
 85:      * @param Nette\Database\Selection $selection
 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:      * Zkontroluje integritu menu (zda má každá akce uvedenou cestu)
 98:      * a opraví chybné řádky. Pracuje s kopii tabulky
 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:      * vytvoří v cesty (sloupec path) ve vybrané části menu
109:      * @param Nette\Database\Table\Selection $selection
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: 
sberp API API documentation generated by ApiGen