/** * Save appointment form (for both create and edit). */ public function executeSaveAppointmentForm() { $response = array('success' => false); $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'); $appointment_id = $this->getParameter('id', 0); $customers = json_decode($this->getParameter('customers', '[]'), true); $staff_service = new AB_StaffService(); $staff_service->loadBy(array('staff_id' => $staff_id, 'service_id' => $service_id)); // Check for errors. if (!$service_id) { $response['errors']['service_required'] = true; } if (empty($customers)) { $response['errors']['customers_required'] = true; } if (!$this->dateIntervalIsAvailableForAppointment($start_date, $end_date, $staff_id, $appointment_id)) { $response['errors']['date_interval_not_available'] = true; } $number_of_persons = 0; foreach ($customers as $customer) { $number_of_persons += $customer['number_of_persons']; } if ($number_of_persons > $staff_service->get('capacity')) { $response['errors']['overflow_capacity'] = __('The number of customers should be not more than ', 'bookly') . $staff_service->get('capacity'); } if (!$this->getParameter('start_date')) { $response['errors']['time_interval'] = __('Start time must not be empty', 'bookly'); } elseif (!$this->getParameter('end_date')) { $response['errors']['time_interval'] = __('End time must not be empty', 'bookly'); } elseif ($start_date == $end_date) { $response['errors']['time_interval'] = __('End time must not be equal to start time', 'bookly'); } // 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. $appointment->setCustomers($customers); // Google Calendar. $appointment->handleGoogleCalendar(); if ($this->getParameter('email_notification') === 'true') { foreach ($appointment->getCustomerAppointments() as $ca) { AB_NotificationSender::send(AB_NotificationSender::INSTANT_NEW_APPOINTMENT, $ca); } } $startDate = new DateTime($appointment->get('start_date')); $endDate = new DateTime($appointment->get('end_date')); $desc = array(); if ($staff_service->get('capacity') == 1) { $customer_appointments = $appointment->getCustomerAppointments(); if (!empty($customer_appointments)) { $ca = $customer_appointments[0]->customer; foreach (array('name', 'phone', 'email') as $data_entry) { $entry_value = $ca->get($data_entry); if ($entry_value) { $desc[] = '<div class="fc-employee">' . esc_html($entry_value) . '</div>'; } } foreach ($customer_appointments[0]->getCustomFields() as $custom_field) { $desc[] = '<div class="fc-notes">' . wp_strip_all_tags($custom_field['label']) . ': ' . esc_html($custom_field['value']) . '</div>'; } } } else { $signed_up = 0; foreach ($appointment->getCustomerAppointments() as $ca) { $signed_up += $ca->get('number_of_persons'); } $desc[] = '<div class="fc-notes">' . __('Signed up', 'bookly') . ' ' . $signed_up . '</div>'; $desc[] = '<div class="fc-notes">' . __('Capacity', 'bookly') . ' ' . $staff_service->get('capacity') . '</div>'; } $service = new AB_Service(); $service->load($service_id); $response['success'] = true; $response['data'] = array('id' => (int) $appointment->get('id'), 'start' => $startDate->format('Y-m-d H:i:s'), 'end' => $endDate->format('Y-m-d H:i:s'), 'desc' => implode('', $desc), 'title' => $service->get('title') ? $service->get('title') : __('Untitled', 'bookly'), 'color' => $service->get('color'), 'staffId' => $appointment->get('staff_id')); } else { $response['errors'] = array('db' => __('Could not save appointment in database.', 'bookly')); } } wp_send_json($response); }