record-actions.php
2 years ago
record-by-payment.php
2 years ago
record-errors.php
2 years ago
record-fields-view-count.php
2 years ago
record-fields-view.php
2 years ago
record-view-count.php
2 years ago
record-view-forms.php
2 years ago
record-view.php
2 years ago
record-fields-view.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Form_Record\Query_Views; |
| 5 | |
| 6 | use Jet_Form_Builder\Db_Queries\Query_Builder; |
| 7 | use JFB_Modules\Form_Record\Models\Record_Field_Model; |
| 8 | use Jet_Form_Builder\Classes\Tools; |
| 9 | use Jet_Form_Builder\Db_Queries\Query_Conditions_Builder; |
| 10 | use Jet_Form_Builder\Db_Queries\Views\View_Base; |
| 11 | use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 12 | use JFB_Modules\Form_Record\Tools as RecordTools; |
| 13 | |
| 14 | // If this file is called directly, abort. |
| 15 | if ( ! defined( 'WPINC' ) ) { |
| 16 | die; |
| 17 | } |
| 18 | |
| 19 | class Record_Fields_View extends View_Base { |
| 20 | |
| 21 | public function table(): string { |
| 22 | return Record_Field_Model::table(); |
| 23 | } |
| 24 | |
| 25 | public static function get_request( $record_id ): array { |
| 26 | try { |
| 27 | return static::find( |
| 28 | array( |
| 29 | 'record_id' => (int) $record_id, |
| 30 | ) |
| 31 | )->query()->query_all(); |
| 32 | |
| 33 | } catch ( Query_Builder_Exception $exception ) { |
| 34 | return array(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public function set_filters( array $filters ) { |
| 39 | parent::set_filters( $filters ); |
| 40 | |
| 41 | $this->set_record_filter(); |
| 42 | $this->set_names_filter(); |
| 43 | |
| 44 | return $this; |
| 45 | } |
| 46 | |
| 47 | protected function set_record_filter(): Record_Fields_View { |
| 48 | $record_id = absint( $this->filters['record_id'] ?? 0 ); |
| 49 | |
| 50 | if ( empty( $record_id ) ) { |
| 51 | return $this; |
| 52 | } |
| 53 | |
| 54 | return $this->add_conditions( |
| 55 | array( |
| 56 | array( |
| 57 | 'type' => Query_Conditions_Builder::TYPE_EQUAL, |
| 58 | 'values' => array( 'record_id', $record_id ), |
| 59 | ), |
| 60 | ) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | protected function set_names_filter(): Record_Fields_View { |
| 65 | $names = (array) ( $this->filters['names'] ?? array() ); |
| 66 | |
| 67 | if ( empty( $names ) ) { |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | return $this->add_conditions( |
| 72 | array( |
| 73 | array( |
| 74 | 'type' => Query_Conditions_Builder::TYPE_IN, |
| 75 | 'values' => array( 'field_name', $names ), |
| 76 | ), |
| 77 | ) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @since 2.1.6 https://github.com/Crocoblock/issues-tracker/issues/1436 |
| 83 | * @since 2.0.0 Introduced |
| 84 | * |
| 85 | * @param $record_id |
| 86 | * |
| 87 | * @return array |
| 88 | */ |
| 89 | public static function get_request_list( $record_id ): array { |
| 90 | $request = static::get_request( $record_id ); |
| 91 | |
| 92 | return iterator_to_array( RecordTools::iterate_request( $request ) ); |
| 93 | } |
| 94 | |
| 95 | public function query(): Query_Builder { |
| 96 | $this->prepare_dependencies(); |
| 97 | |
| 98 | if ( ! $this->select ) { |
| 99 | $this->set_select( Record_Field_Model::schema_columns() ); |
| 100 | } |
| 101 | |
| 102 | return ( new Query_Builder() )->set_view( $this ); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | } |
| 107 |