PageRenderTime 40ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/application/helper/smarty/form/function.checkbox.php

http://github.com/integry/livecart
PHP | 99 lines | 71 code | 16 blank | 12 comment | 19 complexity | d9e65525fea06e7334571d533ad7d929 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * ...
  4. *
  5. * @param array $params
  6. * @param Smarty $smarty
  7. * @return string
  8. *
  9. * @package application.helper.smarty.form
  10. * @author Integry Systems
  11. */
  12. function smarty_function_checkbox($params, $smarty)
  13. {
  14. if (empty($params['name']))
  15. {
  16. $params['name'] = $smarty->getTemplateVars('input_name');
  17. }
  18. if (empty($params['id']))
  19. {
  20. $params['id'] = uniqid();
  21. }
  22. $smarty->assign('last_fieldType', 'checkbox');
  23. $smarty->assign('last_fieldID', $params['id']);
  24. if (empty($params['class']))
  25. {
  26. $params['class'] = 'checkbox';
  27. }
  28. else
  29. {
  30. $params['class'] .= ' checkbox';
  31. }
  32. $formParams = $smarty->_tag_stack[0][1];
  33. $formHandler = $formParams['handle'];
  34. if (!($formHandler instanceof Form))
  35. {
  36. throw new HelperException('Element must be placed in {form} block');
  37. }
  38. $fieldName = $params['name'];
  39. if(!isset($params['value']))
  40. {
  41. $params['value'] = 1;
  42. }
  43. if (!empty($formParams['model']))
  44. {
  45. $params['ng-model'] = $formParams['model'] . '.' . $params['name'];
  46. }
  47. // Check permissions
  48. if($formParams['readonly'])
  49. {
  50. $params['disabled'] = 'disabled';
  51. }
  52. $formValue = $formHandler->get($fieldName);
  53. // get checkbox state if the form has been submitted
  54. if (1 == $formHandler->get('checkbox_' . $fieldName))
  55. {
  56. if ($formValue == $params['value'] || ('on' == $params['value'] && 1 == $formValue))
  57. {
  58. $params['checked'] = 'checked';
  59. }
  60. else
  61. {
  62. unset($params['checked']);
  63. }
  64. }
  65. else if ($params['value'] == $formValue)
  66. {
  67. $params['checked'] = 'checked';
  68. }
  69. if (empty($params['checked']))
  70. {
  71. unset($params['checked']);
  72. }
  73. $params['ng-true-value'] = 1;
  74. $params['ng-false-value'] = 0;
  75. $output = '<input type="checkbox"';
  76. foreach ($params as $name => $value)
  77. {
  78. $output .= ' ' . $name . '="' . $value . '"';
  79. }
  80. $output .= '/><input type="hidden" name="checkbox_' . $params['name'] . '" value="1" />';
  81. return $output;
  82. }
  83. ?>