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 Resources 
  7:  * @author Jaromír Polášek
  8:  * @version 0.6.7 ACL
  9:  * Encoding UTF-8
 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:  * Objekt Zdrojů
 21:  */
 22: class Resources extends Object {
 23:     
 24:     /**
 25:      * Pole zdrojů s předky a potomky
 26:      * @var array
 27:      */
 28:     private $resources = array();
 29:     
 30:     /**
 31:      * setříděná tabulka se zdroji
 32:      * @var Nette\Database\Table\Selection 
 33:      */
 34:     private $source;
 35:     
 36:     /**
 37:      * Konstruktor načte tabulku zdrojů z database
 38:      * @param \App\Model\Database $database
 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:      * Přidá Zdroj do Resources a Databáze. Kontroluje existenci rodiče 
 52:      * @param string $resource
 53:      * @param integer $parent
 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:      * Vrací TRUE pokud je zadaný zdroj v seznamu.
 68:      * @param  string Název zdroje
 69:      * @return bool
 70:      */
 71:     public function hasResource($resource) {
 72:     $this->checkResource($resource, FALSE);
 73:     return isset($this->resources[$resource]);
 74:     }
 75: 
 76:     /**
 77:      * Vrácí pole všech zdrojů
 78:      * @return array Pole Zdrojů
 79:      */
 80:     public function getResources() {
 81:     return $this->resources;
 82:     }
 83: 
 84:     /**
 85:      * Přidá do seznamu zdroj s jedinečným identifikátorem 
 86:      * @param string $resource Název zdroje (unique)
 87:      * @param string $parent Název předka (musí již existovat)
 88:      * @return \App\Model\Resources
 89:      * @throws Nette\InvalidStateException
 90:      * @throws Nette\InvalidArgumentException
 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:      * Zkontroluje, zda je zdroj validní a existuje
107:      * @param  string
108:      * @param  bool
109:      * @throws Nette\InvalidStateException
110:      * @return void
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: }
sberp API API documentation generated by ApiGen