MultiValue.php 4.6 KB

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