Overview

Namespaces

  • Budovy
  • Kdyby
    • BootstrapFormRenderer
      • DI
      • Latte
  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • Nextras
    • Datagrid
  • None
  • PHP
  • Tester
    • CodeCoverage
    • Runner
      • Output
  • Vodacek
    • Forms
      • Controls
  • WebLoader
    • Filter
    • Nette

Classes

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

Interfaces

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