| 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 |
$exclude_fields = array( |
| 220 |
'__form_id', |
| 221 |
'__refer', |
| 222 |
'__is_ajax', |
| 223 |
'_jfb_verification_token', |
| 224 |
'_jfb_verification_url', |
| 225 |
'_jfb_verification_token_id', |
| 226 |
); |
| 227 |
|
| 228 |
foreach ( jet_fb_context()->generate_request() as $field_name => $value ) { |
| 229 |
if ( |
| 230 |
( empty( $this->settings['save_empty_fields'] ) && Tools::is_empty( $value ) ) || |
| 231 |
array_key_exists( $field_name, $saved ) || |
| 232 |
in_array( $field_name, $exclude_fields ) |
| 233 |
) { |
| 234 |
continue; |
| 235 |
} |
| 236 |
|
| 237 |
$type = jet_fb_context()->get_field_type( $field_name ); |
| 238 |
$current_attrs = jet_fb_context()->get_settings( $field_name ); |
| 239 |
$attrs_to_save = $this->get_attrs_by_field_type( $type, $current_attrs ); |
| 240 |
|
| 241 |
if ( isset( $current_attrs['field_type'] ) && 'password' === $current_attrs['field_type'] ) { |
| 242 |
$value = wp_hash_password( $value ); |
| 243 |
} |
| 244 |
|
| 245 |
if ( ! is_scalar( $value ) ) { |
| 246 |
$value = Tools::encode_json( $value ); |
| 247 |
|
| 248 |
$attrs_to_save['is_encoded'] = true; |
| 249 |
} |
| 250 |
|
| 251 |
yield array( $field_name, $value, $type, $attrs_to_save ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
private function get_attrs_by_field_type( string $type, array $block ) { |
| 256 |
$list = array( 'label' ); |
| 257 |
|
| 258 |
switch ( $type ) { |
| 259 |
case 'text-field': |
| 260 |
$list[] = 'field_type'; |
| 261 |
break; |
| 262 |
} |
| 263 |
$attrs = Block_Helper::get_attrs_from_block( $block, $list ); |
| 264 |
|
| 265 |
return apply_filters( 'jet-form-builder/on-save-record/field-attributes', $attrs, $type, $block ); |
| 266 |
} |
| 267 |
|
| 268 |
private function get_prepared_actions( $source, $status ): array { |
| 269 |
$actions = array(); |
| 270 |
|
| 271 |
/** |
| 272 |
* @var $source Base[] |
| 273 |
*/ |
| 274 |
foreach ( $source as $action ) { |
| 275 |
if ( ! $action->get_executed_events() ) { |
| 276 |
continue; |
| 277 |
} |
| 278 |
foreach ( $action->get_executed_events() as $on_event ) { |
| 279 |
$action_name = ''; |
| 280 |
|
| 281 |
if ( isset( $action->editor_name ) && is_string( $action->editor_name ) ) { |
| 282 |
$action_name = trim( $action->editor_name ); |
| 283 |
} |
| 284 |
|
| 285 |
$actions[] = array( |
| 286 |
'record_id' => $this->record_id, |
| 287 |
'action_slug' => $action->get_id(), |
| 288 |
'action_id' => $action->_id, |
| 289 |
'action_name' => $action_name ? $action_name : $action->get_name(), |
| 290 |
'on_event' => $on_event, |
| 291 |
'status' => $status, |
| 292 |
); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return $actions; |
| 297 |
} |
| 298 |
|
| 299 |
public function maybe_set_user_data(): Controller { |
| 300 |
if ( empty( $this->settings['save_user_data'] ) ) { |
| 301 |
return $this; |
| 302 |
} |
| 303 |
|
| 304 |
$this->columns['user_agent'] = Http_Tools::get_user_agent(); |
| 305 |
$this->columns['ip_address'] = Http_Tools::get_ip_address(); |
| 306 |
|
| 307 |
return $this; |
| 308 |
} |
| 309 |
|
| 310 |
public function set_setting( string $key, $value ) { |
| 311 |
$this->settings[ $key ] = $value; |
| 312 |
|
| 313 |
return $this; |
| 314 |
} |
| 315 |
|
| 316 |
public function set_settings( array $source ) { |
| 317 |
$this->settings = array_merge( $this->settings, $source ); |
| 318 |
|
| 319 |
return $this; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
public function set_columns( array $columns ) { |
| 324 |
$this->columns = array_merge( $this->columns, $columns ); |
| 325 |
|
| 326 |
return $this; |
| 327 |
} |
| 328 |
|
| 329 |
} |
| 330 |
|