FormElement.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ yPHs7xsBRRCNM4mFNT5hgCzK3V38QC7fDdi1d3dE3Ko=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.9 [rev.1.9.01]
  11. */
  12. /**
  13. * text form element view helper
  14. */
  15. namespace Cube\View\Helper;
  16. use Cube\Form\Element;
  17. class FormElement extends AbstractHelper
  18. {
  19. /**
  20. *
  21. * the form element
  22. *
  23. * @var \Cube\Form\Element
  24. */
  25. protected $_element;
  26. /**
  27. *
  28. * method to create a new form element
  29. *
  30. * @param string $element the element type
  31. * @param string $name the name of the element
  32. *
  33. * @return \Cube\Form\Element returns a form element object
  34. */
  35. protected function _createElement($element, $name)
  36. {
  37. $elementClass = '\\Cube\\Form\\Element\\' . ucfirst($element);
  38. if (class_exists($element)) {
  39. return new $element($name);
  40. }
  41. else if (class_exists($elementClass)) {
  42. return new $elementClass($name);
  43. }
  44. else {
  45. return new Element($element, $name);
  46. }
  47. }
  48. /**
  49. *
  50. * create the form element from a view script
  51. *
  52. * @param string|array $element element type or an array element
  53. * @param string $name element name
  54. * @param mixed $value value(s)
  55. *
  56. * @return \Cube\Form\Element
  57. */
  58. public function formElement($element, $name = null, $value = null)
  59. {
  60. if (is_array($element)) {
  61. $type = (!empty($element['element'])) ? $element['element'] : 'text';
  62. $name = ($name === null) ? $element['id'] : $name;
  63. $this->_element = $this->_createElement($type, $name);
  64. foreach ($element as $method => $params) {
  65. $methodName = 'set' . ucfirst($method);
  66. if (method_exists($this->_element, $methodName) && !empty($element[$method])) {
  67. $this->_element->$methodName(
  68. $this->_prepareData($params));
  69. }
  70. }
  71. }
  72. else {
  73. $type = (string)$element;
  74. $this->_element = $this->_createElement($type, $name);
  75. }
  76. $this->_element->setValue($value);
  77. // add header and body code
  78. /* @var \Cube\View\Helper\Script $helper */
  79. $helper = $this->getView()->getHelper('script');
  80. $headerCode = $this->_element->getHeaderCode();
  81. foreach ($headerCode as $code) {
  82. $helper->addHeaderCode($code);
  83. }
  84. $bodyCode = $this->_element->getBodyCode();
  85. foreach ($bodyCode as $code) {
  86. $helper->addBodyCode($code);
  87. }
  88. return $this->_element;
  89. }
  90. /**
  91. *
  92. * prepare serialized data and return it as an array which is parsable by the class methods
  93. *
  94. * @param mixed $data
  95. *
  96. * @return array
  97. */
  98. protected function _prepareData($data)
  99. {
  100. if (!is_array($data)) {
  101. $array = @unserialize($data);
  102. if ($array === false) {
  103. return $data;
  104. }
  105. $keys = (isset($array['key'])) ? array_values($array['key']) : array();
  106. $values = (isset($array['value'])) ? array_values($array['value']) : array();
  107. return array_filter(
  108. array_combine($keys, $values));
  109. }
  110. return $data;
  111. }
  112. }