Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • Container
  • ControlGroup
  • Form
  • Helpers
  • Rule
  • Rules

Interfaces

  • IControl
  • IFormRenderer
  • ISubmitterControl
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Forms;
  9: 
 10: use Nette;
 11: use Nette\Utils\Strings;
 12: use Nette\Utils\Html;
 13: 
 14: 
 15: /**
 16:  * Forms helpers.
 17:  *
 18:  * @author     David Grudl
 19:  */
 20: class Helpers extends Nette\Object
 21: {
 22:     private static $unsafeNames = array(
 23:         'attributes', 'children', 'elements', 'focus', 'length', 'reset', 'style', 'submit', 'onsubmit', 'form',
 24:         'presenter', 'action',
 25:     );
 26: 
 27: 
 28:     /**
 29:      * Extracts and sanitizes submitted form data for single control.
 30:      * @param  array   submitted data
 31:      * @param  string  control HTML name
 32:      * @param  string  type Form::DATA_TEXT, DATA_LINE, DATA_FILE, DATA_KEYS
 33:      * @return string|string[]
 34:      * @internal
 35:      */
 36:     public static function extractHttpData(array $data, $htmlName, $type)
 37:     {
 38:         $name = explode('[', str_replace(array('[]', ']', '.'), array('', '', '_'), $htmlName));
 39:         $data = Nette\Utils\Arrays::get($data, $name, NULL);
 40:         $itype = $type & ~Form::DATA_KEYS;
 41: 
 42:         if (substr($htmlName, -2) === '[]') {
 43:             if (!is_array($data)) {
 44:                 return array();
 45:             }
 46:             foreach ($data as $k => $v) {
 47:                 $data[$k] = $v = static::sanitize($itype, $v);
 48:                 if ($v === NULL) {
 49:                     unset($data[$k]);
 50:                 }
 51:             }
 52:             if ($type & Form::DATA_KEYS) {
 53:                 return $data;
 54:             }
 55:             return array_values($data);
 56:         } else {
 57:             return static::sanitize($itype, $data);
 58:         }
 59:     }
 60: 
 61: 
 62:     private static function sanitize($type, $value)
 63:     {
 64:         if ($type === Form::DATA_TEXT) {
 65:             return is_scalar($value) ? Strings::normalizeNewLines($value) : NULL;
 66: 
 67:         } elseif ($type === Form::DATA_LINE) {
 68:             return is_scalar($value) ? Strings::trim(strtr($value, "\r\n", '  ')) : NULL;
 69: 
 70:         } elseif ($type === Form::DATA_FILE) {
 71:             return $value instanceof Nette\Http\FileUpload ? $value : NULL;
 72: 
 73:         } else {
 74:             throw new Nette\InvalidArgumentException('Unknown data type');
 75:         }
 76:     }
 77: 
 78: 
 79:     /**
 80:      * Converts control name to HTML name.
 81:      * @return string
 82:      */
 83:     public static function generateHtmlName($id)
 84:     {
 85:         $name = str_replace(Nette\ComponentModel\IComponent::NAME_SEPARATOR, '][', $id, $count);
 86:         if ($count) {
 87:             $name = substr_replace($name, '', strpos($name, ']'), 1) . ']';
 88:         }
 89:         if (is_numeric($name) || in_array($name, self::$unsafeNames, TRUE)) {
 90:             $name = '_' . $name;
 91:         }
 92:         return $name;
 93:     }
 94: 
 95: 
 96:     /**
 97:      * @return string
 98:      */
 99:     public static function createInputList(array $items, array $inputAttrs = NULL, array $labelAttrs = NULL, $wrapper = NULL)
100:     {
101:         list($inputAttrs, $inputTag) = self::prepareAttrs($inputAttrs, 'input');
102:         list($labelAttrs, $labelTag) = self::prepareAttrs($labelAttrs, 'label');
103:         $res = '';
104:         $input = Html::el();
105:         $label = Html::el();
106:         list($wrapper, $wrapperEnd) = $wrapper instanceof Html ? array($wrapper->startTag(), $wrapper->endTag()) : array((string) $wrapper, '');
107: 
108:         foreach ($items as $value => $caption) {
109:             foreach ($inputAttrs as $k => $v) {
110:                 $input->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
111:             }
112:             foreach ($labelAttrs as $k => $v) {
113:                 $label->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
114:             }
115:             $input->value = $value;
116:             $res .= ($res === '' && $wrapperEnd === '' ? '' : $wrapper)
117:                 . $labelTag . $label->attributes() . '>'
118:                 . $inputTag . $input->attributes() . (Html::$xhtml ? ' />' : '>')
119:                 . ($caption instanceof Html ? $caption : htmlspecialchars($caption, ENT_NOQUOTES, 'UTF-8'))
120:                 . '</label>'
121:                 . $wrapperEnd;
122:         }
123:         return $res;
124:     }
125: 
126: 
127:     /**
128:      * @return Html
129:      */
130:     public static function createSelectBox(array $items, array $optionAttrs = NULL)
131:     {
132:         list($optionAttrs, $optionTag) = self::prepareAttrs($optionAttrs, 'option');
133:         $option = Html::el();
134:         $res = $tmp = '';
135:         foreach ($items as $group => $subitems) {
136:             if (is_array($subitems)) {
137:                 $res .= Html::el('optgroup')->label($group)->startTag();
138:                 $tmp = '</optgroup>';
139:             } else {
140:                 $subitems = array($group => $subitems);
141:             }
142:             foreach ($subitems as $value => $caption) {
143:                 $option->value = $value;
144:                 foreach ($optionAttrs as $k => $v) {
145:                     $option->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
146:                 }
147:                 if ($caption instanceof Html) {
148:                     $caption = clone $caption;
149:                     $res .= $caption->setName('option')->addAttributes($option->attrs);
150:                 } else {
151:                     $res .= $optionTag . $option->attributes() . '>'
152:                         . htmlspecialchars($caption, ENT_NOQUOTES, 'UTF-8')
153:                         . '</option>';
154:                 }
155:             }
156:             $res .= $tmp;
157:             $tmp = '';
158:         }
159:         return Html::el('select')->setHtml($res);
160:     }
161: 
162: 
163:     private static function prepareAttrs($attrs, $name)
164:     {
165:         $dynamic = array();
166:         foreach ((array) $attrs as $k => $v) {
167:             $p = str_split($k, strlen($k) - 1);
168:             if ($p[1] === '?' || $p[1] === ':') {
169:                 unset($attrs[$k], $attrs[$p[0]]);
170:                 if ($p[1] === '?') {
171:                     $dynamic[$p[0]] = array_fill_keys((array) $v, TRUE);
172:                 } elseif (is_array($v) && $v) {
173:                     $dynamic[$p[0]] = $v;
174:                 } else {
175:                     $attrs[$p[0]] = $v;
176:                 }
177:             }
178:         }
179:         return array($dynamic, '<' . $name . Html::el(NULL, $attrs)->attributes());
180:     }
181: 
182: }
183: 
Nette 2.2 API documentation generated by ApiGen 2.8.0