<?php

// Load WP functions

require( '../wp-load.php' );


// Set default timezone

date_default_timezone_set('GMT');


// Open CSV file

$file_data = fopen("add-location.csv", "r");


// Initiate counter so we can skip the header row

$i = 0;


// While not the end of file, loop trough CSV line by line 

while (!feof($file_data) ) {


// Start counter

$i++;


// Grab 1 CSV row at a time and put into array

$file_data_row = fgetcsv($file_data, 1024);


//Create variables using $file_data_row array

$someVariable = $file_data_row[0];



// Join some variables together for later use 

$post_title = $OutletName . ", " . $OutletShortAddress;

$post_name = str_replace(" ", "-", strtolower($OutletName . "-" . $OutletShortAddress));

$latlng = $lat . "," . $lng;


// echo out variable for dubug

echo $someVariable . ",";



// Check to not insert the header row using counter 

if ($i > 1 && !empty($OutletName)) {


// Create post object

$pub_row = array(

'post_title' => $post_title,

'post_content' => '',

'post_status' => 'publish',

'post_author' => 1,

'post_category' => array(11),

'comment_status' => 'closed',

'ping_status' => 'closed',

'post_name' => $post_name,

'post_parent' => 0,

'post_type' => 'post',

);


// Insert the post into the database. On success, post_id is returned into $post_id

$post_id = wp_insert_post($pub_row);

echo '<p>' . $post_id . '</p>';


// Create new map array

$map_array = array(

'id' => '',

'active' => '1',

'map_length' => '370',

'map_height' => '300',

'map_zoom' => '14',

'center_lat' => $lat,

'center_long' => $lng,

'map_type' => 'normal',

'alignment' => 'none',

'map_address' => $latlng,

'map_controls' => 'none',

'post_id' => $post_id, // Use returned $post_id

);


// Insert map array into table

$wpdb->insert( $wpdb->prefix . 'wpgmappity_maps', $map_array );


//Grab last map id to use in marker array

$last_map_id = $wpdb->get_row( $wpdb->prepare('SELECT max(id) as map_id FROM prefix_wpgmappity_maps'));


//Create maker array of newly created map

$marker_array = array(

'id' => '',

'map_id' => $last_map_id->map_id,

'active' => '1',

'marker_lat' => $lat,

'marker_long' => $lng,

);


//Insert marker array into table

$wpdb->insert( $wpdb->prefix . 'wpgmappity_markers', $marker_array);


//Update Pub post_meta checking variable are not empty

if (!empty($someVariable)) add_post_meta( $post_id, 'someVariableName', $someVariable, true );




}

}

// Close CSV file

fclose($file_data);

?>