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