buttons
3 years ago
exceptions
3 years ago
notices
3 years ago
pages
3 years ago
single-pages
3 years ago
table-views
3 years ago
tabs-handlers
3 years ago
vui-boxes
3 years ago
admin-page-interface.php
3 years ago
admin-page-trait.php
3 years ago
editor.php
3 years ago
table-advanced-record-prepare-trait.php
3 years ago
table-record-prepare-trait.php
3 years ago
table-record-prepare-trait.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Admin; |
| 5 | |
| 6 | |
| 7 | trait Table_Record_Prepare_Trait { |
| 8 | |
| 9 | abstract public function get_columns_handlers(): array; |
| 10 | |
| 11 | abstract public function get_columns_headings(): array; |
| 12 | |
| 13 | abstract public function get_raw_list( array $args ): array; |
| 14 | |
| 15 | abstract public function get_list(): array; |
| 16 | |
| 17 | public function prepare_list( array $custom_list = array() ): array { |
| 18 | $list = empty( $custom_list ) ? $this->get_list() : $custom_list; |
| 19 | |
| 20 | if ( ! $list || ! is_array( $list ) ) { |
| 21 | return array(); |
| 22 | } |
| 23 | $prepared = array(); |
| 24 | |
| 25 | foreach ( $list as $item_id => $record ) { |
| 26 | $prepared[ $item_id ] = $this->prepare_record( $record ); |
| 27 | } |
| 28 | |
| 29 | return $prepared; |
| 30 | } |
| 31 | |
| 32 | public function before_prepare_record( array $record, $column_attrs, $column_name ) { |
| 33 | } |
| 34 | |
| 35 | public function after_prepare_record( $prepared, array $record, $column_name ) { |
| 36 | } |
| 37 | |
| 38 | public function prepare_record( $record ): array { |
| 39 | $prepared = array(); |
| 40 | |
| 41 | foreach ( $this->get_columns_handlers() as $column_name => $column_attrs ) { |
| 42 | $this->before_prepare_record( $record, $column_attrs, $column_name ); |
| 43 | |
| 44 | $prepared[ $column_name ] = $this->prepare_column_attrs( $record, $column_attrs, $column_name ); |
| 45 | |
| 46 | $this->after_prepare_record( $prepared[ $column_name ], $record, $column_name ); |
| 47 | } |
| 48 | |
| 49 | return $prepared; |
| 50 | } |
| 51 | |
| 52 | public function prepare_column_attrs( array $record, $column_attrs, $column_name ): array { |
| 53 | $value = $column_attrs['value'] ?? $record[ $column_name ] ?? false; |
| 54 | $column_attrs['type'] = $column_attrs['type'] ?? 'string'; |
| 55 | $column_attrs['default'] = $column_attrs['default'] ?? false; |
| 56 | |
| 57 | $default = $this->prepare_record_value_type( $column_attrs['default'], $column_attrs ); |
| 58 | |
| 59 | if ( ! is_callable( $value ) ) { |
| 60 | $column_attrs['value'] = $value ?: $default; |
| 61 | } else { |
| 62 | $column_attrs['value'] = $this->prepare_record_value_type( |
| 63 | call_user_func( $value, $record, $default ), |
| 64 | $column_attrs |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | return $column_attrs; |
| 69 | } |
| 70 | |
| 71 | public function transform_to_columns_values( array $columns ): array { |
| 72 | $transformed = array(); |
| 73 | |
| 74 | foreach ( $this->get_columns_handlers() as $column_name => $column_attrs ) { |
| 75 | if ( ! isset( $columns[ $column_name ] ) ) { |
| 76 | continue; |
| 77 | } |
| 78 | $column_attrs['type'] = $column_attrs['type'] ?? 'string'; |
| 79 | $column_attrs['default'] = $column_attrs['default'] ?? false; |
| 80 | $column_attrs['value'] = $columns[ $column_name ]; |
| 81 | |
| 82 | $transformed[ $column_name ] = $column_attrs; |
| 83 | } |
| 84 | |
| 85 | return $transformed; |
| 86 | } |
| 87 | |
| 88 | protected function prepare_record_value_type( $value, $column ) { |
| 89 | $type = $column['type'] ?? 'string'; |
| 90 | |
| 91 | switch ( $type ) { |
| 92 | case 'integer': |
| 93 | return (int) $value; |
| 94 | case 'float': |
| 95 | return (float) $value; |
| 96 | case 'array': |
| 97 | return (array) $value; |
| 98 | case 'string': |
| 99 | return (string) $value; |
| 100 | case 'rawArray': |
| 101 | default: |
| 102 | return $value; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } |