| 1 |
<?php |
| 2 |
/** |
| 3 |
* Booking entry |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Timetics\Core\Bookings; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Booking Entry |
| 12 |
*/ |
| 13 |
class Booking_Entry { |
| 14 |
|
| 15 |
/** |
| 16 |
* Store post type |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
private $post_type = 'timetics-schedule'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Store prefix |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $prefix = '_timetics_schedule_'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Store entry id |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private $id; |
| 35 |
|
| 36 |
/** |
| 37 |
* Store entries |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
private $entries = []; |
| 42 |
|
| 43 |
/** |
| 44 |
* Store entry data |
| 45 |
* |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
private $data = [ |
| 49 |
'meeting_id' => '', |
| 50 |
'staff_id' => '', |
| 51 |
'customer_id' => '', |
| 52 |
'booking_id' => '', |
| 53 |
'booked' => '', |
| 54 |
'date' => '', |
| 55 |
'start' => '', |
| 56 |
'end' => '', |
| 57 |
]; |
| 58 |
|
| 59 |
/** |
| 60 |
* Constructor for Booking Entry |
| 61 |
* |
| 62 |
* @param integer | Object $entry |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function __construct( $entry = 0 ) { |
| 67 |
if ( $entry instanceof self ) { |
| 68 |
$this->set_id( $entry->get_id() ); |
| 69 |
} elseif ( ! empty( $entry->ID ) ) { |
| 70 |
$this->set_id( $entry->ID ); |
| 71 |
} elseif ( is_numeric( $entry ) && $entry > 0 ) { |
| 72 |
$this->set_id( $entry ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get booking entry id |
| 78 |
* |
| 79 |
* @return integer |
| 80 |
*/ |
| 81 |
public function get_id() { |
| 82 |
return $this->id; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get meeting id |
| 87 |
* |
| 88 |
* @return integer |
| 89 |
*/ |
| 90 |
public function get_meeting_id() { |
| 91 |
return $this->get_prop( 'meeting_id' ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get staff id |
| 96 |
* |
| 97 |
* @return integer |
| 98 |
*/ |
| 99 |
public function get_staff_id() { |
| 100 |
return $this->get_prop( 'staff_id' ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get customer id |
| 105 |
* |
| 106 |
* @return integer |
| 107 |
*/ |
| 108 |
public function get_customer_id() { |
| 109 |
return $this->get_prop( 'customer_id' ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get booking id |
| 114 |
* |
| 115 |
* @return integer |
| 116 |
*/ |
| 117 |
public function get_booking_id() { |
| 118 |
return $this->get_prop( 'booking_id' ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get booked |
| 123 |
* |
| 124 |
* @return integer |
| 125 |
*/ |
| 126 |
public function get_booked() { |
| 127 |
return $this->get_prop( 'booked' ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get start time |
| 132 |
* |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public function get_start() { |
| 136 |
return $this->get_prop( 'start' ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get end time |
| 141 |
* |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
public function get_end() { |
| 145 |
return $this->get_prop( 'end' ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Get date |
| 150 |
* |
| 151 |
* @return string |
| 152 |
*/ |
| 153 |
public function get_date() { |
| 154 |
return $this->get_prop( 'date' ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get entry data |
| 159 |
* |
| 160 |
* @return array |
| 161 |
*/ |
| 162 |
public function get_data() { |
| 163 |
return [ |
| 164 |
'id' => $this->get_id(), |
| 165 |
'meeting_id' => $this->get_meeting_id(), |
| 166 |
'staff_id' => $this->get_staff_id(), |
| 167 |
'booking_id' => $this->get_booking_id(), |
| 168 |
'customer_id' => $this->get_customer_id(), |
| 169 |
'booked' => $this->get_booked(), |
| 170 |
'date' => $this->get_date(), |
| 171 |
'start' => $this->get_start(), |
| 172 |
'end' => $this->get_end(), |
| 173 |
]; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Get prop |
| 178 |
* |
| 179 |
* @param string $name |
| 180 |
* |
| 181 |
* @return mixed |
| 182 |
*/ |
| 183 |
public function get_prop( $name ) { |
| 184 |
$key = $this->prefix . $name; |
| 185 |
|
| 186 |
return get_post_meta( $this->id, $key, true ); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Find booking entry |
| 191 |
* |
| 192 |
* @param array $args |
| 193 |
* |
| 194 |
* @return array |
| 195 |
*/ |
| 196 |
public function find( $args = [] ) { |
| 197 |
$data = [ |
| 198 |
'post_type' => $this->post_type, |
| 199 |
'post_status' => 'publish', |
| 200 |
'numberposts' => -1, |
| 201 |
]; |
| 202 |
|
| 203 |
if ( $args ) { |
| 204 |
$query = [ |
| 205 |
'relation' => 'AND', |
| 206 |
]; |
| 207 |
foreach ( $args as $key => $value ) { |
| 208 |
$meta_key = $this->prefix . $key; |
| 209 |
$meta_query = [ |
| 210 |
'key' => $meta_key, |
| 211 |
'value' => $value, |
| 212 |
'compare' => '=', |
| 213 |
]; |
| 214 |
|
| 215 |
$query[] = $meta_query; |
| 216 |
} |
| 217 |
|
| 218 |
// @codingStandardsIgnoreStart |
| 219 |
$data['meta_query'] = $query; |
| 220 |
// @codingStandardsIgnoreEnd |
| 221 |
} |
| 222 |
|
| 223 |
$entries = $this->collection( get_posts( $data ) ); |
| 224 |
|
| 225 |
$this->entries = $entries; |
| 226 |
|
| 227 |
return $entries; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Get all booking entries |
| 232 |
* |
| 233 |
* @return array |
| 234 |
*/ |
| 235 |
public function all() { |
| 236 |
$entries = get_posts( |
| 237 |
[ |
| 238 |
'post_type' => $this->post_type, |
| 239 |
'post_status' => 'publish', |
| 240 |
'numberposts' => -1, |
| 241 |
] |
| 242 |
); |
| 243 |
|
| 244 |
$this->entries = $entries; |
| 245 |
$entries = $this->collection( $entries ); |
| 246 |
|
| 247 |
return $entries; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Get first entry from the entries |
| 252 |
* |
| 253 |
* @return Object | null |
| 254 |
*/ |
| 255 |
public function first() { |
| 256 |
if ( $this->entries ) { |
| 257 |
return $this->entries[0]; |
| 258 |
} |
| 259 |
|
| 260 |
return null; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Get instance of the current entry |
| 265 |
* |
| 266 |
* @param mixed $entry |
| 267 |
* |
| 268 |
* @return Object |
| 269 |
*/ |
| 270 |
private function get_instance( $entry ) { |
| 271 |
return new self( $entry ); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Get collection of object |
| 276 |
* |
| 277 |
* @param array $data |
| 278 |
* |
| 279 |
* @return array |
| 280 |
*/ |
| 281 |
private function collection( $data = [] ) { |
| 282 |
return array_map( [ $this, 'get_instance' ], $data ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Set entry id |
| 287 |
* |
| 288 |
* @param integer $id |
| 289 |
* |
| 290 |
* @return void |
| 291 |
*/ |
| 292 |
private function set_id( $id ) { |
| 293 |
$this->id = $id; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Create booking entry |
| 298 |
* |
| 299 |
* @param array $args |
| 300 |
* |
| 301 |
* @return integer |
| 302 |
*/ |
| 303 |
public function create( $args = [] ) { |
| 304 |
$args = wp_parse_args( $args, $this->data ); |
| 305 |
|
| 306 |
$entry = wp_insert_post( |
| 307 |
[ |
| 308 |
'post_title' => $this->post_type . $args['start'], |
| 309 |
'post_type' => $this->post_type, |
| 310 |
'post_status' => 'publish', |
| 311 |
'post_author' => 1, |
| 312 |
] |
| 313 |
); |
| 314 |
|
| 315 |
if ( ! is_wp_error( $entry ) ) { |
| 316 |
$this->set_id( $entry ); |
| 317 |
$this->update_metadata( $args ); |
| 318 |
} |
| 319 |
|
| 320 |
return $entry; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Update booking entry |
| 325 |
* |
| 326 |
* @param array $args |
| 327 |
* |
| 328 |
* @return void |
| 329 |
*/ |
| 330 |
public function update( $args ) { |
| 331 |
$this->update_metadata( $args ); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Update metadata |
| 336 |
* |
| 337 |
* @param array $data |
| 338 |
* |
| 339 |
* @return void |
| 340 |
*/ |
| 341 |
private function update_metadata( $data = [] ) { |
| 342 |
foreach ( $data as $key => $value ) { |
| 343 |
if ( ! array_key_exists( $key, $this->data ) ) { |
| 344 |
continue; |
| 345 |
} |
| 346 |
|
| 347 |
$meta_key = $this->prefix . $key; |
| 348 |
|
| 349 |
update_post_meta( $this->id, $meta_key, $value ); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|