コード例 #1
0
ファイル: AB_Google.php プロジェクト: patrickcurl/monks
 /**
  * @param AB_Appointment $appointment
  */
 private function handleEventData(AB_Appointment $appointment)
 {
     $start_datetime = new Google_Service_Calendar_EventDateTime();
     $start_datetime->setDateTime(DateTime::createFromFormat('Y-m-d H:i:s', $appointment->get('start_date'), new DateTimeZone(AB_Utils::getTimezoneString()))->format(DateTime::RFC3339));
     $end_datetime = new Google_Service_Calendar_EventDateTime();
     $end_datetime->setDateTime(DateTime::createFromFormat('Y-m-d H:i:s', $appointment->get('end_date'), new DateTimeZone(AB_Utils::getTimezoneString()))->format(DateTime::RFC3339));
     $service = new AB_Service();
     $service->load($appointment->get('service_id'));
     $description = __('Service', 'bookly') . ": " . $service->get('title') . PHP_EOL;
     $client_names = array();
     foreach ($appointment->getCustomerAppointments() as $ca) {
         $description .= sprintf("%s: %s\n%s: %s\n%s: %s\n", __('Name', 'bookly'), $ca->customer->get('name'), __('Email', 'bookly'), $ca->customer->get('email'), __('Phone', 'bookly'), $ca->customer->get('phone'));
         $description .= $ca->getFormattedCustomFields('text');
         $description .= PHP_EOL;
         $client_names[] = $ca->customer->get('name');
     }
     $staff = new AB_Staff();
     $staff->load($appointment->get('staff_id'));
     $title = strtr(get_option('ab_settings_google_event_title', '[[SERVICE_NAME]]'), array('[[SERVICE_NAME]]' => $service->get('title'), '[[CLIENT_NAMES]]' => implode(', ', $client_names), '[[STAFF_NAME]]' => $staff->get('full_name')));
     $this->event->setStart($start_datetime);
     $this->event->setEnd($end_datetime);
     $this->event->setSummary($title);
     $this->event->setDescription($description);
     $extended_property = new Google_Service_Calendar_EventExtendedProperties();
     $extended_property->setPrivate(array('customers' => json_encode(array_map(function ($ca) {
         return $ca->customer->get('id');
     }, $appointment->getCustomerAppointments())), 'service_id' => $service->get('id'), 'appointment_id' => $appointment->get('id')));
     $this->event->setExtendedProperties($extended_property);
 }
コード例 #2
0
 /**
  * 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);
 }