PageRenderTime 23ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/system/helpers/form_helper.php

https://gitlab.com/betanurlaila/UI_onlineshop
PHP | 1032 lines | 641 code | 110 blank | 281 comment | 77 complexity | e0201276a7a264e27c2876584ea3b98e MD5 | raw file
  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 - 2016, 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 - 2016, 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. // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
  85. if ($CI->config->item('csrf_protection') === TRUE && strpos($action, $CI->config->base_url()) !== FALSE && ! stripos($form, 'method="get"'))
  86. {
  87. $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
  88. }
  89. if (is_array($hidden))
  90. {
  91. foreach ($hidden as $name => $value)
  92. {
  93. $form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value).'" style="display:none;" />'."\n";
  94. }
  95. }
  96. return $form;
  97. }
  98. }
  99. // ------------------------------------------------------------------------
  100. if ( ! function_exists('form_open_multipart'))
  101. {
  102. /**
  103. * Form Declaration - Multipart type
  104. *
  105. * Creates the opening portion of the form, but with "multipart/form-data".
  106. *
  107. * @param string the URI segments of the form destination
  108. * @param array a key/value pair of attributes
  109. * @param array a key/value pair hidden data
  110. * @return string
  111. */
  112. function form_open_multipart($action = '', $attributes = array(), $hidden = array())
  113. {
  114. if (is_string($attributes))
  115. {
  116. $attributes .= ' enctype="multipart/form-data"';
  117. }
  118. else
  119. {
  120. $attributes['enctype'] = 'multipart/form-data';
  121. }
  122. return form_open($action, $attributes, $hidden);
  123. }
  124. }
  125. // ------------------------------------------------------------------------
  126. if ( ! function_exists('form_hidden'))
  127. {
  128. /**
  129. * Hidden Input Field
  130. *
  131. * Generates hidden fields. You can pass a simple key/value string or
  132. * an associative array with multiple values.
  133. *
  134. * @param mixed $name Field name
  135. * @param string $value Field value
  136. * @param bool $recursing
  137. * @return string
  138. */
  139. function form_hidden($name, $value = '', $recursing = FALSE)
  140. {
  141. static $form;
  142. if ($recursing === FALSE)
  143. {
  144. $form = "\n";
  145. }
  146. if (is_array($name))
  147. {
  148. foreach ($name as $key => $val)
  149. {
  150. form_hidden($key, $val, TRUE);
  151. }
  152. return $form;
  153. }
  154. if ( ! is_array($value))
  155. {
  156. $form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value)."\" />\n";
  157. }
  158. else
  159. {
  160. foreach ($value as $k => $v)
  161. {
  162. $k = is_int($k) ? '' : $k;
  163. form_hidden($name.'['.$k.']', $v, TRUE);
  164. }
  165. }
  166. return $form;
  167. }
  168. }
  169. // ------------------------------------------------------------------------
  170. if ( ! function_exists('form_input'))
  171. {
  172. /**
  173. * Text Input Field
  174. *
  175. * @param mixed
  176. * @param string
  177. * @param mixed
  178. * @return string
  179. */
  180. function form_input($data = '', $value = '', $extra = '')
  181. {
  182. $defaults = array(
  183. 'type' => 'text',
  184. 'name' => is_array($data) ? '' : $data,
  185. 'value' => $value
  186. );
  187. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  188. }
  189. }
  190. // ------------------------------------------------------------------------
  191. if ( ! function_exists('form_password'))
  192. {
  193. /**
  194. * Password Field
  195. *
  196. * Identical to the input function but adds the "password" type
  197. *
  198. * @param mixed
  199. * @param string
  200. * @param mixed
  201. * @return string
  202. */
  203. function form_password($data = '', $value = '', $extra = '')
  204. {
  205. is_array($data) OR $data = array('name' => $data);
  206. $data['type'] = 'password';
  207. return form_input($data, $value, $extra);
  208. }
  209. }
  210. // ------------------------------------------------------------------------
  211. if ( ! function_exists('form_upload'))
  212. {
  213. /**
  214. * Upload Field
  215. *
  216. * Identical to the input function but adds the "file" type
  217. *
  218. * @param mixed
  219. * @param string
  220. * @param mixed
  221. * @return string
  222. */
  223. function form_upload($data = '', $value = '', $extra = '')
  224. {
  225. $defaults = array('type' => 'file', 'name' => '');
  226. is_array($data) OR $data = array('name' => $data);
  227. $data['type'] = 'file';
  228. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  229. }
  230. }
  231. // ------------------------------------------------------------------------
  232. if ( ! function_exists('form_textarea'))
  233. {
  234. /**
  235. * Textarea field
  236. *
  237. * @param mixed $data
  238. * @param string $value
  239. * @param mixed $extra
  240. * @return string
  241. */
  242. function form_textarea($data = '', $value = '', $extra = '')
  243. {
  244. $defaults = array(
  245. 'name' => is_array($data) ? '' : $data,
  246. 'cols' => '40',
  247. 'rows' => '10'
  248. );
  249. if ( ! is_array($data) OR ! isset($data['value']))
  250. {
  251. $val = $value;
  252. }
  253. else
  254. {
  255. $val = $data['value'];
  256. unset($data['value']); // textareas don't use the value attribute
  257. }
  258. return '<textarea '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
  259. .html_escape($val)
  260. ."</textarea>\n";
  261. }
  262. }
  263. // ------------------------------------------------------------------------
  264. if ( ! function_exists('form_multiselect'))
  265. {
  266. /**
  267. * Multi-select menu
  268. *
  269. * @param string
  270. * @param array
  271. * @param mixed
  272. * @param mixed
  273. * @return string
  274. */
  275. function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
  276. {
  277. $extra = _attributes_to_string($extra);
  278. if (stripos($extra, 'multiple') === FALSE)
  279. {
  280. $extra .= ' multiple="multiple"';
  281. }
  282. return form_dropdown($name, $options, $selected, $extra);
  283. }
  284. }
  285. // --------------------------------------------------------------------
  286. if ( ! function_exists('form_dropdown'))
  287. {
  288. /**
  289. * Drop-down Menu
  290. *
  291. * @param mixed $data
  292. * @param mixed $options
  293. * @param mixed $selected
  294. * @param mixed $extra
  295. * @return string
  296. */
  297. function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
  298. {
  299. $defaults = array();
  300. if (is_array($data))
  301. {
  302. if (isset($data['selected']))
  303. {
  304. $selected = $data['selected'];
  305. unset($data['selected']); // select tags don't have a selected attribute
  306. }
  307. if (isset($data['options']))
  308. {
  309. $options = $data['options'];
  310. unset($data['options']); // select tags don't use an options attribute
  311. }
  312. }
  313. else
  314. {
  315. $defaults = array('name' => $data);
  316. }
  317. is_array($selected) OR $selected = array($selected);
  318. is_array($options) OR $options = array($options);
  319. // If no selected state was submitted we will attempt to set it automatically
  320. if (empty($selected))
  321. {
  322. if (is_array($data))
  323. {
  324. if (isset($data['name'], $_POST[$data['name']]))
  325. {
  326. $selected = array($_POST[$data['name']]);
  327. }
  328. }
  329. elseif (isset($_POST[$data]))
  330. {
  331. $selected = array($_POST[$data]);
  332. }
  333. }
  334. $extra = _attributes_to_string($extra);
  335. $multiple = (count($selected) > 1 && stripos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
  336. $form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
  337. foreach ($options as $key => $val)
  338. {
  339. $key = (string) $key;
  340. if (is_array($val))
  341. {
  342. if (empty($val))
  343. {
  344. continue;
  345. }
  346. $form .= '<optgroup label="'.$key."\">\n";
  347. foreach ($val as $optgroup_key => $optgroup_val)
  348. {
  349. $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
  350. $form .= '<option value="'.html_escape($optgroup_key).'"'.$sel.'>'
  351. .(string) $optgroup_val."</option>\n";
  352. }
  353. $form .= "</optgroup>\n";
  354. }
  355. else
  356. {
  357. $form .= '<option value="'.html_escape($key).'"'
  358. .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
  359. .(string) $val."</option>\n";
  360. }
  361. }
  362. return $form."</select>\n";
  363. }
  364. }
  365. // ------------------------------------------------------------------------
  366. if ( ! function_exists('form_checkbox'))
  367. {
  368. /**
  369. * Checkbox Field
  370. *
  371. * @param mixed
  372. * @param string
  373. * @param bool
  374. * @param mixed
  375. * @return string
  376. */
  377. function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
  378. {
  379. $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
  380. if (is_array($data) && array_key_exists('checked', $data))
  381. {
  382. $checked = $data['checked'];
  383. if ($checked == FALSE)
  384. {
  385. unset($data['checked']);
  386. }
  387. else
  388. {
  389. $data['checked'] = 'checked';
  390. }
  391. }
  392. if ($checked == TRUE)
  393. {
  394. $defaults['checked'] = 'checked';
  395. }
  396. else
  397. {
  398. unset($defaults['checked']);
  399. }
  400. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  401. }
  402. }
  403. // ------------------------------------------------------------------------
  404. if ( ! function_exists('form_radio'))
  405. {
  406. /**
  407. * Radio Button
  408. *
  409. * @param mixed
  410. * @param string
  411. * @param bool
  412. * @param mixed
  413. * @return string
  414. */
  415. function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
  416. {
  417. is_array($data) OR $data = array('name' => $data);
  418. $data['type'] = 'radio';
  419. return form_checkbox($data, $value, $checked, $extra);
  420. }
  421. }
  422. // ------------------------------------------------------------------------
  423. if ( ! function_exists('form_submit'))
  424. {
  425. /**
  426. * Submit Button
  427. *
  428. * @param mixed
  429. * @param string
  430. * @param mixed
  431. * @return string
  432. */
  433. function form_submit($data = '', $value = '', $extra = '')
  434. {
  435. $defaults = array(
  436. 'type' => 'submit',
  437. 'name' => is_array($data) ? '' : $data,
  438. 'value' => $value
  439. );
  440. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  441. }
  442. }
  443. // ------------------------------------------------------------------------
  444. if ( ! function_exists('form_reset'))
  445. {
  446. /**
  447. * Reset Button
  448. *
  449. * @param mixed
  450. * @param string
  451. * @param mixed
  452. * @return string
  453. */
  454. function form_reset($data = '', $value = '', $extra = '')
  455. {
  456. $defaults = array(
  457. 'type' => 'reset',
  458. 'name' => is_array($data) ? '' : $data,
  459. 'value' => $value
  460. );
  461. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  462. }
  463. }
  464. // ------------------------------------------------------------------------
  465. if ( ! function_exists('form_button'))
  466. {
  467. /**
  468. * Form Button
  469. *
  470. * @param mixed
  471. * @param string
  472. * @param mixed
  473. * @return string
  474. */
  475. function form_button($data = '', $content = '', $extra = '')
  476. {
  477. $defaults = array(
  478. 'name' => is_array($data) ? '' : $data,
  479. 'type' => 'button'
  480. );
  481. if (is_array($data) && isset($data['content']))
  482. {
  483. $content = $data['content'];
  484. unset($data['content']); // content is not an attribute
  485. }
  486. return '<button '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
  487. .$content
  488. ."</button>\n";
  489. }
  490. }
  491. // ------------------------------------------------------------------------
  492. if ( ! function_exists('form_label'))
  493. {
  494. /**
  495. * Form Label Tag
  496. *
  497. * @param string The text to appear onscreen
  498. * @param string The id the label applies to
  499. * @param string Additional attributes
  500. * @return string
  501. */
  502. function form_label($label_text = '', $id = '', $attributes = array())
  503. {
  504. $label = '<label';
  505. if ($id !== '')
  506. {
  507. $label .= ' for="'.$id.'"';
  508. }
  509. if (is_array($attributes) && count($attributes) > 0)
  510. {
  511. foreach ($attributes as $key => $val)
  512. {
  513. $label .= ' '.$key.'="'.$val.'"';
  514. }
  515. }
  516. return $label.'>'.$label_text.'</label>';
  517. }
  518. }
  519. // ------------------------------------------------------------------------
  520. if ( ! function_exists('form_fieldset'))
  521. {
  522. /**
  523. * Fieldset Tag
  524. *
  525. * Used to produce <fieldset><legend>text</legend>. To close fieldset
  526. * use form_fieldset_close()
  527. *
  528. * @param string The legend text
  529. * @param array Additional attributes
  530. * @return string
  531. */
  532. function form_fieldset($legend_text = '', $attributes = array())
  533. {
  534. $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
  535. if ($legend_text !== '')
  536. {
  537. return $fieldset.'<legend>'.$legend_text."</legend>\n";
  538. }
  539. return $fieldset;
  540. }
  541. }
  542. // ------------------------------------------------------------------------
  543. if ( ! function_exists('form_fieldset_close'))
  544. {
  545. /**
  546. * Fieldset Close Tag
  547. *
  548. * @param string
  549. * @return string
  550. */
  551. function form_fieldset_close($extra = '')
  552. {
  553. return '</fieldset>'.$extra;
  554. }
  555. }
  556. // ------------------------------------------------------------------------
  557. if ( ! function_exists('form_close'))
  558. {
  559. /**
  560. * Form Close Tag
  561. *
  562. * @param string
  563. * @return string
  564. */
  565. function form_close($extra = '')
  566. {
  567. return '</form>'.$extra;
  568. }
  569. }
  570. // ------------------------------------------------------------------------
  571. if ( ! function_exists('form_prep'))
  572. {
  573. /**
  574. * Form Prep
  575. *
  576. * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
  577. *
  578. * @deprecated 3.0.0 An alias for html_escape()
  579. * @param string|string[] $str Value to escape
  580. * @return string|string[] Escaped values
  581. */
  582. function form_prep($str)
  583. {
  584. return html_escape($str, TRUE);
  585. }
  586. }
  587. // ------------------------------------------------------------------------
  588. if ( ! function_exists('set_value'))
  589. {
  590. /**
  591. * Form Value
  592. *
  593. * Grabs a value from the POST array for the specified field so you can
  594. * re-populate an input field or textarea. If Form Validation
  595. * is active it retrieves the info from the validation class
  596. *
  597. * @param string $field Field name
  598. * @param string $default Default value
  599. * @param bool $html_escape Whether to escape HTML special characters or not
  600. * @return string
  601. */
  602. function set_value($field, $default = '', $html_escape = TRUE)
  603. {
  604. $CI =& get_instance();
  605. $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  606. ? $CI->form_validation->set_value($field, $default)
  607. : $CI->input->post($field, FALSE);
  608. isset($value) OR $value = $default;
  609. return ($html_escape) ? html_escape($value) : $value;
  610. }
  611. }
  612. // ------------------------------------------------------------------------
  613. if ( ! function_exists('set_select'))
  614. {
  615. /**
  616. * Set Select
  617. *
  618. * Let's you set the selected value of a <select> menu via data in the POST array.
  619. * If Form Validation is active it retrieves the info from the validation class
  620. *
  621. * @param string
  622. * @param string
  623. * @param bool
  624. * @return string
  625. */
  626. function set_select($field, $value = '', $default = FALSE)
  627. {
  628. $CI =& get_instance();
  629. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  630. {
  631. return $CI->form_validation->set_select($field, $value, $default);
  632. }
  633. elseif (($input = $CI->input->post($field, FALSE)) === NULL)
  634. {
  635. return ($default === TRUE) ? ' selected="selected"' : '';
  636. }
  637. $value = (string) $value;
  638. if (is_array($input))
  639. {
  640. // Note: in_array('', array(0)) returns TRUE, do not use it
  641. foreach ($input as &$v)
  642. {
  643. if ($value === $v)
  644. {
  645. return ' selected="selected"';
  646. }
  647. }
  648. return '';
  649. }
  650. return ($input === $value) ? ' selected="selected"' : '';
  651. }
  652. }
  653. // ------------------------------------------------------------------------
  654. if ( ! function_exists('set_checkbox'))
  655. {
  656. /**
  657. * Set Checkbox
  658. *
  659. * Let's you set the selected value of a checkbox via the value in the POST array.
  660. * If Form Validation is active it retrieves the info from the validation class
  661. *
  662. * @param string
  663. * @param string
  664. * @param bool
  665. * @return string
  666. */
  667. function set_checkbox($field, $value = '', $default = FALSE)
  668. {
  669. $CI =& get_instance();
  670. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  671. {
  672. return $CI->form_validation->set_checkbox($field, $value, $default);
  673. }
  674. // Form inputs are always strings ...
  675. $value = (string) $value;
  676. $input = $CI->input->post($field, FALSE);
  677. if (is_array($input))
  678. {
  679. // Note: in_array('', array(0)) returns TRUE, do not use it
  680. foreach ($input as &$v)
  681. {
  682. if ($value === $v)
  683. {
  684. return ' checked="checked"';
  685. }
  686. }
  687. return '';
  688. }
  689. // Unchecked checkbox and radio inputs are not even submitted by browsers ...
  690. if ($CI->input->method() === 'post')
  691. {
  692. return ($input === $value) ? ' checked="checked"' : '';
  693. }
  694. return ($default === TRUE) ? ' checked="checked"' : '';
  695. }
  696. }
  697. // ------------------------------------------------------------------------
  698. if ( ! function_exists('set_radio'))
  699. {
  700. /**
  701. * Set Radio
  702. *
  703. * Let's you set the selected value of a radio field via info in the POST array.
  704. * If Form Validation is active it retrieves the info from the validation class
  705. *
  706. * @param string $field
  707. * @param string $value
  708. * @param bool $default
  709. * @return string
  710. */
  711. function set_radio($field, $value = '', $default = FALSE)
  712. {
  713. $CI =& get_instance();
  714. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  715. {
  716. return $CI->form_validation->set_radio($field, $value, $default);
  717. }
  718. // Form inputs are always strings ...
  719. $value = (string) $value;
  720. $input = $CI->input->post($field, FALSE);
  721. if (is_array($input))
  722. {
  723. // Note: in_array('', array(0)) returns TRUE, do not use it
  724. foreach ($input as &$v)
  725. {
  726. if ($value === $v)
  727. {
  728. return ' checked="checked"';
  729. }
  730. }
  731. return '';
  732. }
  733. // Unchecked checkbox and radio inputs are not even submitted by browsers ...
  734. if ($CI->input->method() === 'post')
  735. {
  736. return ($input === $value) ? ' checked="checked"' : '';
  737. }
  738. return ($default === TRUE) ? ' checked="checked"' : '';
  739. }
  740. }
  741. // ------------------------------------------------------------------------
  742. if ( ! function_exists('form_error'))
  743. {
  744. /**
  745. * Form Error
  746. *
  747. * Returns the error for a specific form field. This is a helper for the
  748. * form validation class.
  749. *
  750. * @param string
  751. * @param string
  752. * @param string
  753. * @return string
  754. */
  755. function form_error($field = '', $prefix = '', $suffix = '')
  756. {
  757. if (FALSE === ($OBJ =& _get_validation_object()))
  758. {
  759. return '';
  760. }
  761. return $OBJ->error($field, $prefix, $suffix);
  762. }
  763. }
  764. // ------------------------------------------------------------------------
  765. if ( ! function_exists('validation_errors'))
  766. {
  767. /**
  768. * Validation Error String
  769. *
  770. * Returns all the errors associated with a form submission. This is a helper
  771. * function for the form validation class.
  772. *
  773. * @param string
  774. * @param string
  775. * @return string
  776. */
  777. function validation_errors($prefix = '', $suffix = '')
  778. {
  779. if (FALSE === ($OBJ =& _get_validation_object()))
  780. {
  781. return '';
  782. }
  783. return $OBJ->error_string($prefix, $suffix);
  784. }
  785. }
  786. // ------------------------------------------------------------------------
  787. if ( ! function_exists('_parse_form_attributes'))
  788. {
  789. /**
  790. * Parse the form attributes
  791. *
  792. * Helper function used by some of the form helpers
  793. *
  794. * @param array $attributes List of attributes
  795. * @param array $default Default values
  796. * @return string
  797. */
  798. function _parse_form_attributes($attributes, $default)
  799. {
  800. if (is_array($attributes))
  801. {
  802. foreach ($default as $key => $val)
  803. {
  804. if (isset($attributes[$key]))
  805. {
  806. $default[$key] = $attributes[$key];
  807. unset($attributes[$key]);
  808. }
  809. }
  810. if (count($attributes) > 0)
  811. {
  812. $default = array_merge($default, $attributes);
  813. }
  814. }
  815. $att = '';
  816. foreach ($default as $key => $val)
  817. {
  818. if ($key === 'value')
  819. {
  820. $val = html_escape($val);
  821. }
  822. elseif ($key === 'name' && ! strlen($default['name']))
  823. {
  824. continue;
  825. }
  826. $att .= $key.'="'.$val.'" ';
  827. }
  828. return $att;
  829. }
  830. }
  831. // ------------------------------------------------------------------------
  832. if ( ! function_exists('_attributes_to_string'))
  833. {
  834. /**
  835. * Attributes To String
  836. *
  837. * Helper function used by some of the form helpers
  838. *
  839. * @param mixed
  840. * @return string
  841. */
  842. function _attributes_to_string($attributes)
  843. {
  844. if (empty($attributes))
  845. {
  846. return '';
  847. }
  848. if (is_object($attributes))
  849. {
  850. $attributes = (array) $attributes;
  851. }
  852. if (is_array($attributes))
  853. {
  854. $atts = '';
  855. foreach ($attributes as $key => $val)
  856. {
  857. $atts .= ' '.$key.'="'.$val.'"';
  858. }
  859. return $atts;
  860. }
  861. if (is_string($attributes))
  862. {
  863. return ' '.$attributes;
  864. }
  865. return FALSE;
  866. }
  867. }
  868. // ------------------------------------------------------------------------
  869. if ( ! function_exists('_get_validation_object'))
  870. {
  871. /**
  872. * Validation Object
  873. *
  874. * Determines what the form validation class was instantiated as, fetches
  875. * the object and returns it.
  876. *
  877. * @return mixed
  878. */
  879. function &_get_validation_object()
  880. {
  881. $CI =& get_instance();
  882. // We set this as a variable since we're returning by reference.
  883. $return = FALSE;
  884. if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
  885. {
  886. if ( ! isset($CI->$object) OR ! is_object($CI->$object))
  887. {
  888. return $return;
  889. }
  890. return $CI->$object;
  891. }
  892. return $return;
  893. }
  894. }