action-button-render.php
4 years ago
base-select-radio-check.php
4 years ago
base.php
4 years ago
calculated-field-render.php
4 years ago
checkbox-field-render.php
4 years ago
date-field-render.php
4 years ago
datetime-field-render.php
4 years ago
form-builder.php
4 years ago
group-break-field-render.php
4 years ago
heading-field-render.php
4 years ago
media-field-render.php
4 years ago
number-field-render.php
4 years ago
radio-field-render.php
4 years ago
range-field-render.php
4 years ago
repeater-field-render.php
4 years ago
select-field-render.php
4 years ago
text-field-render.php
4 years ago
textarea-field-render.php
4 years ago
time-field-render.php
4 years ago
wysiwyg-field-render.php
4 years ago
repeater-field-render.php
141 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Blocks\Render; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 | |
| 8 | if ( ! defined( 'WPINC' ) ) { |
| 9 | die; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Define text field renderer class |
| 14 | */ |
| 15 | class Repeater_Field_Render extends Base { |
| 16 | |
| 17 | public $current_repeater_i = false; |
| 18 | public $current_repeater; |
| 19 | |
| 20 | public function get_name() { |
| 21 | return 'repeater-field'; |
| 22 | } |
| 23 | |
| 24 | public function render( $wp_block = null, $template = null ) { |
| 25 | if ( empty( $wp_block['innerBlocks'] ) ) { |
| 26 | return ''; |
| 27 | } |
| 28 | |
| 29 | $this->add_attribute( 'class', 'jet-form-builder-repeater jet-form-builder__field' ); |
| 30 | $this->add_attribute( 'class', $this->maybe_get_error_class( $this->block_type->block_attrs ) ); |
| 31 | $this->add_attribute( 'class', $this->block_type->block_attrs['class_name'] ); |
| 32 | |
| 33 | $template = sprintf( |
| 34 | '<template class="jet-form-builder-repeater__initial">%1$s</template>', |
| 35 | $this->with_repeater_row_wrapper( $this->block_type->block_content ) |
| 36 | ); |
| 37 | |
| 38 | $html = '<div class="jet-form-builder__field-wrap">'; |
| 39 | |
| 40 | $html .= sprintf( |
| 41 | '<div %5$s data-repeater="1" |
| 42 | data-field-name="%1$s" name="%1$s" data-settings="%2$s" %3$s>%4$s', |
| 43 | esc_attr( $this->block_type->block_attrs['name'] ), |
| 44 | $this->block_type->settings, |
| 45 | $this->block_type->calc_dataset, |
| 46 | $template, |
| 47 | $this->get_attributes_string() |
| 48 | ); |
| 49 | |
| 50 | $html .= sprintf( |
| 51 | '<div class="jet-form-builder-repeater__items">%s</div>', |
| 52 | $this->maybe_render_rows( $wp_block ) |
| 53 | ); |
| 54 | |
| 55 | if ( 'manually' === $this->block_type->manage_items ) { |
| 56 | $html .= sprintf( |
| 57 | '<div class="jet-form-builder-repeater__actions"> |
| 58 | <button type="button" class="jet-form-builder-repeater__new">%1$s</button> |
| 59 | </div>', |
| 60 | wp_kses_post( $this->block_type->new_item_label ) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | $html .= '</div>'; |
| 65 | $html .= $this->maybe_render_error( $this->block_type->block_attrs ); |
| 66 | $html .= '</div>'; |
| 67 | |
| 68 | return parent::render( null, $html ); |
| 69 | } |
| 70 | |
| 71 | public function maybe_render_rows( $wp_block ) { |
| 72 | $values = $this->block_type->get_current_repeater( 'values' ); |
| 73 | $response = ''; |
| 74 | |
| 75 | if ( empty( $values ) || ! is_array( $values ) ) { |
| 76 | return $response; |
| 77 | } |
| 78 | |
| 79 | for ( $i = 0; $i < count( $values ); $i ++ ) { |
| 80 | $response .= $this->render_repeater_row( $wp_block, $i ); |
| 81 | } |
| 82 | |
| 83 | $this->block_type->set_current_repeater( |
| 84 | array( |
| 85 | 'index' => false, |
| 86 | ) |
| 87 | ); |
| 88 | |
| 89 | return $response; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Render current repeater row |
| 94 | * |
| 95 | * @param $wp_block |
| 96 | * @param int $index |
| 97 | * |
| 98 | * @return string |
| 99 | */ |
| 100 | public function render_repeater_row( $wp_block, $index = 0 ) { |
| 101 | $html = ''; |
| 102 | |
| 103 | $this->block_type->set_current_repeater( |
| 104 | array( |
| 105 | 'index' => $index, |
| 106 | ) |
| 107 | ); |
| 108 | |
| 109 | foreach ( $wp_block['innerBlocks'] as $block ) { |
| 110 | $html .= Block_Helper::render_with_context( |
| 111 | $block, |
| 112 | array( |
| 113 | 'jet-forms/repeater-field--name' => $this->block_type->parent_repeater_name(), |
| 114 | ) |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | return $this->with_repeater_row_wrapper( $html, $index ); |
| 119 | } |
| 120 | |
| 121 | public function with_repeater_row_wrapper( $content, $index = 0 ) { |
| 122 | $html = sprintf( |
| 123 | '<div class="jet-form-builder-repeater__row" data-repeater-row="1" data-index="%1$s" %2$s>', |
| 124 | $index, |
| 125 | $this->block_type->calc_dataset |
| 126 | ); |
| 127 | |
| 128 | $html .= sprintf( '<div class="jet-form-builder-repeater__row-fields">%s</div>', $content ); |
| 129 | |
| 130 | if ( 'manually' === $this->block_type->manage_items ) { |
| 131 | $html .= '<div class="jet-form-builder-repeater__row-remove">'; |
| 132 | $html .= '<button type="button" class="jet-form-builder-repeater__remove">×</button>'; |
| 133 | $html .= '</div>'; |
| 134 | } |
| 135 | |
| 136 | return $html . '</div>'; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | } |
| 141 |