actions
4 years ago
addons
4 years ago
admin
4 years ago
blocks
4 years ago
classes
4 years ago
compatibility
4 years ago
db-queries
4 years ago
dev-mode
4 years ago
exceptions
4 years ago
form-actions
4 years ago
form-messages
4 years ago
form-patterns
4 years ago
form-response
4 years ago
gateways
4 years ago
generators
4 years ago
integrations
4 years ago
presets
4 years ago
request
4 years ago
rest-api
4 years ago
shortcodes
4 years ago
widgets
4 years ago
autoloader.php
4 years ago
file-upload.php
4 years ago
form-break.php
4 years ago
form-handler.php
4 years ago
form-manager.php
4 years ago
live-form.php
4 years ago
plugin.php
4 years ago
post-type.php
4 years ago
form-break.php
236 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 | use Jet_Form_Builder\Classes\Get_Template_Trait; |
| 8 | use Jet_Form_Builder\Classes\Tools; |
| 9 | |
| 10 | class Form_Break { |
| 11 | |
| 12 | use Get_Template_Trait; |
| 13 | |
| 14 | private $pages = 0; |
| 15 | private $form_breaks = array(); |
| 16 | private $count_form_breaks = null; |
| 17 | private $current_page = 1; |
| 18 | private $current_form_break = 0; |
| 19 | private $is_editor = false; |
| 20 | private $progress_type = 'default'; |
| 21 | |
| 22 | public function get_pages() { |
| 23 | return $this->pages; |
| 24 | } |
| 25 | |
| 26 | public function get_breaks() { |
| 27 | return $this->form_breaks; |
| 28 | } |
| 29 | |
| 30 | public function get_count_breaks() { |
| 31 | if ( is_null( $this->count_form_breaks ) ) { |
| 32 | $this->count_form_breaks = count( $this->form_breaks ); |
| 33 | } |
| 34 | |
| 35 | return $this->count_form_breaks; |
| 36 | } |
| 37 | |
| 38 | public function get_current() { |
| 39 | return $this->current_page; |
| 40 | } |
| 41 | |
| 42 | public function set_progress_type( $type ) { |
| 43 | if ( ! is_string( $type ) ) { |
| 44 | return; |
| 45 | } |
| 46 | $this->progress_type = $type; |
| 47 | } |
| 48 | |
| 49 | public function set_editor_mode( $is_editor ) { |
| 50 | if ( ! $is_editor ) { |
| 51 | $this->is_editor = false; |
| 52 | $this->pages = 0; |
| 53 | $this->form_breaks = array(); |
| 54 | $this->count_form_breaks = 0; |
| 55 | |
| 56 | return $this; |
| 57 | } |
| 58 | |
| 59 | $this->is_editor = true; |
| 60 | $this->pages = 2; |
| 61 | $this->form_breaks = array( |
| 62 | array( |
| 63 | 'label' => 'Start Page', |
| 64 | ), |
| 65 | array( |
| 66 | 'label' => 'Second Page', |
| 67 | ), |
| 68 | array( |
| 69 | 'label' => 'Last Page', |
| 70 | ), |
| 71 | ); |
| 72 | |
| 73 | return $this; |
| 74 | } |
| 75 | |
| 76 | public function add_progress( $last_progress ) { |
| 77 | $this->form_breaks[] = $last_progress; |
| 78 | |
| 79 | return $this; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | public function set_pages( $blocks, $last_page_from_blocks = true ) { |
| 84 | $count_blocks = count( $blocks ); |
| 85 | $last_break = false; |
| 86 | |
| 87 | foreach ( $blocks as $index => $field ) { |
| 88 | if ( ! stripos( $field['blockName'] ?? '', 'form-break' ) ) { |
| 89 | continue; |
| 90 | } |
| 91 | $form_break = Plugin::instance()->blocks->get_field_attrs( $field['blockName'], $field['attrs'] ); |
| 92 | |
| 93 | if ( $last_page_from_blocks && $index + 1 === $count_blocks ) { |
| 94 | $last_break = $form_break; |
| 95 | unset( $blocks[ $index ] ); |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | $this->pages ++; |
| 100 | $this->form_breaks[] = $form_break; |
| 101 | } |
| 102 | if ( $last_page_from_blocks && ! empty( $this->form_breaks ) ) { |
| 103 | $this->form_breaks[] = $last_break ? $last_break : array( 'label' => __( 'Last Page' ) ); |
| 104 | } |
| 105 | |
| 106 | return $blocks; |
| 107 | } |
| 108 | |
| 109 | public function with_progress_wrapper( $content, $additional_classes = array() ) { |
| 110 | $type = $this->progress_type ?: 'default'; |
| 111 | |
| 112 | $classes = array_merge( |
| 113 | array( |
| 114 | 'jet-form-builder-progress-pages', |
| 115 | "jfb-progress-type--$type", |
| 116 | ), |
| 117 | $additional_classes |
| 118 | ); |
| 119 | |
| 120 | return sprintf( |
| 121 | '<div class="%1$s" data-type="%2$s">%3$s</div>', |
| 122 | implode( ' ', $classes ), |
| 123 | $type, |
| 124 | $content |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | public function render_default_progress() { |
| 129 | ob_start(); |
| 130 | include Tools::get_global_template( 'fields/progress-bar.php' ); |
| 131 | |
| 132 | return ob_get_clean(); |
| 133 | } |
| 134 | |
| 135 | public function render_progress( $type = '', $additional_classes = array() ) { |
| 136 | if ( $type ) { |
| 137 | $this->set_progress_type( $type ); |
| 138 | } |
| 139 | |
| 140 | switch ( $this->progress_type ) { |
| 141 | case 'default': |
| 142 | $content = $this->render_default_progress(); |
| 143 | break; |
| 144 | default: |
| 145 | $content = apply_filters( "jet-form-builder/render/progress-bar/$this->progress_type", '', $this ); |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | return $this->with_progress_wrapper( $content, $additional_classes ); |
| 150 | } |
| 151 | |
| 152 | public function get_progress_item_class( $index ) { |
| 153 | $classes = array( 'jet-form-builder-progress-pages__item--wrapper' ); |
| 154 | |
| 155 | if ( $this->is_editor ) { |
| 156 | if ( 0 === $index ) { |
| 157 | $classes[] = 'passed-page'; |
| 158 | } |
| 159 | if ( 1 === $index ) { |
| 160 | $classes[] = 'active-page'; |
| 161 | } |
| 162 | } elseif ( $index === $this->current_form_break ) { |
| 163 | $classes[] = 'active-page'; |
| 164 | } elseif ( $index < $this->current_form_break ) { |
| 165 | $classes[] = 'passed-page'; |
| 166 | } |
| 167 | |
| 168 | return implode( ' ', $classes ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Maybe start new page |
| 173 | * |
| 174 | * @param bool $force_first |
| 175 | * |
| 176 | * @return string |
| 177 | */ |
| 178 | public function maybe_start_page( $force_first = false ) { |
| 179 | |
| 180 | if ( 0 >= $this->pages ) { |
| 181 | return ''; |
| 182 | } |
| 183 | |
| 184 | if ( $force_first ) { |
| 185 | $this->current_page = 1; |
| 186 | $this->current_form_break = 0; |
| 187 | } else { |
| 188 | $this->current_page ++; |
| 189 | $this->current_form_break ++; |
| 190 | } |
| 191 | |
| 192 | ob_start(); |
| 193 | do_action( 'jet-form-builder/before-page-start', $this ); |
| 194 | |
| 195 | $hidden_class = ''; |
| 196 | |
| 197 | if ( 1 < $this->current_page ) { |
| 198 | $hidden_class = 'jet-form-builder-page--hidden'; |
| 199 | } |
| 200 | |
| 201 | include Tools::get_global_template( 'common/start-page.php' ); |
| 202 | |
| 203 | do_action( 'jet-form-builder/after-page-start', $this ); |
| 204 | |
| 205 | return ob_get_clean(); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Maybe start new page |
| 210 | * |
| 211 | * @param $is_last |
| 212 | * @param $field |
| 213 | * |
| 214 | * @return string |
| 215 | */ |
| 216 | public function maybe_end_page( $is_last = false, $field = false ) { |
| 217 | if ( 0 >= $this->pages ) { |
| 218 | return ''; |
| 219 | } |
| 220 | |
| 221 | if ( ! $is_last && 'form-break-field' !== Block_Helper::delete_namespace( $field ) ) { |
| 222 | return ''; |
| 223 | } |
| 224 | |
| 225 | ob_start(); |
| 226 | do_action( 'jet-form-builder/before-page-end', $this ); |
| 227 | |
| 228 | include Tools::get_global_template( 'common/end-page.php' ); |
| 229 | |
| 230 | do_action( 'jet-form-builder/after-page-end', $this ); |
| 231 | |
| 232 | return ob_get_clean(); |
| 233 | } |
| 234 | |
| 235 | } |
| 236 |