      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:

    <?php

    // See https://docs.google.com/document/d/16NdGw4C4cd5Q20OwQDGpMB_GiYnojz0Mrug-QRS5zoE/edit#

    namespace WPGMZA;

    class MarkerFilter extends Factory
    {
        protected $_center;
        protected $_radius;

        public function __construct($options=null)
        {
            if($options)
                foreach($options as $key => $value)
                    $this->{$key} = $value;
        }

        public function __get($name)
        {
            if(property_exists($this, "_$name"))
                return $this->{"_$name"};

            return $this->{$name};
        }

        public function __set($name, $value)
        {
            if(property_exists($this, "_$name"))
            {
                switch($name)
                {
                    case 'center':

                        if(!is_array($value) && !is_object($value))
                            throw new \Exception('Value must be an object or array with lat and lng');

                        $arr = (array)$value;

                        if(!isset($arr['lat']) || !isset($arr['lng']))
                            throw new \Exception('Value must have lat and lng');

                        $this->_center = $arr;

                        break;

                    default:

                        $this->{"_$name"} = $value;

                        break;
                }

                return;
            }

            $this->{$name} = $value;
        }

        protected function loadMap()
        {
            global $wpdb;

            $id = $wpdb->get_var("SELECT id FROM {$wpdb->prefix}wpgmza_maps LIMIT 1");

            if(!$id)
                return;

            $this->map = new Map($id);
        }

        protected function applyRadiusClause($query)
        {
            if(!$this->center || !$this->radius)
                return;

            $lat = $this->_center['lat'] / 180 * 3.1415926;
            $lng = $this->_center['lng'] / 180 * 3.1415926;
            $radius = $this->radius;

            if($this->map && $this->map->storeLocatorDistanceUnits == Distance::UNITS_MI)
                $radius *= Distance::KILOMETERS_PER_MILE;

            $query->where['radius'] = '
                (
                    6381 *

                    2 *

                    ATAN2(
                        SQRT(
                            POW( SIN( ( (X(latlng) / 180 * 3.1415926) - %f ) / 2 ), 2 ) +
                            COS( X(latlng) / 180 * 3.1415926 ) * COS( %f ) *
                            POW( SIN( ( (Y(latlng) / 180 * 3.1415926) - %f ) / 2 ), 2 )
                        ),

                        SQRT(1 -
                            (
                                POW( SIN( ( (X(latlng) / 180 * 3.1415926) - %f ) / 2 ), 2 ) +
                                COS( X(latlng) / 180 * 3.1415926 ) * COS( %f ) *
                                POW( SIN( ( (Y(latlng) / 180 * 3.1415926) - %f ) / 2 ), 2 )
                            )
                        )
                    )
                )

                < %f
            ';

            $query->params[] = $lat;
            $query->params[] = $lat;
            $query->params[] = $lng;

            $query->params[] = $lat;
            $query->params[] = $lat;
            $query->params[] = $lng;

            $query->params[] = $radius;
        }

        public function getQuery()
        {
            global $WPGMZA_TABLE_NAME_MARKERS;

            $query = new Query();

            $query->type    = 'SELECT';
            $query->table   = $WPGMZA_TABLE_NAME_MARKERS;

            $this->applyRadiusClause($query);

            return $query;
        }

        public function getFilteredMarkers($fields=null)
        {
            global $wpdb;

            $query = $this->getQuery();

            if(empty($fields))
                $query->fields[] = '*';
            else
                foreach($fields as $field)
                    $query->fields[] = $field;

            $sql = $query->build();

            $results = $wpdb->get_results($sql);

            return $results;
        }

        public function getFilteredIDs()
        {
            global $wpdb;

            $query = $this->getQuery();

            $query->fields[] = 'id';

            $sql = $query->build();

            return $wpdb->get_col($sql);
        }

    }