/** * Constructor. */ public function __construct() { global $wpdb; // Select all services (with categories and staff members) // which have at least one staff member assigned. $rows = $wpdb->get_results($wpdb->prepare(' SELECT IFNULL(`c`.`id`,0) AS `category_id`, IFNULL(`c`.`name`,%s) AS `category_name`, `c`.`position` AS `category_position`, `s`.`id` AS `service_id`, `s`.`position` AS `service_position`, `s`.`title` AS `service_name`, `st`.`id` AS `staff_id`, `st`.`position` AS `staff_position`, `st`.`full_name` AS `staff_name`, `ss`.`capacity` AS `capacity`, `ss`.`price` AS `price` FROM `' . AB_Service::getTableName() . '` `s` INNER JOIN `' . AB_StaffService::getTableName() . '` `ss` ON `s`.`id` = `ss`.`service_id` LEFT JOIN `' . AB_Category::getTableName() . '` `c` ON `s`.`category_id` = `c`.`id` LEFT JOIN `' . AB_Staff::getTableName() . '` `st` ON `ss`.`staff_id` = `st`.`id` ORDER BY `service_name` ', __('Uncategorized', 'bookly')), ARRAY_A); foreach ($rows as $row) { if (!isset($this->services[$row['service_id']])) { $this->services[$row['service_id']] = array('id' => $row['service_id'], 'name' => AB_Utils::getTranslatedString('service_' . $row['service_id'], $row['service_name']), 'category_id' => $row['category_id'], 'staff' => array(), 'max_capacity' => $row['capacity'], 'position' => $row['service_position']); } else { if ($this->services[$row['service_id']]['max_capacity'] < $row['capacity']) { // Detect the max capacity for each service //(it is the max capacity from all staff members who provides this service). $this->services[$row['service_id']]['max_capacity'] = $row['capacity']; } } if (!isset($this->staff[$row['staff_id']])) { $this->staff[$row['staff_id']] = array('id' => $row['staff_id'], 'name' => AB_Utils::getTranslatedString('staff_' . $row['staff_id'], $row['staff_name']), 'services' => array(), 'position' => $row['staff_position']); } if ($row['category_id'] != '' && !isset($this->categories[$row['category_id']])) { $this->categories[$row['category_id']] = array('id' => $row['category_id'], 'name' => AB_Utils::getTranslatedString('category_' . $row['category_id'], $row['category_name']), 'services' => array(), 'position' => $row['category_position']); } if (!isset($this->services[$row['service_id']]['staff'][$row['staff_id']])) { $staff_member = $this->staff[$row['staff_id']]; unset($staff_member['services']); if (self::isPaymentDisabled() == false) { $staff_member['name'] .= ' (' . AB_Utils::formatPrice($row['price']) . ')'; } $this->services[$row['service_id']]['staff'][$row['staff_id']] = $staff_member; } if (!isset($this->staff[$row['staff_id']]['services'][$row['service_id']])) { $service = $this->services[$row['service_id']]; unset($service['staff']); $service['max_capacity'] = $row['capacity']; $this->staff[$row['staff_id']]['services'][$row['service_id']] = $service; } if (!isset($this->categories[intval($row['category_id'])]['staff'][$row['staff_id']])) { $staff_member = $this->staff[$row['staff_id']]; unset($staff_member['services']); $this->categories[intval($row['category_id'])]['staff'][$row['staff_id']] = $staff_member; } if (!isset($this->categories[intval($row['category_id'])]['services'][$row['service_id']])) { $service = $this->services[$row['service_id']]; unset($service['staff']); $this->categories[intval($row['category_id'])]['services'][$row['service_id']] = $service; } } }