/** * @param array $post * @param array $files */ public function bind(array $post, array $files = array()) { parent::bind($post); if (isset($files['avatar']) && $files['avatar']['tmp_name']) { if (in_array($files['avatar']['type'], array("image/gif", "image/jpeg", "image/png"))) { $uploaded = wp_handle_upload($files['avatar'], array('test_form' => false)); if ($uploaded) { $editor = wp_get_image_editor($uploaded['file']); $editor->resize(200, 200); $editor->save($uploaded['file']); $this->data['avatar_path'] = $uploaded['file']; $this->data['avatar_url'] = $uploaded['url']; // Remove old image. $staff = new AB_Staff(); $staff->load($post['id']); if (file_exists($staff->get('avatar_path'))) { unlink($staff->get('avatar_path')); } } } } }
/** * Get staff name. * * @return string */ public function getStaffName() { $staff_id = $this->getStaffId(); if ($staff_id) { $staff = new AB_Staff(); $staff->load($staff_id); return $staff->get('full_name'); } return __('Any', 'bookly'); }
/** * Prepare data for email. * * @param AB_CustomerAppointment $ca * @return array */ private static function _prepareData(AB_CustomerAppointment $ca) { $appointment = new AB_Appointment(); $appointment->load($ca->get('appointment_id')); $customer = new AB_Customer(); $customer->load($ca->get('customer_id')); $staff = new AB_Staff(); $staff->load($appointment->get('staff_id')); $service = new AB_Service(); $service->load($appointment->get('service_id')); $staff_service = new AB_StaffService(); $staff_service->loadBy(array('staff_id' => $staff->get('id'), 'service_id' => $service->get('id'))); $price = $staff_service->get('price'); if ($ca->get('coupon_discount') or $ca->get('coupon_deduction')) { $coupon = new AB_Coupon(); $coupon->set('discount', $ca->get('coupon_discount')); $coupon->set('deduction', $ca->get('coupon_deduction')); $price = $coupon->apply($price); } $codes = new AB_NotificationCodes(); $codes->set('appointment_datetime', $appointment->get('start_date')); $codes->set('appointment_token', $ca->get('token')); $codes->set('category_name', $service->getCategoryName()); $codes->set('client_name', $customer->get('name')); $codes->set('client_phone', $customer->get('phone')); $codes->set('client_email', $customer->get('email')); $codes->set('custom_fields', $ca->getFormattedCustomFields('text')); $codes->set('custom_fields_2c', $ca->getFormattedCustomFields('html')); $codes->set('number_of_persons', $ca->get('number_of_persons')); $codes->set('service_name', $service->getTitle()); $codes->set('service_price', $price); $codes->set('staff_name', $staff->get('full_name')); $codes->set('staff_email', $staff->get('email')); $codes->set('staff_phone', $staff->get('phone')); $codes->set('staff_photo', $staff->get('avatar_url')); return array($codes, $staff, $appointment, $customer); }
/** * Get item data for cart. * * @param $other_data * @param $cart_item * * @return array */ function getItemData($other_data, $cart_item) { if (isset($cart_item['bookly'])) { $info_name = get_option('ab_woocommerce_cart_info_name'); $info_value = get_option('ab_woocommerce_cart_info_value'); $staff = new AB_Staff(); $staff->load($cart_item['bookly']['staff_ids'][0]); $service = new AB_Service(); $service->load($cart_item['bookly']['service_id']); $info_value = strtr($info_value, array('[[APPOINTMENT_TIME]]' => AB_DateTimeUtils::formatTime($cart_item['bookly']['appointment_datetime']), '[[APPOINTMENT_DATE]]' => AB_DateTimeUtils::formatDate($cart_item['bookly']['appointment_datetime']), '[[CATEGORY_NAME]]' => $service->getCategoryName(), '[[SERVICE_NAME]]' => $service->getTitle(), '[[SERVICE_PRICE]]' => $service->get('price'), '[[STAFF_NAME]]' => $staff->get('full_name'))); $other_data[] = array('name' => $info_name, 'value' => $info_value); } return $other_data; }
public function executeRenderPayment() { $form_id = $this->getParameter('form_id'); $response = null; if ($form_id) { $payment_disabled = AB_BookingConfiguration::isPaymentDisabled(); $this->userData = new AB_UserBookingData($form_id); $this->userData->load(); if ($this->userData->hasData()) { if ($this->userData->getServicePrice() <= 0) { $payment_disabled = true; } } if ($payment_disabled == false) { $this->form_id = $form_id; $this->info_text = nl2br(esc_html(get_option('ab_appearance_text_info_fourth_step'))); $this->info_text_coupon = $this->_prepareInfoText(4, $this->userData); if ($this->userData->hasData()) { $employee = new AB_Staff(); $employee->load($this->userData->getStaffId()); $service = new AB_Service(); $service->load($this->userData->getServiceId()); $price = $this->getWpdb()->get_var($this->getWpdb()->prepare(' SELECT price FROM ab_staff_service WHERE staff_id = %d AND service_id = %d', $employee->get('id'), $service->get('id'))); $this->_prepareProgressTracker(4, $price); // Set response. $response = array('status' => 'success', 'html' => $this->render('payment', array(), false)); } else { if (isset($_SESSION['tmp_booking_data'])) { $tmp_booking_data = AB_CommonUtils::getTemporaryBookingData(); if (!empty($tmp_booking_data)) { $tmp_form_id = $tmp_booking_data['form_id']; if (isset($_SESSION['appointment_booking'][$tmp_form_id]) && $_SESSION['appointment_booking'][$tmp_form_id]['cancelled'] === true) { $employee = new AB_Staff(); $employee->load($tmp_booking_data['staff_id'][0]); $service = new AB_Service(); $service->load($tmp_booking_data['service_id']); $price = $this->getWpdb()->get_var($this->getWpdb()->prepare(' SELECT price FROM ab_staff_service WHERE staff_id = %d AND service_id = %d', $employee->get('id'), $service->get('id'))); // create a paypal object $paypal = new PayPal(); $product = new stdClass(); $product->name = $service->get('title'); $product->desc = $service->getTitleWithDuration(); $product->price = $price; $product->qty = 1; $paypal->addProduct($product); // get the products information from the $_POST and create the Product objects $this->paypal = $paypal; $this->_prepareProgressTracker(4, $price); $error_msg = isset($_SESSION['appointment_booking'][$tmp_form_id]['paypal_error']) ? $_SESSION['appointment_booking'][$tmp_form_id]['paypal_error'] : ""; unset($_SESSION['appointment_booking'][$tmp_form_id]['paypal_error']); // Set response. $response = array('status' => 'success', 'html' => $this->render('payment', array('form_id' => $tmp_form_id, 'error_msg' => $error_msg), false)); } } } } } } // Output JSON response. if ($response === null) { $response = array('status' => 'no-data'); } header('Content-Type: application/json'); echo json_encode($response); exit(0); }
/** * Save appointment form (for both create and edit). */ public function executeSaveAppointmentForm() { /** * @var WPDB $wpdb */ global $wpdb; $response = array('status' => 'error'); $start_date = date('Y-m-d H:i:s', strtotime($this->getParameter('start_date'))); $end_date = date('Y-m-d H:i:s', strtotime($this->getParameter('end_date'))); $staff_id = $this->getParameter('staff_id'); $service_id = $this->getParameter('service_id', null); $appointment_id = $this->getParameter('id', 0); $customers = json_decode($this->getParameter('customers', '[]')); $notes = $this->getParameter('notes', ''); $staff_service = new AB_StaffService(); $staff_service->loadByStaffAndService($staff_id, $service_id); // Check for errors. if (!$this->dateIntervalIsAvailableForAppointment($start_date, $end_date, $staff_id, $appointment_id)) { $response['errors'] = array('date_interval_not_available' => true); } if (count($customers) > $staff_service->get('capacity')) { $response['errors']['overflow_capacity'] = true; $response['errors']['overflow_capacity_message'] = __('Number of customers should be not more than ', 'ab') . $staff_service->get('capacity'); } // If no errors then try to save the appointment. if (!isset($response['errors'])) { $appointment = new AB_Appointment(); if ($appointment_id) { // edit $appointment->load($appointment_id); } $appointment->set('start_date', $start_date); $appointment->set('end_date', $end_date); $appointment->set('staff_id', $staff_id); $appointment->set('service_id', $service_id); if ($appointment->save() !== false) { // save customers $current_customers = $appointment->getCustomers(); foreach (array_diff(array_keys($current_customers), $customers) as $el) { $wpdb->delete('ab_customer_appointment', array('appointment_id' => $appointment->get('id'), 'customer_id' => $el)); } foreach (array_diff($customers, array_keys($current_customers)) as $el) { $customer_appointment = new AB_Customer_Appointment(); $customer_appointment->set('appointment_id', $appointment->get('id')); $customer_appointment->set('customer_id', $el); while (true) { $token = md5(uniqid(time(), true)); $result = $wpdb->get_row($wpdb->prepare('SELECT * FROM `ab_customer_appointment` WHERE token = %s', $token)); if (!$result) { break; } } $customer_appointment->set('token', $token); $customer_appointment->save(); } $startDate = new DateTime($appointment->get('start_date')); $endDate = new DateTime($appointment->get('end_date')); $staff = new AB_Staff(); $staff->load($staff_id); $service = new AB_Service(); $service->load($service_id); $response['status'] = 'ok'; $desc = array(); $appointment_additional_info = $wpdb->get_row($wpdb->prepare('SELECT ss.capacity AS max_capacity, COUNT( ca.id ) AS current_capacity, ca.customer_id, ca.notes, ca.id AS ca_id FROM ab_appointment a LEFT JOIN ab_customer_appointment ca ON ca.appointment_id = a.id LEFT JOIN ab_staff_service ss ON ss.staff_id = a.staff_id AND ss.service_id = a.service_id WHERE a.id = %d', $appointment->get('id'))); if ($appointment_additional_info->max_capacity == 1) { // save notes $customer_appointment = new AB_Customer_Appointment(); $customer_appointment->load($appointment_additional_info->ca_id); $customer_appointment->set('notes', $notes); $customer_appointment->save(); $customer = new AB_Customer(); $customer->load($appointment_additional_info->customer_id); foreach (array('name', 'phone', 'email') as $data_entry) { $entry_value = $customer->get($data_entry); if ($entry_value) { $desc[] = '<div class="wc-employee">' . esc_html($entry_value) . '</div>'; } } $desc[] = '<div class="wc-notes">' . nl2br(esc_html($notes ?: $appointment_additional_info->notes)) . '</div>'; } else { // save notes $customer_appointment = new AB_Customer_Appointment(); $customer_appointment->load($appointment_additional_info->ca_id); $customer_appointment->set('notes', null); $customer_appointment->save(); $desc[] = '<div class="wc-notes">Signed up ' . $appointment_additional_info->current_capacity . '</div>'; $desc[] = '<div class="wc-notes">Capacity ' . $appointment_additional_info->max_capacity . '</div>'; } $response['data'] = array('id' => (int) $appointment->get('id'), 'start' => $startDate->format('m/d/Y H:i'), 'end' => $endDate->format('m/d/Y H:i'), 'desc' => implode('', $desc), 'title' => $service->get('title') ? $service->get('title') : __('Untitled', 'ab'), 'color' => $service->get('color'), 'userId' => (int) $appointment->get('staff_id')); // refresh data $current_customers = $appointment->getCustomers(); if ($this->getParameter('email_notification') === 'true') { // Send email notification to client with appointment info $client_notification = $wpdb->get_row('SELECT * FROM ab_notifications WHERE slug = "client_info" AND active = 1'); // Send email notification to service provider with appointment info $staff_notification = $wpdb->get_row('SELECT * FROM ab_notifications WHERE slug = "provider_info" AND active = 1'); foreach ($current_customers as $customer) { if ($client_notification) { $replacement = new AB_NotificationReplacement(); $replacement->setClientName($customer->name); $replacement->setClientPhone($customer->phone); $replacement->setClientEmail($customer->email); // $replacement->setClientNotes( nl2br( esc_html( $notes ) ) ); $replacement->setAppointmentTime($appointment->get('start_date')); $replacement->setServiceName($service->get('title') ? $service->get('title') : __('Untitled', 'ab')); $replacement->setServicePrice($staff_service->get('price')); $replacement->setAppointmentToken($customer->token); $replacement->setStaffName($staff->get('full_name')); $message = wpautop($replacement->replace($client_notification->message)); $subject = $replacement->replaceSubject($client_notification->subject); wp_mail($customer->email, $subject, $message, AB_CommonUtils::getEmailHeaderFrom()); } if ($staff_notification) { $replacement = new AB_NotificationReplacement(); $replacement->setClientName($customer->name); $replacement->setClientPhone($customer->phone); $replacement->setClientEmail($customer->email); // $replacement->setClientNotes( nl2br( esc_html( $notes ) ) ); $replacement->setAppointmentTime($appointment->get('start_date')); $replacement->setServiceName($service->get('title') ? $service->get('title') : __('Untitled', 'ab')); $replacement->setServicePrice($staff_service->get('price')); $replacement->setAppointmentToken($customer->token); $replacement->setStaffName($staff->get('full_name')); $message = wpautop($replacement->replace($staff_notification->message)); $subject = $replacement->replaceSubject($staff_notification->subject); // Send copy to administrators if ($staff_notification->copy) { $admin_emails = AB_CommonUtils::getAdminEmails(); if (!empty($admin_emails)) { wp_mail($admin_emails, $subject, $message, AB_CommonUtils::getEmailHeaderFrom()); } } wp_mail($staff->get('email'), $subject, $message, AB_CommonUtils::getEmailHeaderFrom()); } } } } else { $response['errors'] = array('unknown' => true); } } exit(json_encode($response)); }
/** * @return AB_Appointment */ public function save() { /** @var wpdb $wpdb */ global $wpdb; // #11094: if customer with such name & e-mail exists, append new booking to him, otherwise - create new customer $customer_exists = $wpdb->get_row($wpdb->prepare('SELECT * FROM ab_customer WHERE name = %s AND email = %s', $this->name, $this->email)); $customer = new AB_Customer(); if ($customer_exists) { $customer->set('id', $customer_exists->id); $customer->set('name', $customer_exists->name); $customer->set('email', $customer_exists->email); $customer->set('phone', $customer_exists->phone); } else { $customer->set('name', $this->name); $customer->set('email', $this->email); $customer->set('phone', $this->phone); $customer->save(); } $this->customer_id = $customer->get('id'); $service = new AB_Service(); $service->load($this->service_id); $category = new AB_Category(); $category->load($service->get('category_id')); /** * Get appointment, with same params. * If it is -> create connection to this appointment, * otherwise create appointment and connect customer to new appointment */ $booking = $wpdb->get_row($wpdb->prepare("SELECT * from ab_appointment a WHERE a.staff_id = %d and a.service_id = %d and a.start_date = %s LIMIT 1;", $this->getStaffId(), $this->service_id, $this->booked_datetime)); $appointment = new AB_Appointment(); if ($booking) { $appointment->load($booking->id); } else { $appointment->set('staff_id', $this->getStaffId()); $appointment->set('service_id', $this->service_id); $appointment->set('start_date', date('Y-m-d H:i:s', strtotime($this->booked_datetime))); $endDate = new DateTime($this->booked_datetime); $di = "+ {$service->get('duration')} sec"; $endDate->modify($di); $appointment->set('end_date', $endDate->format('Y-m-d H:i:s')); $appointment->save(); } $customer_appointment = new AB_Customer_Appointment(); $customer_appointment->set('appointment_id', $appointment->get('id')); $customer_appointment->set('customer_id', $customer->get('id')); $customer_appointment->set('token', md5($this->form_id)); $customer_appointment->set('notes', $this->notes); $customer_appointment->save(); $staff = new AB_Staff(); $staff->load($this->getStaffId()); return $appointment; }
/** * @param $staff_id * @return mixed */ public function logoutByStaffId($staff_id) { $staff = new AB_Staff(); $staff->load($staff_id); try { $this->loadByStaff($staff); $this->client->revokeToken(); } catch (Exception $e) { $this->errors[] = $e->getMessage(); } $staff->set('google_data', null); $staff->set('google_calendar_id', null); $staff->save(); return $staff->get('id'); }
public function executeDeleteStaffAvatar() { $staff = new AB_Staff(); $staff->load(1); unlink($staff->get('avatar_path')); $staff->set('avatar_url', ''); $staff->set('avatar_path', ''); $staff->save(); exit; }
/** * Extend parent method to control access on staff member level. * * @param string $action * @return bool */ protected function hasAccess($action) { if (parent::hasAccess($action)) { if (!AB_Utils::isCurrentUserAdmin()) { $staff = new AB_Staff(); switch ($action) { case 'executeEditStaff': case 'executeDeleteStaffAvatar': case 'executeStaffServices': case 'executeStaffSchedule': case 'executeStaffHolidays': $staff->load($this->getParameter('id')); break; case 'executeStaffServicesUpdate': case 'executeStaffHolidaysUpdate': $staff->load($this->getParameter('staff_id')); break; case 'executeStaffScheduleHandleBreak': $staffScheduleItem = new AB_StaffScheduleItem(); $staffScheduleItem->load($this->getParameter('staff_schedule_item_id')); $staff->load($staffScheduleItem->get('staff_id')); break; case 'executeDeleteStaffScheduleBreak': $break = new AB_ScheduleItemBreak(); $break->load($this->getParameter('id')); $staffScheduleItem = new AB_StaffScheduleItem(); $staffScheduleItem->load($break->get('staff_schedule_item_id')); $staff->load($staffScheduleItem->get('staff_id')); break; case 'executeStaffScheduleUpdate': if ($this->hasParameter('days')) { foreach ($this->getParameter('days') as $id => $day_index) { $staffScheduleItem = new AB_StaffScheduleItem(); $staffScheduleItem->load($id); $staff = new AB_Staff(); $staff->load($staffScheduleItem->get('staff_id')); if ($staff->get('wp_user_id') != get_current_user_id()) { return false; } } } break; default: return false; } return $staff->get('wp_user_id') == get_current_user_id(); } return true; } return false; }