action-types
2 years ago
admin
2 years ago
assets
2 years ago
constraints
2 years ago
export
2 years ago
models
2 years ago
query-views
2 years ago
rest-endpoints
2 years ago
controller.php
2 years ago
module.php
2 years ago
records-rest-controller.php
2 years ago
tools.php
2 years ago
controller.php
310 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Form_Record; |
| 5 | |
| 6 | use JFB_Modules\Form_Record\Models; |
| 7 | use JFB_Modules\Form_Record\Query_Views\Record_Fields_View; |
| 8 | use Jet_Form_Builder\Actions\Types\Base; |
| 9 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 10 | use Jet_Form_Builder\Classes\Http\Http_Tools; |
| 11 | use Jet_Form_Builder\Classes\Tools; |
| 12 | use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 13 | use Jet_Form_Builder\Dev_Mode\Logger; |
| 14 | use Jet_Form_Builder\Live_Form; |
| 15 | |
| 16 | // If this file is called directly, abort. |
| 17 | if ( ! defined( 'WPINC' ) ) { |
| 18 | die; |
| 19 | } |
| 20 | |
| 21 | class Controller { |
| 22 | |
| 23 | protected $settings = array( |
| 24 | 'save_user_data' => false, |
| 25 | 'save_empty_fields' => false, |
| 26 | 'save_errors' => false, |
| 27 | ); |
| 28 | protected $columns = array(); |
| 29 | protected $record_id; |
| 30 | protected $is_new = true; |
| 31 | |
| 32 | public function get_record_id(): int { |
| 33 | return (int) $this->record_id; |
| 34 | } |
| 35 | |
| 36 | public function set_record_id( $record_id ): Controller { |
| 37 | $this->record_id = (int) $record_id; |
| 38 | $this->is_new = false; |
| 39 | |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @return $this |
| 45 | * @throws Sql_Exception |
| 46 | */ |
| 47 | public function save() { |
| 48 | /** |
| 49 | * Saving general information about the request from the form |
| 50 | * in `*_jet_fb_records` |
| 51 | * by \JFB_Modules\Form_Record\Record_Model |
| 52 | */ |
| 53 | $this->record_id = $this->save_record(); |
| 54 | |
| 55 | /** |
| 56 | * Saving each field as a separate record |
| 57 | * in `*_jet_fb_records_fields` |
| 58 | * by \JFB_Modules\Form_Record\Record_Field_Model |
| 59 | */ |
| 60 | $this->save_fields(); |
| 61 | |
| 62 | /** |
| 63 | * We save information about completed actions |
| 64 | * in `*_jet_fb_records_actions` |
| 65 | * by \JFB_Modules\Form_Record\Record_Action_Result_Model |
| 66 | */ |
| 67 | $this->save_actions(); |
| 68 | |
| 69 | /** |
| 70 | * We save the errors that the Logger collected. |
| 71 | * in `*_jet_fb_records_errors` |
| 72 | * by \JFB_Modules\Form_Record\Record_Error_Model |
| 73 | */ |
| 74 | $this->save_errors(); |
| 75 | |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return int |
| 81 | * @throws Sql_Exception |
| 82 | */ |
| 83 | public function save_record(): int { |
| 84 | $args = jet_form_builder()->form_handler->get_response_args(); |
| 85 | $columns = array( |
| 86 | 'form_id' => jet_fb_handler()->get_form_id(), |
| 87 | 'referrer' => jet_fb_handler()->refer, |
| 88 | 'submit_type' => jet_form_builder()->form_handler->is_ajax() ? 'ajax' : 'reload', |
| 89 | 'user_id' => get_current_user_id(), |
| 90 | 'from_content_id' => Live_Form::instance()->post->ID ?? 0, |
| 91 | 'from_content_type' => Live_Form::instance()->post->post_type, /* it can be replaced by CCT slug */ |
| 92 | 'status' => $args['status'] ?? '', |
| 93 | ); |
| 94 | |
| 95 | $this->set_columns( $columns ); |
| 96 | $this->maybe_set_user_data(); |
| 97 | |
| 98 | return ( new Models\Record_Model() )->insert( $this->columns ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @return int[] |
| 103 | * @throws Sql_Exception |
| 104 | */ |
| 105 | public function save_actions(): array { |
| 106 | list( $success, $skipped, $failed ) = $this->get_chunked_actions(); |
| 107 | |
| 108 | $actions = array_merge( |
| 109 | $this->get_prepared_actions( $success, 'success' ), |
| 110 | $this->get_prepared_actions( $skipped, 'skipped' ), |
| 111 | $this->get_prepared_actions( $failed, 'failed' ) |
| 112 | ); |
| 113 | |
| 114 | return ( new Models\Record_Action_Result_Model() )->insert_many( $actions ); |
| 115 | } |
| 116 | |
| 117 | public function get_chunked_actions(): array { |
| 118 | list( $passed, $skipped ) = array( |
| 119 | jet_fb_action_handler()->get_passed_actions(), |
| 120 | jet_fb_action_handler()->get_skipped_actions(), |
| 121 | ); |
| 122 | |
| 123 | $passed_actions = array(); |
| 124 | $skipped_actions = array(); |
| 125 | $with_errors = array(); |
| 126 | |
| 127 | foreach ( jet_fb_action_handler()->get_all() as $index => $action ) { |
| 128 | if ( jet_fb_action_handler()->get_position() === $index ) { |
| 129 | continue; |
| 130 | } |
| 131 | if ( in_array( $index, $passed, true ) ) { |
| 132 | $passed_actions[] = $action; |
| 133 | } elseif ( in_array( $index, $skipped, true ) ) { |
| 134 | $skipped_actions[] = $action; |
| 135 | } else { |
| 136 | $with_errors[] = $action; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return array( |
| 141 | $passed_actions, |
| 142 | $skipped_actions, |
| 143 | $with_errors, |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @return int[] |
| 149 | * @throws Sql_Exception |
| 150 | */ |
| 151 | public function save_fields(): array { |
| 152 | $fields = $this->get_prepared_fields(); |
| 153 | |
| 154 | return ( new Models\Record_Field_Model() )->insert_many( $fields ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return int[] |
| 159 | * @throws Sql_Exception |
| 160 | */ |
| 161 | public function save_errors(): array { |
| 162 | $errors = $this->get_prepared_errors(); |
| 163 | |
| 164 | return ( new Models\Record_Error_Model() )->insert_many( $errors ); |
| 165 | } |
| 166 | |
| 167 | private function get_prepared_errors(): array { |
| 168 | $errors = array(); |
| 169 | |
| 170 | if ( empty( $this->settings['save_errors'] ) ) { |
| 171 | return array(); |
| 172 | } |
| 173 | |
| 174 | foreach ( Logger::instance()->get_logs() as $instance => $logs ) { |
| 175 | foreach ( $logs as $log ) { |
| 176 | $errors[] = array( |
| 177 | 'record_id' => $this->record_id, |
| 178 | 'name' => $instance, |
| 179 | 'message' => $log['message'], |
| 180 | 'file' => $log['file'], |
| 181 | 'line' => $log['line'], |
| 182 | 'data' => Tools::encode_json( $log['data'] ), |
| 183 | ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return $errors; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @since 2.1.6 https://github.com/Crocoblock/issues-tracker/issues/1436 |
| 192 | * @since 2.0.0 Introduced |
| 193 | * |
| 194 | * @return array |
| 195 | */ |
| 196 | private function get_prepared_fields(): array { |
| 197 | $fields = array(); |
| 198 | |
| 199 | foreach ( $this->generate_request() as list( $field_name, $value, $type, $attrs ) ) { |
| 200 | $fields[] = array( |
| 201 | 'record_id' => $this->record_id, |
| 202 | 'field_name' => $field_name, |
| 203 | 'field_type' => empty( $type ) ? 'computed' : $type, |
| 204 | 'field_value' => $value, |
| 205 | 'field_attrs' => Tools::encode_json( $attrs ), |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | return $fields; |
| 210 | } |
| 211 | |
| 212 | private function generate_request(): \Generator { |
| 213 | $saved = array(); |
| 214 | |
| 215 | if ( ! $this->is_new ) { |
| 216 | $saved = Record_Fields_View::get_request_list( $this->record_id ); |
| 217 | } |
| 218 | |
| 219 | foreach ( jet_fb_context()->generate_request() as $field_name => $value ) { |
| 220 | if ( |
| 221 | ( empty( $this->settings['save_empty_fields'] ) && Tools::is_empty( $value ) ) || |
| 222 | array_key_exists( $field_name, $saved ) || |
| 223 | jet_fb_context()->is_secure( $field_name ) |
| 224 | ) { |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | $type = jet_fb_context()->get_field_type( $field_name ); |
| 229 | $current_attrs = jet_fb_context()->get_settings( $field_name ); |
| 230 | $attrs_to_save = $this->get_attrs_by_field_type( $type, $current_attrs ); |
| 231 | |
| 232 | if ( ! is_scalar( $value ) ) { |
| 233 | $value = Tools::encode_json( $value ); |
| 234 | |
| 235 | $attrs_to_save['is_encoded'] = true; |
| 236 | } |
| 237 | |
| 238 | yield array( $field_name, $value, $type, $attrs_to_save ); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | private function get_attrs_by_field_type( string $type, array $block ) { |
| 243 | $list = array( 'label' ); |
| 244 | |
| 245 | switch ( $type ) { |
| 246 | case 'text-field': |
| 247 | $list[] = 'field_type'; |
| 248 | break; |
| 249 | } |
| 250 | $attrs = Block_Helper::get_attrs_from_block( $block, $list ); |
| 251 | |
| 252 | return apply_filters( 'jet-form-builder/on-save-record/field-attributes', $attrs, $type, $block ); |
| 253 | } |
| 254 | |
| 255 | private function get_prepared_actions( $source, $status ): array { |
| 256 | $actions = array(); |
| 257 | |
| 258 | /** |
| 259 | * @var $source Base[] |
| 260 | */ |
| 261 | foreach ( $source as $action ) { |
| 262 | if ( ! $action->get_executed_events() ) { |
| 263 | continue; |
| 264 | } |
| 265 | foreach ( $action->get_executed_events() as $on_event ) { |
| 266 | $actions[] = array( |
| 267 | 'record_id' => $this->record_id, |
| 268 | 'action_slug' => $action->get_id(), |
| 269 | 'action_id' => $action->_id, |
| 270 | 'on_event' => $on_event, |
| 271 | 'status' => $status, |
| 272 | ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return $actions; |
| 277 | } |
| 278 | |
| 279 | public function maybe_set_user_data(): Controller { |
| 280 | if ( empty( $this->settings['save_user_data'] ) ) { |
| 281 | return $this; |
| 282 | } |
| 283 | |
| 284 | $this->columns['user_agent'] = Http_Tools::get_user_agent(); |
| 285 | $this->columns['ip_address'] = Http_Tools::get_ip_address(); |
| 286 | |
| 287 | return $this; |
| 288 | } |
| 289 | |
| 290 | public function set_setting( string $key, $value ) { |
| 291 | $this->settings[ $key ] = $value; |
| 292 | |
| 293 | return $this; |
| 294 | } |
| 295 | |
| 296 | public function set_settings( array $source ) { |
| 297 | $this->settings = array_merge( $this->settings, $source ); |
| 298 | |
| 299 | return $this; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | public function set_columns( array $columns ) { |
| 304 | $this->columns = array_merge( $this->columns, $columns ); |
| 305 | |
| 306 | return $this; |
| 307 | } |
| 308 | |
| 309 | } |
| 310 |