PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.3
Booktics – Appointment Booking Calendar for Service Businesses v1.0.3
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / base / abstracts / model.php

model.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.3, at base/abstracts/model.php

165 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics\Abstracts;
4
5 abstract class Model {
6 protected $table;
7 protected $fillable = [];
8
9 /**
10 * @var Booktics_Database
11 */
12 protected $db;
13
14 /**
15 * Get total number of records based on conditions
16 *
17 * @param array $conditions
18 *
19 * @return int
20 */
21 public function count( array $conditions = [] ) {
22 $query = $this->db->table( $this->table );
23 foreach ( $conditions as $field => $value ) {
24 if ( is_array( $value ) ) {
25 $query->where( $field, $value[0] ?? '=', $value[1] );
26 } else {
27 $query->where( $field, '=', $value );
28 }
29 }
30
31 return $query->count();
32 }
33
34 /**
35 * Get sum of a field based on conditions
36 *
37 * @param string $field
38 * @param array $conditions
39 *
40 * @return float
41 */
42 public function sum( string $field, array $conditions = [] ) {
43 $query = $this->db->table( $this->table );
44 foreach ( $conditions as $field_name => $value ) {
45 if ( is_array( $value ) ) {
46 $query->where( $field_name, $value[0] ?? '=', $value[1] );
47 } else {
48 $query->where( $field_name, '=', $value );
49 }
50 }
51
52 return (float) $query->sum( $field );
53 }
54
55 /**
56 * Constructor for BookticsModel
57 *
58 * @param Booktics_Database $db
59 */
60 public function __construct( Booktics_Database $db ) {
61 $this->db = $db;
62 }
63
64 /**
65 * Create a new record in the database
66 *
67 * @param array $data
68 *
69 * @return mixed|false
70 */
71 public function create( array $data ) {
72 $filtered = array_intersect_key( $data, array_flip( $this->fillable ) );
73
74 $result = $this->db->table( $this->table )->insert( $filtered );
75
76 if ( $result > 0 ) {
77 return $this->find( [ 'id' => $result ] );
78 }
79
80 return false;
81 }
82
83 /**
84 * Update an existing record in the database
85 *
86 * @param int $id
87 * @param array $data
88 *
89 * @return mixed
90 */
91 public function update( int $id, array $data ) {
92 $filtered = array_intersect_key( $data, array_flip( $this->fillable ) );
93
94 return $this->db->table( $this->table )->where( 'id', '=', $id )->update( $filtered );
95 }
96
97 /**
98 * Delete a record from the database
99 *
100 * @param int $id
101 *
102 * @return mixed
103 */
104 public function delete( int $id ) {
105 return $this->db->table( $this->table )->where( 'id', '=', $id )->delete();
106 }
107
108 /**
109 * Get multiple records from the database with pagination
110 *
111 * @param array $conditions
112 * @param int $page Current page number
113 * @param int $per_page Items per page
114 *
115 * @return array
116 */
117 public function get( array $conditions = [], int $page = 1, int $per_page = 10, $order_by = [] ) {
118 $query = $this->db->table( $this->table );
119 foreach ( $conditions as $field => $value ) {
120 if ( is_array( $value ) ) {
121 $query->where( $field, $value[0] ?? '=', $value[1] );
122 } else {
123 $query->where( $field, '=', $value );
124 }
125 }
126
127 $total = $query->count();
128 $offset = ( $page - 1 ) * $per_page;
129
130 $data = $query->limit( $per_page )->offset( $offset )
131 ->order_by( $order_by )->get();
132
133 return [
134 'items' => $data,
135 'current_page' => $page,
136 'per_page' => $per_page,
137 'total' => $total,
138 'total_pages' => ceil( $total / $per_page )
139 ];
140 }
141
142 /**
143 * Find a single record from the database
144 *
145 * @param array $conditions
146 *
147 * @return mixed
148 */
149 public function find( array $conditions = [] ) {
150 $query = $this->db->table( $this->table );
151 foreach ( $conditions as $field => $value ) {
152 if ( is_array( $value ) ) {
153 $query->where( $field, $value[0] ?? '=', $value[1] );
154 } else {
155 $query->where( $field, '=', $value );
156 }
157 }
158
159 return $query->first();
160 }
161
162 public function get_table_name() {
163 return $this->table;
164 }
165 }