      1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345:

    <?php

    namespace WPGMZA;

    class MarkerListing extends AjaxTable
    {
        const STYLE_NONE                    = 0;
        const STYLE_BASIC_TABLE             = 1;
        const STYLE_BASIC_LIST              = 4;
        const STYLE_ADVANCED_TABLE          = 2;
        const STYLE_CAROUSEL                = 3;
        const STYLE_MODERN                  = 6;

        const ORDER_BY_ID                   = 1;
        const ORDER_BY_TITLE                = 2;
        const ORDER_BY_ADDRESS              = 3;
        const ORDER_BY_DESCRIPTION          = 4;
        const ORDER_BY_CATEGORY             = 5;
        const ORDER_BY_CATEGORY_PRIORITY    = 6;
        const ORDER_BY_DISTANCE             = 7;

        const ORDER_ASC                     = 1;
        const ORDER_DESC                    = 2;

        private static $_cachedColumnNames;
        protected $map;

        // TODO: Pass the map object rather than ID
        public function __construct($map_id)
        {
            global $wpdb;
            global $wpgmza;

            AjaxTable::__construct("{$wpdb->prefix}wpgmza", '/marker-listing/');

            $this->map = Map::createInstance($map_id);

            // HTML attributes
            $this->element->setAttribute('data-wpgmza-marker-listing', null);

            // Legacy HTML
            if($wpgmza->settings->useLegacyHTML)
            {
                $this->element->addClass('wpgmza_marker_list_class');
                $this->element->setInlineStyle('width', '100%');
            }

            // Pagination
            $proDir = plugin_dir_url(dirname(__DIR__));
            wp_enqueue_style('wpgmza_pagination', $proDir . 'lib/pagination.css');
            wp_enqueue_script('wpgmza_pagination', $proDir . 'lib/pagination.min.js');
        }

        public function __get($name)
        {
            global $wpgmza;

            switch($name)
            {
                case 'hideIcon':
                    return !empty($wpgmza->settings->wpgmza_settings_markerlist_icon);
                    break;

                case 'hideLink':
                    return !empty($wpgmza->settings->wpgmza_settings_markerlist_link);
                    break;

                case 'hideTitle':
                    return !empty($wpgmza->settings->wpgmza_settings_markerlist_title);
                    break;

                case 'hideAddress':
                    return !empty($wpgmza->settings->wpgmza_settings_markerlist_address);
                    break;

                case 'hideCategories':
                    // Only applicable for AdvancedTable
                    break;

                case 'hideDescription':
                    return !empty($wpgmza->settings->wpgmza_settings_markerlist_description);
                    break;
            }

            return AjaxTable::__get($name);
        }

        protected function getItemHTMLPath()
        {
            global $wpgmza;

            $path = plugin_dir_path(dirname(__DIR__)) . 'html/marker-listings/';

            if($wpgmza->settings->useLegacyHTML)
                $path .= 'legacy/';

            return $path;
        }

        public function getColumns()
        {
            global $wpdb;

            if(empty(MarkerListing::$_cachedColumnNames))
            {
                $cols = $wpdb->get_col("SHOW COLUMNS FROM {$this->table_name}");
                MarkerListing::$_cachedColumnNames = array_combine($cols, $cols);
            }

            $result = (array)MarkerListing::$_cachedColumnNames;

            return $result;
        }

        protected function filterColumns(&$columns, $input_params)
        {
            AjaxTable::filterColumns($columns, $input_params);

            foreach($columns as $key => $value)
            {
                $name = $this->getColumnNameByIndex($key);

                switch($name)
                {
                    case 'icon':

                        $columns[$key] = \WPGMZA\ProMarker::getIconSQL($this->map->id);

                        break;
                }
            }

            if($this->map->order_markers_by == MarkerListing::ORDER_BY_DISTANCE && !empty($input_params['filteringParams']['center']))
            {
                $lat = floatval($input_params['filteringParams']['center']['lat']) / 180 * 3.1415926;
                $lng = floatval($input_params['filteringParams']['center']['lng']) / 180 * 3.1415926;

                $columns['distance'] = "
                    (
                        6381 *

                        2 *

                        ATAN2(
                            SQRT(
                                POW( SIN( ( (X(latlng) / 180 * 3.1415926) - $lat ) / 2 ), 2 ) +
                                COS( X(latlng) / 180 * 3.1415926 ) * COS( $lat ) *
                                POW( SIN( ( (Y(latlng) / 180 * 3.1415926) - $lng ) / 2 ), 2 )
                            ),

                            SQRT(1 -
                                (
                                    POW( SIN( ( (X(latlng) / 180 * 3.1415926) - $lat ) / 2 ), 2 ) +
                                    COS( X(latlng) / 180 * 3.1415926 ) * COS( $lat ) *
                                    POW( SIN( ( (Y(latlng) / 180 * 3.1415926) - $lng ) / 2 ), 2 )
                                )
                            )
                        )
                    ) AS distance
                ";
            }

            return $columns;
        }

        public function filterOrderBy($orderBy, $keys)
        {
            global $WPGMZA_TABLE_NAME_MARKERS;
            global $WPGMZA_TABLE_NAME_MARKERS_HAS_CATEGORIES;
            global $WPGMZA_TABLE_NAME_CATEGORIES;

            $column = (empty($this->map->order_markers_by) ? MarkerListing::ORDER_BY_ID : $this->map->order_markers_by);

            switch($column)
            {
                case MarkerListing::ORDER_BY_TITLE:
                    $orderBy = "title";
                    break;

                case MarkerListing::ORDER_BY_ADDRESS:
                    $orderBy = "address";
                    break;

                case MarkerListing::ORDER_BY_DESCRIPTION:
                    $orderBy = "description";
                    break;

                case MarkerListing::ORDER_BY_CATEGORY:
                    $orderBy = "category";
                    break;

                case MarkerListing::ORDER_BY_CATEGORY_PRIORITY:
                    return "(
                        SELECT MAX(priority) FROM $WPGMZA_TABLE_NAME_CATEGORIES
                        WHERE $WPGMZA_TABLE_NAME_CATEGORIES.id IN (
                            SELECT category_id FROM $WPGMZA_TABLE_NAME_MARKERS_HAS_CATEGORIES
                            WHERE marker_id = $WPGMZA_TABLE_NAME_MARKERS.id
                        )
                    )";
                    break;

                case MarkerListing::ORDER_BY_DISTANCE:
                    if(!empty($_REQUEST['filteringParams']['center']))
                    {
                        $orderBy = "distance";
                        break;
                    }

                default:
                    $orderBy = "$WPGMZA_TABLE_NAME_MARKERS.id";
                    break;
            }

            return $orderBy;
        }

        public function filterOrderDirection($input_params)
        {
            $dir = (empty($this->map->order_markers_choice) ? MarkerListing::ORDER_DESC : $this->map->order_markers_choice);

            return ($dir == MarkerListing::ORDER_DESC ? 'DESC' : 'ASC');
        }

        public function setAjaxParameters($params)
        {
            global $wpgmza;

            AjaxTable::setAjaxParameters($params);

            $obj = (object)$params;

            if($wpgmza->settings->useLegacyHTML && property_exists($obj, 'map_id'))
                $this->element->setAttribute('id', 'wpgmza_marker_list_' . $obj->map_id);
        }

        public function getImageDimensions()
        {
            global $wpgmza;

            $dimensions = (object)array(
                'width'     => 100,
                'height'    => 'auto'
            );

            if(!empty($wpgmza->settings->wpgmza_settings_image_width))
                $dimensions->width = $wpgmza->settings->wpgmza_settings_image_width;

            if(!empty($wpgmza->settings->wpgmza_settings_image_height))
                $dimensions->height = $wpgmza->settings->wpgmza_settings_image_height;

            return $dimensions;
        }

        protected function removeHiddenFields($item, $marker)
        {
            global $wpgmza;

            // Hide the icon if setting is selected
            if($this->hideIcon && $el = $item->querySelector('.wpgmza_marker_icon'))
                $el->remove();

            // Hide the link if selected
            if($link = $item->querySelector('.wpgmza-link, .wpgmza_marker_link'))
            {
                if(empty($marker->link) || $this->hideLink)
                    $link->remove();
            }

            // Hide title if selected
            if($this->hideTitle && $el = $item->querySelector('.wpgmza_marker_title, .wpgmza_div_title'))
                $el->remove();

            // Hide address if selected
            if($this->hideAddress && $el = $item->querySelector('.wpgmza_div_address, .wpgmza-address, .wpgmza_marker_address'))
                $el->remove();

            // Hide description if selected
            if($this->hideDescription && $el = $item->querySelector('.wpgmza-desc, .wpgmza_marker_description'))
                $el->remove();
        }

        protected function appendListingItem($document, $item, $marker)
        {
            global $wpgmza;

            $container = $document->querySelector('body');

            if($wpgmza->settings->useLegacyHTML)
            {
                $even = ($container->childNodes->length % 2 == 1);
                $item->addClass(($even ? 'wpgmaps_even' : 'wpgmaps_odd'));

                if($customFieldsContainer = $item->querySelector(".wpgmza_custom_fields"))
                {
                    $customFields = new CustomMarkerFields($marker->id);
                    $customFieldsContainer->import($customFields->html());
                }
            }

            $this->removeHiddenFields($item, $marker);

            $container->appendChild($item);
        }

        public static function createInstanceFromStyle($style, $map_id)
        {
            $class = apply_filters('wpgmza_get_marker_listing_class_from_style', $style);

            if(!$class)
                return null;

            return $class::createInstance($map_id);
        }
    }

    // TODO: Don't use a closure, allow this filter to be removed
    add_filter('wpgmza_get_marker_listing_class_from_style', function($style) {

        switch($style)
        {
            case MarkerListing::STYLE_BASIC_TABLE:
                return '\WPGMZA\MarkerListing\BasicTable';
                break;

            case MarkerListing::STYLE_BASIC_LIST:
                return '\WPGMZA\MarkerListing\BasicList';
                break;

            case MarkerListing::STYLE_ADVANCED_TABLE:
                return '\WPGMZA\MarkerListing\AdvancedTable';
                break;

            case MarkerListing::STYLE_CAROUSEL:
                return '\WPGMZA\MarkerListing\Carousel';
                break;

            case MarkerListing::STYLE_MODERN:
                return '\WPGMZA\MarkerListing\Modern';
                break;
        }

        return null;

    }, 1, 1);