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 Base 
  7:  * @author Jaromír Polášek
  8:  * @version 0.9.1.AFTERMATCH
  9:  * Encoding UTF-8
 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:  * Zabezpečuje přístup k datů a nese informace o jejich struktuře. 
 26:  */
 27: class Action extends Object {
 28: 
 29:     /** konstanty STRUCT a DATA zastupují požadované objekty */
 30:     const STRUCT = 'action',
 31:         DATA = 'data';
 32: 
 33:     /** @var App\Model\Database Připojení k Databázi */
 34:     private $database;
 35: 
 36:     /** @var Nette\Security\User Objekt Uživatele */
 37:     private $user;
 38: 
 39:     /** @var App\Lib\Table|App\Lib\Tree Data z Database */
 40:     private $data;
 41: 
 42:     /** @var Nette\Database\Table\ActiveRow Paramtry Akce */
 43:     private $action;
 44: 
 45:     /** @var App\Lib\Structure Struktura dat */
 46:     private $structure;
 47: 
 48:     /** @var array Parametry url */
 49:     private $params;
 50: 
 51:     /** @var boolean Staus */
 52:     private $state;
 53: 
 54:     /** @var App\Model\Jidash Model pro evidenci změn databáze */
 55:     private $jidash;
 56: 
 57:     /** @var Nette\Database\Table\ActiveRow akce subsekce */
 58:     private $child;
 59: 
 60:     /**
 61:      * Konstruktor požaduje databázi
 62:      * @param \App\Model\Database $database
 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:     //$state = (new \App\Lib\Repair\Repair($database))->repairSources();//Pomocná knihovna pro vytvoření základních dat
 70:     }
 71: 
 72:     /**
 73:      * Oekává parametry akce (parametry URL). Pokud je jednou nastaveno, jiné parametry ignoruje
 74:      * @param array $params
 75:      */
 76:     public function setParams($params) {
 77:     if (!$this->state) {
 78:         $this->params = $params;
 79:         $this->state = TRUE;
 80:     }
 81:     }
 82: 
 83:     /**
 84:      * Vrací stav. Pokud jsou parametry nastaveny, vrací true
 85:      * @return boolean
 86:      * @throws Nette\InvalidStateException
 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:      * Vrací true, pokud akce má vazbu
 98:      * @return boolean
 99:      */
100:     public function getParent() {
101:     $parent = (!$this->lazy(self::STRUCT)) ? NULL : $this->action[Cons::COLUMN_PARENT];
102:     return $parent;
103:     }
104: 
105:     /**
106:      * Vrací relevatní strukturu
107:      * @return array
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:      * Vrací relevatní data
117:      * @return \Nette\Database\Table\Selection
118:      */
119:     public function getRelevance() {
120:     $child = $this->getChild();
121:     return $this->data->related($child[Cons::COLUMN_TABLE][Cons::COLUMN_OBJECT]); //.'.'. Cons::COLUMN_NAME);
122:     }
123: 
124:     /**
125:      * Vrátí akci potomka
126:      * @return Nette\Database\ActiveRow
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:      * Vrací ID akce z tabulky akcí
137:      * @return integer 
138:      */
139:     public function getSource() {
140:     return (!$this->lazy(self::STRUCT)) ? NULL : $this->action[Cons::COLUMN_ID];
141:     }
142: 
143:     /**
144:      * Vrací jméno akce 
145:      * @return string
146:      */
147:     public function getName() {
148:     return (!$this->lazy(self::STRUCT)) ? NULL : $this->action[Cons::COLUMN_NAME];
149:     }
150: 
151:     /**
152:      * Vrací Strukturu tabulky
153:      * @return App\Lib\Structure
154:      */
155:     public function getTable() {
156:     return (!$this->lazy(self::STRUCT)) ? NULL : $this->structure;
157:     }
158: 
159:     /**
160:      * Vrací přístup k datům z akce požadované data
161:      * @return Nette\Database\Selection
162:      */
163:     public function getData() {
164:     return (!$this->lazy(self::DATA)) ? NULL : $this->data;
165:     }
166: 
167:     /**
168:      * Vrací povolení k vytvoření nové položky
169:      * @return boolean
170:      */
171:     public function getCreate() {
172:     return $this->acl(Cons::CREATE);
173:     }
174: 
175:     /**
176:      * Ověření práv a smazání položky
177:      * @param array $row požadovaný řádek
178:      * @return boolean
179:      */
180:     public function delete($row) {
181:     return ($this->acl(Cons::DELETE)) ? $this->data->delete($row[Cons::COLUMN_ID]) : FALSE;
182:     }
183: 
184:     /**
185:      * Ověření práv a úprava, nebo vytvoření položky
186:      * @param array $data
187:      * @param int $id
188:      * @return boolean
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:      * @deprecated since version 0.7.1.FORM 
208:      * Dělá totéž co table
209:      * @return Nette\Database\Table\GroupedSelection
210:      */
211:     public function getFields() {
212:     return $this->getTable();
213:     }
214: 
215:     /**
216:      * Objekt user pro ověření práv a nastavení dat
217:      * @return Nette\Secutity\User
218:      */
219:     public function getUser() {
220:     return $this->user;
221:     }
222: 
223:     /**
224:      * Vrátí data pro potřeby relace
225:      * @param string $table
226:      * @param string $column
227:      * @return array
228:      */
229:     public function getRelation($table, $column) {
230:     $name = strtolower($table);
231:     if ($name === 'stock_source_id' || $name === 'stock_target_id') {
232:         //Nette neumí dva klíče na stejnou tabulku
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:     //totéž ale upravené ->fetchPairs(Cons::COLUMN_ID, $column);
241:     }
242: 
243:     /**
244:      * Vrací pole posledních zásahu do databáze
245:      * @return array
246:      */
247:     public function getJidash() {
248:     return $this->jidash->show($this->getdata());
249:     }
250: 
251:     /**
252:      * Vytvoří kompletní strukturu (Tabulka, Pole, Data)
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:     //$this->aclFields();
260:     }
261: 
262:     /**
263:      * Ověří pořadovaný typ oprávnění na aktuální tabulce.
264:      * @param string $permision
265:      * @return boolean 
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:      * Nastaví data na Nette\Database\Selection, Nette\Database\Table\ActiveRow, 
278:      * nebo array() v závislosti na právech. Pokud není co vrátit nastává
279:      * ForbiddenRequestException 403.
280:      * @throws Nette\Application\ForbiddenRequestException
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:      * Vratí objekt s daty podle typu (Tree|Table)
298:      * @param string $type formát dat
299:      * @param string $object Název Objektu s daty
300:      * @return App\Lib\Tree|App\Lib\Table
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:      * Na požádání načte potřebná data a struktury
310:      * @param string $object
311:      * @throws Nette\Nette\InvalidArgumentException
312:      * @throws Nette\InvalidStateException
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:      * Vyjme pole, které nemá uživatel právo vidět
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:      * Aplikuje na data Filtr uvedený v tabulce akcí. Proměnnou %user% nahradí uživatelským ID
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: 
sberp API API documentation generated by ApiGen