      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:

    <?php

    namespace WPGMZA;

    // TODO: Remove, autoloaders are now used
    require_once(plugin_dir_path(__FILE__) . '/class.crud.php');

    /**
     * This class represents a marker
     */
    class Marker extends Crud implements \JsonSerializable
    {
        const DEFAULT_ICON = "//maps.gstatic.com/mapfiles/api-3/images/spotlight-poi2.png";

        private static $columns;
        protected $custom_fields;

        /**
         * Constructor
         * @param int|array|object An integer ID to read a marker, or an array or object to read data from to create a new one. If this argument is not specified, a new marker will be created.
         */
        public function __construct($id_or_fields=-1)
        {
            global $wpdb;

            Crud::__construct("{$wpdb->prefix}wpgmza", $id_or_fields);

            if(class_exists('WPGMZA\\CustomMarkerFields'))
                $this->custom_fields = apply_filters('wpgmza_get_marker_custom_fields', $this->id);
        }

        public static function getColumns()
        {
            global $wpdb;
            global $WPGMZA_TABLE_NAME_MARKERS;

            if(Marker::$columns)
                return Marker::$columns;

            Marker::$columns = $wpdb->get_results('SHOW COLUMNS FROM ' . $WPGMZA_TABLE_NAME_MARKERS);

            return Marker::$columns;
        }

        /**
         * Deprecated. The Factory class will take over this functionality
         * @deprecated
         */
        public static function create_instance($id_or_fields=-1)
        {
            return apply_filters('wpgmza_create_marker_instance', $id_or_fields);
        }

        public static function get_table_name_static()
        {
            global $wpdb;
            return "{$wpdb->prefix}wpgmza";
        }

        /**
         * Returns a clone of this marker for JSON serialization. Unsets latlng binary spatial data which corrupts JSON, and sets custom field data.
         * @return array A JSON representation of this marker, without spatial data and with custom field ata.
         */
        public function jsonSerialize()
        {
            $json = Crud::jsonSerialize();

            unset($json['latlng']);

            return $json;
        }

        /**
         * Overrides Crud::get_placeholder_by_type to correctly handle inserting and updating spatial data placeholders.
         * @param string $type The column type
         * @return string If ths column type is point, the correct text-to-spatial function and placeholder. Otherwise, falls back to the default.
         */
        protected function get_placeholder_by_type($type)
        {
            global $wpgmza;

            if($type == 'point')
                return "{$wpgmza->spatialFunctionPrefix}GeomFromText(%s)";

            return Crud::get_placeholder_by_type($type);
        }

        /**
         * Overrides Crud::get_column_parameter to correctly handle inserting and updating spatial data values.
         * @param string $name The colum name
         * @return string If ths column name is latlng, the POINT function with this markers latitude and longitude. Otherwise, falls back to the default.
         */
        protected function get_column_parameter($name)
        {
            if($name == 'latlng')
                return "POINT(" . floatval($this->lat) . " " . floatval($this->lng) . ")";

            return Crud::get_column_parameter($name);
        }

        /**
         * Returns "other_data", which is the name of the column used to store arbitrary data on thet marker table.
         * @return string Always other_data for the marker table.
         */
        protected function get_arbitrary_data_column_name()
        {
            return 'other_data';
        }

        public function update()
        {
            Crud::update();

            // TODO: Update markers-has-categories
        }

        /**
         * Called to update the latlng column for this marker in the database, when any changes are made to this objects properties lat, lng or latlng.
         * @return void
         */
        protected function update_latlng()
        {
            global $wpgmza;
            global $wpdb;

            $params = array(
                $this->lat,
                $this->lng,
                $this->get_column_parameter('latlng'),
                $this->id
            );

            $stmt = $wpdb->prepare("UPDATE " . $this->get_table_name() . " SET lat=%s, lng=%s, latlng={$wpgmza->spatialFunctionPrefix}GeomFromText(%s) WHERE id=%d", $params);

            $wpdb->query($stmt);
        }

        /**
         * Sets the named property, calling update_latlng where needed.
         */
        public function __set($name, $value)
        {
            Crud::__set($name, $value);

            switch($name)
            {
                case 'lat':
                case 'lng':
                case 'latlng':
                    $this->update_latlng();
                    break;
            }
        }

        public function getPosition()
        {
            return new LatLng($this->lat, $this->lng);
        }
    }

    // DEPRECATED: This will be handed over to the factory class
    // You can remove this filter and add your own delegate to subclass Marker
    add_filter('wpgmza_create_marker_instance', 'WPGMZA\\create_marker_instance_delegate', 10, 1);

    function create_marker_instance_delegate($id)
    {
        return new Marker($id);
    }