PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/bonfire/ci3/helpers/form_helper.php

http://github.com/ci-bonfire/Bonfire
PHP | 1055 lines | 659 code | 112 blank | 284 comment | 77 complexity | 481f358c8e2ed55fa4633a6ffc2d26cd MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Form Helpers
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Helpers
  44. * @category Helpers
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/helpers/form_helper.html
  47. */
  48. // ------------------------------------------------------------------------
  49. if ( ! function_exists('form_open'))
  50. {
  51. /**
  52. * Form Declaration
  53. *
  54. * Creates the opening portion of the form.
  55. *
  56. * @param string the URI segments of the form destination
  57. * @param array a key/value pair of attributes
  58. * @param array a key/value pair hidden data
  59. * @return string
  60. */
  61. function form_open($action = '', $attributes = array(), $hidden = array())
  62. {
  63. $CI =& get_instance();
  64. // If no action is provided then set to the current url
  65. if ( ! $action)
  66. {
  67. $action = $CI->config->site_url($CI->uri->uri_string());
  68. }
  69. // If an action is not a full URL then turn it into one
  70. elseif (strpos($action, '://') === FALSE)
  71. {
  72. $action = $CI->config->site_url($action);
  73. }
  74. $attributes = _attributes_to_string($attributes);
  75. if (stripos($attributes, 'method=') === FALSE)
  76. {
  77. $attributes .= ' method="post"';
  78. }
  79. if (stripos($attributes, 'accept-charset=') === FALSE)
  80. {
  81. $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
  82. }
  83. $form = '<form action="'.$action.'"'.$attributes.">\n";
  84. if (is_array($hidden))
  85. {
  86. foreach ($hidden as $name => $value)
  87. {
  88. $form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value).'" />'."\n";
  89. }
  90. }
  91. // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
  92. if ($CI->config->item('csrf_protection') === TRUE && strpos($action, $CI->config->base_url()) !== FALSE && ! stripos($form, 'method="get"'))
  93. {
  94. // Prepend/append random-length "white noise" around the CSRF
  95. // token input, as a form of protection against BREACH attacks
  96. if (FALSE !== ($noise = $CI->security->get_random_bytes(1)))
  97. {
  98. list(, $noise) = unpack('c', $noise);
  99. }
  100. else
  101. {
  102. $noise = mt_rand(-128, 127);
  103. }
  104. // Prepend if $noise has a negative value, append if positive, do nothing for zero
  105. $prepend = $append = '';
  106. if ($noise < 0)
  107. {
  108. $prepend = str_repeat(" ", abs($noise));
  109. }
  110. elseif ($noise > 0)
  111. {
  112. $append = str_repeat(" ", $noise);
  113. }
  114. $form .= sprintf(
  115. '%s<input type="hidden" name="%s" value="%s" />%s%s',
  116. $prepend,
  117. $CI->security->get_csrf_token_name(),
  118. $CI->security->get_csrf_hash(),
  119. $append,
  120. "\n"
  121. );
  122. }
  123. return $form;
  124. }
  125. }
  126. // ------------------------------------------------------------------------
  127. if ( ! function_exists('form_open_multipart'))
  128. {
  129. /**
  130. * Form Declaration - Multipart type
  131. *
  132. * Creates the opening portion of the form, but with "multipart/form-data".
  133. *
  134. * @param string the URI segments of the form destination
  135. * @param array a key/value pair of attributes
  136. * @param array a key/value pair hidden data
  137. * @return string
  138. */
  139. function form_open_multipart($action = '', $attributes = array(), $hidden = array())
  140. {
  141. if (is_string($attributes))
  142. {
  143. $attributes .= ' enctype="multipart/form-data"';
  144. }
  145. else
  146. {
  147. $attributes['enctype'] = 'multipart/form-data';
  148. }
  149. return form_open($action, $attributes, $hidden);
  150. }
  151. }
  152. // ------------------------------------------------------------------------
  153. if ( ! function_exists('form_hidden'))
  154. {
  155. /**
  156. * Hidden Input Field
  157. *
  158. * Generates hidden fields. You can pass a simple key/value string or
  159. * an associative array with multiple values.
  160. *
  161. * @param mixed $name Field name
  162. * @param string $value Field value
  163. * @param bool $recursing
  164. * @return string
  165. */
  166. function form_hidden($name, $value = '', $recursing = FALSE)
  167. {
  168. static $form;
  169. if ($recursing === FALSE)
  170. {
  171. $form = "\n";
  172. }
  173. if (is_array($name))
  174. {
  175. foreach ($name as $key => $val)
  176. {
  177. form_hidden($key, $val, TRUE);
  178. }
  179. return $form;
  180. }
  181. if ( ! is_array($value))
  182. {
  183. $form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value)."\" />\n";
  184. }
  185. else
  186. {
  187. foreach ($value as $k => $v)
  188. {
  189. $k = is_int($k) ? '' : $k;
  190. form_hidden($name.'['.$k.']', $v, TRUE);
  191. }
  192. }
  193. return $form;
  194. }
  195. }
  196. // ------------------------------------------------------------------------
  197. if ( ! function_exists('form_input'))
  198. {
  199. /**
  200. * Text Input Field
  201. *
  202. * @param mixed
  203. * @param string
  204. * @param mixed
  205. * @return string
  206. */
  207. function form_input($data = '', $value = '', $extra = '')
  208. {
  209. $defaults = array(
  210. 'type' => 'text',
  211. 'name' => is_array($data) ? '' : $data,
  212. 'value' => $value
  213. );
  214. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  215. }
  216. }
  217. // ------------------------------------------------------------------------
  218. if ( ! function_exists('form_password'))
  219. {
  220. /**
  221. * Password Field
  222. *
  223. * Identical to the input function but adds the "password" type
  224. *
  225. * @param mixed
  226. * @param string
  227. * @param mixed
  228. * @return string
  229. */
  230. function form_password($data = '', $value = '', $extra = '')
  231. {
  232. is_array($data) OR $data = array('name' => $data);
  233. $data['type'] = 'password';
  234. return form_input($data, $value, $extra);
  235. }
  236. }
  237. // ------------------------------------------------------------------------
  238. if ( ! function_exists('form_upload'))
  239. {
  240. /**
  241. * Upload Field
  242. *
  243. * Identical to the input function but adds the "file" type
  244. *
  245. * @param mixed
  246. * @param string
  247. * @param mixed
  248. * @return string
  249. */
  250. function form_upload($data = '', $value = '', $extra = '')
  251. {
  252. $defaults = array('type' => 'file', 'name' => '');
  253. is_array($data) OR $data = array('name' => $data);
  254. $data['type'] = 'file';
  255. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  256. }
  257. }
  258. // ------------------------------------------------------------------------
  259. if ( ! function_exists('form_textarea'))
  260. {
  261. /**
  262. * Textarea field
  263. *
  264. * @param mixed $data
  265. * @param string $value
  266. * @param mixed $extra
  267. * @return string
  268. */
  269. function form_textarea($data = '', $value = '', $extra = '')
  270. {
  271. $defaults = array(
  272. 'name' => is_array($data) ? '' : $data,
  273. 'cols' => '40',
  274. 'rows' => '10'
  275. );
  276. if ( ! is_array($data) OR ! isset($data['value']))
  277. {
  278. $val = $value;
  279. }
  280. else
  281. {
  282. $val = $data['value'];
  283. unset($data['value']); // textareas don't use the value attribute
  284. }
  285. return '<textarea '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
  286. .html_escape($val)
  287. ."</textarea>\n";
  288. }
  289. }
  290. // ------------------------------------------------------------------------
  291. if ( ! function_exists('form_multiselect'))
  292. {
  293. /**
  294. * Multi-select menu
  295. *
  296. * @param string
  297. * @param array
  298. * @param mixed
  299. * @param mixed
  300. * @return string
  301. */
  302. function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
  303. {
  304. $extra = _attributes_to_string($extra);
  305. if (stripos($extra, 'multiple') === FALSE)
  306. {
  307. $extra .= ' multiple="multiple"';
  308. }
  309. return form_dropdown($name, $options, $selected, $extra);
  310. }
  311. }
  312. // --------------------------------------------------------------------
  313. if ( ! function_exists('form_dropdown'))
  314. {
  315. /**
  316. * Drop-down Menu
  317. *
  318. * @param mixed $data
  319. * @param mixed $options
  320. * @param mixed $selected
  321. * @param mixed $extra
  322. * @return string
  323. */
  324. function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
  325. {
  326. $defaults = array();
  327. if (is_array($data))
  328. {
  329. if (isset($data['selected']))
  330. {
  331. $selected = $data['selected'];
  332. unset($data['selected']); // select tags don't have a selected attribute
  333. }
  334. if (isset($data['options']))
  335. {
  336. $options = $data['options'];
  337. unset($data['options']); // select tags don't use an options attribute
  338. }
  339. }
  340. else
  341. {
  342. $defaults = array('name' => $data);
  343. }
  344. is_array($selected) OR $selected = array($selected);
  345. is_array($options) OR $options = array($options);
  346. // If no selected state was submitted we will attempt to set it automatically
  347. if (empty($selected))
  348. {
  349. if (is_array($data))
  350. {
  351. if (isset($data['name'], $_POST[$data['name']]))
  352. {
  353. $selected = array($_POST[$data['name']]);
  354. }
  355. }
  356. elseif (isset($_POST[$data]))
  357. {
  358. $selected = array($_POST[$data]);
  359. }
  360. }
  361. $extra = _attributes_to_string($extra);
  362. $multiple = (count($selected) > 1 && stripos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
  363. $form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
  364. foreach ($options as $key => $val)
  365. {
  366. $key = (string) $key;
  367. if (is_array($val))
  368. {
  369. if (empty($val))
  370. {
  371. continue;
  372. }
  373. $form .= '<optgroup label="'.$key."\">\n";
  374. foreach ($val as $optgroup_key => $optgroup_val)
  375. {
  376. $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
  377. $form .= '<option value="'.html_escape($optgroup_key).'"'.$sel.'>'
  378. .(string) $optgroup_val."</option>\n";
  379. }
  380. $form .= "</optgroup>\n";
  381. }
  382. else
  383. {
  384. $form .= '<option value="'.html_escape($key).'"'
  385. .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
  386. .(string) $val."</option>\n";
  387. }
  388. }
  389. return $form."</select>\n";
  390. }
  391. }
  392. // ------------------------------------------------------------------------
  393. if ( ! function_exists('form_checkbox'))
  394. {
  395. /**
  396. * Checkbox Field
  397. *
  398. * @param mixed
  399. * @param string
  400. * @param bool
  401. * @param mixed
  402. * @return string
  403. */
  404. function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
  405. {
  406. $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
  407. if (is_array($data) && array_key_exists('checked', $data))
  408. {
  409. $checked = $data['checked'];
  410. if ($checked == FALSE)
  411. {
  412. unset($data['checked']);
  413. }
  414. else
  415. {
  416. $data['checked'] = 'checked';
  417. }
  418. }
  419. if ($checked == TRUE)
  420. {
  421. $defaults['checked'] = 'checked';
  422. }
  423. else
  424. {
  425. unset($defaults['checked']);
  426. }
  427. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  428. }
  429. }
  430. // ------------------------------------------------------------------------
  431. if ( ! function_exists('form_radio'))
  432. {
  433. /**
  434. * Radio Button
  435. *
  436. * @param mixed
  437. * @param string
  438. * @param bool
  439. * @param mixed
  440. * @return string
  441. */
  442. function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
  443. {
  444. is_array($data) OR $data = array('name' => $data);
  445. $data['type'] = 'radio';
  446. return form_checkbox($data, $value, $checked, $extra);
  447. }
  448. }
  449. // ------------------------------------------------------------------------
  450. if ( ! function_exists('form_submit'))
  451. {
  452. /**
  453. * Submit Button
  454. *
  455. * @param mixed
  456. * @param string
  457. * @param mixed
  458. * @return string
  459. */
  460. function form_submit($data = '', $value = '', $extra = '')
  461. {
  462. $defaults = array(
  463. 'type' => 'submit',
  464. 'name' => is_array($data) ? '' : $data,
  465. 'value' => $value
  466. );
  467. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  468. }
  469. }
  470. // ------------------------------------------------------------------------
  471. if ( ! function_exists('form_reset'))
  472. {
  473. /**
  474. * Reset Button
  475. *
  476. * @param mixed
  477. * @param string
  478. * @param mixed
  479. * @return string
  480. */
  481. function form_reset($data = '', $value = '', $extra = '')
  482. {
  483. $defaults = array(
  484. 'type' => 'reset',
  485. 'name' => is_array($data) ? '' : $data,
  486. 'value' => $value
  487. );
  488. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  489. }
  490. }
  491. // ------------------------------------------------------------------------
  492. if ( ! function_exists('form_button'))
  493. {
  494. /**
  495. * Form Button
  496. *
  497. * @param mixed
  498. * @param string
  499. * @param mixed
  500. * @return string
  501. */
  502. function form_button($data = '', $content = '', $extra = '')
  503. {
  504. $defaults = array(
  505. 'name' => is_array($data) ? '' : $data,
  506. 'type' => 'button'
  507. );
  508. if (is_array($data) && isset($data['content']))
  509. {
  510. $content = $data['content'];
  511. unset($data['content']); // content is not an attribute
  512. }
  513. return '<button '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
  514. .$content
  515. ."</button>\n";
  516. }
  517. }
  518. // ------------------------------------------------------------------------
  519. if ( ! function_exists('form_label'))
  520. {
  521. /**
  522. * Form Label Tag
  523. *
  524. * @param string The text to appear onscreen
  525. * @param string The id the label applies to
  526. * @param mixed Additional attributes
  527. * @return string
  528. */
  529. function form_label($label_text = '', $id = '', $attributes = array())
  530. {
  531. $label = '<label';
  532. if ($id !== '')
  533. {
  534. $label .= ' for="'.$id.'"';
  535. }
  536. $label .= _attributes_to_string($attributes);
  537. return $label.'>'.$label_text.'</label>';
  538. }
  539. }
  540. // ------------------------------------------------------------------------
  541. if ( ! function_exists('form_fieldset'))
  542. {
  543. /**
  544. * Fieldset Tag
  545. *
  546. * Used to produce <fieldset><legend>text</legend>. To close fieldset
  547. * use form_fieldset_close()
  548. *
  549. * @param string The legend text
  550. * @param array Additional attributes
  551. * @return string
  552. */
  553. function form_fieldset($legend_text = '', $attributes = array())
  554. {
  555. $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
  556. if ($legend_text !== '')
  557. {
  558. return $fieldset.'<legend>'.$legend_text."</legend>\n";
  559. }
  560. return $fieldset;
  561. }
  562. }
  563. // ------------------------------------------------------------------------
  564. if ( ! function_exists('form_fieldset_close'))
  565. {
  566. /**
  567. * Fieldset Close Tag
  568. *
  569. * @param string
  570. * @return string
  571. */
  572. function form_fieldset_close($extra = '')
  573. {
  574. return '</fieldset>'.$extra;
  575. }
  576. }
  577. // ------------------------------------------------------------------------
  578. if ( ! function_exists('form_close'))
  579. {
  580. /**
  581. * Form Close Tag
  582. *
  583. * @param string
  584. * @return string
  585. */
  586. function form_close($extra = '')
  587. {
  588. return '</form>'.$extra;
  589. }
  590. }
  591. // ------------------------------------------------------------------------
  592. if ( ! function_exists('form_prep'))
  593. {
  594. /**
  595. * Form Prep
  596. *
  597. * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
  598. *
  599. * @deprecated 3.0.0 An alias for html_escape()
  600. * @param string|string[] $str Value to escape
  601. * @return string|string[] Escaped values
  602. */
  603. function form_prep($str)
  604. {
  605. return html_escape($str, TRUE);
  606. }
  607. }
  608. // ------------------------------------------------------------------------
  609. if ( ! function_exists('set_value'))
  610. {
  611. /**
  612. * Form Value
  613. *
  614. * Grabs a value from the POST array for the specified field so you can
  615. * re-populate an input field or textarea. If Form Validation
  616. * is active it retrieves the info from the validation class
  617. *
  618. * @param string $field Field name
  619. * @param string $default Default value
  620. * @param bool $html_escape Whether to escape HTML special characters or not
  621. * @return string
  622. */
  623. function set_value($field, $default = '', $html_escape = TRUE)
  624. {
  625. $CI =& get_instance();
  626. $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  627. ? $CI->form_validation->set_value($field, $default)
  628. : $CI->input->post($field, FALSE);
  629. isset($value) OR $value = $default;
  630. return ($html_escape) ? html_escape($value) : $value;
  631. }
  632. }
  633. // ------------------------------------------------------------------------
  634. if ( ! function_exists('set_select'))
  635. {
  636. /**
  637. * Set Select
  638. *
  639. * Let's you set the selected value of a <select> menu via data in the POST array.
  640. * If Form Validation is active it retrieves the info from the validation class
  641. *
  642. * @param string
  643. * @param string
  644. * @param bool
  645. * @return string
  646. */
  647. function set_select($field, $value = '', $default = FALSE)
  648. {
  649. $CI =& get_instance();
  650. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  651. {
  652. return $CI->form_validation->set_select($field, $value, $default);
  653. }
  654. elseif (($input = $CI->input->post($field, FALSE)) === NULL)
  655. {
  656. return ($default === TRUE) ? ' selected="selected"' : '';
  657. }
  658. $value = (string) $value;
  659. if (is_array($input))
  660. {
  661. // Note: in_array('', array(0)) returns TRUE, do not use it
  662. foreach ($input as &$v)
  663. {
  664. if ($value === $v)
  665. {
  666. return ' selected="selected"';
  667. }
  668. }
  669. return '';
  670. }
  671. return ($input === $value) ? ' selected="selected"' : '';
  672. }
  673. }
  674. // ------------------------------------------------------------------------
  675. if ( ! function_exists('set_checkbox'))
  676. {
  677. /**
  678. * Set Checkbox
  679. *
  680. * Let's you set the selected value of a checkbox via the value in the POST array.
  681. * If Form Validation is active it retrieves the info from the validation class
  682. *
  683. * @param string
  684. * @param string
  685. * @param bool
  686. * @return string
  687. */
  688. function set_checkbox($field, $value = '', $default = FALSE)
  689. {
  690. $CI =& get_instance();
  691. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  692. {
  693. return $CI->form_validation->set_checkbox($field, $value, $default);
  694. }
  695. // Form inputs are always strings ...
  696. $value = (string) $value;
  697. $input = $CI->input->post($field, FALSE);
  698. if (is_array($input))
  699. {
  700. // Note: in_array('', array(0)) returns TRUE, do not use it
  701. foreach ($input as &$v)
  702. {
  703. if ($value === $v)
  704. {
  705. return ' checked="checked"';
  706. }
  707. }
  708. return '';
  709. }
  710. // Unchecked checkbox and radio inputs are not even submitted by browsers ...
  711. if ($CI->input->method() === 'post')
  712. {
  713. return ($input === $value) ? ' checked="checked"' : '';
  714. }
  715. return ($default === TRUE) ? ' checked="checked"' : '';
  716. }
  717. }
  718. // ------------------------------------------------------------------------
  719. if ( ! function_exists('set_radio'))
  720. {
  721. /**
  722. * Set Radio
  723. *
  724. * Let's you set the selected value of a radio field via info in the POST array.
  725. * If Form Validation is active it retrieves the info from the validation class
  726. *
  727. * @param string $field
  728. * @param string $value
  729. * @param bool $default
  730. * @return string
  731. */
  732. function set_radio($field, $value = '', $default = FALSE)
  733. {
  734. $CI =& get_instance();
  735. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  736. {
  737. return $CI->form_validation->set_radio($field, $value, $default);
  738. }
  739. // Form inputs are always strings ...
  740. $value = (string) $value;
  741. $input = $CI->input->post($field, FALSE);
  742. if (is_array($input))
  743. {
  744. // Note: in_array('', array(0)) returns TRUE, do not use it
  745. foreach ($input as &$v)
  746. {
  747. if ($value === $v)
  748. {
  749. return ' checked="checked"';
  750. }
  751. }
  752. return '';
  753. }
  754. // Unchecked checkbox and radio inputs are not even submitted by browsers ...
  755. if ($CI->input->method() === 'post')
  756. {
  757. return ($input === $value) ? ' checked="checked"' : '';
  758. }
  759. return ($default === TRUE) ? ' checked="checked"' : '';
  760. }
  761. }
  762. // ------------------------------------------------------------------------
  763. if ( ! function_exists('form_error'))
  764. {
  765. /**
  766. * Form Error
  767. *
  768. * Returns the error for a specific form field. This is a helper for the
  769. * form validation class.
  770. *
  771. * @param string
  772. * @param string
  773. * @param string
  774. * @return string
  775. */
  776. function form_error($field = '', $prefix = '', $suffix = '')
  777. {
  778. if (FALSE === ($OBJ =& _get_validation_object()))
  779. {
  780. return '';
  781. }
  782. return $OBJ->error($field, $prefix, $suffix);
  783. }
  784. }
  785. // ------------------------------------------------------------------------
  786. if ( ! function_exists('validation_errors'))
  787. {
  788. /**
  789. * Validation Error String
  790. *
  791. * Returns all the errors associated with a form submission. This is a helper
  792. * function for the form validation class.
  793. *
  794. * @param string
  795. * @param string
  796. * @return string
  797. */
  798. function validation_errors($prefix = '', $suffix = '')
  799. {
  800. if (FALSE === ($OBJ =& _get_validation_object()))
  801. {
  802. return '';
  803. }
  804. return $OBJ->error_string($prefix, $suffix);
  805. }
  806. }
  807. // ------------------------------------------------------------------------
  808. if ( ! function_exists('_parse_form_attributes'))
  809. {
  810. /**
  811. * Parse the form attributes
  812. *
  813. * Helper function used by some of the form helpers
  814. *
  815. * @param array $attributes List of attributes
  816. * @param array $default Default values
  817. * @return string
  818. */
  819. function _parse_form_attributes($attributes, $default)
  820. {
  821. if (is_array($attributes))
  822. {
  823. foreach ($default as $key => $val)
  824. {
  825. if (isset($attributes[$key]))
  826. {
  827. $default[$key] = $attributes[$key];
  828. unset($attributes[$key]);
  829. }
  830. }
  831. if (count($attributes) > 0)
  832. {
  833. $default = array_merge($default, $attributes);
  834. }
  835. }
  836. $att = '';
  837. foreach ($default as $key => $val)
  838. {
  839. if ($key === 'value')
  840. {
  841. $val = html_escape($val);
  842. }
  843. elseif ($key === 'name' && ! strlen($default['name']))
  844. {
  845. continue;
  846. }
  847. $att .= $key.'="'.$val.'" ';
  848. }
  849. return $att;
  850. }
  851. }
  852. // ------------------------------------------------------------------------
  853. if ( ! function_exists('_attributes_to_string'))
  854. {
  855. /**
  856. * Attributes To String
  857. *
  858. * Helper function used by some of the form helpers
  859. *
  860. * @param mixed
  861. * @return string
  862. */
  863. function _attributes_to_string($attributes)
  864. {
  865. if (empty($attributes))
  866. {
  867. return '';
  868. }
  869. if (is_object($attributes))
  870. {
  871. $attributes = (array) $attributes;
  872. }
  873. if (is_array($attributes))
  874. {
  875. $atts = '';
  876. foreach ($attributes as $key => $val)
  877. {
  878. $atts .= ' '.$key.'="'.$val.'"';
  879. }
  880. return $atts;
  881. }
  882. if (is_string($attributes))
  883. {
  884. return ' '.$attributes;
  885. }
  886. return FALSE;
  887. }
  888. }
  889. // ------------------------------------------------------------------------
  890. if ( ! function_exists('_get_validation_object'))
  891. {
  892. /**
  893. * Validation Object
  894. *
  895. * Determines what the form validation class was instantiated as, fetches
  896. * the object and returns it.
  897. *
  898. * @return mixed
  899. */
  900. function &_get_validation_object()
  901. {
  902. $CI =& get_instance();
  903. // We set this as a variable since we're returning by reference.
  904. $return = FALSE;
  905. if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
  906. {
  907. if ( ! isset($CI->$object) OR ! is_object($CI->$object))
  908. {
  909. return $return;
  910. }
  911. return $CI->$object;
  912. }
  913. return $return;
  914. }
  915. }