EventsList.php
266 lines
| 1 | <?php |
| 2 | |
| 3 | use AmeliaBooking\Infrastructure\WP\GutenbergBlock\GutenbergBlock; |
| 4 | use AmeliaBooking\Infrastructure\WP\Translations\BackendStrings; |
| 5 | |
| 6 | class DIVI_EventsList extends ET_Builder_Module |
| 7 | { |
| 8 | |
| 9 | public $slug = 'divi_events_list_booking'; |
| 10 | public $vb_support = 'on'; |
| 11 | |
| 12 | private $events = array(); |
| 13 | private $tags = array(); |
| 14 | |
| 15 | private $locations = array(); |
| 16 | |
| 17 | |
| 18 | protected $module_credits = array( |
| 19 | 'module_uri' => '', |
| 20 | 'author' => '', |
| 21 | 'author_uri' => '', |
| 22 | ); |
| 23 | |
| 24 | public function init() |
| 25 | { |
| 26 | $this->name = esc_html__(BackendStrings::get('events_list_booking_divi'), 'divi-divi_amelia'); |
| 27 | |
| 28 | if (!is_admin()) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | $data = GutenbergBlock::getEntitiesData()['data']; |
| 33 | |
| 34 | |
| 35 | foreach ($data['events'] as $event) { |
| 36 | $this->events[$event['id']] = $event['name'] . ' (id: ' . $event['id'] . ') - ' . $event['formattedPeriodStart']; |
| 37 | } |
| 38 | |
| 39 | foreach ($data['tags'] as $tag) { |
| 40 | $this->tags[$tag['name']] = $tag['name'] . ' (id: ' . $tag['id'] . ')'; |
| 41 | } |
| 42 | |
| 43 | foreach ($data['locations'] as $location) { |
| 44 | $this->locations[$location['id']] = $location['name']; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Advanced Fields Config |
| 50 | * |
| 51 | * @return array |
| 52 | */ |
| 53 | public function get_advanced_fields_config() |
| 54 | { |
| 55 | return array( |
| 56 | 'button' => false, |
| 57 | 'link_options' => false |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | public function get_fields() |
| 62 | { |
| 63 | return array( |
| 64 | 'booking_params' => array( |
| 65 | 'label' => esc_html__(BackendStrings::get('filter'), 'divi-divi_amelia'), |
| 66 | 'type' => 'yes_no_button', |
| 67 | 'options' => array( |
| 68 | 'on' => esc_html__(BackendStrings::get('yes'), 'divi-divi_amelia'), |
| 69 | 'off' => esc_html__(BackendStrings::get('no'), 'divi-divi_amelia'), |
| 70 | ), |
| 71 | 'toggle_slug' => 'main_content', |
| 72 | 'option_category' => 'basic_option', |
| 73 | ), |
| 74 | 'event_to_show' => array( |
| 75 | 'label' => esc_html__(BackendStrings::get('event_time_scope'), 'divi-divi_amelia'), |
| 76 | 'type' => 'select', |
| 77 | 'options' => array( |
| 78 | 'all' => BackendStrings::get('all_events'), |
| 79 | 'future' => BackendStrings::get('future_events'), |
| 80 | 'past' => BackendStrings::get('past_events'), |
| 81 | 'custom' => BackendStrings::get('custom_range'), |
| 82 | ), |
| 83 | 'default' => 'all', |
| 84 | 'toggle_slug' => 'main_content', |
| 85 | 'option_category' => 'basic_option', |
| 86 | 'show_if' => array( |
| 87 | 'booking_params' => 'on', |
| 88 | ), |
| 89 | ), |
| 90 | 'start_date' => array( |
| 91 | 'label' => esc_html__(BackendStrings::get('red_start_date'), 'divi-divi_amelia'), |
| 92 | 'type' => 'amelia_date_picker', |
| 93 | 'toggle_slug' => 'main_content', |
| 94 | 'option_category' => 'basic_option', |
| 95 | 'show_if' => array( |
| 96 | 'booking_params' => 'on', |
| 97 | 'event_to_show' => 'custom', |
| 98 | ), |
| 99 | ), |
| 100 | 'end_date' => array( |
| 101 | 'label' => esc_html__(BackendStrings::get('red_end_date'), 'divi-divi_amelia'), |
| 102 | 'type' => 'amelia_date_picker', |
| 103 | 'toggle_slug' => 'main_content', |
| 104 | 'option_category' => 'basic_option', |
| 105 | 'show_if' => array( |
| 106 | 'booking_params' => 'on', |
| 107 | 'event_to_show' => 'custom', |
| 108 | ), |
| 109 | ), |
| 110 | 'events' => array( |
| 111 | 'label' => esc_html__(BackendStrings::get('select_event'), 'divi-divi_amelia'), |
| 112 | 'type' => 'amelia_multi_select', |
| 113 | 'showAllText' => BackendStrings::get('show_all_events'), |
| 114 | 'options' => $this->events, |
| 115 | 'toggle_slug' => 'main_content', |
| 116 | 'option_category' => 'basic_option', |
| 117 | 'show_if' => array( |
| 118 | 'booking_params' => 'on', |
| 119 | ), |
| 120 | ), |
| 121 | 'tags' => array( |
| 122 | 'label' => esc_html__(BackendStrings::get('select_tag'), 'divi-divi_amelia'), |
| 123 | 'type' => 'amelia_multi_select', |
| 124 | 'showAllText' => BackendStrings::get('show_all_tags'), |
| 125 | 'toggle_slug' => 'main_content', |
| 126 | 'options' => $this->tags, |
| 127 | 'option_category' => 'basic_option', |
| 128 | 'brackets' => true, |
| 129 | 'show_if' => array( |
| 130 | 'booking_params' => 'on', |
| 131 | ), |
| 132 | ), |
| 133 | 'recurring' => array( |
| 134 | 'label' => esc_html__(BackendStrings::get('recurring_event'), '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 | 'show_if' => array( |
| 143 | 'booking_params' => 'on', |
| 144 | ), |
| 145 | ), |
| 146 | 'locations' => array( |
| 147 | 'label' => esc_html__(BackendStrings::get('select_location'), 'divi-divi_amelia'), |
| 148 | 'type' => 'amelia_multi_select', |
| 149 | 'showAllText' => BackendStrings::get('show_all_locations'), |
| 150 | 'toggle_slug' => 'main_content', |
| 151 | 'options' => $this->locations, |
| 152 | 'option_category' => 'basic_option', |
| 153 | 'show_if' => array( |
| 154 | 'booking_params' => 'on', |
| 155 | ), |
| 156 | ), |
| 157 | 'trigger' => array( |
| 158 | 'label' => esc_html__(BackendStrings::get('manually_loading'), 'divi-divi_amelia'), |
| 159 | 'type' => 'text', |
| 160 | 'toggle_slug' => 'main_content', |
| 161 | 'option_category' => 'basic_option', |
| 162 | 'description' => BackendStrings::get('manually_loading_description'), |
| 163 | ), |
| 164 | 'trigger_type' => array( |
| 165 | 'label' => esc_html__(BackendStrings::get('trigger_type'), 'divi-divi_amelia'), |
| 166 | 'type' => 'select', |
| 167 | 'options' => array( |
| 168 | 'id' => BackendStrings::get('trigger_type_id'), |
| 169 | 'class' => BackendStrings::get('trigger_type_class') |
| 170 | ), |
| 171 | 'toggle_slug' => 'main_content', |
| 172 | 'option_category' => 'basic_option', |
| 173 | ), |
| 174 | 'in_dialog' => array( |
| 175 | 'label' => esc_html__(BackendStrings::get('in_dialog'), 'divi-divi_amelia'), |
| 176 | 'type' => 'yes_no_button', |
| 177 | 'options' => array( |
| 178 | 'on' => esc_html__(BackendStrings::get('yes'), 'divi-divi_amelia'), |
| 179 | 'off' => esc_html__(BackendStrings::get('no'), 'divi-divi_amelia'), |
| 180 | ), |
| 181 | 'toggle_slug' => 'main_content', |
| 182 | 'option_category' => 'basic_option', |
| 183 | ), |
| 184 | ); |
| 185 | } |
| 186 | |
| 187 | public function checkValues($val) |
| 188 | { |
| 189 | if ($val !== null) { |
| 190 | $val = explode(',', $val); |
| 191 | if (is_array($val)) { |
| 192 | $newVals = []; |
| 193 | foreach ($val as $parameter) { |
| 194 | if ($parameter) { |
| 195 | $newVals[] = !is_numeric($parameter) ? (strpos($parameter, 'id:') ? substr(explode('id: ', $parameter)[1], 0, -1) : $parameter) : $parameter; |
| 196 | } |
| 197 | } |
| 198 | return count($newVals) > 0 ? $newVals : []; |
| 199 | } |
| 200 | return []; |
| 201 | } |
| 202 | return []; |
| 203 | } |
| 204 | |
| 205 | public function render($attrs, $content = null, $render_slug = null) |
| 206 | { |
| 207 | $preselect = $this->props['booking_params']; |
| 208 | $shortcode = '[ameliaeventslistbooking'; |
| 209 | $trigger = $this->props['trigger']; |
| 210 | $trigger_type = $this->props['trigger_type']; |
| 211 | $in_dialog = $this->props['in_dialog']; |
| 212 | if ($trigger !== null && $trigger !== '') { |
| 213 | $shortcode .= ' trigger='.$trigger; |
| 214 | } |
| 215 | if (!empty($trigger) && !empty($trigger_type)) { |
| 216 | $shortcode .= ' trigger_type='.$trigger_type; |
| 217 | } |
| 218 | if (!empty($trigger) && $in_dialog === 'on') { |
| 219 | $shortcode .= ' in_dialog=1'; |
| 220 | } |
| 221 | if ($preselect === 'on') { |
| 222 | $event = $this->checkValues($this->props['events']); |
| 223 | |
| 224 | // Add event_to_show range logic (only if no specific event is selected) |
| 225 | if (empty($event) && !empty($this->props['event_to_show']) && $this->props['event_to_show'] !== 'all') { |
| 226 | if ($this->props['event_to_show'] === 'custom') { |
| 227 | $startDate = !empty($this->props['start_date']) ? $this->props['start_date'] : ''; |
| 228 | $endDate = !empty($this->props['end_date']) ? $this->props['end_date'] : ''; |
| 229 | |
| 230 | $has_valid_start = $startDate && preg_match('/^\d{4}-\d{2}-\d{2}$/', $startDate); |
| 231 | $has_valid_end = $endDate && preg_match('/^\d{4}-\d{2}-\d{2}$/', $endDate); |
| 232 | |
| 233 | if (!$has_valid_start || !$has_valid_end) { |
| 234 | return ''; |
| 235 | } |
| 236 | |
| 237 | $shortcode .= ' range="' . $startDate . ' - ' . $endDate . '"'; |
| 238 | } else { |
| 239 | $shortcode .= ' range="' . $this->props['event_to_show'] . '"'; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | $tag = $this->props['tags']; |
| 244 | if ($event && count($event) > 0) { |
| 245 | $shortcode .= ' event=' . implode(',', $event); |
| 246 | } |
| 247 | if ($tag) { |
| 248 | $shortcode .= ' tag="' . $tag . '"'; |
| 249 | } |
| 250 | $recurring = $this->props['recurring']; |
| 251 | if ($recurring === 'on') { |
| 252 | $shortcode .= ' recurring=1'; |
| 253 | } |
| 254 | $locations = $this->checkValues($this->props['locations']); |
| 255 | if ($locations && count($locations) > 0) { |
| 256 | $shortcode .= ' location=' . implode(',', $locations); |
| 257 | } |
| 258 | } |
| 259 | $shortcode .= ']'; |
| 260 | |
| 261 | return do_shortcode($shortcode); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | new DIVI_EventsList; |
| 266 |