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