PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.1.2
JetFormBuilder — Dynamic Blocks Form Builder v3.1.2
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / form-record / controller.php
jetformbuilder / modules / form-record Last commit date
.config 2 years ago 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 .babelrc 2 years ago .gitattributes 2 years ago controller.php 2 years ago module.php 2 years ago package.json 2 years ago records-rest-controller.php 2 years ago tools.php 2 years ago
controller.php
313 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'] ) && empty( $value ) ) ||
222 array_key_exists( $field_name, $saved )
223 ) {
224 continue;
225 }
226
227 if ( jet_fb_context()->is_secure( $field_name ) ) {
228 continue;
229 }
230
231 $type = jet_fb_context()->get_field_type( $field_name );
232 $current_attrs = jet_fb_context()->get_settings( $field_name );
233 $attrs_to_save = $this->get_attrs_by_field_type( $type, $current_attrs );
234
235 if ( ! is_scalar( $value ) ) {
236 $value = Tools::encode_json( $value );
237
238 $attrs_to_save['is_encoded'] = true;
239 }
240
241 yield array( $field_name, $value, $type, $attrs_to_save );
242 }
243 }
244
245 private function get_attrs_by_field_type( string $type, array $block ) {
246 $list = array( 'label' );
247
248 switch ( $type ) {
249 case 'text-field':
250 $list[] = 'field_type';
251 break;
252 }
253 $attrs = Block_Helper::get_attrs_from_block( $block, $list );
254
255 return apply_filters( 'jet-form-builder/on-save-record/field-attributes', $attrs, $type, $block );
256 }
257
258 private function get_prepared_actions( $source, $status ): array {
259 $actions = array();
260
261 /**
262 * @var $source Base[]
263 */
264 foreach ( $source as $action ) {
265 if ( ! $action->get_executed_events() ) {
266 continue;
267 }
268 foreach ( $action->get_executed_events() as $on_event ) {
269 $actions[] = array(
270 'record_id' => $this->record_id,
271 'action_slug' => $action->get_id(),
272 'action_id' => $action->_id,
273 'on_event' => $on_event,
274 'status' => $status,
275 );
276 }
277 }
278
279 return $actions;
280 }
281
282 public function maybe_set_user_data(): Controller {
283 if ( empty( $this->settings['save_user_data'] ) ) {
284 return $this;
285 }
286
287 $this->columns['user_agent'] = Http_Tools::get_user_agent();
288 $this->columns['ip_address'] = Http_Tools::get_ip_address();
289
290 return $this;
291 }
292
293 public function set_setting( string $key, $value ) {
294 $this->settings[ $key ] = $value;
295
296 return $this;
297 }
298
299 public function set_settings( array $source ) {
300 $this->settings = array_merge( $this->settings, $source );
301
302 return $this;
303 }
304
305
306 public function set_columns( array $columns ) {
307 $this->columns = array_merge( $this->columns, $columns );
308
309 return $this;
310 }
311
312 }
313