FormHelper.php 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  1. <?php
  2. /**
  3. * Automatic generation of HTML FORMs from given data.
  4. *
  5. * Used for scaffolding.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake.libs.view.helpers
  18. * @since CakePHP(tm) v 0.10.0.1076
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('AppHelper', 'View/Helper');
  22. /**
  23. * Form helper library.
  24. *
  25. * Automatic generation of HTML FORMs from given data.
  26. *
  27. * @package cake.libs.view.helpers
  28. * @link http://book.cakephp.org/view/1383/Form
  29. */
  30. class FormHelper extends AppHelper {
  31. /**
  32. * Other helpers used by FormHelper
  33. *
  34. * @var array
  35. * @access public
  36. */
  37. public $helpers = array('Html');
  38. /**
  39. * Holds the fields array('field_name' => array('type'=> 'string', 'length'=> 100),
  40. * primaryKey and validates array('field_name')
  41. *
  42. * @access public
  43. */
  44. public $fieldset = array();
  45. /**
  46. * Options used by DateTime fields
  47. *
  48. * @var array
  49. */
  50. private $__options = array(
  51. 'day' => array(), 'minute' => array(), 'hour' => array(),
  52. 'month' => array(), 'year' => array(), 'meridian' => array()
  53. );
  54. /**
  55. * List of fields created, used with secure forms.
  56. *
  57. * @var array
  58. */
  59. public $fields = array();
  60. /**
  61. * Constant used internally to skip the securing process,
  62. * and neither add the field to the hash or to the unlocked fields.
  63. *
  64. * @var string
  65. */
  66. const SECURE_SKIP = 'skip';
  67. /**
  68. * Defines the type of form being created. Set by FormHelper::create().
  69. *
  70. * @var string
  71. * @access public
  72. */
  73. public $requestType = null;
  74. /**
  75. * The default model being used for the current form.
  76. *
  77. * @var string
  78. * @access public
  79. */
  80. public $defaultModel = null;
  81. /**
  82. * Persistent default options used by input(). Set by FormHelper::create().
  83. *
  84. * @var array
  85. * @access protected
  86. */
  87. protected $_inputDefaults = array();
  88. /**
  89. * An array of fieldnames that have been excluded from
  90. * the Token hash used by SecurityComponent's validatePost method
  91. *
  92. * @see FormHelper::__secure()
  93. * @see SecurityComponent::validatePost()
  94. * @var array
  95. */
  96. protected $_unlockedFields = array();
  97. /**
  98. * Holds the model references already loaded by this helper
  99. * product of trying to inspect them out of field names
  100. *
  101. * @var array
  102. */
  103. protected $_models = array();
  104. /**
  105. * Holds all the validation errors for models loaded and inspected
  106. * it can also be set manually to be able to display custom error messages
  107. * in the any of the input fields generated by this helper
  108. *
  109. * @var array
  110. */
  111. public $validationErrors = array();
  112. /**
  113. * Copies the validationErrors variable from the View object into this instance
  114. *
  115. * @param View $View The View this helper is being attached to.
  116. * @param array $settings Configuration settings for the helper.
  117. */
  118. public function __construct(View $View, $settings = array()) {
  119. parent::__construct($View, $settings);
  120. $this->validationErrors =& $View->validationErrors;
  121. }
  122. /**
  123. * Guess the location for a model based on its name and tries to create a new instance
  124. * or get an already created instance of the model
  125. *
  126. * @return Model model instance
  127. */
  128. protected function _getModel($model) {
  129. $object = null;
  130. if (!$model || $model === 'Model') {
  131. return $object;
  132. }
  133. if (array_key_exists($model, $this->_models)) {
  134. return $this->_models[$model];
  135. }
  136. if (ClassRegistry::isKeySet($model)) {
  137. $object = ClassRegistry::getObject($model);
  138. } elseif (isset($this->request->params['models'][$model])) {
  139. $plugin = $this->request->params['models'][$model]['plugin'];
  140. $plugin .= ($plugin) ? '.' : null;
  141. $object = ClassRegistry::init(array(
  142. 'class' => $plugin . $this->request->params['models'][$model]['className'],
  143. 'alias' => $model
  144. ));
  145. } else {
  146. $object = ClassRegistry::init($model, true);
  147. }
  148. $this->_models[$model] = $object;
  149. if (!$object) {;
  150. return null;
  151. }
  152. $this->fieldset[$model] = array('fields' => null, 'key' => $object->primaryKey, 'validates' => null);
  153. return $object;
  154. }
  155. /**
  156. * Inspects the model properties to extract information from them.
  157. * Currently it can extract information from the the fields, the primary key and required fields
  158. *
  159. * The $key parameter accepts the following list of values:
  160. *
  161. * - key: Returns the name of the primary key for the model
  162. * - fields: Returns the model schema
  163. * - validates: returns the list of fields that are required
  164. * - errors: returns the list of validation errors
  165. *
  166. * If the $field parameter is passed if will return the information for that sole field.
  167. *
  168. * `$this->_introspectModel('Post', 'fields', 'title');` will return the schema information for title column
  169. *
  170. * @param string $model name of the model to extract information from
  171. * @param string $key name of the special information key to obtain (key, fields, validates, errors)
  172. * @param string $field name of the model field to get information from
  173. * @return mixed information extracted for the special key and field in a model
  174. */
  175. protected function _introspectModel($model, $key, $field = null) {
  176. $object = $this->_getModel($model);
  177. if (!$object) {
  178. return;
  179. }
  180. if ($key === 'key') {
  181. return $this->fieldset[$model]['key'];
  182. }
  183. if ($key === 'fields') {
  184. if (!isset($this->fieldset[$model]['fields'])) {
  185. $fields = $this->fieldset[$model]['fields'] = $object->schema();
  186. }
  187. if (empty($field)) {
  188. foreach ($object->hasAndBelongsToMany as $alias => $assocData) {
  189. $this->fieldset[$object->alias]['fields'][$alias] = array('type' => 'multiple');
  190. }
  191. return $this->fieldset[$model]['fields'];
  192. } elseif (isset($this->fieldset[$model]['fields'][$field])) {
  193. return $this->fieldset[$model]['fields'][$field];
  194. } else {
  195. return isset($object->hasAndBelongsToMany[$field]) ? array('type' => 'multiple') : null;
  196. }
  197. }
  198. if ($key === 'errors' && !isset($this->validationErrors[$model])) {
  199. $this->validationErrors[$model] =& $object->validationErrors;
  200. return $this->validationErrors[$model];
  201. } elseif ($key === 'errors' && isset($this->validationErrors[$model])) {
  202. return $this->validationErrors[$model];
  203. }
  204. if ($key === 'validates' && !isset($this->fieldset[$model]['validates'])) {
  205. $validates = array();
  206. if (!empty($object->validate)) {
  207. foreach ($object->validate as $validateField => $validateProperties) {
  208. if ($this->_isRequiredField($validateProperties)) {
  209. $validates[$validateField] = true;
  210. }
  211. }
  212. }
  213. $this->fieldset[$model]['validates'] = $validates;
  214. }
  215. if ($key === 'validates') {
  216. if (empty($field)) {
  217. return $this->fieldset[$model]['validates'];
  218. } else {
  219. return isset($this->fieldset[$model]['validates'][$field]) ?
  220. $this->fieldset[$model]['validates'] : null;
  221. }
  222. }
  223. }
  224. /**
  225. * Returns if a field is required to be filled based on validation properties from the validating object
  226. *
  227. * @return boolean true if field is required to be filled, false otherwise
  228. */
  229. protected function _isRequiredField($validateProperties) {
  230. $required = false;
  231. if (is_array($validateProperties)) {
  232. $dims = Set::countDim($validateProperties);
  233. if ($dims == 1 || ($dims == 2 && isset($validateProperties['rule']))) {
  234. $validateProperties = array($validateProperties);
  235. }
  236. foreach ($validateProperties as $rule => $validateProp) {
  237. if (isset($validateProp['allowEmpty']) && $validateProp['allowEmpty'] === true) {
  238. return false;
  239. }
  240. $rule = isset($validateProp['rule']) ? $validateProp['rule'] : false;
  241. $required = $rule || empty($validateProp);
  242. if ($required) {
  243. break;
  244. }
  245. }
  246. }
  247. return $required;
  248. }
  249. /**
  250. * Returns false if given form field described by the current entity has no errors.
  251. * Otherwise it returns the validation message
  252. *
  253. * @return boolean True on errors.
  254. */
  255. public function tagIsInvalid() {
  256. $entity = $this->entity();
  257. $model = array_shift($entity);
  258. if (!empty($entity) && isset($this->validationErrors[$model])) {
  259. return Set::classicExtract($this->validationErrors[$model], join('.', $entity));
  260. }
  261. if (!empty($entity)) {
  262. $errors = $this->_introspectModel($model, 'errors');
  263. return ($errors) ? Set::classicExtract($errors, join('.', $entity)) : false;
  264. }
  265. }
  266. /**
  267. * Returns an HTML FORM element.
  268. *
  269. * ### Options:
  270. *
  271. * - `type` Form method defaults to POST
  272. * - `action` The controller action the form submits to, (optional).
  273. * - `url` The url the form submits to. Can be a string or a url array. If you use 'url'
  274. * you should leave 'action' undefined.
  275. * - `default` Allows for the creation of Ajax forms.
  276. * - `onsubmit` Used in conjunction with 'default' to create ajax forms.
  277. * - `inputDefaults` set the default $options for FormHelper::input(). Any options that would
  278. * be set when using FormHelper::input() can be set here. Options set with `inputDefaults`
  279. * can be overridden when calling input()
  280. * - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')`
  281. *
  282. * @access public
  283. * @param string $model The model object which the form is being defined for
  284. * @param array $options An array of html attributes and options.
  285. * @return string An formatted opening FORM tag.
  286. * @link http://book.cakephp.org/view/1384/Creating-Forms
  287. */
  288. public function create($model = null, $options = array()) {
  289. $created = $id = false;
  290. $append = '';
  291. if (is_array($model) && empty($options)) {
  292. $options = $model;
  293. $model = null;
  294. }
  295. if (empty($model) && $model !== false && !empty($this->request->params['models'])) {
  296. $model = key($this->request->params['models']);
  297. $this->defaultModel = $model;
  298. } elseif (empty($model) && empty($this->request->params['models'])) {
  299. $model = false;
  300. }
  301. $key = null;
  302. if ($model !== false) {
  303. $object = $this->_getModel($model);
  304. $key = $this->_introspectModel($model, 'key');
  305. $this->setEntity($model, true);
  306. }
  307. if ($model !== false && $key) {
  308. $recordExists = (
  309. isset($this->request->data[$model]) &&
  310. !empty($this->request->data[$model][$key]) &&
  311. !is_array($this->request->data[$model][$key])
  312. );
  313. if ($recordExists) {
  314. $created = true;
  315. $id = $this->request->data[$model][$key];
  316. }
  317. }
  318. $options = array_merge(array(
  319. 'type' => ($created && empty($options['action'])) ? 'put' : 'post',
  320. 'action' => null,
  321. 'url' => null,
  322. 'default' => true,
  323. 'encoding' => strtolower(Configure::read('App.encoding')),
  324. 'inputDefaults' => array()),
  325. $options);
  326. $this->_inputDefaults = $options['inputDefaults'];
  327. unset($options['inputDefaults']);
  328. if (!isset($options['id'])) {
  329. $domId = isset($options['action']) ? $options['action'] : $this->request['action'];
  330. $options['id'] = $this->domId($domId . 'Form');
  331. }
  332. if ($options['action'] === null && $options['url'] === null) {
  333. $options['action'] = $this->request->here(false);
  334. } elseif (empty($options['url']) || is_array($options['url'])) {
  335. if (empty($options['url']['controller'])) {
  336. if (!empty($model) && $model != $this->defaultModel) {
  337. $options['url']['controller'] = Inflector::underscore(Inflector::pluralize($model));
  338. } elseif (!empty($this->request->params['controller'])) {
  339. $options['url']['controller'] = Inflector::underscore($this->request->params['controller']);
  340. }
  341. }
  342. if (empty($options['action'])) {
  343. $options['action'] = $this->request->params['action'];
  344. }
  345. $plugin = null;
  346. if ($this->plugin) {
  347. $plugin = Inflector::underscore($this->plugin);
  348. }
  349. $actionDefaults = array(
  350. 'plugin' => $plugin,
  351. 'controller' => $this->_View->viewPath,
  352. 'action' => $options['action'],
  353. );
  354. $options['action'] = array_merge($actionDefaults, (array)$options['url']);
  355. if (empty($options['action'][0]) && !empty($id)) {
  356. $options['action'][0] = $id;
  357. }
  358. } elseif (is_string($options['url'])) {
  359. $options['action'] = $options['url'];
  360. }
  361. unset($options['url']);
  362. switch (strtolower($options['type'])) {
  363. case 'get':
  364. $htmlAttributes['method'] = 'get';
  365. break;
  366. case 'file':
  367. $htmlAttributes['enctype'] = 'multipart/form-data';
  368. $options['type'] = ($created) ? 'put' : 'post';
  369. case 'post':
  370. case 'put':
  371. case 'delete':
  372. $append .= $this->hidden('_method', array(
  373. 'name' => '_method', 'value' => strtoupper($options['type']), 'id' => null,
  374. 'secure' => self::SECURE_SKIP
  375. ));
  376. default:
  377. $htmlAttributes['method'] = 'post';
  378. break;
  379. }
  380. $this->requestType = strtolower($options['type']);
  381. $action = $this->url($options['action']);
  382. unset($options['type'], $options['action']);
  383. if ($options['default'] == false) {
  384. if (isset($htmlAttributes['onSubmit']) || isset($htmlAttributes['onsubmit'])) {
  385. $htmlAttributes['onsubmit'] .= ' event.returnValue = false; return false;';
  386. } else {
  387. $htmlAttributes['onsubmit'] = 'event.returnValue = false; return false;';
  388. }
  389. }
  390. if (!empty($options['encoding'])) {
  391. $htmlAttributes['accept-charset'] = $options['encoding'];
  392. unset($options['encoding']);
  393. }
  394. unset($options['default']);
  395. $htmlAttributes = array_merge($options, $htmlAttributes);
  396. $this->fields = array();
  397. if (!empty($this->request->params['_Token'])) {
  398. $append .= $this->hidden('_Token.key', array(
  399. 'value' => $this->request->params['_Token']['key'], 'id' => 'Token' . mt_rand(),
  400. 'secure' => self::SECURE_SKIP
  401. ));
  402. if (!empty($this->request['_Token']['unlockedFields'])) {
  403. foreach ((array)$this->request['_Token']['unlockedFields'] as $unlocked) {
  404. $this->_unlockedFields[] = $unlocked;
  405. }
  406. }
  407. }
  408. if (!empty($append)) {
  409. $append = $this->Html->useTag('block', ' style="display:none;"', $append);
  410. }
  411. if ($model !== false) {
  412. $this->setEntity($model, true);
  413. }
  414. $attributes = sprintf('action="%s" ', $action) . $this->_parseAttributes($htmlAttributes, null, '');
  415. return $this->Html->useTag('form', $action, $attributes) . $append;
  416. }
  417. /**
  418. * Closes an HTML form, cleans up values set by FormHelper::create(), and writes hidden
  419. * input fields where appropriate.
  420. *
  421. * If $options is set a form submit button will be created. Options can be either a string or an array.
  422. *
  423. * {{{
  424. * array usage:
  425. *
  426. * array('label' => 'save'); value="save"
  427. * array('label' => 'save', 'name' => 'Whatever'); value="save" name="Whatever"
  428. * array('name' => 'Whatever'); value="Submit" name="Whatever"
  429. * array('label' => 'save', 'name' => 'Whatever', 'div' => 'good') <div class="good"> value="save" name="Whatever"
  430. * array('label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')); <div class="good"> value="save" name="Whatever"
  431. * }}}
  432. *
  433. * @param mixed $options as a string will use $options as the value of button,
  434. * @return string a closing FORM tag optional submit button.
  435. * @access public
  436. * @link http://book.cakephp.org/view/1389/Closing-the-Form
  437. */
  438. public function end($options = null) {
  439. $out = null;
  440. $submit = null;
  441. if ($options !== null) {
  442. $submitOptions = array();
  443. if (is_string($options)) {
  444. $submit = $options;
  445. } else {
  446. if (isset($options['label'])) {
  447. $submit = $options['label'];
  448. unset($options['label']);
  449. }
  450. $submitOptions = $options;
  451. }
  452. $out .= $this->submit($submit, $submitOptions);
  453. }
  454. if (isset($this->request['_Token']) && !empty($this->request['_Token'])) {
  455. $out .= $this->secure($this->fields);
  456. $this->fields = array();
  457. }
  458. $this->setEntity(null);
  459. $out .= $this->Html->useTag('formend');
  460. $this->_View->modelScope = false;
  461. return $out;
  462. }
  463. /**
  464. * Generates a hidden field with a security hash based on the fields used in the form.
  465. *
  466. * @param array $fields The list of fields to use when generating the hash
  467. * @return string A hidden input field with a security hash
  468. */
  469. public function secure($fields = array()) {
  470. if (!isset($this->request['_Token']) || empty($this->request['_Token'])) {
  471. return;
  472. }
  473. $locked = array();
  474. $unlockedFields = $this->_unlockedFields;
  475. foreach ($fields as $key => $value) {
  476. if (!is_int($key)) {
  477. $locked[$key] = $value;
  478. unset($fields[$key]);
  479. }
  480. }
  481. sort($unlockedFields, SORT_STRING);
  482. sort($fields, SORT_STRING);
  483. ksort($locked, SORT_STRING);
  484. $fields += $locked;
  485. $locked = implode(array_keys($locked), '|');
  486. $unlocked = implode($unlockedFields, '|');
  487. $fields = Security::hash(serialize($fields) . $unlocked . Configure::read('Security.salt'));
  488. $out = $this->hidden('_Token.fields', array(
  489. 'value' => urlencode($fields . ':' . $locked),
  490. 'id' => 'TokenFields' . mt_rand()
  491. ));
  492. $out .= $this->hidden('_Token.unlocked', array(
  493. 'value' => urlencode($unlocked),
  494. 'id' => 'TokenUnlocked' . mt_rand()
  495. ));
  496. return $this->Html->useTag('block', ' style="display:none;"', $out);
  497. }
  498. /**
  499. * Add to or get the list of fields that are currently unlocked.
  500. * Unlocked fields are not included in the field hash used by SecurityComponent
  501. * unlocking a field once its been added to the list of secured fields will remove
  502. * it from the list of fields.
  503. *
  504. * @param string $name The dot separated name for the field.
  505. * @return mixed Either null, or the list of fields.
  506. */
  507. public function unlockField($name = null) {
  508. if ($name === null) {
  509. return $this->_unlockedFields;
  510. }
  511. if (!in_array($name, $this->_unlockedFields)) {
  512. $this->_unlockedFields[] = $name;
  513. }
  514. $index = array_search($name, $this->fields);
  515. if ($index !== false) {
  516. unset($this->fields[$index]);
  517. }
  518. unset($this->fields[$name]);
  519. }
  520. /**
  521. * Determine which fields of a form should be used for hash.
  522. * Populates $this->fields
  523. *
  524. * @param boolean $lock Whether this field should be part of the validation
  525. * or excluded as part of the unlockedFields.
  526. * @param mixed $field Reference to field to be secured
  527. * @param mixed $value Field value, if value should not be tampered with.
  528. * @return void
  529. */
  530. protected function __secure($lock, $field = null, $value = null) {
  531. if (!$field) {
  532. $field = $this->entity();
  533. } elseif (is_string($field)) {
  534. $field = Set::filter(explode('.', $field), true);
  535. }
  536. foreach ($this->_unlockedFields as $unlockField) {
  537. $unlockParts = explode('.', $unlockField);
  538. if (array_values(array_intersect($field, $unlockParts)) === $unlockParts) {
  539. return;
  540. }
  541. }
  542. $field = implode('.', $field);
  543. if ($lock) {
  544. if (!in_array($field, $this->fields)) {
  545. if ($value !== null) {
  546. return $this->fields[$field] = $value;
  547. }
  548. $this->fields[] = $field;
  549. }
  550. } else {
  551. $this->unlockField($field);
  552. }
  553. }
  554. /**
  555. * Returns true if there is an error for the given field, otherwise false
  556. *
  557. * @param string $field This should be "Modelname.fieldname"
  558. * @return boolean If there are errors this method returns true, else false.
  559. * @access public
  560. * @link http://book.cakephp.org/view/1426/isFieldError
  561. */
  562. public function isFieldError($field) {
  563. $this->setEntity($field);
  564. return (bool)$this->tagIsInvalid();
  565. }
  566. /**
  567. * Returns a formatted error message for given FORM field, NULL if no errors.
  568. *
  569. * ### Options:
  570. *
  571. * - `escape` bool Whether or not to html escape the contents of the error.
  572. * - `wrap` mixed Whether or not the error message should be wrapped in a div. If a
  573. * string, will be used as the HTML tag to use.
  574. * - `class` string The classname for the error message
  575. *
  576. * @param string $field A field name, like "Modelname.fieldname"
  577. * @param mixed $text Error message as string or array of messages.
  578. * If array contains `attributes` key it will be used as options for error container
  579. * @param array $options Rendering options for <div /> wrapper tag
  580. * @return string If there are errors this method returns an error message, otherwise null.
  581. * @access public
  582. * @link http://book.cakephp.org/view/1423/error
  583. */
  584. public function error($field, $text = null, $options = array()) {
  585. $defaults = array('wrap' => true, 'class' => 'error-message', 'escape' => true);
  586. $options = array_merge($defaults, $options);
  587. $this->setEntity($field);
  588. if (!$error = $this->tagIsInvalid()) {
  589. return null;
  590. }
  591. if (is_array($text)) {
  592. if (isset($text['attributes']) && is_array($text['attributes'])) {
  593. $options = array_merge($options, $text['attributes']);
  594. unset($text['attributes']);
  595. }
  596. $tmp = array();
  597. foreach ($error as &$e) {
  598. if (isset($text[$e])) {
  599. $tmp []= $text[$e];
  600. } else {
  601. $tmp []= $e;
  602. }
  603. }
  604. $text = $tmp;
  605. }
  606. if ($text != null) {
  607. $error = $text;
  608. }
  609. if (is_array($error)) {
  610. foreach ($error as &$e) {
  611. if (is_numeric($e)) {
  612. $e = __d('cake', 'Error in field %s', Inflector::humanize($this->field()));
  613. }
  614. }
  615. }
  616. if ($options['escape']) {
  617. $error = h($error);
  618. unset($options['escape']);
  619. }
  620. if (is_array($error)) {
  621. if (count($error) > 1) {
  622. $listParams = array();
  623. if (isset($options['listOptions'])) {
  624. if (is_string($options['listOptions'])) {
  625. $listParams []= $options['listOptions'];
  626. } else {
  627. if (isset($options['listOptions']['itemOptions'])) {
  628. $listParams []= $options['listOptions']['itemOptions'];
  629. unset($options['listOptions']['itemOptions']);
  630. } else {
  631. $listParams []= array();
  632. }
  633. if (isset($options['listOptions']['tag'])) {
  634. $listParams []= $options['listOptions']['tag'];
  635. unset($options['listOptions']['tag']);
  636. }
  637. array_unshift($listParams, $options['listOptions']);
  638. }
  639. unset($options['listOptions']);
  640. }
  641. array_unshift($listParams, $error);
  642. $error = call_user_func_array(array($this->Html, 'nestedList'), $listParams);
  643. } else {
  644. $error = array_pop($error);
  645. }
  646. }
  647. if ($options['wrap']) {
  648. $tag = is_string($options['wrap']) ? $options['wrap'] : 'div';
  649. unset($options['wrap']);
  650. return $this->Html->tag($tag, $error, $options);
  651. } else {
  652. return $error;
  653. }
  654. }
  655. /**
  656. * Returns a formatted LABEL element for HTML FORMs. Will automatically generate
  657. * a for attribute if one is not provided.
  658. *
  659. * @param string $fieldName This should be "Modelname.fieldname"
  660. * @param string $text Text that will appear in the label field.
  661. * @param mixed $options An array of HTML attributes, or a string, to be used as a class name.
  662. * @return string The formatted LABEL element
  663. * @link http://book.cakephp.org/view/1427/label
  664. */
  665. public function label($fieldName = null, $text = null, $options = array()) {
  666. if (empty($fieldName)) {
  667. $fieldName = implode('.', $this->entity());
  668. }
  669. if ($text === null) {
  670. if (strpos($fieldName, '.') !== false) {
  671. $fieldElements = explode('.', $fieldName);
  672. $text = array_pop($fieldElements);
  673. } else {
  674. $text = $fieldName;
  675. }
  676. if (substr($text, -3) == '_id') {
  677. $text = substr($text, 0, strlen($text) - 3);
  678. }
  679. $text = __d('cake', Inflector::humanize(Inflector::underscore($text)));
  680. }
  681. if (is_string($options)) {
  682. $options = array('class' => $options);
  683. }
  684. if (isset($options['for'])) {
  685. $labelFor = $options['for'];
  686. unset($options['for']);
  687. } else {
  688. $labelFor = $this->domId($fieldName);
  689. }
  690. return $this->Html->useTag('label', $labelFor, $options, $text);
  691. }
  692. /**
  693. * Generate a set of inputs for `$fields`. If $fields is null the current model
  694. * will be used.
  695. *
  696. * In addition to controller fields output, `$fields` can be used to control legend
  697. * and fieldset rendering with the `fieldset` and `legend` keys.
  698. * `$form->inputs(array('legend' => 'My legend'));` Would generate an input set with
  699. * a custom legend. You can customize individual inputs through `$fields` as well.
  700. *
  701. * {{{
  702. * $form->inputs(array(
  703. * 'name' => array('label' => 'custom label')
  704. * ));
  705. * }}}
  706. *
  707. * In addition to fields control, inputs() allows you to use a few additional options.
  708. *
  709. * - `fieldset` Set to false to disable the fieldset. If a string is supplied it will be used as
  710. * the classname for the fieldset element.
  711. * - `legend` Set to false to disable the legend for the generated input set. Or supply a string
  712. * to customize the legend text.
  713. *
  714. * @param mixed $fields An array of fields to generate inputs for, or null.
  715. * @param array $blacklist a simple array of fields to not create inputs for.
  716. * @return string Completed form inputs.
  717. */
  718. public function inputs($fields = null, $blacklist = null) {
  719. $fieldset = $legend = true;
  720. $model = $this->model();
  721. if (is_array($fields)) {
  722. if (array_key_exists('legend', $fields)) {
  723. $legend = $fields['legend'];
  724. unset($fields['legend']);
  725. }
  726. if (isset($fields['fieldset'])) {
  727. $fieldset = $fields['fieldset'];
  728. unset($fields['fieldset']);
  729. }
  730. } elseif ($fields !== null) {
  731. $fieldset = $legend = $fields;
  732. if (!is_bool($fieldset)) {
  733. $fieldset = true;
  734. }
  735. $fields = array();
  736. }
  737. if (empty($fields)) {
  738. $fields = array_keys($this->_introspectModel($model, 'fields'));
  739. }
  740. if ($legend === true) {
  741. $actionName = __d('cake', 'New %s');
  742. $isEdit = (
  743. strpos($this->request->params['action'], 'update') !== false ||
  744. strpos($this->request->params['action'], 'edit') !== false
  745. );
  746. if ($isEdit) {
  747. $actionName = __d('cake', 'Edit %s');
  748. }
  749. $modelName = Inflector::humanize(Inflector::underscore($model));
  750. $legend = sprintf($actionName, __($modelName));
  751. }
  752. $out = null;
  753. foreach ($fields as $name => $options) {
  754. if (is_numeric($name) && !is_array($options)) {
  755. $name = $options;
  756. $options = array();
  757. }
  758. $entity = explode('.', $name);
  759. $blacklisted = (
  760. is_array($blacklist) &&
  761. (in_array($name, $blacklist) || in_array(end($entity), $blacklist))
  762. );
  763. if ($blacklisted) {
  764. continue;
  765. }
  766. $out .= $this->input($name, $options);
  767. }
  768. if (is_string($fieldset)) {
  769. $fieldsetClass = sprintf(' class="%s"', $fieldset);
  770. } else {
  771. $fieldsetClass = '';
  772. }
  773. if ($fieldset && $legend) {
  774. return $this->Html->useTag('fieldset', $fieldsetClass, $this->Html->useTag('legend', $legend) . $out);
  775. } elseif ($fieldset) {
  776. return $this->Html->useTag('fieldset', $fieldsetClass, $out);
  777. } else {
  778. return $out;
  779. }
  780. }
  781. /**
  782. * Generates a form input element complete with label and wrapper div
  783. *
  784. * ### Options
  785. *
  786. * See each field type method for more information. Any options that are part of
  787. * $attributes or $options for the different **type** methods can be included in `$options` for input().i
  788. * Additionally, any unknown keys that are not in the list below, or part of the selected type's options
  789. * will be treated as a regular html attribute for the generated input.
  790. *
  791. * - `type` - Force the type of widget you want. e.g. `type => 'select'`
  792. * - `label` - Either a string label, or an array of options for the label. See FormHelper::label()
  793. * - `div` - Either `false` to disable the div, or an array of options for the div.
  794. * See HtmlHelper::div() for more options.
  795. * - `options` - for widgets that take options e.g. radio, select
  796. * - `error` - control the error message that is produced
  797. * - `empty` - String or boolean to enable empty select box options.
  798. * - `before` - Content to place before the label + input.
  799. * - `after` - Content to place after the label + input.
  800. * - `between` - Content to place between the label + input.
  801. * - `format` - format template for element order. Any element that is not in the array, will not be in the output.
  802. * - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error')
  803. * - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error')
  804. * - Hidden input will not be formatted
  805. * - Radio buttons cannot have the order of input and label elements controlled with these settings.
  806. *
  807. * @param string $fieldName This should be "Modelname.fieldname"
  808. * @param array $options Each type of input takes different options.
  809. * @return string Completed form widget.
  810. * @access public
  811. * @link http://book.cakephp.org/view/1390/Automagic-Form-Elements
  812. */
  813. public function input($fieldName, $options = array()) {
  814. $this->setEntity($fieldName);
  815. $options = array_merge(
  816. array('before' => null, 'between' => null, 'after' => null, 'format' => null),
  817. $this->_inputDefaults,
  818. $options
  819. );
  820. $modelKey = $this->model();
  821. $fieldKey = $this->field();
  822. if (!isset($options['type'])) {
  823. $magicType = true;
  824. $options['type'] = 'text';
  825. if (isset($options['options'])) {
  826. $options['type'] = 'select';
  827. } elseif (in_array($fieldKey, array('psword', 'passwd', 'password'))) {
  828. $options['type'] = 'password';
  829. } elseif (isset($options['checked'])) {
  830. $options['type'] = 'checkbox';
  831. } elseif ($fieldDef = $this->_introspectModel($modelKey, 'fields', $fieldKey)) {
  832. $type = $fieldDef['type'];
  833. $primaryKey = $this->fieldset[$modelKey]['key'];
  834. }
  835. if (isset($type)) {
  836. $map = array(
  837. 'string' => 'text', 'datetime' => 'datetime',
  838. 'boolean' => 'checkbox', 'timestamp' => 'datetime',
  839. 'text' => 'textarea', 'time' => 'time',
  840. 'date' => 'date', 'float' => 'text'
  841. );
  842. if (isset($this->map[$type])) {
  843. $options['type'] = $this->map[$type];
  844. } elseif (isset($map[$type])) {
  845. $options['type'] = $map[$type];
  846. }
  847. if ($fieldKey == $primaryKey) {
  848. $options['type'] = 'hidden';
  849. }
  850. }
  851. if (preg_match('/_id$/', $fieldKey) && $options['type'] !== 'hidden') {
  852. $options['type'] = 'select';
  853. }
  854. if ($modelKey === $fieldKey) {
  855. $options['type'] = 'select';
  856. if (!isset($options['multiple'])) {
  857. $options['multiple'] = 'multiple';
  858. }
  859. }
  860. }
  861. $types = array('checkbox', 'radio', 'select');
  862. if (
  863. (!isset($options['options']) && in_array($options['type'], $types)) ||
  864. (isset($magicType) && $options['type'] == 'text')
  865. ) {
  866. $varName = Inflector::variable(
  867. Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey))
  868. );
  869. $varOptions = $this->_View->getVar($varName);
  870. if (is_array($varOptions)) {
  871. if ($options['type'] !== 'radio') {
  872. $options['type'] = 'select';
  873. }
  874. $options['options'] = $varOptions;
  875. }
  876. }
  877. $autoLength = (!array_key_exists('maxlength', $options) && isset($fieldDef['length']));
  878. if ($autoLength && $options['type'] == 'text') {
  879. $options['maxlength'] = $fieldDef['length'];
  880. }
  881. if ($autoLength && $fieldDef['type'] == 'float') {
  882. $options['maxlength'] = array_sum(explode(',', $fieldDef['length']))+1;
  883. }
  884. $divOptions = array();
  885. $div = $this->_extractOption('div', $options, true);
  886. unset($options['div']);
  887. if (!empty($div)) {
  888. $divOptions['class'] = 'input';
  889. $divOptions = $this->addClass($divOptions, $options['type']);
  890. if (is_string($div)) {
  891. $divOptions['class'] = $div;
  892. } elseif (is_array($div)) {
  893. $divOptions = array_merge($divOptions, $div);
  894. }
  895. if ($this->_introspectModel($modelKey, 'validates', $fieldKey)) {
  896. $divOptions = $this->addClass($divOptions, 'required');
  897. }
  898. if (!isset($divOptions['tag'])) {
  899. $divOptions['tag'] = 'div';
  900. }
  901. }
  902. $label = null;
  903. if (isset($options['label']) && $options['type'] !== 'radio') {
  904. $label = $options['label'];
  905. unset($options['label']);
  906. }
  907. if ($options['type'] === 'radio') {
  908. $label = false;
  909. if (isset($options['options'])) {
  910. $radioOptions = (array)$options['options'];
  911. unset($options['options']);
  912. }
  913. }
  914. if ($label !== false) {
  915. $label = $this->_inputLabel($fieldName, $label, $options);
  916. }
  917. $error = $this->_extractOption('error', $options, null);
  918. unset($options['error']);
  919. $selected = $this->_extractOption('selected', $options, null);
  920. unset($options['selected']);
  921. if (isset($options['rows']) || isset($options['cols'])) {
  922. $options['type'] = 'textarea';
  923. }
  924. if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time' || $options['type'] === 'select') {
  925. $options += array('empty' => false);
  926. }
  927. if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') {
  928. $dateFormat = $this->_extractOption('dateFormat', $options, 'MDY');
  929. $timeFormat = $this->_extractOption('timeFormat', $options, 12);
  930. unset($options['dateFormat'], $options['timeFormat']);
  931. }
  932. $type = $options['type'];
  933. $out = array_merge(
  934. array('before' => null, 'label' => null, 'between' => null, 'input' => null, 'after' => null, 'error' => null),
  935. array('before' => $options['before'], 'label' => $label, 'between' => $options['between'], 'after' => $options['after'])
  936. );
  937. $format = null;
  938. if (is_array($options['format']) && in_array('input', $options['format'])) {
  939. $format = $options['format'];
  940. }
  941. unset($options['type'], $options['before'], $options['between'], $options['after'], $options['format']);
  942. switch ($type) {
  943. case 'hidden':
  944. $input = $this->hidden($fieldName, $options);
  945. $format = array('input');
  946. unset($divOptions);
  947. break;
  948. case 'checkbox':
  949. $input = $this->checkbox($fieldName, $options);
  950. $format = $format ? $format : array('before', 'input', 'between', 'label', 'after', 'error');
  951. break;
  952. case 'radio':
  953. $input = $this->radio($fieldName, $radioOptions, $options);
  954. break;
  955. case 'file':
  956. $input = $this->file($fieldName, $options);
  957. break;
  958. case 'select':
  959. $options += array('options' => array(), 'value' => $selected);
  960. $list = $options['options'];
  961. unset($options['options']);
  962. $input = $this->select($fieldName, $list, $options);
  963. break;
  964. case 'time':
  965. $options['value'] = $selected;
  966. $input = $this->dateTime($fieldName, null, $timeFormat, $options);
  967. break;
  968. case 'date':
  969. $options['value'] = $selected;
  970. $input = $this->dateTime($fieldName, $dateFormat, null, $options);
  971. break;
  972. case 'datetime':
  973. $options['value'] = $selected;
  974. $input = $this->dateTime($fieldName, $dateFormat, $timeFormat, $options);
  975. break;
  976. case 'textarea':
  977. $input = $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
  978. break;
  979. default:
  980. $input = $this->{$type}($fieldName, $options);
  981. }
  982. if ($type != 'hidden' && $error !== false) {
  983. $errMsg = $this->error($fieldName, $error);
  984. if ($errMsg) {
  985. $divOptions = $this->addClass($divOptions, 'error');
  986. $out['error'] = $errMsg;
  987. }
  988. }
  989. $out['input'] = $input;
  990. $format = $format ? $format : array('before', 'label', 'between', 'input', 'after', 'error');
  991. $output = '';
  992. foreach ($format as $element) {
  993. $output .= $out[$element];
  994. unset($out[$element]);
  995. }
  996. if (!empty($divOptions['tag'])) {
  997. $tag = $divOptions['tag'];
  998. unset($divOptions['tag']);
  999. $output = $this->Html->tag($tag, $output, $divOptions);
  1000. }
  1001. return $output;
  1002. }
  1003. /**
  1004. * Extracts a single option from an options array.
  1005. *
  1006. * @param string $name The name of the option to pull out.
  1007. * @param array $options The array of options you want to extract.
  1008. * @param mixed $default The default option value
  1009. * @return the contents of the option or default
  1010. */
  1011. protected function _extractOption($name, $options, $default = null) {
  1012. if (array_key_exists($name, $options)) {
  1013. return $options[$name];
  1014. }
  1015. return $default;
  1016. }
  1017. /**
  1018. * Generate a label for an input() call.
  1019. *
  1020. * @param array $options Options for the label element.
  1021. * @return string Generated label element
  1022. */
  1023. protected function _inputLabel($fieldName, $label, $options) {
  1024. $labelAttributes = $this->domId(array(), 'for');
  1025. if ($options['type'] === 'date' || $options['type'] === 'datetime') {
  1026. if (isset($options['dateFormat']) && $options['dateFormat'] === 'NONE') {
  1027. $labelAttributes['for'] .= 'Hour';
  1028. $idKey = 'hour';
  1029. } else {
  1030. $labelAttributes['for'] .= 'Month';
  1031. $idKey = 'month';
  1032. }
  1033. if (isset($options['id']) && isset($options['id'][$idKey])) {
  1034. $labelAttributes['for'] = $options['id'][$idKey];
  1035. }
  1036. } elseif ($options['type'] === 'time') {
  1037. $labelAttributes['for'] .= 'Hour';
  1038. if (isset($options['id']) && isset($options['id']['hour'])) {
  1039. $labelAttributes['for'] = $options['id']['hour'];
  1040. }
  1041. }
  1042. if (is_array($label)) {
  1043. $labelText = null;
  1044. if (isset($label['text'])) {
  1045. $labelText = $label['text'];
  1046. unset($label['text']);
  1047. }
  1048. $labelAttributes = array_merge($labelAttributes, $label);
  1049. } else {
  1050. $labelText = $label;
  1051. }
  1052. if (isset($options['id']) && is_string($options['id'])) {
  1053. $labelAttributes = array_merge($labelAttributes, array('for' => $options['id']));
  1054. }
  1055. return $this->label($fieldName, $labelText, $labelAttributes);
  1056. }
  1057. /**
  1058. * Creates a checkbox input widget.
  1059. *
  1060. * ### Options:
  1061. *
  1062. * - `value` - the value of the checkbox
  1063. * - `checked` - boolean indicate that this checkbox is checked.
  1064. * - `hiddenField` - boolean to indicate if you want the results of checkbox() to include
  1065. * a hidden input with a value of ''.
  1066. * - `disabled` - create a disabled input.
  1067. * - `default` - Set the default value for the checkbox. This allows you to start checkboxes
  1068. * as checked, without having to check the POST data. A matching POST data value, will overwrite
  1069. * the default value.
  1070. *
  1071. * @param string $fieldName Name of a field, like this "Modelname.fieldname"
  1072. * @param array $options Array of HTML attributes.
  1073. * @return string An HTML text input element.
  1074. * @access public
  1075. * @link http://book.cakephp.org/view/1414/checkbox
  1076. */
  1077. public function checkbox($fieldName, $options = array()) {
  1078. $valueOptions = array();
  1079. if(isset($options['default'])){
  1080. $valueOptions['default'] = $options['default'];
  1081. unset($options['default']);
  1082. }
  1083. $options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true);
  1084. $value = current($this->value($valueOptions));
  1085. $output = "";
  1086. if (empty($options['value'])) {
  1087. $options['value'] = 1;
  1088. }
  1089. if (
  1090. (!isset($options['checked']) && !empty($value) && $value == $options['value']) ||
  1091. !empty($options['checked'])
  1092. ) {
  1093. $options['checked'] = 'checked';
  1094. }
  1095. if ($options['hiddenField']) {
  1096. $hiddenOptions = array(
  1097. 'id' => $options['id'] . '_', 'name' => $options['name'],
  1098. 'value' => '0', 'secure' => false
  1099. );
  1100. if (isset($options['disabled']) && $options['disabled'] == true) {
  1101. $hiddenOptions['disabled'] = 'disabled';
  1102. }
  1103. $output = $this->hidden($fieldName, $hiddenOptions);
  1104. }
  1105. unset($options['hiddenField']);
  1106. return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => '')));
  1107. }
  1108. /**
  1109. * Creates a set of radio widgets. Will create a legend and fieldset
  1110. * by default. Use $options to control this
  1111. *
  1112. * ### Attributes:
  1113. *
  1114. * - `separator` - define the string in between the radio buttons
  1115. * - `legend` - control whether or not the widget set has a fieldset & legend
  1116. * - `value` - indicate a value that is should be checked
  1117. * - `label` - boolean to indicate whether or not labels for widgets show be displayed
  1118. * - `hiddenField` - boolean to indicate if you want the results of radio() to include
  1119. * a hidden input with a value of ''. This is useful for creating radio sets that non-continuous
  1120. *
  1121. * @param string $fieldName Name of a field, like this "Modelname.fieldname"
  1122. * @param array $options Radio button options array.
  1123. * @param array $attributes Array of HTML attributes, and special attributes above.
  1124. * @return string Completed radio widget set.
  1125. * @access public
  1126. * @link http://book.cakephp.org/view/1429/radio
  1127. */
  1128. public function radio($fieldName, $options = array(), $attributes = array()) {
  1129. $attributes = $this->_initInputField($fieldName, $attributes);
  1130. $legend = false;
  1131. $disabled = array();
  1132. if (isset($attributes['legend'])) {
  1133. $legend = $attributes['legend'];
  1134. unset($attributes['legend']);
  1135. } elseif (count($options) > 1) {
  1136. $legend = __(Inflector::humanize($this->field()));
  1137. }
  1138. $label = true;
  1139. if (isset($attributes['label'])) {
  1140. $label = $attributes['label'];
  1141. unset($attributes['label']);
  1142. }
  1143. $inbetween = null;
  1144. if (isset($attributes['separator'])) {
  1145. $inbetween = $attributes['separator'];
  1146. unset($attributes['separator']);
  1147. }
  1148. if (isset($attributes['value'])) {
  1149. $value = $attributes['value'];
  1150. } else {
  1151. $value = $this->value($fieldName);
  1152. }
  1153. if (isset($attributes['disabled'])) {
  1154. $disabled = $attributes['disabled'];
  1155. }
  1156. $out = array();
  1157. $hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
  1158. unset($attributes['hiddenField']);
  1159. foreach ($options as $optValue => $optTitle) {
  1160. $optionsHere = array('value' => $optValue);
  1161. if (isset($value) && $optValue == $value) {
  1162. $optionsHere['checked'] = 'checked';
  1163. }
  1164. if (!empty($disabled) && in_array($optValue, $disabled)) {
  1165. $optionsHere['disabled'] = true;
  1166. }
  1167. $tagName = Inflector::camelize(
  1168. $attributes['id'] . '_' . Inflector::slug($optValue)
  1169. );
  1170. if ($label) {
  1171. $optTitle = $this->Html->useTag('label', $tagName, '', $optTitle);
  1172. }
  1173. $allOptions = array_merge($attributes, $optionsHere);
  1174. $out[] = $this->Html->useTag('radio', $attributes['name'], $tagName,
  1175. array_diff_key($allOptions, array('name' => '', 'type' => '', 'id' => '')),
  1176. $optTitle
  1177. );
  1178. }
  1179. $hidden = null;
  1180. if ($hiddenField) {
  1181. if (!isset($value) || $value === '') {
  1182. $hidden = $this->hidden($fieldName, array(
  1183. 'id' => $attributes['id'] . '_', 'value' => '', 'name' => $attributes['name']
  1184. ));
  1185. }
  1186. }
  1187. $out = $hidden . implode($inbetween, $out);
  1188. if ($legend) {
  1189. $out = $this->Html->useTag('fieldset', '', $this->Html->useTag('legend', $legend) . $out);
  1190. }
  1191. return $out;
  1192. }
  1193. /**
  1194. * Missing method handler - implements various simple input types. Is used to create inputs
  1195. * of various types. e.g. `$this->Form->text();` will create `<input type="text" />` while
  1196. * `$this->Form->range();` will create `<input type="range" />`
  1197. *
  1198. * ### Usage
  1199. *
  1200. * `$this->Form->search('User.query', array('value' => 'test'));`
  1201. *
  1202. * Will make an input like:
  1203. *
  1204. * `<input type="search" id="UserQuery" name="data[User][query]" value="test" />`
  1205. *
  1206. * The first argument to an input type should always be the fieldname, in `Model.field` format.
  1207. * The second argument should always be an array of attributes for the input.
  1208. *
  1209. * @param string $method Method name / input type to make.
  1210. * @param array $params Parameters for the method call
  1211. * @return string Formatted input method.
  1212. * @throws CakeException When there are no params for the method call.
  1213. */
  1214. public function __call($method, $params) {
  1215. $options = array();
  1216. if (empty($params)) {
  1217. throw new CakeException(__d('cake_dev', 'Missing field name for FormHelper::%s', $method));
  1218. }
  1219. if (isset($params[1])) {
  1220. $options = $params[1];
  1221. }
  1222. if (!isset($options['type'])) {
  1223. $options['type'] = $method;
  1224. }
  1225. $options = $this->_initInputField($params[0], $options);
  1226. return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => '')));
  1227. }
  1228. /**
  1229. * Creates a textarea widget.
  1230. *
  1231. * ### Options:
  1232. *
  1233. * - `escape` - Whether or not the contents of the textarea should be escaped. Defaults to true.
  1234. *
  1235. * @param string $fieldName Name of a field, in the form "Modelname.fieldname"
  1236. * @param array $options Array of HTML attributes, and special options above.
  1237. * @return string A generated HTML text input element
  1238. * @access public
  1239. * @link http://book.cakephp.org/view/1433/textarea
  1240. */
  1241. public function textarea($fieldName, $options = array()) {
  1242. $options = $this->_initInputField($fieldName, $options);
  1243. $value = null;
  1244. if (array_key_exists('value', $options)) {
  1245. $value = $options['value'];
  1246. if (!array_key_exists('escape', $options) || $options['escape'] !== false) {
  1247. $value = h($value);
  1248. }
  1249. unset($options['value']);
  1250. }
  1251. return $this->Html->useTag('textarea', $options['name'], array_diff_key($options, array('type' => '', 'name' => '')), $value);
  1252. }
  1253. /**
  1254. * Creates a hidden input field.
  1255. *
  1256. * @param string $fieldName Name of a field, in the form of "Modelname.fieldname"
  1257. * @param array $options Array of HTML attributes.
  1258. * @return string A generated hidden input
  1259. * @access public
  1260. * @link http://book.cakephp.org/view/1425/hidden
  1261. */
  1262. public function hidden($fieldName, $options = array()) {
  1263. $secure = true;
  1264. if (isset($options['secure'])) {
  1265. $secure = $options['secure'];
  1266. unset($options['secure']);
  1267. }
  1268. $options = $this->_initInputField($fieldName, array_merge(
  1269. $options, array('secure' => self::SECURE_SKIP)
  1270. ));
  1271. if ($secure && $secure !== self::SECURE_SKIP) {
  1272. $this->__secure(true, null, '' . $options['value']);
  1273. }
  1274. return $this->Html->useTag('hidden', $options['name'], array_diff_key($options, array('name' => '')));
  1275. }
  1276. /**
  1277. * Creates file input widget.
  1278. *
  1279. * @param string $fieldName Name of a field, in the form "Modelname.fieldname"
  1280. * @param array $options Array of HTML attributes.
  1281. * @return string A generated file input.
  1282. * @access public
  1283. * @link http://book.cakephp.org/view/1424/file
  1284. */
  1285. public function file($fieldName, $options = array()) {
  1286. $options += array('secure' => true);
  1287. $secure = $options['secure'];
  1288. $options['secure'] = self::SECURE_SKIP;
  1289. $options = $this->_initInputField($fieldName, $options);
  1290. $field = $this->entity();
  1291. foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) {
  1292. $this->__secure($secure, array_merge($field, array($suffix)));
  1293. }
  1294. return $this->Html->useTag('file', $options['name'], array_diff_key($options, array('name' => '')));
  1295. }
  1296. /**
  1297. * Creates a `<button>` tag. The type attribute defaults to `type="submit"`
  1298. * You can change it to a different value by using `$options['type']`.
  1299. *
  1300. * ### Options:
  1301. *
  1302. * - `escape` - HTML entity encode the $title of the button. Defaults to false.
  1303. *
  1304. * @param string $title The button's caption. Not automatically HTML encoded
  1305. * @param array $options Array of options and HTML attributes.
  1306. * @return string A HTML button tag.
  1307. * @access public
  1308. * @link http://book.cakephp.org/view/1415/button
  1309. */
  1310. public function button($title, $options = array()) {
  1311. $options += array('type' => 'submit', 'escape' => false, 'secure' => false);
  1312. if ($options['escape']) {
  1313. $title = h($title);
  1314. }
  1315. if (isset($options['name'])) {
  1316. $this->__secure($options['secure'], $options['name']);
  1317. }
  1318. return $this->Html->useTag('button', $options['type'], array_diff_key($options, array('type' => '')), $title);
  1319. }
  1320. /**
  1321. * Create a `<button>` tag with a surrounding `<form>` that submits via POST.
  1322. *
  1323. * This method creates a `<form>` element. So do not use this method in some opened form.
  1324. * Instead use FormHelper::submit() or FormHelper::button() to create buttons inside opened forms.
  1325. *
  1326. * ### Options:
  1327. *
  1328. * - `data` - Array with key/value to pass in input hidden
  1329. * - Other options is the same of button method.
  1330. *
  1331. * @param string $title The button's caption. Not automatically HTML encoded
  1332. * @param mixed $url URL as string or array
  1333. * @param array $options Array of options and HTML attributes.
  1334. * @return string A HTML button tag.
  1335. */
  1336. public function postButton($title, $url, $options = array()) {
  1337. $out = $this->create(false, array('id' => false, 'url' => $url, 'style' => 'display:none;'));
  1338. if (isset($options['data']) && is_array($options['data'])) {
  1339. foreach ($options['data'] as $key => $value) {
  1340. $out .= $this->hidden($key, array('value' => $value, 'id' => false));
  1341. }
  1342. unset($options['data']);
  1343. }
  1344. $out .= $this->button($title, $options);
  1345. $out .= $this->end();
  1346. return $out;
  1347. }
  1348. /**
  1349. * Creates an HTML link, but access the url using method POST.
  1350. * Requires javascript to be enabled in browser.
  1351. *
  1352. * This method creates a `<form>` element. So do not use this method inside an existing form.
  1353. * Instead you should add a submit button using FormHelper::submit()
  1354. *
  1355. * ### Options:
  1356. *
  1357. * - `data` - Array with key/value to pass in input hidden
  1358. * - `confirm` - Can be used instead of $confirmMessage.
  1359. * - Other options is the same of HtmlHelper::link() method.
  1360. * - The option `onclick` will be replaced.
  1361. *
  1362. * @param string $title The content to be wrapped by <a> tags.
  1363. * @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
  1364. * @param array $options Array of HTML attributes.
  1365. * @param string $confirmMessage JavaScript confirmation message.
  1366. * @return string An `<a />` element.
  1367. */
  1368. public function postLink($title, $url = null, $options = array(), $confirmMessage = false) {
  1369. if (!empty($options['confirm'])) {
  1370. $confirmMessage = $options['confirm'];
  1371. unset($options['confirm']);
  1372. }
  1373. $formName = uniqid('post_');
  1374. $formUrl = $this->url($url);
  1375. $out = $this->Html->useTag('form', $formUrl, array('name' => $formName, 'id' => $formName, 'style' => 'display:none;', 'method' => 'post'));
  1376. $out .= $this->Html->useTag('block', ' style="display:none;"', $this->Html->useTag('hidden', '_method', ' value="POST"'));
  1377. if (isset($options['data']) && is_array($options['data'])) {
  1378. foreach ($options['data'] as $key => $value) {
  1379. $out .= $this->hidden($key, array('value' => $value, 'id' => false));
  1380. }
  1381. unset($options['data']);
  1382. }
  1383. $out .= $this->Html->useTag('formend');
  1384. $url = '#';
  1385. $onClick = 'document.' . $formName . '.submit();';
  1386. if ($confirmMessage) {
  1387. $confirmMessage = str_replace(array("'", '"'), array("\'", '\"'), $confirmMessage);
  1388. $options['onclick'] = "if (confirm('{$confirmMessage}')) { {$onClick} }";
  1389. } else {
  1390. $options['onclick'] = $onClick;
  1391. }
  1392. $options['onclick'] .= ' event.returnValue = false; return false;';
  1393. $out .= $this->Html->link($title, $url, $options);
  1394. return $out;
  1395. }
  1396. /**
  1397. * Creates a submit button element. This method will generate `<input />` elements that
  1398. * can be used to submit, and reset forms by using $options. image submits can be created by supplying an
  1399. * image path for $caption.
  1400. *
  1401. * ### Options
  1402. *
  1403. * - `div` - Include a wrapping div? Defaults to true. Accepts sub options similar to
  1404. * FormHelper::input().
  1405. * - `before` - Content to include before the input.
  1406. * - `after` - Content to include after the input.
  1407. * - `type` - Set to 'reset' for reset inputs. Defaults to 'submit'
  1408. * - Other attributes will be assigned to the input element.
  1409. *
  1410. * ### Options
  1411. *
  1412. * - `div` - Include a wrapping div? Defaults to true. Accepts sub options similar to
  1413. * FormHelper::input().
  1414. * - Other attributes will be assigned to the input element.
  1415. *
  1416. * @param string $caption The label appearing on the button OR if string contains :// or the
  1417. * extension .jpg, .jpe, .jpeg, .gif, .png use an image if the extension
  1418. * exists, AND the first character is /, image is relative to webroot,
  1419. * OR if the first character is not /, image is relative to webroot/img.
  1420. * @param array $options Array of options. See above.
  1421. * @return string A HTML submit button
  1422. * @access public
  1423. * @link http://book.cakephp.org/view/1431/submit
  1424. */
  1425. public function submit($caption = null, $options = array()) {
  1426. if (!is_string($caption) && empty($caption)) {
  1427. $caption = __d('cake', 'Submit');
  1428. }
  1429. $out = null;
  1430. $div = true;
  1431. if (isset($options['div'])) {
  1432. $div = $options['div'];
  1433. unset($options['div']);
  1434. }
  1435. $options += array('type' => 'submit', 'before' => null, 'after' => null, 'secure' => false);
  1436. $divOptions = array('tag' => 'div');
  1437. if ($div === true) {
  1438. $divOptions['class'] = 'submit';
  1439. } elseif ($div === false) {
  1440. unset($divOptions);
  1441. } elseif (is_string($div)) {
  1442. $divOptions['class'] = $div;
  1443. } elseif (is_array($div)) {
  1444. $divOptions = array_merge(array('class' => 'submit', 'tag' => 'div'), $div);
  1445. }
  1446. if (isset($options['name'])) {
  1447. $this->__secure($options['secure'], $options['name']);
  1448. }
  1449. unset($options['secure']);
  1450. $before = $options['before'];
  1451. $after = $options['after'];
  1452. unset($options['before'], $options['after']);
  1453. if (strpos($caption, '://') !== false) {
  1454. unset($options['type']);
  1455. $out .= $before . $this->Html->useTag('submitimage', $caption, $options) . $after;
  1456. } elseif (preg_match('/\.(jpg|jpe|jpeg|gif|png|ico)$/', $caption)) {
  1457. unset($options['type']);
  1458. if ($caption{0} !== '/') {
  1459. $url = $this->webroot(IMAGES_URL . $caption);
  1460. } else {
  1461. $caption = trim($caption, '/');
  1462. $url = $this->webroot($caption);
  1463. }
  1464. $out .= $before . $this->Html->useTag('submitimage', $url, $options) . $after;
  1465. } else {
  1466. $options['value'] = $caption;
  1467. $out .= $before . $this->Html->useTag('submit', $options) . $after;
  1468. }
  1469. if (isset($divOptions)) {
  1470. $tag = $divOptions['tag'];
  1471. unset($divOptions['tag']);
  1472. $out = $this->Html->tag($tag, $out, $divOptions);
  1473. }
  1474. return $out;
  1475. }
  1476. /**
  1477. * Returns a formatted SELECT element.
  1478. *
  1479. * ### Attributes:
  1480. *
  1481. * - `showParents` - If included in the array and set to true, an additional option element
  1482. * will be added for the parent of each option group. You can set an option with the same name
  1483. * and it's key will be used for the value of the option.
  1484. * - `multiple` - show a multiple select box. If set to 'checkbox' multiple checkboxes will be
  1485. * created instead.
  1486. * - `empty` - If true, the empty select option is shown. If a string,
  1487. * that string is displayed as the empty element.
  1488. * - `escape` - If true contents of options will be HTML entity encoded. Defaults to true.
  1489. * - `value` The selected value of the input.
  1490. * - `class` - When using multiple = checkbox the classname to apply to the divs. Defaults to 'checkbox'.
  1491. *
  1492. * ### Using options
  1493. *
  1494. * A simple array will create normal options:
  1495. *
  1496. * {{{
  1497. * $options = array(1 => 'one', 2 => 'two);
  1498. * $this->Form->select('Model.field', $options));
  1499. * }}}
  1500. *
  1501. * While a nested options array will create optgroups with options inside them.
  1502. * {{{
  1503. * $options = array(
  1504. * 1 => 'bill',
  1505. * 'fred' => array(
  1506. * 2 => 'fred',
  1507. * 3 => 'fred jr.'
  1508. * )
  1509. * );
  1510. * $this->Form->select('Model.field', $options);
  1511. * }}}
  1512. *
  1513. * In the above `2 => 'fred'` will not generate an option element. You should enable the `showParents`
  1514. * attribute to show the fred option.
  1515. *
  1516. * @param string $fieldName Name attribute of the SELECT
  1517. * @param array $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the
  1518. * SELECT element
  1519. * @param array $attributes The HTML attributes of the select element.
  1520. * @return string Formatted SELECT element
  1521. * @access public
  1522. * @link http://book.cakephp.org/view/1430/select
  1523. */
  1524. public function select($fieldName, $options = array(), $attributes = array()) {
  1525. $select = array();
  1526. $style = null;
  1527. $tag = null;
  1528. $attributes += array(
  1529. 'class' => null,
  1530. 'escape' => true,
  1531. 'secure' => true,
  1532. 'empty' => '',
  1533. 'showParents' => false,
  1534. 'hiddenField' => true
  1535. );
  1536. $escapeOptions = $this->_extractOption('escape', $attributes);
  1537. $secure = $this->_extractOption('secure', $attributes);
  1538. $showEmpty = $this->_extractOption('empty', $attributes);
  1539. $showParents = $this->_extractOption('showParents', $attributes);
  1540. $hiddenField = $this->_extractOption('hiddenField', $attributes);
  1541. unset($attributes['escape'], $attributes['secure'], $attributes['empty'], $attributes['showParents'], $attributes['hiddenField']);
  1542. $id = $this->_extractOption('id', $attributes);
  1543. $attributes = $this->_initInputField($fieldName, array_merge(
  1544. (array)$attributes, array('secure' => self::SECURE_SKIP)
  1545. ));
  1546. if (is_string($options) && isset($this->__options[$options])) {
  1547. $options = $this->__generateOptions($options);
  1548. } elseif (!is_array($options)) {
  1549. $options = array();
  1550. }
  1551. if (isset($attributes['type'])) {
  1552. unset($attributes['type']);
  1553. }
  1554. if (!isset($selected)) {
  1555. $selected = $attributes['value'];
  1556. }
  1557. if (!empty($attributes['multiple'])) {
  1558. $style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null;
  1559. $template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart';
  1560. $tag = $template;
  1561. if ($hiddenField) {
  1562. $hiddenAttributes = array(
  1563. 'value' => '',
  1564. 'id' => $attributes['id'] . ($style ? '' : '_'),
  1565. 'secure' => false,
  1566. 'name' => $attributes['name']
  1567. );
  1568. $select[] = $this->hidden(null, $hiddenAttributes);
  1569. }
  1570. } else {
  1571. $tag = 'selectstart';
  1572. }
  1573. if (!empty($tag) || isset($template)) {
  1574. if (!isset($secure) || $secure == true) {
  1575. $this->__secure(true);
  1576. }
  1577. $select[] = $this->Html->useTag($tag, $attributes['name'], array_diff_key($attributes, array('name' => '', 'value' => '')));
  1578. }
  1579. $emptyMulti = (
  1580. $showEmpty !== null && $showEmpty !== false && !(
  1581. empty($showEmpty) && (isset($attributes) &&
  1582. array_key_exists('multiple', $attributes))
  1583. )
  1584. );
  1585. if ($emptyMulti) {
  1586. $showEmpty = ($showEmpty === true) ? '' : $showEmpty;
  1587. $options = array_reverse($options, true);
  1588. $options[''] = $showEmpty;
  1589. $options = array_reverse($options, true);
  1590. }
  1591. if (!$id) {
  1592. $attributes['id'] = Inflector::camelize($attributes['id']);
  1593. }
  1594. $select = array_merge($select, $this->__selectOptions(
  1595. array_reverse($options, true),
  1596. array(),
  1597. $showParents,
  1598. array(
  1599. 'escape' => $escapeOptions,
  1600. 'style' => $style,
  1601. 'name' => $attributes['name'],
  1602. 'value' => $attributes['value'],
  1603. 'class' => $attributes['class'],
  1604. 'id' => $attributes['id']
  1605. )
  1606. ));
  1607. $template = ($style == 'checkbox') ? 'checkboxmultipleend' : 'selectend';
  1608. $select[] = $this->Html->useTag($template);
  1609. return implode("\n", $select);
  1610. }
  1611. /**
  1612. * Returns a SELECT element for days.
  1613. *
  1614. * ### Attributes:
  1615. *
  1616. * - `empty` - If true, the empty select option is shown. If a string,
  1617. * that string is displayed as the empty element.
  1618. * - `value` The selected value of the input.
  1619. *
  1620. * @param string $fieldName Prefix name for the SELECT element
  1621. * @param array $attributes HTML attributes for the select element
  1622. * @return string A generated day select box.
  1623. * @access public
  1624. * @link http://book.cakephp.org/view/1419/day
  1625. */
  1626. public function day($fieldName = null, $attributes = array()) {
  1627. $attributes += array('empty' => true, 'value' => null);
  1628. $attributes = $this->__dateTimeSelected('day', $fieldName, $attributes);
  1629. if (strlen($attributes['value']) > 2) {
  1630. $attributes['value'] = date('d', strtotime($attributes['value']));
  1631. } elseif ($attributes['value'] === false) {
  1632. $attributes['value'] = null;
  1633. }
  1634. return $this->select($fieldName . ".day", $this->__generateOptions('day'), $attributes);
  1635. }
  1636. /**
  1637. * Returns a SELECT element for years
  1638. *
  1639. * ### Attributes:
  1640. *
  1641. * - `empty` - If true, the empty select option is shown. If a string,
  1642. * that string is displayed as the empty element.
  1643. * - `orderYear` - Ordering of year values in select options.
  1644. * Possible values 'asc', 'desc'. Default 'desc'
  1645. * - `value` The selected value of the input.
  1646. *
  1647. * @param string $fieldName Prefix name for the SELECT element
  1648. * @param integer $minYear First year in sequence
  1649. * @param integer $maxYear Last year in sequence
  1650. * @param array $attributes Attribute array for the select elements.
  1651. * @return string Completed year select input
  1652. * @access public
  1653. * @link http://book.cakephp.org/view/1416/year
  1654. */
  1655. public function year($fieldName, $minYear = null, $maxYear = null, $attributes = array()) {
  1656. $attributes += array('empty' => true, 'value' => null);
  1657. if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
  1658. if (is_array($value)) {
  1659. extract($value);
  1660. $attributes['value'] = $year;
  1661. } else {
  1662. if (empty($value)) {
  1663. if (!$attributes['empty'] && !$maxYear) {
  1664. $attributes['value'] = 'now';
  1665. } elseif (!$attributes['empty'] && $maxYear && !$attributes['value']) {
  1666. $attributes['value'] = $maxYear;
  1667. }
  1668. } else {
  1669. $attributes['value'] = $value;
  1670. }
  1671. }
  1672. }
  1673. if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') {
  1674. $attributes['value'] = date('Y', strtotime($attributes['value']));
  1675. } elseif ($attributes['value'] === false) {
  1676. $attributes['value'] = null;
  1677. }
  1678. $yearOptions = array('min' => $minYear, 'max' => $maxYear, 'order' => 'desc');
  1679. if (isset($attributes['orderYear'])) {
  1680. $yearOptions['order'] = $attributes['orderYear'];
  1681. unset($attributes['orderYear']);
  1682. }
  1683. return $this->select(
  1684. $fieldName . '.year', $this->__generateOptions('year', $yearOptions),
  1685. $attributes
  1686. );
  1687. }
  1688. /**
  1689. * Returns a SELECT element for months.
  1690. *
  1691. * ### Attributes:
  1692. *
  1693. * - `monthNames` - If false, 2 digit numbers will be used instead of text.
  1694. * If a array, the given array will be used.
  1695. * - `empty` - If true, the empty select option is shown. If a string,
  1696. * that string is displayed as the empty element.
  1697. * - `value` The selected value of the input.
  1698. *
  1699. * @param string $fieldName Prefix name for the SELECT element
  1700. * @param array $attributes Attributes for the select element
  1701. * @return string A generated month select dropdown.
  1702. * @access public
  1703. * @link http://book.cakephp.org/view/1417/month
  1704. */
  1705. public function month($fieldName, $attributes = array()) {
  1706. $attributes += array('empty' => true, 'value' => null);
  1707. $attributes = $this->__dateTimeSelected('month', $fieldName, $attributes);
  1708. if (strlen($attributes['value']) > 2) {
  1709. $attributes['value'] = date('m', strtotime($attributes['value']));
  1710. } elseif ($attributes['value'] === false) {
  1711. $attributes['value'] = null;
  1712. }
  1713. $defaults = array('monthNames' => true);
  1714. $attributes = array_merge($defaults, (array) $attributes);
  1715. $monthNames = $attributes['monthNames'];
  1716. unset($attributes['monthNames']);
  1717. return $this->select(
  1718. $fieldName . ".month",
  1719. $this->__generateOptions('month', array('monthNames' => $monthNames)),
  1720. $attributes
  1721. );
  1722. }
  1723. /**
  1724. * Returns a SELECT element for hours.
  1725. *
  1726. * ### Attributes:
  1727. *
  1728. * - `empty` - If true, the empty select option is shown. If a string,
  1729. * that string is displayed as the empty element.
  1730. * - `value` The selected value of the input.
  1731. *
  1732. * @param string $fieldName Prefix name for the SELECT element
  1733. * @param boolean $format24Hours True for 24 hours format
  1734. * @param array $attributes List of HTML attributes
  1735. * @return string Completed hour select input
  1736. * @access public
  1737. * @link http://book.cakephp.org/view/1420/hour
  1738. */
  1739. public function hour($fieldName, $format24Hours = false, $attributes = array()) {
  1740. $attributes += array('empty' => true, 'value' => null);
  1741. $attributes = $this->__dateTimeSelected('hour', $fieldName, $attributes);
  1742. if (strlen($attributes['value']) > 2) {
  1743. if ($format24Hours) {
  1744. $attributes['value'] = date('H', strtotime($attributes['value']));
  1745. } else {
  1746. $attributes['value'] = date('g', strtotime($attributes['value']));
  1747. }
  1748. } elseif ($attributes['value'] === false) {
  1749. $attributes['value'] = null;
  1750. }
  1751. return $this->select(
  1752. $fieldName . ".hour",
  1753. $this->__generateOptions($format24Hours ? 'hour24' : 'hour'),
  1754. $attributes
  1755. );
  1756. }
  1757. /**
  1758. * Returns a SELECT element for minutes.
  1759. *
  1760. * ### Attributes:
  1761. *
  1762. * - `empty` - If true, the empty select option is shown. If a string,
  1763. * that string is displayed as the empty element.
  1764. * - `value` The selected value of the input.
  1765. *
  1766. * @param string $fieldName Prefix name for the SELECT element
  1767. * @param string $attributes Array of Attributes
  1768. * @return string Completed minute select input.
  1769. * @access public
  1770. * @link http://book.cakephp.org/view/1421/minute
  1771. */
  1772. public function minute($fieldName, $attributes = array()) {
  1773. $attributes += array('empty' => true, 'value' => null);
  1774. $attributes = $this->__dateTimeSelected('min', $fieldName, $attributes);
  1775. if (strlen($attributes['value']) > 2) {
  1776. $attributes['value'] = date('i', strtotime($attributes['value']));
  1777. } elseif ($attributes['value'] === false) {
  1778. $attributes['value'] = null;
  1779. }
  1780. $minuteOptions = array();
  1781. if (isset($attributes['interval'])) {
  1782. $minuteOptions['interval'] = $attributes['interval'];
  1783. unset($attributes['interval']);
  1784. }
  1785. return $this->select(
  1786. $fieldName . ".min", $this->__generateOptions('minute', $minuteOptions),
  1787. $attributes
  1788. );
  1789. }
  1790. /**
  1791. * Selects values for dateTime selects.
  1792. *
  1793. * @param string $select Name of element field. ex. 'day'
  1794. * @param string $fieldName Name of fieldName being generated ex. Model.created
  1795. * @param array $attributes Array of attributes, must contain 'empty' key.
  1796. * @return array Attributes array with currently selected value.
  1797. * @access private
  1798. */
  1799. function __dateTimeSelected($select, $fieldName, $attributes) {
  1800. if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
  1801. if (is_array($value) && isset($value[$select])) {
  1802. $attributes['value'] = $value[$select];
  1803. } else {
  1804. if (empty($value)) {
  1805. if (!$attributes['empty']) {
  1806. $attributes['value'] = 'now';
  1807. }
  1808. } else {
  1809. $attributes['value'] = $value;
  1810. }
  1811. }
  1812. }
  1813. return $attributes;
  1814. }
  1815. /**
  1816. * Returns a SELECT element for AM or PM.
  1817. *
  1818. * ### Attributes:
  1819. *
  1820. * - `empty` - If true, the empty select option is shown. If a string,
  1821. * that string is displayed as the empty element.
  1822. * - `value` The selected value of the input.
  1823. *
  1824. * @param string $fieldName Prefix name for the SELECT element
  1825. * @param string $attributes Array of Attributes
  1826. * @param bool $showEmpty Show/Hide an empty option
  1827. * @return string Completed meridian select input
  1828. * @access public
  1829. * @link http://book.cakephp.org/view/1422/meridian
  1830. */
  1831. public function meridian($fieldName, $attributes = array()) {
  1832. $attributes += array('empty' => true, 'value' => null);
  1833. if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
  1834. if (is_array($value)) {
  1835. extract($value);
  1836. $attributes['value'] = $meridian;
  1837. } else {
  1838. if (empty($value)) {
  1839. if (!$attributes['empty']) {
  1840. $attributes['value'] = date('a');
  1841. }
  1842. } else {
  1843. $attributes['value'] = date('a', strtotime($value));
  1844. }
  1845. }
  1846. }
  1847. if ($attributes['value'] === false) {
  1848. $attributes['value'] = null;
  1849. }
  1850. return $this->select(
  1851. $fieldName . ".meridian", $this->__generateOptions('meridian'),
  1852. $attributes
  1853. );
  1854. }
  1855. /**
  1856. * Returns a set of SELECT elements for a full datetime setup: day, month and year, and then time.
  1857. *
  1858. * ### Attributes:
  1859. *
  1860. * - `monthNames` If false, 2 digit numbers will be used instead of text.
  1861. * If a array, the given array will be used.
  1862. * - `minYear` The lowest year to use in the year select
  1863. * - `maxYear` The maximum year to use in the year select
  1864. * - `interval` The interval for the minutes select. Defaults to 1
  1865. * - `separator` The contents of the string between select elements. Defaults to '-'
  1866. * - `empty` - If true, the empty select option is shown. If a string,
  1867. * that string is displayed as the empty element.
  1868. * - `value` | `default` The default value to be used by the input. A value in `$this->data`
  1869. * matching the field name will override this value. If no default is provided `time()` will be used.
  1870. *
  1871. * @param string $fieldName Prefix name for the SELECT element
  1872. * @param string $dateFormat DMY, MDY, YMD.
  1873. * @param string $timeFormat 12, 24.
  1874. * @param string $attributes array of Attributes
  1875. * @return string Generated set of select boxes for the date and time formats chosen.
  1876. * @access public
  1877. * @link http://book.cakephp.org/view/1418/dateTime
  1878. */
  1879. public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $attributes = array()) {
  1880. $attributes += array('empty' => true, 'value' => null);
  1881. $year = $month = $day = $hour = $min = $meridian = null;
  1882. if (empty($attributes['value'])) {
  1883. $attributes = $this->value($attributes, $fieldName);
  1884. }
  1885. if ($attributes['value'] === null && $attributes['empty'] != true) {
  1886. $attributes['value'] = time();
  1887. }
  1888. if (!empty($attributes['value'])) {
  1889. if (is_array($attributes['value'])) {
  1890. extract($attributes['value']);
  1891. } else {
  1892. if (is_numeric($attributes['value'])) {
  1893. $attributes['value'] = strftime('%Y-%m-%d %H:%M:%S', $attributes['value']);
  1894. }
  1895. $meridian = 'am';
  1896. $pos = strpos($attributes['value'], '-');
  1897. if ($pos !== false) {
  1898. $date = explode('-', $attributes['value']);
  1899. $days = explode(' ', $date[2]);
  1900. $day = $days[0];
  1901. $month = $date[1];
  1902. $year = $date[0];
  1903. } else {
  1904. $days[1] = $attributes['value'];
  1905. }
  1906. if (!empty($timeFormat)) {
  1907. $time = explode(':', $days[1]);
  1908. if (($time[0] > 12) && $timeFormat == '12') {
  1909. $time[0] = $time[0] - 12;
  1910. $meridian = 'pm';
  1911. } elseif ($time[0] == '00' && $timeFormat == '12') {
  1912. $time[0] = 12;
  1913. } elseif ($time[0] >= 12) {
  1914. $meridian = 'pm';
  1915. }
  1916. if ($time[0] == 0 && $timeFormat == '12') {
  1917. $time[0] = 12;
  1918. }
  1919. $hour = $min = null;
  1920. if (isset($time[1])) {
  1921. $hour = $time[0];
  1922. $min = $time[1];
  1923. }
  1924. }
  1925. }
  1926. }
  1927. $elements = array('Day', 'Month', 'Year', 'Hour', 'Minute', 'Meridian');
  1928. $defaults = array(
  1929. 'minYear' => null, 'maxYear' => null, 'separator' => '-',
  1930. 'interval' => 1, 'monthNames' => true
  1931. );
  1932. $attributes = array_merge($defaults, (array) $attributes);
  1933. if (isset($attributes['minuteInterval'])) {
  1934. $attributes['interval'] = $attributes['minuteInterval'];
  1935. unset($attributes['minuteInterval']);
  1936. }
  1937. $minYear = $attributes['minYear'];
  1938. $maxYear = $attributes['maxYear'];
  1939. $separator = $attributes['separator'];
  1940. $interval = $attributes['interval'];
  1941. $monthNames = $attributes['monthNames'];
  1942. $attributes = array_diff_key($attributes, $defaults);
  1943. if (isset($attributes['id'])) {
  1944. if (is_string($attributes['id'])) {
  1945. // build out an array version
  1946. foreach ($elements as $element) {
  1947. $selectAttrName = 'select' . $element . 'Attr';
  1948. ${$selectAttrName} = $attributes;
  1949. ${$selectAttrName}['id'] = $attributes['id'] . $element;
  1950. }
  1951. } elseif (is_array($attributes['id'])) {
  1952. // check for missing ones and build selectAttr for each element
  1953. $attributes['id'] += array(
  1954. 'month' => '', 'year' => '', 'day' => '',
  1955. 'hour' => '', 'minute' => '', 'meridian' => ''
  1956. );
  1957. foreach ($elements as $element) {
  1958. $selectAttrName = 'select' . $element . 'Attr';
  1959. ${$selectAttrName} = $attributes;
  1960. ${$selectAttrName}['id'] = $attributes['id'][strtolower($element)];
  1961. }
  1962. }
  1963. } else {
  1964. // build the selectAttrName with empty id's to pass
  1965. foreach ($elements as $element) {
  1966. $selectAttrName = 'select' . $element . 'Attr';
  1967. ${$selectAttrName} = $attributes;
  1968. }
  1969. }
  1970. $selects = array();
  1971. foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {
  1972. switch ($char) {
  1973. case 'Y':
  1974. $selectYearAttr['value'] = $year;
  1975. $selects[] = $this->year(
  1976. $fieldName, $minYear, $maxYear, $selectYearAttr
  1977. );
  1978. break;
  1979. case 'M':
  1980. $selectMonthAttr['value'] = $month;
  1981. $selectMonthAttr['monthNames'] = $monthNames;
  1982. $selects[] = $this->month($fieldName, $selectMonthAttr);
  1983. break;
  1984. case 'D':
  1985. $selectDayAttr['value'] = $day;
  1986. $selects[] = $this->day($fieldName, $selectDayAttr);
  1987. break;
  1988. }
  1989. }
  1990. $opt = implode($separator, $selects);
  1991. if (!empty($interval) && $interval > 1 && !empty($min)) {
  1992. $min = round($min * (1 / $interval)) * $interval;
  1993. }
  1994. $selectMinuteAttr['interval'] = $interval;
  1995. switch ($timeFormat) {
  1996. case '24':
  1997. $selectHourAttr['value'] = $hour;
  1998. $selectMinuteAttr['value'] = $min;
  1999. $opt .= $this->hour($fieldName, true, $selectHourAttr) . ':' .
  2000. $this->minute($fieldName, $selectMinuteAttr);
  2001. break;
  2002. case '12':
  2003. $selectHourAttr['value'] = $hour;
  2004. $selectMinuteAttr['value'] = $min;
  2005. $selectMeridianAttr['value'] = $meridian;
  2006. $opt .= $this->hour($fieldName, false, $selectHourAttr) . ':' .
  2007. $this->minute($fieldName, $selectMinuteAttr) . ' ' .
  2008. $this->meridian($fieldName, $selectMeridianAttr);
  2009. break;
  2010. default:
  2011. $opt .= '';
  2012. break;
  2013. }
  2014. return $opt;
  2015. }
  2016. /**
  2017. * Add support for special HABTM syntax.
  2018. *
  2019. * Sets this helper's model and field properties to the dot-separated value-pair in $entity.
  2020. *
  2021. * @param mixed $entity A field name, like "ModelName.fieldName" or "ModelName.ID.fieldName"
  2022. * @param boolean $setScope Sets the view scope to the model specified in $tagValue
  2023. * @return void
  2024. */
  2025. function setEntity($entity, $setScope = false) {
  2026. parent::setEntity($entity, $setScope);
  2027. $parts = explode('.', $entity);
  2028. $field = $this->_introspectModel($this->_modelScope, 'fields', $parts[0]);
  2029. if (!empty($field) && $field['type'] === 'multiple') {
  2030. $this->_entityPath = $parts[0] . '.' . $parts[0];
  2031. }
  2032. }
  2033. /**
  2034. * Gets the input field name for the current tag
  2035. *
  2036. * @param array $options
  2037. * @param string $key
  2038. * @return array
  2039. */
  2040. protected function _name($options = array(), $field = null, $key = 'name') {
  2041. if ($this->requestType == 'get') {
  2042. if ($options === null) {
  2043. $options = array();
  2044. } elseif (is_string($options)) {
  2045. $field = $options;
  2046. $options = 0;
  2047. }
  2048. if (!empty($field)) {
  2049. $this->setEntity($field);
  2050. }
  2051. if (is_array($options) && isset($options[$key])) {
  2052. return $options;
  2053. }
  2054. $entity = $this->entity();
  2055. $model = $this->model();
  2056. $name = $model === $entity[0] && isset($entity[1]) ? $entity[1] : $entity[0];
  2057. $last = $entity[count($entity) - 1];
  2058. if (in_array($last, $this->_fieldSuffixes)) {
  2059. $name .= '[' . $last . ']';
  2060. }
  2061. if (is_array($options)) {
  2062. $options[$key] = $name;
  2063. return $options;
  2064. } else {
  2065. return $name;
  2066. }
  2067. }
  2068. return parent::_name($options, $field, $key);
  2069. }
  2070. /**
  2071. * Returns an array of formatted OPTION/OPTGROUP elements
  2072. * @access private
  2073. * @return array
  2074. */
  2075. function __selectOptions($elements = array(), $parents = array(), $showParents = null, $attributes = array()) {
  2076. $select = array();
  2077. $attributes = array_merge(
  2078. array('escape' => true, 'style' => null, 'value' => null, 'class' => null),
  2079. $attributes
  2080. );
  2081. $selectedIsEmpty = ($attributes['value'] === '' || $attributes['value'] === null);
  2082. $selectedIsArray = is_array($attributes['value']);
  2083. foreach ($elements as $name => $title) {
  2084. $htmlOptions = array();
  2085. if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
  2086. if (!empty($name)) {
  2087. if ($attributes['style'] === 'checkbox') {
  2088. $select[] = $this->Html->useTag('fieldsetend');
  2089. } else {
  2090. $select[] = $this->Html->useTag('optiongroupend');
  2091. }
  2092. $parents[] = $name;
  2093. }
  2094. $select = array_merge($select, $this->__selectOptions(
  2095. $title, $parents, $showParents, $attributes
  2096. ));
  2097. if (!empty($name)) {
  2098. $name = $attributes['escape'] ? h($name) : $name;
  2099. if ($attributes['style'] === 'checkbox') {
  2100. $select[] = $this->Html->useTag('fieldsetstart', $name);
  2101. } else {
  2102. $select[] = $this->Html->useTag('optiongroup', $name, '');
  2103. }
  2104. }
  2105. $name = null;
  2106. } elseif (is_array($title)) {
  2107. $htmlOptions = $title;
  2108. $name = $title['value'];
  2109. $title = $title['name'];
  2110. unset($htmlOptions['name'], $htmlOptions['value']);
  2111. }
  2112. if ($name !== null) {
  2113. if (
  2114. (!$selectedIsArray && !$selectedIsEmpty && (string)$attributes['value'] == (string)$name) ||
  2115. ($selectedIsArray && in_array($name, $attributes['value']))
  2116. ) {
  2117. if ($attributes['style'] === 'checkbox') {
  2118. $htmlOptions['checked'] = true;
  2119. } else {
  2120. $htmlOptions['selected'] = 'selected';
  2121. }
  2122. }
  2123. if ($showParents || (!in_array($title, $parents))) {
  2124. $title = ($attributes['escape']) ? h($title) : $title;
  2125. if ($attributes['style'] === 'checkbox') {
  2126. $htmlOptions['value'] = $name;
  2127. $tagName = $attributes['id'] . Inflector::camelize(Inflector::slug($name));
  2128. $htmlOptions['id'] = $tagName;
  2129. $label = array('for' => $tagName);
  2130. if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) {
  2131. $label['class'] = 'selected';
  2132. }
  2133. $name = $attributes['name'];
  2134. if (empty($attributes['class'])) {
  2135. $attributes['class'] = 'checkbox';
  2136. } elseif ($attributes['class'] === 'form-error') {
  2137. $attributes['class'] = 'checkbox ' . $attributes['class'];
  2138. }
  2139. $label = $this->label(null, $title, $label);
  2140. $item = $this->Html->useTag('checkboxmultiple', $name, $htmlOptions);
  2141. $select[] = $this->Html->div($attributes['class'], $item . $label);
  2142. } else {
  2143. $select[] = $this->Html->useTag('selectoption', $name, $htmlOptions, $title);
  2144. }
  2145. }
  2146. }
  2147. }
  2148. return array_reverse($select, true);
  2149. }
  2150. /**
  2151. * Generates option lists for common <select /> menus
  2152. * @access private
  2153. */
  2154. function __generateOptions($name, $options = array()) {
  2155. if (!empty($this->options[$name])) {
  2156. return $this->options[$name];
  2157. }
  2158. $data = array();
  2159. switch ($name) {
  2160. case 'minute':
  2161. if (isset($options['interval'])) {
  2162. $interval = $options['interval'];
  2163. } else {
  2164. $interval = 1;
  2165. }
  2166. $i = 0;
  2167. while ($i < 60) {
  2168. $data[sprintf('%02d', $i)] = sprintf('%02d', $i);
  2169. $i += $interval;
  2170. }
  2171. break;
  2172. case 'hour':
  2173. for ($i = 1; $i <= 12; $i++) {
  2174. $data[sprintf('%02d', $i)] = $i;
  2175. }
  2176. break;
  2177. case 'hour24':
  2178. for ($i = 0; $i <= 23; $i++) {
  2179. $data[sprintf('%02d', $i)] = $i;
  2180. }
  2181. break;
  2182. case 'meridian':
  2183. $data = array('am' => 'am', 'pm' => 'pm');
  2184. break;
  2185. case 'day':
  2186. $min = 1;
  2187. $max = 31;
  2188. if (isset($options['min'])) {
  2189. $min = $options['min'];
  2190. }
  2191. if (isset($options['max'])) {
  2192. $max = $options['max'];
  2193. }
  2194. for ($i = $min; $i <= $max; $i++) {
  2195. $data[sprintf('%02d', $i)] = $i;
  2196. }
  2197. break;
  2198. case 'month':
  2199. if ($options['monthNames'] === true) {
  2200. $data['01'] = __d('cake', 'January');
  2201. $data['02'] = __d('cake', 'February');
  2202. $data['03'] = __d('cake', 'March');
  2203. $data['04'] = __d('cake', 'April');
  2204. $data['05'] = __d('cake', 'May');
  2205. $data['06'] = __d('cake', 'June');
  2206. $data['07'] = __d('cake', 'July');
  2207. $data['08'] = __d('cake', 'August');
  2208. $data['09'] = __d('cake', 'September');
  2209. $data['10'] = __d('cake', 'October');
  2210. $data['11'] = __d('cake', 'November');
  2211. $data['12'] = __d('cake', 'December');
  2212. } else if (is_array($options['monthNames'])) {
  2213. $data = $options['monthNames'];
  2214. } else {
  2215. for ($m = 1; $m <= 12; $m++) {
  2216. $data[sprintf("%02s", $m)] = strftime("%m", mktime(1, 1, 1, $m, 1, 1999));
  2217. }
  2218. }
  2219. break;
  2220. case 'year':
  2221. $current = intval(date('Y'));
  2222. if (!isset($options['min'])) {
  2223. $min = $current - 20;
  2224. } else {
  2225. $min = $options['min'];
  2226. }
  2227. if (!isset($options['max'])) {
  2228. $max = $current + 20;
  2229. } else {
  2230. $max = $options['max'];
  2231. }
  2232. if ($min > $max) {
  2233. list($min, $max) = array($max, $min);
  2234. }
  2235. for ($i = $min; $i <= $max; $i++) {
  2236. $data[$i] = $i;
  2237. }
  2238. if ($options['order'] != 'asc') {
  2239. $data = array_reverse($data, true);
  2240. }
  2241. break;
  2242. }
  2243. $this->__options[$name] = $data;
  2244. return $this->__options[$name];
  2245. }
  2246. /**
  2247. * Sets field defaults and adds field to form security input hash
  2248. *
  2249. * ### Options
  2250. *
  2251. * - `secure` - boolean whether or not the field should be added to the security fields.
  2252. *
  2253. * @param string $field Name of the field to initialize options for.
  2254. * @param array $options Array of options to append options into.
  2255. * @return array Array of options for the input.
  2256. */
  2257. protected function _initInputField($field, $options = array()) {
  2258. if (isset($options['secure'])) {
  2259. $secure = $options['secure'];
  2260. unset($options['secure']);
  2261. } else {
  2262. $secure = (isset($this->request['_Token']) && !empty($this->request['_Token']));
  2263. }
  2264. $result = parent::_initInputField($field, $options);
  2265. if ($secure === self::SECURE_SKIP) {
  2266. return $result;
  2267. }
  2268. $fieldName = null;
  2269. if (!empty($options['name'])) {
  2270. preg_match_all('/\[(.*?)\]/', $options['name'], $matches);
  2271. if (isset($matches[1])) {
  2272. $fieldName = $matches[1];
  2273. }
  2274. }
  2275. $this->__secure($secure, $fieldName);
  2276. return $result;
  2277. }
  2278. }