PageRenderTime 70ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/integry/livecart
PHP | 121 lines | 72 code | 20 blank | 29 comment | 17 complexity | 93f811e9c5138301b3472a57d56fed2c MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Renders text field
  4. *
  5. * If you wish to use autocomplete on a text field an additional parameter needs to be passed:
  6. *
  7. * <code>
  8. * autocomplete="controller=somecontroller field=fieldname"
  9. * </code>
  10. *
  11. * The controller needs to implement an autoComplete method, which must return the AutoCompleteResponse
  12. *
  13. * @param array $params
  14. * @param Smarty $smarty
  15. * @return string
  16. *
  17. * @package application.helper.smarty.form
  18. * @author Integry Systems
  19. */
  20. function smarty_function_textfield($params, Smarty_Internal_Template $smarty)
  21. {
  22. if (empty($params['name']))
  23. {
  24. $params['name'] = $smarty->getTemplateVars('input_name');
  25. }
  26. $smarty->assign('last_fieldType', 'textfield');
  27. $formParams = $smarty->_tag_stack[0][1];
  28. $formHandler = $formParams['handle'];
  29. $fieldName = $params['name'];
  30. if (!($formHandler instanceof Form))
  31. {
  32. throw new HelperException('Element must be placed in {form} block');
  33. }
  34. if (!isset($params['type']))
  35. {
  36. $params['type'] = 'text';
  37. }
  38. if (isset($params['ng_model']))
  39. {
  40. $params['ng-model'] = $params['ng_model'];
  41. unset($params['ng_model']);
  42. }
  43. else if (!empty($formParams['model']))
  44. {
  45. $params['ng-model'] = $formParams['model'] . '.' . $params['name'];
  46. }
  47. $params = $smarty->applyFieldValidation($params, $formHandler);
  48. // Check permissions
  49. if($formParams['readonly'])
  50. {
  51. $params['readonly'] = 'readonly';
  52. }
  53. $value = array_pop(array_filter(array(isset($params['value']) ? $params['value'] : '', isset($params['default']) ? $params['default'] : '', $formHandler->get($fieldName))));
  54. unset($params['value'], $params['default']);
  55. if (isset($params['autocomplete']) && ($params['autocomplete'] != 'off') && empty($params['id']))
  56. {
  57. $params['id'] = uniqid();
  58. }
  59. if (!empty($params['placeholder']))
  60. {
  61. $params['placeholder'] = $smarty->getApplication()->translate($params['placeholder']);
  62. }
  63. if (isset($params['autocomplete']) && $params['autocomplete'] != 'off')
  64. {
  65. $autocomplete = $params['autocomplete'];
  66. $params['autocomplete'] = 'off';
  67. }
  68. $content = '<input';
  69. $content = $smarty->appendParams($content, $params);
  70. $content .= ' value="' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '"';
  71. $content .= '/>';
  72. $content = $smarty->formatControl($content, $params);
  73. if (!empty($autocomplete))
  74. {
  75. $acparams = array();
  76. foreach (explode(' ', $params['autocomplete']) as $param)
  77. {
  78. list($p, $v) = explode('=', $param, 2);
  79. $acparams[$p] = $v;
  80. }
  81. $url = $smarty->getApplication()->getRouter()->createURL(array('controller' => $acparams['controller'],
  82. 'action' => 'autoComplete',
  83. 'query' => 'field=' . $acparams['field']), true);
  84. if (empty($acparams['field']))
  85. {
  86. $acparams['field'] = 'query';
  87. }
  88. /*
  89. $content .= '<script type="text/javascript">
  90. jQuery("#' . $params['id'] . '").typeahead({
  91. source: function (query, process) {
  92. return jQuery.get("' . $url . '", { ' . $acparams['field'] . ': query }, function (data) {
  93. return process(jQuery.parseJSON(data));
  94. });
  95. }});
  96. </script>';
  97. */
  98. }
  99. return $content;
  100. }
  101. ?>