| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class Front_View_LC_HC_MVC extends _HC_MVC |
| 3 |
{ |
| 4 |
public function params() |
| 5 |
{ |
| 6 |
$ret = array( |
| 7 |
'partial' => '', |
| 8 |
'layout' => 'map|list', |
| 9 |
'form-after-map' => 0, |
| 10 |
'start' => '', |
| 11 |
|
| 12 |
'map-start-address' => null, |
| 13 |
'map-start-zoom' => null, |
| 14 |
'map-max-zoom' => null, |
| 15 |
'map-hide-loc-title' => null, |
| 16 |
|
| 17 |
'limit' => 2000, |
| 18 |
|
| 19 |
'group' => NULL, |
| 20 |
// 'empty-group-label' => __('Uncategorized', 'locatoraid'), |
| 21 |
'empty-group-label' => '', |
| 22 |
'group-jump' => 0, |
| 23 |
'list-group' => NULL, |
| 24 |
'sort' => NULL, |
| 25 |
'map-style' => 'height: 400px; width: 100%;', |
| 26 |
'list-style' => 'height: 400px; overflow-y: scroll;', |
| 27 |
|
| 28 |
'search-bias-country' => '', // australia, uk, finland etc |
| 29 |
'radius' => '10, 25, 50, 100, 200, 500', |
| 30 |
'radius-select' => 0, |
| 31 |
'id' => NULL, |
| 32 |
'clustering' => 0, |
| 33 |
|
| 34 |
'locate' => 1, // 0,1,auto |
| 35 |
'skipgeo' => 0, // 0,1 |
| 36 |
); |
| 37 |
|
| 38 |
$p = $this->app->make('/locations/presenter'); |
| 39 |
$also_take = $p->database_fields(); |
| 40 |
foreach( $also_take as $tk ){ |
| 41 |
$ret[ 'where-' . $tk ] = NULL; |
| 42 |
} |
| 43 |
$ret[ 'where-product' ] = NULL; |
| 44 |
|
| 45 |
return $ret; |
| 46 |
} |
| 47 |
|
| 48 |
public function render( $pass_params = array() ) |
| 49 |
{ |
| 50 |
$enqueuer = $this->app->make('/app/enqueuer'); |
| 51 |
$enqueuer |
| 52 |
->register_script( 'lc_front', 'modules/front/assets/js/front.js?hcver=' . LC3_VERSION ) |
| 53 |
->enqueue_script( 'lc_front' ) |
| 54 |
; |
| 55 |
|
| 56 |
$enqueuer |
| 57 |
->register_script( 'hc', 'happ2/assets/js/hc2.js?hcver=' . LC3_VERSION ) |
| 58 |
->register_style( 'hc', 'happ2/assets/css/hc.css?hcver=' . LC3_VERSION ) |
| 59 |
; |
| 60 |
|
| 61 |
$enqueuer |
| 62 |
->enqueue_script( 'hc' ) |
| 63 |
->enqueue_style( 'hc' ) |
| 64 |
; |
| 65 |
|
| 66 |
$enqueuer |
| 67 |
->register_script( 'gmaps', 'happ2/modules/maps_google/assets/js/gmaps.js' ) |
| 68 |
; |
| 69 |
|
| 70 |
$app_settings = $this->app->make('/app/settings'); |
| 71 |
$api_key = $app_settings->get('maps_google:api_key'); |
| 72 |
if( is_array($api_key) ){ |
| 73 |
$api_key = array_shift($api_key); |
| 74 |
} |
| 75 |
|
| 76 |
if( $api_key == 'none' ){ |
| 77 |
$api_key = ''; |
| 78 |
} |
| 79 |
$api_key = trim($api_key); |
| 80 |
|
| 81 |
$language = $app_settings->get('maps_google:language'); |
| 82 |
if( is_array($language) ){ |
| 83 |
$language = array_shift($language); |
| 84 |
} |
| 85 |
$language = trim( $language ); |
| 86 |
|
| 87 |
$map_style = $app_settings->get('maps_google:map_style'); |
| 88 |
$scrollwheel = $app_settings->get('maps_google:scrollwheel'); |
| 89 |
$scrollwheel = $scrollwheel ? TRUE : FALSE; |
| 90 |
$more_options = $app_settings->get('maps_google:more_options'); |
| 91 |
|
| 92 |
$icon = ''; |
| 93 |
$icon_id = $app_settings->get('maps_google:icon'); |
| 94 |
if( $icon_id ){ |
| 95 |
$your_img_src = wp_get_attachment_image_src( $icon_id, 'full' ); |
| 96 |
$have_img = is_array( $your_img_src ); |
| 97 |
if( $have_img ){ |
| 98 |
$icon = $your_img_src[0]; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
$async = $app_settings->get('maps_google:async'); |
| 103 |
$async = $async ? true : false; |
| 104 |
|
| 105 |
$params = array( |
| 106 |
'api_key' => $api_key, |
| 107 |
'language' => $language, |
| 108 |
'map_style' => $map_style, |
| 109 |
'scrollwheel' => $scrollwheel, |
| 110 |
'async' => $async, |
| 111 |
'more_options' => $more_options, |
| 112 |
'icon' => $icon, |
| 113 |
); |
| 114 |
|
| 115 |
$enqueuer |
| 116 |
->localize_script( 'gmaps', $params ) |
| 117 |
->enqueue_script( 'gmaps' ) |
| 118 |
; |
| 119 |
|
| 120 |
// parse params |
| 121 |
$default_params = $this->params(); |
| 122 |
|
| 123 |
// _print_r( $default_params ); |
| 124 |
// exit; |
| 125 |
// _print_r( $pass_params ); |
| 126 |
|
| 127 |
$params = array(); |
| 128 |
foreach( $default_params as $k => $default_v ){ |
| 129 |
if( ! array_key_exists($k, $pass_params) ){ |
| 130 |
$params[$k] = $default_v; |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
if( ! is_array($default_v) ){ |
| 135 |
$params[$k] = $pass_params[$k]; |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
if( ! is_array($pass_params[$k]) ){ |
| 140 |
$pass_params[$k] = array( $pass_params[$k] ); |
| 141 |
} |
| 142 |
|
| 143 |
$v = array(); |
| 144 |
foreach( $pass_params[$k] as $pass_v ){ |
| 145 |
if( in_array($pass_v, $default_v) ){ |
| 146 |
$v[] = $pass_v; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if( ! $v ){ |
| 151 |
$v = $default_v; |
| 152 |
} |
| 153 |
$params[$k] = $v; |
| 154 |
} |
| 155 |
|
| 156 |
if( isset($_GET['lpr-search']) ){ |
| 157 |
$get_search = sanitize_text_field($_GET['lpr-search']); |
| 158 |
$params['start'] = $get_search; |
| 159 |
} |
| 160 |
|
| 161 |
// also can override any of the params by GET |
| 162 |
$keys = array_keys($default_params); |
| 163 |
foreach( $keys as $key ){ |
| 164 |
$get_key = 'lctr-' . $key; |
| 165 |
if( ! array_key_exists($get_key, $_GET) ){ |
| 166 |
continue; |
| 167 |
} |
| 168 |
$get = sanitize_text_field( $_GET[$get_key] ); |
| 169 |
$params[ $key ] = $get; |
| 170 |
} |
| 171 |
|
| 172 |
// _print_r( $pass_params ); |
| 173 |
// _print_r( $params ); |
| 174 |
|
| 175 |
if( isset($params['clustering']) && $params['clustering'] ){ |
| 176 |
$img_path = 'happ2/modules/maps_google/assets/js/images/m'; |
| 177 |
$img_path = $this->app->make('/layout.wordpress/path') |
| 178 |
->full_path( $img_path ) |
| 179 |
; |
| 180 |
$clusterer_params = array( |
| 181 |
'img_path' => $img_path, |
| 182 |
'count' => $params['clustering'], |
| 183 |
); |
| 184 |
$enqueuer |
| 185 |
->localize_script( 'gmapsclusterer', $clusterer_params ) |
| 186 |
; |
| 187 |
$enqueuer |
| 188 |
->enqueue_script( 'gmapsclusterer' ) |
| 189 |
; |
| 190 |
} |
| 191 |
|
| 192 |
// parse layout |
| 193 |
$layout_conf_setting = $params['layout']; |
| 194 |
$allowed_components = array('map', 'list'); |
| 195 |
|
| 196 |
$partial = isset( $params['partial'] ) ? $params['partial'] : ''; |
| 197 |
|
| 198 |
$explode_by = ''; |
| 199 |
$layout = array(); |
| 200 |
if( strpos($layout_conf_setting, '|') !== FALSE ){ |
| 201 |
$explode_by = '|'; |
| 202 |
} |
| 203 |
elseif( strpos($layout_conf_setting, '/') !== FALSE ){ |
| 204 |
$explode_by = '/'; |
| 205 |
} |
| 206 |
|
| 207 |
if( $explode_by ){ |
| 208 |
$layout_setting_array = explode($explode_by, $layout_conf_setting); |
| 209 |
foreach( $layout_setting_array as $ls ){ |
| 210 |
$ls = strtolower(trim($ls)); |
| 211 |
if( ! strlen($ls) ){ |
| 212 |
continue; |
| 213 |
} |
| 214 |
if( ! in_array($ls, $allowed_components) ){ |
| 215 |
continue; |
| 216 |
} |
| 217 |
$layout[] = $ls; |
| 218 |
} |
| 219 |
if( count($layout) > 1 ){ |
| 220 |
$layout[] = $explode_by; |
| 221 |
} |
| 222 |
} |
| 223 |
else { |
| 224 |
$layout[] = $layout_conf_setting; |
| 225 |
} |
| 226 |
|
| 227 |
if( ! $layout ){ |
| 228 |
$layout = array('map', 'list', '|'); |
| 229 |
} |
| 230 |
|
| 231 |
if( $params['id'] ){ |
| 232 |
$layout = array('map'); |
| 233 |
$layout = array('map', 'list', '/'); |
| 234 |
} |
| 235 |
|
| 236 |
$view_type = 'stack'; |
| 237 |
if( (count($layout) > 1) && ($layout[count($layout)-1] == '|') ){ |
| 238 |
$view_type = 'grid'; |
| 239 |
} |
| 240 |
|
| 241 |
$lc_front_params = array(); |
| 242 |
if( $params['search-bias-country'] ){ |
| 243 |
$search_bias_country = $params['search-bias-country']; |
| 244 |
$search_bias_country = explode(',', $search_bias_country); |
| 245 |
$lc_front_params['search_bias_country'] = $search_bias_country; |
| 246 |
} |
| 247 |
$enqueuer |
| 248 |
->localize_script( 'lc_front', $lc_front_params ) |
| 249 |
; |
| 250 |
|
| 251 |
// parse radius |
| 252 |
if( isset($params['radius']) ){ |
| 253 |
$supplied = $params['radius']; |
| 254 |
if( ! is_array($supplied) ){ |
| 255 |
$supplied = explode(',', $supplied); |
| 256 |
} |
| 257 |
|
| 258 |
$final = array(); |
| 259 |
foreach( $supplied as $r ){ |
| 260 |
$r = trim($r); |
| 261 |
if( (string)(int) $r == $r ){ |
| 262 |
$final[] = $r; |
| 263 |
} |
| 264 |
} |
| 265 |
$final = array_unique( $final ); |
| 266 |
$params['radius'] = $final; |
| 267 |
} |
| 268 |
else { |
| 269 |
$params['radius'] = array(); |
| 270 |
} |
| 271 |
|
| 272 |
$form = $this->app->make('/front/view/form') |
| 273 |
->render($params) |
| 274 |
; |
| 275 |
|
| 276 |
$form_view = $this->app->make('/html/element')->tag('div') |
| 277 |
->add( $form ) |
| 278 |
->add_attr('class', 'hc-mb3') |
| 279 |
; |
| 280 |
|
| 281 |
$views = array(); |
| 282 |
if( in_array('map', $layout) ){ |
| 283 |
$views['map'] = $this->app->make('/front/view/map') |
| 284 |
->render($params) |
| 285 |
; |
| 286 |
$widths['map'] = 8; |
| 287 |
} |
| 288 |
|
| 289 |
if( in_array('list', $layout) ){ |
| 290 |
$need_list_params = array('group', 'group-jump', 'list-style', 'empty-group-label'); |
| 291 |
$list_params = array(); |
| 292 |
foreach( $params as $k => $v ){ |
| 293 |
if( ! in_array($k, $need_list_params) ){ |
| 294 |
continue; |
| 295 |
} |
| 296 |
if( null === $v ){ |
| 297 |
continue; |
| 298 |
} |
| 299 |
$v = trim($v); |
| 300 |
if( ! strlen($v) ){ |
| 301 |
continue; |
| 302 |
} |
| 303 |
$list_params[$k] = $v; |
| 304 |
} |
| 305 |
$views['list'] = $this->app->make('/front/view/list') |
| 306 |
->render($list_params) |
| 307 |
; |
| 308 |
$widths['list'] = 4; |
| 309 |
} |
| 310 |
|
| 311 |
$formAfterMap = isset( $params['form-after-map'] ) && $params['form-after-map'] ? TRUE : FALSE; |
| 312 |
|
| 313 |
$form_view = $this->app->make('/html/element')->tag('div') |
| 314 |
->add( $form_view ) |
| 315 |
->add_attr('id', 'locatoraid-form-container') |
| 316 |
; |
| 317 |
|
| 318 |
if( 'form' == $partial ){ |
| 319 |
return $form_view; |
| 320 |
} |
| 321 |
if( 'map' == $partial ){ |
| 322 |
return $views['map']; |
| 323 |
} |
| 324 |
if( 'list' == $partial ){ |
| 325 |
return $views['list']; |
| 326 |
} |
| 327 |
|
| 328 |
if( $formAfterMap ){ |
| 329 |
$form_view->add_attr( 'class', 'hc-mt2' ); |
| 330 |
} |
| 331 |
|
| 332 |
if( count($layout) > 1 ){ |
| 333 |
switch( $view_type ){ |
| 334 |
case 'grid': |
| 335 |
$grid_id = 'hclc_grid'; |
| 336 |
$out2 = $this->app->make('/html/grid') |
| 337 |
->set_gutter(2) |
| 338 |
; |
| 339 |
|
| 340 |
foreach( $layout as $k ){ |
| 341 |
if( ! isset($views[$k]) ){ |
| 342 |
continue; |
| 343 |
} |
| 344 |
|
| 345 |
$out2 |
| 346 |
->add( $views[$k], $widths[$k] ) |
| 347 |
; |
| 348 |
} |
| 349 |
|
| 350 |
$out2 = $this->app->make('/html/element')->tag('div') |
| 351 |
->add( $out2 ) |
| 352 |
->add_attr('id', $grid_id) |
| 353 |
// ->add_attr('style', 'height: 400px;') |
| 354 |
; |
| 355 |
|
| 356 |
if( $formAfterMap && isset($views['map']) ){ |
| 357 |
$out2 |
| 358 |
->add( $form_view ) |
| 359 |
; |
| 360 |
} |
| 361 |
break; |
| 362 |
|
| 363 |
default: |
| 364 |
if( $formAfterMap && isset($views['map']) ){ |
| 365 |
$views['map'] = $this->app->make('/html/element')->tag('div') |
| 366 |
->add( $views['map'] ) |
| 367 |
->add( $form_view ) |
| 368 |
; |
| 369 |
} |
| 370 |
|
| 371 |
$out2 = $this->app->make('/html/element')->tag(NULL); |
| 372 |
foreach( $layout as $k ){ |
| 373 |
if( ! isset($views[$k]) ){ |
| 374 |
continue; |
| 375 |
} |
| 376 |
|
| 377 |
$out2 |
| 378 |
->add( |
| 379 |
$this->app->make('/html/element')->tag('div') |
| 380 |
->add( $views[$k] ) |
| 381 |
->add_attr('class', 'hc-mb3') |
| 382 |
) |
| 383 |
; |
| 384 |
} |
| 385 |
break; |
| 386 |
} |
| 387 |
} |
| 388 |
else { |
| 389 |
$out2 = $views[$layout[0]]; |
| 390 |
} |
| 391 |
|
| 392 |
$out2 = $this->app->make('/html/element')->tag('div') |
| 393 |
->add( $out2 ) |
| 394 |
->add_attr('id', 'locatoraid-map-list-container') |
| 395 |
; |
| 396 |
|
| 397 |
$out = $this->app->make('/html/element')->tag(NULL); |
| 398 |
|
| 399 |
if( $formAfterMap && isset($views['map']) ){ |
| 400 |
} |
| 401 |
else { |
| 402 |
$out |
| 403 |
->add( $form_view ) |
| 404 |
; |
| 405 |
} |
| 406 |
|
| 407 |
$out |
| 408 |
->add( $out2 ) |
| 409 |
; |
| 410 |
|
| 411 |
$out = $this->app |
| 412 |
->after( $this, $out ) |
| 413 |
; |
| 414 |
|
| 415 |
return $out; |
| 416 |
} |
| 417 |
} |