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