| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Form_Record\Query_Views; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Db_Queries\Query_Builder; |
| 7 |
use Jet_Form_Builder\Db_Queries\Query_Conditions_Builder; |
| 8 |
use Jet_Form_Builder\Db_Queries\Views\View_Base; |
| 9 |
use JFB_Modules\Form_Record\Models; |
| 10 |
|
| 11 |
// If this file is called directly, abort. |
| 12 |
if ( ! defined( 'WPINC' ) ) { |
| 13 |
die; |
| 14 |
} |
| 15 |
|
| 16 |
class Record_View extends View_Base { |
| 17 |
|
| 18 |
/** |
| 19 |
* @since 3.1.0 |
| 20 |
*/ |
| 21 |
const AVAILABLE_STATUSES = array( |
| 22 |
'success', |
| 23 |
'failed', |
| 24 |
); |
| 25 |
|
| 26 |
protected $order_by = array( |
| 27 |
array( |
| 28 |
'column' => 'id', |
| 29 |
'sort' => self::FROM_HIGH_TO_LOW, |
| 30 |
), |
| 31 |
); |
| 32 |
|
| 33 |
public function table(): string { |
| 34 |
return Models\Record_Model::table(); |
| 35 |
} |
| 36 |
|
| 37 |
public function set_filters( array $filters ) { |
| 38 |
parent::set_filters( $filters ); |
| 39 |
|
| 40 |
$this->set_form_filter(); |
| 41 |
$this->set_date_from_filter(); |
| 42 |
$this->set_date_to_filter(); |
| 43 |
$this->set_status_filter(); |
| 44 |
|
| 45 |
return $this; |
| 46 |
} |
| 47 |
|
| 48 |
protected function set_id_filter() { |
| 49 |
$id = absint( $this->filters['id'] ?? 0 ); |
| 50 |
|
| 51 |
if ( empty( $form ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$this->add_conditions( |
| 56 |
array( |
| 57 |
array( |
| 58 |
'type' => Query_Conditions_Builder::TYPE_EQUAL, |
| 59 |
'values' => array( 'id', $id ), |
| 60 |
), |
| 61 |
) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
protected function set_form_filter() { |
| 66 |
$form = absint( $this->filters['form'] ?? '' ); |
| 67 |
|
| 68 |
if ( empty( $form ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$this->add_conditions( |
| 73 |
array( |
| 74 |
array( |
| 75 |
'type' => 'in', |
| 76 |
'values' => array( 'form_id', array( $form ) ), |
| 77 |
), |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
protected function set_date_from_filter() { |
| 83 |
$date_from = $this->filters['date_from'] ?? ''; |
| 84 |
$time = strtotime( $date_from ); |
| 85 |
|
| 86 |
if ( false === $time ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$this->add_conditions( |
| 91 |
array( |
| 92 |
array( |
| 93 |
'type' => Query_Conditions_Builder::TYPE_MORE_OR_EQUAL_STATIC, |
| 94 |
'format' => 'DATE(%s)', |
| 95 |
'format_col' => 'DATE(%s)', |
| 96 |
'values' => array( 'created_at', $date_from ), |
| 97 |
), |
| 98 |
) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
protected function set_date_to_filter() { |
| 103 |
$date_to = $this->filters['date_to'] ?? ''; |
| 104 |
$time = strtotime( $date_to ); |
| 105 |
|
| 106 |
if ( false === $time ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
$this->add_conditions( |
| 111 |
array( |
| 112 |
array( |
| 113 |
'type' => Query_Conditions_Builder::TYPE_LESS_OR_EQUAL_STATIC, |
| 114 |
'format' => 'DATE(%s)', |
| 115 |
'format_col' => 'DATE(%s)', |
| 116 |
'values' => array( 'created_at', $date_to ), |
| 117 |
), |
| 118 |
) |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
protected function set_status_filter() { |
| 123 |
$status = $this->filters['status'] ?? ''; |
| 124 |
|
| 125 |
if ( ! in_array( $status, self::AVAILABLE_STATUSES, true ) ) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
if ( 'success' !== $status ) { |
| 130 |
$this->add_conditions( |
| 131 |
array( |
| 132 |
array( |
| 133 |
'type' => Query_Conditions_Builder::TYPE_NOT_EQUAL, |
| 134 |
'values' => array( 'status', 'success' ), |
| 135 |
), |
| 136 |
array( |
| 137 |
'type' => Query_Conditions_Builder::TYPE_NOT_LIKE_END, |
| 138 |
'values' => array( 'status', 'dsuccess' ), |
| 139 |
), |
| 140 |
) |
| 141 |
); |
| 142 |
|
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
$conditions = new Query_Conditions_Builder(); |
| 147 |
$conditions->set_relation_or(); |
| 148 |
$conditions->set_condition( |
| 149 |
array( |
| 150 |
'type' => Query_Conditions_Builder::TYPE_EQUAL, |
| 151 |
'values' => array( 'status', 'success' ), |
| 152 |
) |
| 153 |
); |
| 154 |
$conditions->set_condition( |
| 155 |
array( |
| 156 |
'type' => Query_Conditions_Builder::TYPE_LIKE_END, |
| 157 |
'values' => array( 'status', 'dsuccess' ), |
| 158 |
) |
| 159 |
); |
| 160 |
|
| 161 |
$this->add_conditions( array( $conditions ) ); |
| 162 |
} |
| 163 |
|
| 164 |
public function query(): Query_Builder { |
| 165 |
$this->prepare_dependencies(); |
| 166 |
|
| 167 |
if ( ! $this->select ) { |
| 168 |
$this->set_select( Models\Record_Model::schema_columns() ); |
| 169 |
} |
| 170 |
|
| 171 |
return ( new Query_Builder() )->set_view( $this ); |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|