PluginProbe
Bookit — Booking & Appointment Calendar / 2.6.0.5
Bookit — Booking & Appointment Calendar v2.6.0.5
2.6.0.5 2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 All 62 releases
bookit / includes / classes / database / Staff.php

Staff.php in Bookit — Booking & Appointment Calendar 2.6.0.5, at includes/classes/database/Staff.php

249 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Bookit\Classes\Database;
4
5 use Bookit\Classes\Vendor\DatabaseModel;
6
7 class Staff extends DatabaseModel {
8
9 /**
10 * Create Table
11 */
12 public static function create_table() {
13 global $wpdb;
14 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
15 $table_name = self::_table();
16 $primary_key = self::$primary_key;
17
18 $sql = "CREATE TABLE {$table_name} (
19 id INT UNSIGNED NOT NULL AUTO_INCREMENT,
20 wp_user_id BIGINT(20),
21 full_name VARCHAR(255) NOT NULL,
22 email VARCHAR(255),
23 phone VARCHAR(255),
24 PRIMARY KEY ({$primary_key})
25 ) {$wpdb->get_charset_collate()};";
26
27 maybe_create_table( $table_name, $sql );
28 }
29
30 /**
31 * Get All Staff
32 * @return mixed
33 */
34 //todo refactor
35 public static function get_all() {
36 global $wpdb;
37
38 $staffList = $wpdb->get_results(
39 sprintf(
40 'SELECT `%1$s`.*,
41 CONCAT( \'[\', GROUP_CONCAT(DISTINCT CONCAT(
42 \'{"id":\', `%2$s`.id,
43 \', "weekday":\', `%2$s`.weekday,
44 \', "start_time":"\', IFNULL(LEFT(`%2$s`.start_time, 8), "NULL"),
45 \'", "end_time":"\', IFNULL(LEFT(`%2$s`.end_time, 8), "NULL"),
46 \'", "break_from":"\', IFNULL(LEFT(`%2$s`.break_from, 8), "NULL"),
47 \'", "break_to":"\', IFNULL(LEFT(`%2$s`.break_to, 8), "NULL"),
48 \'"}\' ) ), \']\' ) as working_hours
49 FROM `%1$s`
50 LEFT JOIN `%2$s` ON `%2$s`.staff_id = `%1$s`.id
51 GROUP BY `%1$s`.`%3$s` ORDER BY `%1$s`.full_name DESC',
52 esc_sql( self::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
53 esc_sql( Staff_Working_Hours::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
54 esc_sql( static::$primary_key ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
55 ),
56 ARRAY_A
57 );
58 $services = array();
59 /** append staff service data */
60 if ( count( $staffList ) > 0 ) {
61 $services = Staff_Services::get_staff_services( array_column( $staffList, 'id' ) );
62 }
63
64 foreach ( $staffList as $key => $employee ) {
65 $staffServices = array();
66 $keys = array_keys( array_column( $services, 'staff_id' ), $employee['id'] );
67
68 for ( $i = 0; $i < count( $keys ); $i++ ) { // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
69 $service['id'] = $services[ $keys[ $i ] ]['serviceId'];
70 $service['price'] = $services[ $keys[ $i ] ]['price'];
71 $service['service_title'] = $services[ $keys[ $i ] ]['title'];
72 array_push( $staffServices, $service );
73 }
74 $staffList[ $key ]['staff_services'] = json_encode( $staffServices );
75 }
76 return $staffList;
77 }
78
79 /**
80 * Get Staff by id and service
81 * @return mixed
82 */
83 public static function get_by_id_and_service( $id, $service_id ) {
84 global $wpdb;
85 $sql = sprintf(
86 'SELECT `%1$s`.id FROM `%1$s`
87 LEFT JOIN `%2$s` ON `%1$s`.id = `%2$s`.staff_id
88 WHERE `%1$s`.id = %%d AND `%2$s`.service_id = %%d',
89 esc_sql( self::_table() ),
90 esc_sql( Staff_Services::_table() ),
91 esc_sql( static::$primary_key )
92 );
93
94 return $wpdb->get_var( $wpdb->prepare( $sql, intval( $id ), intval( $service_id ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
95 }
96
97 /**
98 * Get All Staff
99 * @return mixed
100 */
101 public static function get_all_shorted() {
102 global $wpdb;
103 return $wpdb->get_results(
104 sprintf(
105 'SELECT * FROM `%s` ORDER BY `%s` DESC',
106 esc_sql( self::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
107 esc_sql( static::$primary_key ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
108 ),
109 ARRAY_A
110 );
111 }
112
113 /**
114 * Get Staff
115 * @param $staff_id
116 * @return mixed
117 */
118 public static function get_one( $staff_id ) {
119 global $wpdb;
120 return $wpdb->get_results(
121 sprintf(
122 'SELECT `%1$s`.*,
123 CONCAT( \'[\', GROUP_CONCAT(DISTINCT CONCAT(
124 \'{"id":\', `%2$s`.id,
125 \', "category_id":\', `%2$s`.category_id,
126 \', "title":"\', `%2$s`.title,
127 \'", "price":"\', `%3$s`.price,
128 \'"}\' ) ), \']\' ) as staff_services,
129 CONCAT( \'[\', GROUP_CONCAT(DISTINCT CONCAT(
130 \'{"id":\', `%4$s`.id,
131 \', "weekday":\', `%4$s`.weekday,
132 \', "start_time":"\', IFNULL(LEFT(`%4$s`.start_time, 8), "NULL"),
133 \'", "end_time":"\', IFNULL(LEFT(`%4$s`.end_time, 8), "NULL"),
134 \'", "break_from":"\', IFNULL(LEFT(`%4$s`.break_from, 8), "NULL"),
135 \'", "break_to":"\', IFNULL(LEFT(`%4$s`.break_to, 8), "NULL"),
136 \'"}\' ) ), \']\' ) as working_hours
137 FROM `%1$s`
138 LEFT JOIN `%3$s` ON `%3$s`.staff_id = `%1$s`.id
139 LEFT JOIN `%2$s` ON `%3$s`.service_id = `%2$s`.id
140 LEFT JOIN `%4$s` ON `%4$s`.staff_id = `%1$s`.id
141 WHERE `%1$s`.id = %6$d
142 GROUP BY `%1$s`.`%5$s` ORDER BY `%1$s`.`%5$s` DESC',
143 esc_sql( self::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
144 esc_sql( Services::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
145 esc_sql( Staff_Services::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
146 esc_sql( Staff_Working_Hours::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
147 esc_sql( static::$primary_key ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
148 intval( $staff_id )
149 ),
150 ARRAY_A
151 );
152 }
153
154 /**
155 * Get All Service assosiated to staff
156 */
157 public static function get_staff_total_service( $staff_id ) {
158 global $wpdb;
159 $sql = sprintf(
160 'SELECT COUNT(`%2$s`.id)
161 FROM `%1$s`
162 LEFT JOIN `%2$s` ON `%1$s`.id = `%2$s`.staff_id
163 WHERE `%1$s`.id = %%d',
164 esc_sql( self::_table() ),
165 esc_sql( Staff_Services::_table() )
166 );
167 return $wpdb->get_var( $wpdb->prepare( $sql, intval( $staff_id ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
168 }
169
170 /**
171 * Delete Staff
172 * set staff appointments status = delete
173 * remove staff connection from service
174 * add notes about staff to appointments
175 */
176 public static function deleteStaff( $id ) {
177 global $wpdb;
178
179 $wpdb->query( 'START TRANSACTION' );
180
181 $staff = self::get( 'id', $id );
182 $staffAppointments = Appointments::staff_appointments( $id );
183
184 foreach ( $staffAppointments as $appointment ) {
185 $notes = unserialize( $appointment->notes );
186 $notes['delete_staff'] = $staff;
187
188 $wpdb->update(
189 Appointments::_table(),
190 array(
191 'notes' => serialize( $notes ),
192 'status' => Appointments::$delete,
193 ),
194 array( 'id' => $appointment->id )
195 );
196 }
197 // delete service connection
198 Staff_Services::delete_where( 'staff_id', $id );
199
200 // delete staff working hours
201 Staff_Working_Hours::delete_where( 'staff_id', $id );
202
203 // delete staff
204 self::delete( $id );
205
206 $wpdb->query( 'COMMIT' );
207 }
208
209 /**
210 * Get Staff by wp user id
211 * @param $staff_id
212 * @return mixed
213 */
214 public static function get_by_wp_user_id( $wp_user_id ) {
215 global $wpdb;
216 return $wpdb->get_results(
217 sprintf(
218 'SELECT `%1$s`.*,
219 CONCAT( \'[\', GROUP_CONCAT(DISTINCT CONCAT(
220 \'{"id":\', `%2$s`.id,
221 \', "title":"\', `%2$s`.title,
222 \'", "price":"\', `%3$s`.price,
223 \'"}\' ) ), \']\' ) as staff_services,
224 CONCAT( \'[\', GROUP_CONCAT(DISTINCT CONCAT(
225 \'{"id":\', `%4$s`.id,
226 \', "weekday":\', `%4$s`.weekday,
227 \', "start_time":"\', IFNULL(LEFT(`%4$s`.start_time, 8), "NULL"),
228 \'", "end_time":"\', IFNULL(LEFT(`%4$s`.end_time, 8), "NULL"),
229 \'", "break_from":"\', IFNULL(LEFT(`%4$s`.break_from, 8), "NULL"),
230 \'", "break_to":"\', IFNULL(LEFT(`%4$s`.break_to, 8), "NULL"),
231 \'"}\' ) ), \']\' ) as working_hours
232 FROM `%1$s`
233 LEFT JOIN `%3$s` ON `%3$s`.staff_id = `%1$s`.id
234 LEFT JOIN `%2$s` ON `%3$s`.service_id = `%2$s`.id
235 LEFT JOIN `%4$s` ON `%4$s`.staff_id = `%1$s`.id
236 WHERE `%1$s`.wp_user_id = %6$d
237 GROUP BY `%1$s`.`%5$s` ORDER BY `%1$s`.`%5$s` DESC',
238 esc_sql( self::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
239 esc_sql( Services::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
240 esc_sql( Staff_Services::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
241 esc_sql( Staff_Working_Hours::_table() ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
242 esc_sql( static::$primary_key ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
243 intval( $wp_user_id )
244 ),
245 ARRAY_A
246 );
247 }
248 }
249