      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: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424:

    <?php
    /**
     * WP Google Maps Pro Import / Export API: Export class
     *
     * @package WPGMapsPro\ImportExport
     * @since 7.0.0
     */

    namespace WPGMZA;

    /**
     * Exporter for WP Google Maps Pro
     *
     * This handles exporting of maps, categories, markers, circles,
     * polygons, polylines, rectangles and datasets.
     *
     * @since 7.0.0
     */
    class Export {

        const JSON_VERSION = '1.01';

        /**
         * Export arguments.
         *
         * @var array $export_args Documented in constructor.
         */
        private $export_args = array();

        /**
         * Export constructor.
         *
         * @param array $args {
         *     Optional. Export arguments.
         *
         *     @type array $maps       Integer array of map ids to export.
         *                             Empty array for all. Default empty.
         *     @type bool  $categories Export categories? Default true.
         *     @type bool  $markers    Export markers? Default true.
         *     @type bool  $circles    Export circles? Default true.
         *     @type bool  $polygons   Export polygons? Default true.
         *     @type bool  $polylines  Export polylines? Default true.
         *     @type bool  $rectangles Export rectangles? Default true.
         *     @type bool  $datasets   Export datasets? Default true.
         * }
         */
        public function __construct( $args = array() ) {

            $export_args = wp_parse_args( $args, array(
                'maps'         => array(),
                'categories'   => true,
                'customfields' => true,
                'markers'      => true,
                'circles'      => true,
                'polygons'     => true,
                'polylines'    => true,
                'rectangles'   => true,
                'datasets'     => true,
            ) );

            if ( is_array( $export_args['maps'] ) ) {

                $export_args['maps'] = $this->sanitize_map_ids( $export_args['maps'] );

            } else {

                $export_args['maps'] = array();

            }

            $this->export_args = $export_args;

        }

        /**
         * Sanitize map ids.
         *
         * @param array $maps Integer array of map ids.
         * @return array Integer array of map ids.
         */
        private function sanitize_map_ids( $maps ) {

            $map_count = count( $maps );

            for ( $i = 0; $i < $map_count; $i++ ) {

                if ( ! is_numeric( $maps[ $i ] ) ) {

                    unset( $maps[ $i ] );
                    continue;

                }

                $maps[ $i ] = absint( $maps[ $i ] );

                if ( $maps[ $i ] < 1 ) {

                    unset( $maps[ $i ] );

                }
            }

            return $maps;

        }

        /**
         * Generates the JSON export file for download.
         *
         * @global string $wpgmza_pro_version WP Google Maps Pro version number.
         */
        public function download() {

            global $wpgmza_pro_version;

            $site_name = sanitize_key( get_bloginfo( 'name' ) );

            if ( ! empty( $site_name ) ) {

                $site_name .= '.';

            }

            $file_name = $site_name . 'wpgooglemaps.' . current_time( 'Y-m-d' ) . '.json';

            header( 'Content-Description: File Transfer' );
            header( 'Content-Disposition: attachment; filename=' . $file_name );
            header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ), true );

            $pretty_print = version_compare( PHP_VERSION, '5.4', '>=' ) && defined( 'JSON_PRETTY_PRINT' ) ? JSON_PRETTY_PRINT : 0;

            echo wp_json_encode($this->get_data(), $pretty_print );

        }

        public function get_data()
        {
            global $wpgmza_pro_version;

            return  array(
                'creator'      => 'WPGoogleMapsPro',
                'version'      => $wpgmza_pro_version,
                'json_version' => Export::JSON_VERSION,

                'maps'         => $this->get_maps_data(),
                'categories'   => $this->get_categories_data(),
                'customfields' => $this->get_custom_fields_data(),
                'markers'      => $this->get_markers_data(),
                'circles'      => $this->get_circles_data(),
                'polygons'     => $this->get_polygons_data(),
                'polylines'    => $this->get_polylines_data(),
                'rectangles'   => $this->get_rectangles_data(),
                'datasets'     => $this->get_datasets_data(),
            );
        }

        /**
         * Get maps data.
         *
         * @global wpdb   $wpdb                WordPress database object.
         * @global string $wpgmza_tblname_maps Maps database table name.
         *
         * @return array Maps database rows as associative array.
         */
        private function get_maps_data() {

            global $wpdb;
            global $wpgmza_tblname_maps;
            global $WPGMZA_TABLE_NAME_CUSTOM_FIELDS;
            global $WPGMZA_TABLE_NAME_MAPS_HAS_CUSTOM_FIELDS_FILTERS;

            $where = empty( $this->export_args['maps'] ) ? '1=1' : '`id` IN (' . implode( ',', $this->export_args['maps'] ) . ')';

            $results = $wpdb->get_results( "SELECT * FROM `$wpgmza_tblname_maps` WHERE $where AND active = '0'", ARRAY_A );

            foreach($results as $key => $array)
            {
                $filter_custom_field_names = $wpdb->get_col("
                    SELECT name
                    FROM $WPGMZA_TABLE_NAME_MAPS_HAS_CUSTOM_FIELDS_FILTERS
                    JOIN $WPGMZA_TABLE_NAME_CUSTOM_FIELDS ON field_id = id
                    WHERE map_id=" . (int)$array['id']
                );

                if(empty($filter_custom_field_names))
                    continue;

                $results[$key]['filter_custom_field_names'] = $filter_custom_field_names;
            }

            return empty( $results ) ? array() : $results;

        }

        /**
         * Get categories data.
         *
         * @global wpdb   $wpdb                         WordPress database object.
         * @global string $wpgmza_tblname_category_maps Categories maps database table name.
         * @global string $wpgmza_tblname_categories    Categories database table name.
         *
         * @return array Categories database rows as associative array.
         */
        private function get_categories_data() {

            if ( ! $this->export_args['categories'] ) {

                return array();

            }

            global $wpdb;
            global $wpgmza_tblname_category_maps;
            global $wpgmza_tblname_categories;

            $where = empty( $this->export_args['maps'] ) ? '1=1' : "`$wpgmza_tblname_category_maps`.`map_id` = 0 OR `$wpgmza_tblname_category_maps`.`map_id` IN (" . implode( ',', $this->export_args['maps'] ) . ')';

            $results = $wpdb->get_results( "SELECT `$wpgmza_tblname_categories`.*, `$wpgmza_tblname_category_maps`.`map_id` FROM `$wpgmza_tblname_categories` JOIN `$wpgmza_tblname_category_maps` ON `$wpgmza_tblname_categories`.`id` = `$wpgmza_tblname_category_maps`.`cat_id` WHERE $where", ARRAY_A );

            return empty( $results ) ? array() : $results;

        }

        /**
         * Get custom fields data.
         *
         * @global wpdb   $wpdb                         WordPress database object.
         * @global string $wpgmza_tblname_category_maps Categories maps database table name.
         * @global string $wpgmza_tblname_categories    Categories database table name.
         *
         * @return array Custom fields database rows as associative array.
         */
        private function get_custom_fields_data() {

            if ( ! $this->export_args['customfields'] ) {

                return array();

            }

            return new CustomFields();

        }

        /**
         * Get markers data.
         *
         * @global wpdb   $wpdb           WordPress database object.
         * @global string $wpgmza_tblname Markers database table name.
         *
         * @return array Markers database rows as associative array.
         */
        private function get_markers_data() {

            if ( ! $this->export_args['markers'] ) {

                return array();

            }

            global $wpdb;
            global $wpgmza_tblname;

            $where = empty( $this->export_args['maps'] ) ? '1=1' : '`map_id` IN (' . implode( ',', $this->export_args['maps'] ) . ')';

            $results = $wpdb->get_results( "SELECT * FROM `$wpgmza_tblname` WHERE $where", ARRAY_A );

            foreach($results as $key => $marker)
            {
                unset($results[$key]['latlng']);

                $customMarkerFields = new CustomMarkerFields($marker['id']);

                if(count($customMarkerFields) > 0)
                    $results[$key]['custom_fields_data'] = $customMarkerFields;
            }

            return empty( $results ) ? array() : $results;

        }

        /**
         * Get circles data.
         *
         * @global wpdb   $wpdb                   WordPress database object.
         * @global string $wpgmza_tblname_circles Circles database table name.
         *
         * @return array Circles database rows as associative array.
         */
        private function get_circles_data() {

            if ( ! $this->export_args['circles'] ) {

                return array();

            }

            global $wpdb;
            global $wpgmza;
            global $wpgmza_tblname_circles;

            $where = empty( $this->export_args['maps'] ) ? '1=1' : '`map_id` IN (' . implode( ',', $this->export_args['maps'] ) . ')';

            $results = $wpdb->get_results(
                "SELECT `id`, `map_id`, `name`, {$wpgmza->spatialFunctionPrefix}X(`center`) AS `centerX`, {$wpgmza->spatialFunctionPrefix}Y(`center`) AS `centerY`, `radius`, `color`, `opacity`
                FROM `$wpgmza_tblname_circles`
                WHERE $where",
                ARRAY_A
            );

            return empty( $results ) ? array() : $results;

        }

        /**
         * Get polygons data.
         *
         * @global wpdb   $wpdb                WordPress database object.
         * @global string $wpgmza_tblname_poly Polygons database table name.
         *
         * @return array Polygons database rows as associative array.
         */
        private function get_polygons_data() {

            if ( ! $this->export_args['polygons'] ) {

                return array();

            }

            global $wpdb;
            global $wpgmza_tblname_poly;

            $where = empty( $this->export_args['maps'] ) ? '1=1' : '`map_id` IN (' . implode( ',', $this->export_args['maps'] ) . ')';

            $results = $wpdb->get_results( "SELECT * FROM `$wpgmza_tblname_poly` WHERE $where", ARRAY_A );

            return empty( $results ) ? array() : $results;

        }

        /**
         * Get polylines data.
         *
         * @global wpdb   $wpdb                     WordPress database object.
         * @global string $wpgmza_tblname_polylines Polylines database table name.
         *
         * @return array Polylines database rows as associative array.
         */
        private function get_polylines_data() {

            if ( ! $this->export_args['polylines'] ) {

                return array();

            }

            global $wpdb;
            global $wpgmza_tblname_polylines;

            $where = empty( $this->export_args['maps'] ) ? '1=1' : '`map_id` IN (' . implode( ',', $this->export_args['maps'] ) . ')';

            $results = $wpdb->get_results( "SELECT * FROM `$wpgmza_tblname_polylines` WHERE $where", ARRAY_A );

            return empty( $results ) ? array() : $results;

        }

        /**
         * Get rectangles data.
         *
         * @global wpdb   $wpdb                      WordPress database object.
         * @global string $wpgmza_tblname_rectangles Rectangles database table name.
         *
         * @return array Rectangles database rows as associative array.
         */
        private function get_rectangles_data() {

            if ( ! $this->export_args['rectangles'] ) {

                return array();

            }

            global $wpdb;
            global $wpgmza;
            global $wpgmza_tblname_rectangles;

            $where = empty( $this->export_args['maps'] ) ? '1=1' : '`map_id` IN (' . implode( ',', $this->export_args['maps'] ) . ')';

            $results = $wpdb->get_results( "SELECT `id`, `map_id`, `name`, {$wpgmza->spatialFunctionPrefix}X(`cornerA`) AS `cornerAX`, {$wpgmza->spatialFunctionPrefix}Y(`cornerA`) AS `cornerAY`, ST_X(`cornerB`) AS `cornerBX`, ST_Y(`cornerB`) AS `cornerBY`, `color`, `opacity` FROM `$wpgmza_tblname_rectangles` WHERE $where", ARRAY_A );

            return empty( $results ) ? array() : $results;

        }

        /**
         * Get datasets data.
         *
         * @global wpdb   $wpdb                    WordPress database object.
         * @global string $wpgmza_tblname_datasets Datasets database table name.
         *
         * @return array Datasets database rows as associative array.
         */
        private function get_datasets_data() {

            if ( ! $this->export_args['datasets'] ) {

                return array();

            }

            global $wpdb;
            global $wpgmza_tblname_datasets;

            $where = empty( $this->export_args['maps'] ) ? '1=1' : '`map_id` IN (' . implode( ',', $this->export_args['maps'] ) . ')';

            $results = $wpdb->get_results( "SELECT * FROM `$wpgmza_tblname_datasets` WHERE $where", ARRAY_A );

            return empty( $results ) ? array() : $results;

        }
    }