stefanbc
3/6/2014 - 10:58 AM

Import Ammap entries to WordPress posts

Import Ammap entries to WordPress posts

<?php
    $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
    require_once ($parse_uri[0] . 'wp-load.php');
    require_once($parse_uri[0] . '/wp-admin/includes/taxonomy.php');

    // Get coordinates for a specified address
	function getCoordinates($address){

		$address = str_replace("Str.", "Strada", $address);

		$address = str_replace("str.", "Strada", $address);

		$address = str_replace(",", "", $address);

	    $address = str_replace(" ", "+", $address); // replcae all the white space with "+" sign to match with google search pattern
	 
	    $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address";

	    // Initializing curl
        $ch = curl_init($url);
         
        // Configuring curl options
        $options = array(
        	CURLOPT_RETURNTRANSFER => true,
        	CURLOPT_HTTPHEADER => array('Content-type: application/json')
        );
         
        // Setting curl options
        curl_setopt_array($ch, $options);
         
        // Getting results
        $result = curl_exec($ch); // Getting jSON result string

        // Close request to clear up some resources
        curl_close($ch);
	 
	    $response = $result;

	    // return $response;
	 
	    $json = json_decode($response,TRUE); //generate array object from the response from the web
	 
	    return ($json['results'][0]['geometry']['location']['lat'].", ".$json['results'][0]['geometry']['location']['lng']);

	}

?>
<!doctype html>
<html>
	<head>
		<title>Importa Agentii</title>
		<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
		<style type="text/css">
			html,
			body {
				margin: 0;
				padding: 0;
				background-color: #2557A1;
				color: #fff;
				font-family: 'Open Sans', sans-serif;
			}
			#importForm {
				width: 450px;
				/*height: 50px;*/
				margin: 0 auto;
				margin-top: 100px;
				background-color: #fff;
				border: 1px solid #fff;
				color: #000;
				text-align: center;
				padding: 10px;
			}
			#importForm input {
				background-color: #fff;
				border: 1px solid #000;
				padding: 10px;
				font-size: 12px;
				text-transform: uppercase;
				color: #000;
				font-family: 'Open Sans', sans-serif;
			}
			section {
				width: 900px;
				margin: 50px auto;
				padding: 10px;
				border: 1px solid #fff;
			}
		</style>
	</head>
	<body>
		<form id="importForm" name="importForm" method="POST" action="" enctype="multipart/form-data">
			<input type="submit" value="Importa Agentii">
			<input type="checkbox" value="1" name="output" id="output">
			<label for="output">Seteaza descriere?</label>
			<input type="hidden" name="post_type_agentii" id="post_type_agentii" value="agentii" />
			<input type="checkbox" value="1" name="postAgentii" id="postAgentii">
			<label for="postAgentii">Post?</label>
            <input type="hidden" name="action_adaugaAgentii" value="post" />
            <?php wp_nonce_field('new-post'); ?>
		</form>
		<section>
			<?php
				if('POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action_adaugaAgentii'])) {
			  	
				  	$agentii = simplexml_load_file( dirname(__FILE__) . '/ammap_data_fr.xml');
				  	$output = '';
				  	$count = 1;

			    	foreach ($agentii->movies as $areas) {

			    		foreach ($areas as $area) {

			    			// Judet
			    			$zone = $area['title'];

			    			// Get initial description
			    			$descriptionWrapper = explode("<br><br>", $area->description);

							foreach ($descriptionWrapper as $key => $item) {

								if($key%2 == 0) {

									// City from description
				    				$city = ucwords(strtolower(strip_tags($item)));

								} else {

									// if ($count == 6) {

										// Address from item
						    			$addressExplode = explode("<br>", $item);	    			

										// Compose address from city and address
										$address =  $addressExplode[1] . ', ' . $city . ', Franta';

										$phoneCheck = trim(substr(strip_tags($addressExplode[2]), 0, 8));
										$mobileCheck = trim(substr(strip_tags($addressExplode[3]), 0, 6));
										$faxCheck = trim(substr(strip_tags($addressExplode[4]), 0, 4));
										$emailCheck = trim(substr(strip_tags($addressExplode[5]), 0, 7));
										$scheduleCheck = trim(substr(strip_tags($addressExplode[6]), 0, 8));

										if ($phoneCheck == 'Telefon')
											$phone = 'Telefon: ' . substr(strip_tags($addressExplode[2]), 8) . '<br>';
										
										if ($mobileCheck == 'Mobil')
											$mobile = 'Mobil: ' . substr(strip_tags($addressExplode[3]), 6) . '<br>';
										
										if ($faxCheck == 'Fax')
											$fax = 'Fax: ' . substr(strip_tags($addressExplode[4]), 4) . '<br>';
										
										if ($emailCheck == 'E-mail')
											$email = 'Email: ' . substr(strip_tags($addressExplode[5]), 7) . '<br>';

										if ($scheduleCheck == 'Program')
											$schedule = 'Program: ' . substr(strip_tags($addressExplode[6]), 8) . '<br>';

										if (isset($_POST['postAgentii'])) {
											// Get coordinates for composed address
											$crds = explode(',', getCoordinates($address));

											// Build the array for the ACF field 
											$location = array();
											$location['address'] = $address;
											$location['lat'] = $crds[0];
											$location['lng'] = $crds[1];
										}

										if (isset($_POST['output'])) {
											if (!isset($_POST['postAgentii'])) {
												$output .= 'Nr. ' . $count . '<br>';
												$output .= 'Judet: ' . $zone . '<br>';
											}			
											$output .= 'Adresa: ' . $address . '<br>';
											$output .= $phone;
											$output .= $mobile;
											$output .= $fax;
											$output .= $email;
											$output .= $schedule;
											// $output .= 'Coordonate: <b>' . $crds[0] .', ' . $crds[1] . '</b>'; 
											// $output .= "<hr>";
										} else {
											$output .= 'Output inexistent.';
										}

										if (isset($_POST['postAgentii'])) {
											// The post options
											$post_details = array(
												'post_title'  => $city,
												'post_content'  => $output,
												'post_status' => 'pending',
												'comment_status'=> 'closed'
											);

											// Insert the post
											$post_id = wp_insert_post($post_details);

											// Check if category exists and create it if not
											$term = term_exists($zone, 'category');

											if ($term == 0 || $term == null) {
												// Create Category
												$category_id = wp_create_category($zone, 197);
											} else {
												$category_id = $term;
											}

											// Set the category
											wp_set_post_terms($post_id, $category_id, 'category');

											// Update fields
											update_field('field_5310690e87994', $location, $post_id);
										}

									// }

									if (isset($_POST['postAgentii'])) {
										$output = '';
									}

									$count++;

								}
			    			}			    			
			    		}

			    		if (!isset($_POST['postAgentii'])) {
				    		echo $output;
				    	}

					}	
				}

				do_action('wp_insert_post', 'wp_insert_post');
			?>
		</section>
	</body>
</html>