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