Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QuickForm2.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. use HTML_QuickForm2;
  11. use HTML_QuickForm2_InvalidArgumentException;
  12. use HTML_QuickForm2_Node;
  13. use HTML_QuickForm2_NotFoundException;
  14. use HTML_QuickForm2_Renderer;
  15. /**
  16. * Manages forms displayed in Twig
  17. *
  18. * For an example, @see Piwik\Plugins\Login\FormLogin
  19. *
  20. * @see HTML_QuickForm2, libs/HTML/QuickForm2.php
  21. * @link http://pear.php.net/package/HTML_QuickForm2/
  22. */
  23. abstract class QuickForm2 extends HTML_QuickForm2
  24. {
  25. protected $a_formElements = array();
  26. function __construct($id, $method = 'post', $attributes = null, $trackSubmit = false)
  27. {
  28. if (!isset($attributes['action'])) {
  29. $attributes['action'] = Url::getCurrentQueryString();
  30. }
  31. if (!isset($attributes['name'])) {
  32. $attributes['name'] = $id;
  33. }
  34. parent::__construct($id, $method, $attributes, $trackSubmit);
  35. $this->init();
  36. }
  37. /**
  38. * Class specific initialization
  39. */
  40. abstract function init();
  41. /**
  42. * The elements in this form
  43. *
  44. * @return array Element names
  45. */
  46. public function getElementList()
  47. {
  48. return $this->a_formElements;
  49. }
  50. /**
  51. * Wrapper around HTML_QuickForm2_Container's addElement()
  52. *
  53. * @param string|HTML_QuickForm2_Node $elementOrType Either type name (treated
  54. * case-insensitively) or an element instance
  55. * @param mixed $name Element name
  56. * @param mixed $attributes Element attributes
  57. * @param array $data Element-specific data
  58. * @return HTML_QuickForm2_Node Added element
  59. * @throws HTML_QuickForm2_InvalidArgumentException
  60. * @throws HTML_QuickForm2_NotFoundException
  61. */
  62. public function addElement($elementOrType, $name = null, $attributes = null,
  63. array $data = array())
  64. {
  65. if ($name != 'submit') {
  66. $this->a_formElements[] = $name;
  67. }
  68. return parent::addElement($elementOrType, $name, $attributes, $data);
  69. }
  70. function setChecked($nameElement)
  71. {
  72. foreach ($this->_elements as $key => $value) {
  73. if ($value->_attributes['name'] == $nameElement) {
  74. $this->_elements[$key]->_attributes['checked'] = 'checked';
  75. }
  76. }
  77. }
  78. function setSelected($nameElement, $value)
  79. {
  80. foreach ($this->_elements as $key => $value) {
  81. if ($value->_attributes['name'] == $nameElement) {
  82. $this->_elements[$key]->_attributes['selected'] = 'selected';
  83. }
  84. }
  85. }
  86. /**
  87. * Ported from HTML_QuickForm to minimize changes to Controllers
  88. *
  89. * @param string $elementName
  90. * @return mixed
  91. */
  92. function getSubmitValue($elementName)
  93. {
  94. $value = $this->getValue();
  95. return isset($value[$elementName]) ? $value[$elementName] : null;
  96. }
  97. public function getErrorMessages()
  98. {
  99. $messages = array();
  100. foreach ($this as $element) {
  101. $messages[] = $element->getError();
  102. }
  103. return array_filter($messages);
  104. }
  105. protected static $registered = false;
  106. /**
  107. * Returns the rendered form as an array.
  108. *
  109. * @param bool $groupErrors Whether to group errors together or not.
  110. * @return array
  111. */
  112. public function getFormData($groupErrors = true)
  113. {
  114. if (!self::$registered) {
  115. HTML_QuickForm2_Renderer::register('smarty', 'HTML_QuickForm2_Renderer_Smarty');
  116. self::$registered = true;
  117. }
  118. // Create the renderer object
  119. $renderer = HTML_QuickForm2_Renderer::factory('smarty');
  120. $renderer->setOption('group_errors', $groupErrors);
  121. // build the HTML for the form
  122. $this->render($renderer);
  123. return $renderer->toArray();
  124. }
  125. }