MultiKeyValue.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ l8oVv278p6rDL94MGFVxLSixqAd9Yo+uZ6DUcXTiXQg=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.1
  11. */
  12. /**
  13. * multi key value form element
  14. *
  15. * creates an element containing an unlimited (jquery powered) list of key => value rows
  16. * the row will contain one text field for "key" and the other for "value"
  17. */
  18. namespace Ppb\Form\Element;
  19. use Cube\Form\Element;
  20. class MultiKeyValue extends Element
  21. {
  22. const FIELD_KEY = 'key';
  23. const FIELD_VALUE = 'value';
  24. /**
  25. *
  26. * type of element - override the variable from the parent class
  27. *
  28. * @var string
  29. */
  30. protected $_element = 'multiKeyValue';
  31. /**
  32. *
  33. * class constructor
  34. *
  35. * @param string $name
  36. */
  37. public function __construct($name)
  38. {
  39. parent::__construct($this->_element, $name);
  40. $translate = $this->getTranslate();
  41. $this->setBodyCode(
  42. "<script type=\"text/javascript\">" . "\n"
  43. . " function moveUp(item) { " . "\n"
  44. . " var before = item.prev(); " . "\n"
  45. . " item.insertBefore(before); " . "\n"
  46. . " } " . "\n"
  47. . " function moveDown(item) { " . "\n"
  48. . " var after = item.next(); " . "\n"
  49. . " item.insertAfter(after); " . "\n"
  50. . " } " . "\n"
  51. . " $(document).on('click', '.delete-field-row', function(e) { " . "\n"
  52. . " e.preventDefault(); " . "\n"
  53. . " $(this).closest('.field-row').remove(); " . "\n"
  54. . " }); " . "\n"
  55. . " $(document).on('click', '.add-field-row', function(e) { " . "\n"
  56. . " e.preventDefault(); " . "\n"
  57. . " var parent = $(this).closest('.form-group').find('.multi-key-value-rows'); " . "\n"
  58. . " var row = $(this).closest('.field-row'); " . "\n"
  59. . " var cloned = row.clone(true, true); " . "\n"
  60. . " row.find('input[type=text]').val(''); " . "\n"
  61. . " cloned.find('.add-field-row').remove(); " . "\n"
  62. . " $('<a>').attr('href', '#').attr('class', 'btn-fld-move-up').html(' <i class=\"fa fa-angle-up\"></i>').appendTo(cloned); " . "\n"
  63. . " $('<a>').attr('href', '#').attr('class', 'btn-fld-move-down').html('<i class=\"fa fa-angle-down\"></i>&nbsp;').appendTo(cloned); " . "\n"
  64. . " $('<button>').attr('class', 'delete-field-row btn btn-default').html('" . $translate->_('Delete') . "').appendTo(cloned); " . "\n"
  65. . " parent.append(cloned); " . "\n"
  66. . " }); " . "\n"
  67. . " $(document).on('click', '.btn-fld-move-up', function(e) { " . "\n"
  68. . " e.preventDefault(); " . "\n"
  69. . " var row = $(this).closest('.field-row'); " . "\n"
  70. . " moveUp(row); " . "\n"
  71. . " }); " . "\n"
  72. . " $(document).on('click', '.btn-fld-move-down', function(e) { " . "\n"
  73. . " e.preventDefault(); " . "\n"
  74. . " var row = $(this).closest('.field-row'); " . "\n"
  75. . " moveDown(row); " . "\n"
  76. . " }); " . "\n"
  77. . "</script>");
  78. }
  79. /**
  80. *
  81. * render the form element
  82. *
  83. * @return string
  84. */
  85. public function render()
  86. {
  87. $output = null;
  88. $values = $this->getValue();
  89. $output .= '<div class="multi-key-value-rows">';
  90. foreach ((array)$values[self::FIELD_KEY] as $id => $key) {
  91. if (!empty($key)) {
  92. $output .= $this->_renderRow(false, $key, $values[self::FIELD_VALUE][$id]);
  93. }
  94. }
  95. $output .= '</div>';
  96. $output .= $this->_renderRow();
  97. return $output;
  98. }
  99. /**
  100. *
  101. * render a single row of the element
  102. *
  103. * @param bool $new
  104. * @param string $key
  105. * @param string $value
  106. *
  107. * @return string
  108. */
  109. protected function _renderRow($new = true, $key = null, $value = null)
  110. {
  111. $translate = $this->getTranslate();
  112. $placeholder = $this->getAttribute('placeholder');
  113. if (!empty($placeholder)) {
  114. $placeholder .= ' ';
  115. }
  116. $this->removeAttribute('placeholder')->addAttribute('placeholder', $translate->_('Key'));
  117. $output = '<div class="field-row">'
  118. . ' <input type="text" name="' . $this->_name . '[' . self::FIELD_KEY . '][]" '
  119. . $this->renderAttributes()
  120. . 'value="' . $key . '" '
  121. . $this->_endTag;
  122. $this->removeAttribute('placeholder')->addAttribute('placeholder', $translate->_('Value'));
  123. $output .= ' <input type="text" name="' . $this->_name . '[' . self::FIELD_VALUE . '][]" '
  124. . $this->renderAttributes()
  125. . 'value="' . $value . '" '
  126. . $this->_endTag
  127. . (($new === true) ?
  128. ' <button class="add-field-row btn btn-default">' . $translate->_('Add') . '</button>' :
  129. ' <a class="btn-fld-move-up" href="#"><i class="fa fa-angle-up"></i></a><a class="btn-fld-move-down" href="#"><i class="fa fa-angle-down"></i></a>
  130. <button class="delete-field-row btn btn-default">' . $translate->_('Delete') . '</button>')
  131. . '</div>';
  132. return $output;
  133. }
  134. }