Booking.php
197 lines
| 1 | <?php |
| 2 | |
| 3 | use AmeliaBooking\Infrastructure\WP\GutenbergBlock\GutenbergBlock; |
| 4 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 5 | |
| 6 | class DIVI_Booking extends ET_Builder_Module |
| 7 | { |
| 8 | |
| 9 | public $slug = 'divi_booking'; |
| 10 | public $vb_support = 'on'; |
| 11 | |
| 12 | private $categories = array(); |
| 13 | private $services = array(); |
| 14 | private $employees = array(); |
| 15 | private $locations = array(); |
| 16 | private $showPackages = true; |
| 17 | |
| 18 | public $type = array(); |
| 19 | |
| 20 | |
| 21 | protected $module_credits = array( |
| 22 | 'module_uri' => '', |
| 23 | 'author' => '', |
| 24 | 'author_uri' => '', |
| 25 | ); |
| 26 | |
| 27 | public function init() |
| 28 | { |
| 29 | $this->name = esc_html__(BackendStrings::get('booking_divi'), 'divi-divi_amelia'); |
| 30 | |
| 31 | $this->type['0'] = BackendStrings::get('show_all'); |
| 32 | $this->type['services'] = BackendStrings::get('services'); |
| 33 | $this->type['packages'] = BackendStrings::get('packages'); |
| 34 | |
| 35 | if (!is_admin()) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $data = GutenbergBlock::getEntitiesData()['data']; |
| 40 | $this->showPackages = !empty($data['packages']); |
| 41 | |
| 42 | $this->categories['0'] = BackendStrings::get('show_all_categories'); |
| 43 | foreach ($data['categories'] as $category) { |
| 44 | $this->categories[$category['id']] = $category['name']. ' (id: ' . $category['id'] . ')'; |
| 45 | } |
| 46 | $this->services['0'] = BackendStrings::get('show_all_services'); |
| 47 | foreach ($data['servicesList'] as $service) { |
| 48 | if ($service) { |
| 49 | $this->services[$service['id']] = $service['name']. ' (id: ' . $service['id'] . ')'; |
| 50 | } |
| 51 | } |
| 52 | $this->employees['0'] = BackendStrings::get('show_all_employees'); |
| 53 | foreach ($data['employees'] as $employee) { |
| 54 | $this->employees[$employee['id']] = $employee['firstName'] . ' ' . $employee['lastName'] . ' (id: ' . $employee['id'] . ')'; |
| 55 | } |
| 56 | $this->locations['0'] = BackendStrings::get('show_all_locations'); |
| 57 | foreach ($data['locations'] as $location) { |
| 58 | $this->locations[$location['id']] = $location['name']. ' (id: ' . $location['id'] . ')'; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Advanced Fields Config |
| 64 | * |
| 65 | * @return array |
| 66 | */ |
| 67 | public function get_advanced_fields_config() |
| 68 | { |
| 69 | return array( |
| 70 | 'button' => false, |
| 71 | 'link_options' => false |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | public function get_fields() |
| 76 | { |
| 77 | $array = array( |
| 78 | 'booking_params' => array( |
| 79 | 'label' => esc_html__(BackendStrings::get('filter'), 'divi-divi_amelia'), |
| 80 | 'type' => 'yes_no_button', |
| 81 | 'options' => array( |
| 82 | 'on' => esc_html__(BackendStrings::get('yes'), 'divi-divi_amelia'), |
| 83 | 'off' => esc_html__(BackendStrings::get('no'), 'divi-divi_amelia'), |
| 84 | ), |
| 85 | 'toggle_slug' => 'main_content', |
| 86 | 'option_category' => 'basic_option', |
| 87 | ), |
| 88 | 'categories' => array( |
| 89 | 'label' => esc_html__(BackendStrings::get('select_category'), 'divi-divi_amelia'), |
| 90 | 'type' => 'select', |
| 91 | 'options' => $this->categories, |
| 92 | 'toggle_slug' => 'main_content', |
| 93 | 'option_category' => 'basic_option', |
| 94 | 'show_if' => array( |
| 95 | 'booking_params' => 'on', |
| 96 | ), |
| 97 | ), |
| 98 | 'services' => array( |
| 99 | 'label' => esc_html__(BackendStrings::get('select_service'), 'divi-divi_amelia'), |
| 100 | 'type' => 'select', |
| 101 | 'toggle_slug' => 'main_content', |
| 102 | 'options' => $this->services, |
| 103 | 'option_category' => 'basic_option', |
| 104 | 'show_if' => array( |
| 105 | 'booking_params' => 'on', |
| 106 | ), |
| 107 | ), |
| 108 | 'employees' => array( |
| 109 | 'label' => esc_html__(BackendStrings::get('select_employee'), 'divi-divi_amelia'), |
| 110 | 'type' => 'select', |
| 111 | 'options' => $this->employees, |
| 112 | 'toggle_slug' => 'main_content', |
| 113 | 'option_category' => 'basic_option', |
| 114 | 'show_if' => array( |
| 115 | 'booking_params' => 'on', |
| 116 | ), |
| 117 | ), |
| 118 | 'locations' => array( |
| 119 | 'label' => esc_html__(BackendStrings::get('select_location'), 'divi-divi_amelia'), |
| 120 | 'type' => 'select', |
| 121 | 'options' => $this->locations, |
| 122 | 'toggle_slug' => 'main_content', |
| 123 | 'option_category' => 'basic_option', |
| 124 | 'show_if' => array( |
| 125 | 'booking_params' => 'on', |
| 126 | ), |
| 127 | ), |
| 128 | ); |
| 129 | |
| 130 | if ($this->showPackages) { |
| 131 | $array['type'] = array( |
| 132 | 'label' => esc_html__(BackendStrings::get('show_all'), 'divi-divi_amelia'), |
| 133 | 'type' => 'select', |
| 134 | 'options' => $this->type, |
| 135 | 'toggle_slug' => 'main_content', |
| 136 | 'option_category' => 'basic_option', |
| 137 | 'show_if' => array( |
| 138 | 'booking_params' => 'on' |
| 139 | )); |
| 140 | } |
| 141 | |
| 142 | $array['trigger'] = array( |
| 143 | 'label' => esc_html__(BackendStrings::get('manually_loading'), 'divi-divi_amelia'), |
| 144 | 'type' => 'text', |
| 145 | 'toggle_slug' => 'main_content', |
| 146 | 'option_category' => 'basic_option', |
| 147 | 'description' => BackendStrings::get('manually_loading_description'), |
| 148 | ); |
| 149 | |
| 150 | return $array; |
| 151 | } |
| 152 | |
| 153 | public function checkValues($val) |
| 154 | { |
| 155 | if ($val !== null) { |
| 156 | return !is_numeric($val) ? (strpos($val, 'id:') ? substr(explode('id: ', $val)[1], 0, -1) : '0') : $val; |
| 157 | } |
| 158 | return '0'; |
| 159 | } |
| 160 | |
| 161 | public function render($attrs, $content = null, $render_slug = null) |
| 162 | { |
| 163 | $preselect = $this->props['booking_params']; |
| 164 | $shortcode = '[ameliabooking'; |
| 165 | $showAll = isset($this->props['type']) ? $this->props['type'] : null; |
| 166 | $trigger = $this->props['trigger']; |
| 167 | if ($showAll !== null && $showAll !== '' && $showAll !== '0') { |
| 168 | $shortcode .= ' show='.$showAll; |
| 169 | } |
| 170 | if ($trigger !== null && $trigger !== '') { |
| 171 | $shortcode .= ' trigger='.$trigger; |
| 172 | } |
| 173 | if ($preselect === 'on') { |
| 174 | $category = $this->checkValues($this->props['categories']); |
| 175 | $service = $this->checkValues($this->props['services']); |
| 176 | $employee = $this->checkValues($this->props['employees']); |
| 177 | $location = $this->checkValues($this->props['locations']); |
| 178 | |
| 179 | if ($service !== '0') { |
| 180 | $shortcode .= ' service=' . $service; |
| 181 | } else if ($category !== '0') { |
| 182 | $shortcode .= ' category=' . $category; |
| 183 | } |
| 184 | if ($employee !== '0') { |
| 185 | $shortcode .= ' employee=' . $employee; |
| 186 | } |
| 187 | if ($location !== '0') { |
| 188 | $shortcode .= ' location=' . $location; |
| 189 | } |
| 190 | } |
| 191 | $shortcode .= ']'; |
| 192 | return do_shortcode($shortcode); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | new DIVI_Booking; |
| 197 |