action-types
1 year ago
admin
1 month ago
assets
1 month ago
constraints
2 years ago
export
2 months ago
models
1 month ago
query-views
1 year ago
rest-endpoints
1 year ago
controller.php
1 month ago
module.php
1 year ago
records-rest-controller.php
2 years ago
tools.php
2 years ago
tools.php
105 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Form_Record; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 | use Jet_Form_Builder\Exceptions\Silence_Exception; |
| 8 | use JFB_Modules\Form_Record\Query_Views\Record_Fields_View; |
| 9 | use Jet_Form_Builder\Classes\Tools as GlobalTools; |
| 10 | use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 11 | |
| 12 | // If this file is called directly, abort. |
| 13 | if ( ! defined( 'WPINC' ) ) { |
| 14 | die; |
| 15 | } |
| 16 | |
| 17 | class Tools { |
| 18 | |
| 19 | /** |
| 20 | * @param array $record |
| 21 | */ |
| 22 | public static function apply_context( array $record ) { |
| 23 | $fields = Record_Fields_View::get_request( $record['id'] ); |
| 24 | |
| 25 | if ( empty( $fields ) ) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | // set fields without request |
| 30 | jet_fb_context()->set_parsers( |
| 31 | Block_Helper::get_blocks_by_post( $record['form_id'] ) |
| 32 | ); |
| 33 | |
| 34 | $request = array(); |
| 35 | |
| 36 | foreach ( $fields as $field ) { |
| 37 | $attrs = GlobalTools::decode_json( $field['field_attrs'] ); |
| 38 | $parsed = iterator_to_array( self::iterate_request_line( $field ) ); |
| 39 | |
| 40 | $value = current( $parsed ); |
| 41 | $name = key( $parsed ); |
| 42 | |
| 43 | $request[ $name ] = $value; |
| 44 | |
| 45 | try { |
| 46 | jet_fb_context()->resolve_parser( $name ); |
| 47 | } catch ( Silence_Exception $exception ) { |
| 48 | jet_fb_context()->set_field_type( $field['field_type'], $name ); |
| 49 | jet_fb_context()->set_field_settings( $attrs, $name ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | jet_fb_context()->set_request( $request ); |
| 54 | jet_fb_context()->apply_request(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @since 3.1.0 |
| 59 | * |
| 60 | * @param array|\Generator $request |
| 61 | * |
| 62 | * @return \Generator |
| 63 | */ |
| 64 | public static function iterate_request( $request ): \Generator { |
| 65 | foreach ( $request as $field ) { |
| 66 | yield from self::iterate_request_line( $field ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @since 3.1.0 |
| 72 | * |
| 73 | * @param array $field |
| 74 | * |
| 75 | * @return \Generator |
| 76 | */ |
| 77 | public static function iterate_request_line( array $field ): \Generator { |
| 78 | $attrs = GlobalTools::decode_json( $field['field_attrs'] ); |
| 79 | |
| 80 | if ( empty( $attrs['is_encoded'] ) ) { |
| 81 | yield $field['field_name'] => $field['field_value']; |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | yield $field['field_name'] => GlobalTools::decode_json( $field['field_value'] ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param int $record_id |
| 90 | * |
| 91 | * @throws Sql_Exception |
| 92 | */ |
| 93 | public static function update_record( int $record_id ) { |
| 94 | if ( ! $record_id ) { |
| 95 | return; |
| 96 | } |
| 97 | $controller = ( new Controller() )->set_record_id( $record_id ); |
| 98 | $controller->set_setting( 'save_errors', jet_form_builder()->has_module( 'dev' ) ); |
| 99 | |
| 100 | $controller->save_actions(); |
| 101 | $controller->save_errors(); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 |