PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/sp_1/system/helpers/form_helper.php

https://gitlab.com/rezaul007/Hospital-information-bank
PHP | 1000 lines | 621 code | 103 blank | 276 comment | 73 complexity | ec36c038daacaed81c497000b5425dc7 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 - 2015, 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. (http://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link http://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 http://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 string
  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).$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 string
  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 string
  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).$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 string $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).$extra.'>'.html_escape($val)."</textarea>\n";
  259. }
  260. }
  261. // ------------------------------------------------------------------------
  262. if ( ! function_exists('form_multiselect'))
  263. {
  264. /**
  265. * Multi-select menu
  266. *
  267. * @param string
  268. * @param array
  269. * @param mixed
  270. * @param string
  271. * @return string
  272. */
  273. function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
  274. {
  275. if ( ! strpos($extra, 'multiple'))
  276. {
  277. $extra .= ' multiple="multiple"';
  278. }
  279. return form_dropdown($name, $options, $selected, $extra);
  280. }
  281. }
  282. // --------------------------------------------------------------------
  283. if ( ! function_exists('form_dropdown'))
  284. {
  285. /**
  286. * Drop-down Menu
  287. *
  288. * @param mixed $data
  289. * @param mixed $options
  290. * @param mixed $selected
  291. * @param mixed $extra
  292. * @return string
  293. */
  294. function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
  295. {
  296. $defaults = array();
  297. if (is_array($data))
  298. {
  299. if (isset($data['selected']))
  300. {
  301. $selected = $data['selected'];
  302. unset($data['selected']); // select tags don't have a selected attribute
  303. }
  304. if (isset($data['options']))
  305. {
  306. $options = $data['options'];
  307. unset($data['options']); // select tags don't use an options attribute
  308. }
  309. }
  310. else
  311. {
  312. $defaults = array('name' => $data);
  313. }
  314. is_array($selected) OR $selected = array($selected);
  315. is_array($options) OR $options = array($options);
  316. // If no selected state was submitted we will attempt to set it automatically
  317. if (empty($selected))
  318. {
  319. if (is_array($data))
  320. {
  321. if (isset($data['name'], $_POST[$data['name']]))
  322. {
  323. $selected = array($_POST[$data['name']]);
  324. }
  325. }
  326. elseif (isset($_POST[$data]))
  327. {
  328. $selected = array($_POST[$data]);
  329. }
  330. }
  331. $extra = _attributes_to_string($extra);
  332. $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
  333. $form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
  334. foreach ($options as $key => $val)
  335. {
  336. $key = (string) $key;
  337. if (is_array($val))
  338. {
  339. if (empty($val))
  340. {
  341. continue;
  342. }
  343. $form .= '<optgroup label="'.$key."\">\n";
  344. foreach ($val as $optgroup_key => $optgroup_val)
  345. {
  346. $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
  347. $form .= '<option value="'.html_escape($optgroup_key).'"'.$sel.'>'
  348. .(string) $optgroup_val."</option>\n";
  349. }
  350. $form .= "</optgroup>\n";
  351. }
  352. else
  353. {
  354. $form .= '<option value="'.html_escape($key).'"'
  355. .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
  356. .(string) $val."</option>\n";
  357. }
  358. }
  359. return $form."</select>\n";
  360. }
  361. }
  362. // ------------------------------------------------------------------------
  363. if ( ! function_exists('form_checkbox'))
  364. {
  365. /**
  366. * Checkbox Field
  367. *
  368. * @param mixed
  369. * @param string
  370. * @param bool
  371. * @param string
  372. * @return string
  373. */
  374. function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
  375. {
  376. $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
  377. if (is_array($data) && array_key_exists('checked', $data))
  378. {
  379. $checked = $data['checked'];
  380. if ($checked == FALSE)
  381. {
  382. unset($data['checked']);
  383. }
  384. else
  385. {
  386. $data['checked'] = 'checked';
  387. }
  388. }
  389. if ($checked == TRUE)
  390. {
  391. $defaults['checked'] = 'checked';
  392. }
  393. else
  394. {
  395. unset($defaults['checked']);
  396. }
  397. return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
  398. }
  399. }
  400. // ------------------------------------------------------------------------
  401. if ( ! function_exists('form_radio'))
  402. {
  403. /**
  404. * Radio Button
  405. *
  406. * @param mixed
  407. * @param string
  408. * @param bool
  409. * @param string
  410. * @return string
  411. */
  412. function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
  413. {
  414. is_array($data) OR $data = array('name' => $data);
  415. $data['type'] = 'radio';
  416. return form_checkbox($data, $value, $checked, $extra);
  417. }
  418. }
  419. // ------------------------------------------------------------------------
  420. if ( ! function_exists('form_submit'))
  421. {
  422. /**
  423. * Submit Button
  424. *
  425. * @param mixed
  426. * @param string
  427. * @param string
  428. * @return string
  429. */
  430. function form_submit($data = '', $value = '', $extra = '')
  431. {
  432. $defaults = array(
  433. 'type' => 'submit',
  434. 'name' => is_array($data) ? '' : $data,
  435. 'value' => $value
  436. );
  437. return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
  438. }
  439. }
  440. // ------------------------------------------------------------------------
  441. if ( ! function_exists('form_reset'))
  442. {
  443. /**
  444. * Reset Button
  445. *
  446. * @param mixed
  447. * @param string
  448. * @param string
  449. * @return string
  450. */
  451. function form_reset($data = '', $value = '', $extra = '')
  452. {
  453. $defaults = array(
  454. 'type' => 'reset',
  455. 'name' => is_array($data) ? '' : $data,
  456. 'value' => $value
  457. );
  458. return '<input '._parse_form_attributes($data, $defaults).$extra." />\n";
  459. }
  460. }
  461. // ------------------------------------------------------------------------
  462. if ( ! function_exists('form_button'))
  463. {
  464. /**
  465. * Form Button
  466. *
  467. * @param mixed
  468. * @param string
  469. * @param string
  470. * @return string
  471. */
  472. function form_button($data = '', $content = '', $extra = '')
  473. {
  474. $defaults = array(
  475. 'name' => is_array($data) ? '' : $data,
  476. 'type' => 'button'
  477. );
  478. if (is_array($data) && isset($data['content']))
  479. {
  480. $content = $data['content'];
  481. unset($data['content']); // content is not an attribute
  482. }
  483. return '<button '._parse_form_attributes($data, $defaults).$extra.'>'.$content."</button>\n";
  484. }
  485. }
  486. // ------------------------------------------------------------------------
  487. if ( ! function_exists('form_label'))
  488. {
  489. /**
  490. * Form Label Tag
  491. *
  492. * @param string The text to appear onscreen
  493. * @param string The id the label applies to
  494. * @param string Additional attributes
  495. * @return string
  496. */
  497. function form_label($label_text = '', $id = '', $attributes = array())
  498. {
  499. $label = '<label';
  500. if ($id !== '')
  501. {
  502. $label .= ' for="'.$id.'"';
  503. }
  504. if (is_array($attributes) && count($attributes) > 0)
  505. {
  506. foreach ($attributes as $key => $val)
  507. {
  508. $label .= ' '.$key.'="'.$val.'"';
  509. }
  510. }
  511. return $label.'>'.$label_text.'</label>';
  512. }
  513. }
  514. // ------------------------------------------------------------------------
  515. if ( ! function_exists('form_fieldset'))
  516. {
  517. /**
  518. * Fieldset Tag
  519. *
  520. * Used to produce <fieldset><legend>text</legend>. To close fieldset
  521. * use form_fieldset_close()
  522. *
  523. * @param string The legend text
  524. * @param array Additional attributes
  525. * @return string
  526. */
  527. function form_fieldset($legend_text = '', $attributes = array())
  528. {
  529. $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
  530. if ($legend_text !== '')
  531. {
  532. return $fieldset.'<legend>'.$legend_text."</legend>\n";
  533. }
  534. return $fieldset;
  535. }
  536. }
  537. // ------------------------------------------------------------------------
  538. if ( ! function_exists('form_fieldset_close'))
  539. {
  540. /**
  541. * Fieldset Close Tag
  542. *
  543. * @param string
  544. * @return string
  545. */
  546. function form_fieldset_close($extra = '')
  547. {
  548. return '</fieldset>'.$extra;
  549. }
  550. }
  551. // ------------------------------------------------------------------------
  552. if ( ! function_exists('form_close'))
  553. {
  554. /**
  555. * Form Close Tag
  556. *
  557. * @param string
  558. * @return string
  559. */
  560. function form_close($extra = '')
  561. {
  562. return '</form>'.$extra;
  563. }
  564. }
  565. // ------------------------------------------------------------------------
  566. if ( ! function_exists('form_prep'))
  567. {
  568. /**
  569. * Form Prep
  570. *
  571. * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
  572. *
  573. * @deprecated 3.0.0 An alias for html_escape()
  574. * @param string|string[] $str Value to escape
  575. * @return string|string[] Escaped values
  576. */
  577. function form_prep($str)
  578. {
  579. return html_escape($str, TRUE);
  580. }
  581. }
  582. // ------------------------------------------------------------------------
  583. if ( ! function_exists('set_value'))
  584. {
  585. /**
  586. * Form Value
  587. *
  588. * Grabs a value from the POST array for the specified field so you can
  589. * re-populate an input field or textarea. If Form Validation
  590. * is active it retrieves the info from the validation class
  591. *
  592. * @param string $field Field name
  593. * @param string $default Default value
  594. * @param bool $html_escape Whether to escape HTML special characters or not
  595. * @return string
  596. */
  597. function set_value($field, $default = '', $html_escape = TRUE)
  598. {
  599. $CI =& get_instance();
  600. $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  601. ? $CI->form_validation->set_value($field, $default)
  602. : $CI->input->post($field, FALSE);
  603. isset($value) OR $value = $default;
  604. return ($html_escape) ? html_escape($value) : $value;
  605. }
  606. }
  607. // ------------------------------------------------------------------------
  608. if ( ! function_exists('set_select'))
  609. {
  610. /**
  611. * Set Select
  612. *
  613. * Let's you set the selected value of a <select> menu via data in the POST array.
  614. * If Form Validation is active it retrieves the info from the validation class
  615. *
  616. * @param string
  617. * @param string
  618. * @param bool
  619. * @return string
  620. */
  621. function set_select($field, $value = '', $default = FALSE)
  622. {
  623. $CI =& get_instance();
  624. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  625. {
  626. return $CI->form_validation->set_select($field, $value, $default);
  627. }
  628. elseif (($input = $CI->input->post($field, FALSE)) === NULL)
  629. {
  630. return ($default === TRUE) ? ' selected="selected"' : '';
  631. }
  632. $value = (string) $value;
  633. if (is_array($input))
  634. {
  635. // Note: in_array('', array(0)) returns TRUE, do not use it
  636. foreach ($input as &$v)
  637. {
  638. if ($value === $v)
  639. {
  640. return ' selected="selected"';
  641. }
  642. }
  643. return '';
  644. }
  645. return ($input === $value) ? ' selected="selected"' : '';
  646. }
  647. }
  648. // ------------------------------------------------------------------------
  649. if ( ! function_exists('set_checkbox'))
  650. {
  651. /**
  652. * Set Checkbox
  653. *
  654. * Let's you set the selected value of a checkbox via the value in the POST array.
  655. * If Form Validation is active it retrieves the info from the validation class
  656. *
  657. * @param string
  658. * @param string
  659. * @param bool
  660. * @return string
  661. */
  662. function set_checkbox($field, $value = '', $default = FALSE)
  663. {
  664. $CI =& get_instance();
  665. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  666. {
  667. return $CI->form_validation->set_checkbox($field, $value, $default);
  668. }
  669. elseif (($input = $CI->input->post($field, FALSE)) === NULL)
  670. {
  671. return ($default === TRUE) ? ' checked="checked"' : '';
  672. }
  673. $value = (string) $value;
  674. if (is_array($input))
  675. {
  676. // Note: in_array('', array(0)) returns TRUE, do not use it
  677. foreach ($input as &$v)
  678. {
  679. if ($value === $v)
  680. {
  681. return ' checked="checked"';
  682. }
  683. }
  684. return '';
  685. }
  686. return ($input === $value) ? ' checked="checked"' : '';
  687. }
  688. }
  689. // ------------------------------------------------------------------------
  690. if ( ! function_exists('set_radio'))
  691. {
  692. /**
  693. * Set Radio
  694. *
  695. * Let's you set the selected value of a radio field via info in the POST array.
  696. * If Form Validation is active it retrieves the info from the validation class
  697. *
  698. * @param string $field
  699. * @param string $value
  700. * @param bool $default
  701. * @return string
  702. */
  703. function set_radio($field, $value = '', $default = FALSE)
  704. {
  705. $CI =& get_instance();
  706. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  707. {
  708. return $CI->form_validation->set_radio($field, $value, $default);
  709. }
  710. elseif (($input = $CI->input->post($field, FALSE)) === NULL)
  711. {
  712. return ($default === TRUE) ? ' checked="checked"' : '';
  713. }
  714. return ($input === (string) $value) ? ' checked="checked"' : '';
  715. }
  716. }
  717. // ------------------------------------------------------------------------
  718. if ( ! function_exists('form_error'))
  719. {
  720. /**
  721. * Form Error
  722. *
  723. * Returns the error for a specific form field. This is a helper for the
  724. * form validation class.
  725. *
  726. * @param string
  727. * @param string
  728. * @param string
  729. * @return string
  730. */
  731. function form_error($field = '', $prefix = '', $suffix = '')
  732. {
  733. if (FALSE === ($OBJ =& _get_validation_object()))
  734. {
  735. return '';
  736. }
  737. return $OBJ->error($field, $prefix, $suffix);
  738. }
  739. }
  740. // ------------------------------------------------------------------------
  741. if ( ! function_exists('validation_errors'))
  742. {
  743. /**
  744. * Validation Error String
  745. *
  746. * Returns all the errors associated with a form submission. This is a helper
  747. * function for the form validation class.
  748. *
  749. * @param string
  750. * @param string
  751. * @return string
  752. */
  753. function validation_errors($prefix = '', $suffix = '')
  754. {
  755. if (FALSE === ($OBJ =& _get_validation_object()))
  756. {
  757. return '';
  758. }
  759. return $OBJ->error_string($prefix, $suffix);
  760. }
  761. }
  762. // ------------------------------------------------------------------------
  763. if ( ! function_exists('_parse_form_attributes'))
  764. {
  765. /**
  766. * Parse the form attributes
  767. *
  768. * Helper function used by some of the form helpers
  769. *
  770. * @param array $attributes List of attributes
  771. * @param array $default Default values
  772. * @return string
  773. */
  774. function _parse_form_attributes($attributes, $default)
  775. {
  776. if (is_array($attributes))
  777. {
  778. foreach ($default as $key => $val)
  779. {
  780. if (isset($attributes[$key]))
  781. {
  782. $default[$key] = $attributes[$key];
  783. unset($attributes[$key]);
  784. }
  785. }
  786. if (count($attributes) > 0)
  787. {
  788. $default = array_merge($default, $attributes);
  789. }
  790. }
  791. $att = '';
  792. foreach ($default as $key => $val)
  793. {
  794. if ($key === 'value')
  795. {
  796. $val = html_escape($val);
  797. }
  798. elseif ($key === 'name' && ! strlen($default['name']))
  799. {
  800. continue;
  801. }
  802. $att .= $key.'="'.$val.'" ';
  803. }
  804. return $att;
  805. }
  806. }
  807. // ------------------------------------------------------------------------
  808. if ( ! function_exists('_attributes_to_string'))
  809. {
  810. /**
  811. * Attributes To String
  812. *
  813. * Helper function used by some of the form helpers
  814. *
  815. * @param mixed
  816. * @return string
  817. */
  818. function _attributes_to_string($attributes)
  819. {
  820. if (empty($attributes))
  821. {
  822. return '';
  823. }
  824. if (is_object($attributes))
  825. {
  826. $attributes = (array) $attributes;
  827. }
  828. if (is_array($attributes))
  829. {
  830. $atts = '';
  831. foreach ($attributes as $key => $val)
  832. {
  833. $atts .= ' '.$key.'="'.$val.'"';
  834. }
  835. return $atts;
  836. }
  837. if (is_string($attributes))
  838. {
  839. return ' '.$attributes;
  840. }
  841. return FALSE;
  842. }
  843. }
  844. // ------------------------------------------------------------------------
  845. if ( ! function_exists('_get_validation_object'))
  846. {
  847. /**
  848. * Validation Object
  849. *
  850. * Determines what the form validation class was instantiated as, fetches
  851. * the object and returns it.
  852. *
  853. * @return mixed
  854. */
  855. function &_get_validation_object()
  856. {
  857. $CI =& get_instance();
  858. // We set this as a variable since we're returning by reference.
  859. $return = FALSE;
  860. if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
  861. {
  862. if ( ! isset($CI->$object) OR ! is_object($CI->$object))
  863. {
  864. return $return;
  865. }
  866. return $CI->$object;
  867. }
  868. return $return;
  869. }
  870. }