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 FormFactory 
  7:  * @author Jaromír Polášek
  8:  * @version 0.7.5.RELATION
  9:  * Encoding UTF-8
 10:  */
 11: 
 12: namespace App\Lib\Forms;
 13: 
 14: use Nette,
 15:     Nette\Application\UI\Form,
 16:     App\Model\Action,
 17:     App\Lib\Statics\Cons,
 18:     App\Lib\Statics\Vars;
 19: 
 20: /**
 21:  * Továrna na generování formuláře z dat v Databázi
 22:  */
 23: class FormFactory extends Controls {
 24: 
 25:     /**
 26:      * Konstruktor zpracovává pole formuláře          
 27:      * @param \App\Model\Action $action
 28:      * @param Nette\ComponentModel\IContainer $parent
 29:      * @param string $name
 30:      */
 31:     public function __construct(Action $action, Nette\ComponentModel\IContainer $parent = NULL, $name = NULL) {
 32:     parent::__construct($parent, $name);
 33:     $this->fields = $action->table;//Vars::leftRightSort($action->table);
 34:     $this->action = $action;
 35:     }
 36: 
 37:     /**
 38:      * vytvoří formulář
 39:      */
 40:     public function create($disabled = FALSE) {
 41:     $form = $this->addContainer(self::FORM);
 42:     foreach ($this->fields as $field) {
 43:         $this->query = $field;
 44:         ($disabled) ? : $this->query[Cons::COLUMN_READONLY] = TRUE;
 45:         if ($this->query[Cons::COLUMN_FIELD] === Cons::COLUMN_PASSWORD) {
 46:         (!$disabled)? : $form = $this->Password($disabled);
 47:         } else if ($this->query[Cons::COLUMN_FIELD] === Cons::COLUMN_STATE) {
 48:         $form = $this->state($disabled);
 49:         } else if ($this->query[Cons::COLUMN_FIELD] === Cons::COLUMN_DESC) {
 50:         $form = $this->desc($disabled);
 51:         } else {
 52:         $form = $this->createField();
 53:         }
 54:     }
 55:     }
 56: 
 57:     /**
 58:      * Přidá do formuláře prvek z $query
 59:      * @return type
 60:      */
 61:     public function createField() {
 62:     $field = $this->field(
 63:         $this->query[Cons::TABLE_TABLE . '_' . Cons::TABLE_ELEMENTS . '_' . Cons::COLUMN_ID], 
 64:         $this->query[Cons::COLUMN_FIELD], 
 65:         $this->query[Cons::COLUMN_NAME], 
 66:         $this->query[Cons::COLUMN_LENGHT]
 67:         );
 68:     (!isset($this->query[Cons::COLUMN_CLASS])) ? : $field->setAttribute('class', $this->query[Cons::COLUMN_CLASS] . ' tooltip');
 69:     (!Vars::trueFalse($this->query[Cons::COLUMN_REQUIRED])) ? : $field->setRequired("Pole %label je povinné");
 70:     (!Vars::trueFalse($this->query[Cons::COLUMN_READONLY])) ? : $field->setDisabled(TRUE);
 71:     (!isset($this->query[Cons::COLUMN_DESC])) ? : $this->tooltip($field);
 72:     return $field;
 73:     }
 74: 
 75:     /**
 76:      * Přidá tooltip k Formulářovému prvku
 77:      */
 78:     private function tooltip($field) {
 79:     return $field->setAttribute('title', $this->query[Cons::COLUMN_DESC]);
 80:     }
 81: 
 82:     /**
 83:      * Přidá ovladač Password do Formuláře
 84:      */
 85:     private function Password() {
 86:     $pass = $this->createField();
 87:     $pass->addCondition(Form::FILLED, TRUE)
 88:         ->addRule(Form::MIN_LENGTH, 'Heslo musí mít alespoň %d znaky', 8)
 89:         ->addRule(Form::PATTERN, 'Musí obsahovat číslici', '.*[0-9].*');
 90: 
 91:     $this[self::FORM]->addPassword('password_verify', 'Heslo pro kontrolu', $this->query[Cons::COLUMN_LENGHT])
 92:         //->setRequired("Pole %label je povinné")
 93:         ->addRule(Form::EQUAL, 'Hesla se neshodují', $pass)
 94:         ->setAttribute('class', $this->query[Cons::COLUMN_CLASS] . ' tooltip')
 95:         ->setAttribute('title', 'Zadejte prosím heslo ještě jednou pro kontrolu')
 96:         ->setOmitted(TRUE);
 97:     }
 98: 
 99:     /**
100:      * Nastaví pole state, do proměné $state
101:      */
102:     private function state($disabled) {
103:     $this->state = $this->query;
104:     ($disabled) ? : $this->state[Cons::COLUMN_READONLY] = TRUE;
105:     }
106: 
107:     /**
108:      * Nastaví pole desc, do proměné $desc
109:      */
110:     private function desc($disabled) {
111:     $this->desc = $this->query;
112:     ($disabled) ? : $this->desc[Cons::COLUMN_READONLY] = TRUE;
113:     }
114:     /**
115:      * Nastaví maximální délku pole
116:      * @deprecated since version 0.7.3.FORM
117:      * @param Nette\Aplication\Ui\Form $field
118:      */
119:     private function setLenght($field) {
120:     $field->addRule(Form::MAX_LENGTH, "Maximální délka pole %label je %d znaků", $this->query['lenght']);
121:     }
122: 
123:     /**
124:      * Vytfoří pole formuláře typu boolean, nebo předá parametry metodě extendField
125:      * @param string $type Název typu pole
126:      * @param string $name Název pole
127:      * @param string $label Označení pole
128:      * @param int $lenght Počet znaků nebo řádků
129:      * @param array $array Pole možných hodnot
130:      * @throws Nette\InvalidArgumentException
131:      */
132:     private function field($type, $field, $label, $lenght) {
133:     $method = 'add' . $type;
134:     try {
135:         if ($lenght !== '1') {
136:         return $this[self::FORM]->{$method}($field, $label, $lenght);
137:         } else {
138:         return $this[self::FORM]->{$method}($field, $label);
139:         }
140:     } catch (Nette\MemberAccessException $e) {
141:         unset($e);
142:         throw new Nette\InvalidArgumentException("Neplatný typ pole formuláře $type");
143:     }
144:     }
145: 
146: }
147: 
sberp API API documentation generated by ApiGen