Esempio n. 1
0
 /**
  * do the plugin action
  *
  */
 function process(&$data, &$listModel)
 {
     $params =& $this->getParams();
     // grab the table model and find table name and PK
     $table =& $listModel->getTable();
     $table_name = $table->db_table_name;
     $primary_key = $table->db_primary_key;
     $primary_key_element = FabrikString::shortColName($table->db_primary_key);
     // for now, we have to read the table ourselves.  We can't rely on the $data passed to us
     // because it can be arbitrarily filtered according to who happened to hit the page when cron
     // needed to run.
     $mydata = array();
     $db = FabrikWorker::getDbo();
     $db->setQuery("SELECT * FROM {$table_name}");
     $mydata[0] = $db->loadObjectList();
     // grab all the params, like GMaps key, field names to use, etc
     $geocode_gmap_key = $params->get('geocode_gmap_key');
     $geocode_is_empty = $params->get('geocode_is_empty');
     $geocode_zoom_level = $params->get('geocode_zoom_level', '4');
     $geocode_map_element_long = $params->get('geocode_map_element');
     $geocode_map_element = FabrikString::shortColName($geocode_map_element_long);
     $geocode_addr1_element_long = $params->get('geocode_addr1_element');
     $geocode_addr1_element = $geocode_addr1_element_long ? FabrikString::shortColName($geocode_addr1_element_long) : '';
     $geocode_addr2_element_long = $params->get('geocode_addr2_element');
     $geocode_addr2_element = $geocode_addr2_element_long ? FabrikString::shortColName($geocode_addr2_element_long) : '';
     $geocode_city_element_long = $params->get('geocode_city_element');
     $geocode_city_element = $geocode_city_element_long ? FabrikString::shortColName($geocode_city_element_long) : '';
     $geocode_state_element_long = $params->get('geocode_state_element');
     $geocode_state_element = $geocode_state_element_long ? FabrikString::shortColName($geocode_state_element_long) : '';
     $geocode_zip_element_long = $params->get('geocode_zip_element');
     $geocode_zip_element = $geocode_zip_element_long ? FabrikString::shortColName($geocode_zip_element_long) : '';
     $geocode_country_element_long = $params->get('geocode_country_userid_element');
     $geocode_country_element = $geocode_country_element_long ? FabrikString::shortColName($geocode_country_element_long) : '';
     // sanity check, make sure required elements have been specified
     if (empty($geocode_gmap_key)) {
         JError::raiseNotice(500, 'No google maps key specified');
         return;
     }
     $gmap = new GMaps($geocode_gmap_key);
     // run through our table data
     $total_encoded = 0;
     foreach ($mydata as $gkey => $group) {
         if (is_array($group)) {
             foreach ($group as $rkey => $row) {
                 // see if the map element is considered empty
                 if (empty($row->{$geocode_map_element}) || $row->{$geocode_map_element} == $geocode_is_empty) {
                     // it's empty, so lets try and geocode.
                     // first, construct the address
                     // we'll build an array of address components, which we'll explode into a string later
                     $a_full_addr = array();
                     // for each address component element, see if one is specific in the params,
                     // if so, see if it has a value in this row
                     // if so, add it to the address array.
                     if ($geocode_addr1_element) {
                         if ($row->{$geocode_addr1_element}) {
                             $a_full_addr[] = $row->{$geocode_addr1_element};
                         }
                     }
                     if ($geocode_addr2_element) {
                         if ($row->{$geocode_addr2_element}) {
                             $a_full_addr[] = $row->{$geocode_addr2_element};
                         }
                     }
                     if ($geocode_city_element) {
                         if ($row->{$geocode_city_element}) {
                             $a_full_addr[] = $row->{$geocode_city_element};
                         }
                     }
                     if ($geocode_state_element) {
                         if ($row->{$geocode_state_element}) {
                             $a_full_addr[] = $row->{$geocode_state_element};
                         }
                     }
                     if ($geocode_zip_element) {
                         if ($row->{$geocode_zip_element}) {
                             $a_full_addr[] = $row->{$geocode_zip_element};
                         }
                     }
                     if ($geocode_country_element) {
                         if ($row->{$geocode_country_element}) {
                             $a_full_addr[] = $row->{$geocode_zip_element};
                         }
                     }
                     // now explode the address into a string
                     $full_addr = implode(',', $a_full_addr);
                     // Did we actually get an address?
                     if (!empty($full_addr)) {
                         // OK!  Lets try and geocode it ...
                         if ($gmap->getInfoLocation($full_addr)) {
                             echo 'found ';
                             $lat = $gmap->getLatitude();
                             $long = $gmap->getLongitude();
                             if (!empty($lat) && !empty($long)) {
                                 $map_value = "({$lat},{$long}):{$geocode_zoom_level}";
                                 $db->setQuery("\r\n\t\t\t\t\t\t\t\t\t\tUPDATE {$table_name}\r\n\t\t\t\t\t\t\t\t\t\tSET {$geocode_map_element} = '{$map_value}'\r\n\t\t\t\t\t\t\t\t\t\tWHERE {$primary_key} = '{$row->{$primary_key_element}}'\r\n\t\t\t\t\t\t\t\t\t");
                                 $db->query();
                                 $total_encoded++;
                             }
                         }
                     }
                 }
             }
         }
     }
     return $total_encoded;
 }
Esempio n. 2
0
<?php

require_once 'GMaps.php';
// Your Google key
$google_key = '';
if (!empty($_POST)) {
    $search = strip_tags($_POST['search']);
}
echo '<form method="post" action="example.php"><input type="text" name="search">';
echo '<input type="submit" value="Get geographic data!"></form>';
if (!empty($search)) {
    // Get the Google Maps Object
    $GMap = new GMaps($google_key);
    if ($GMap->getInfoLocation($search)) {
        echo 'Address: ' . $GMap->getAddress() . '<br>';
        echo 'Country name: ' . $GMap->getCountryName() . '<br>';
        echo 'Country name code: ' . $GMap->getCountryNameCode() . '<br>';
        echo 'Administrative area name: ' . $GMap->getAdministrativeAreaName() . '<br>';
        echo 'Postal code: ' . $GMap->getPostalCode() . '<br>';
        echo 'Latitude: ' . $GMap->getLatitude() . '<br>';
        echo 'Longitude: ' . $GMap->getLongitude() . '<br>';
    } else {
        echo "The response of Google Maps is empty";
    }
}
Esempio n. 3
0
 /**
  * do the plugin action
  *
  */
 function process(&$data, &$tableModel)
 {
     $params =& $this->getParams();
     // grab the table model and find table name and PK
     $table =& $tableModel->getTable();
     $table_name = $table->db_table_name;
     $primary_key = $tableModel->_shortKey();
     $primary_key_element = str_replace("`", "", $primary_key);
     // for now, we have to read the table ourselves.  We can't rely on the $data passed to us
     // because it can be arbitrarily filtered according to who happened to hit the page when cron
     // needed to run.
     // FIXME - this won't work if any of the required elements are in joined data,
     // or the table is not on the default site connection!
     // So instead of doing this, we need to add an option to the main cron run code, which can
     // turn off pre-filters.
     $mydata = array();
     $db = $tableModel->getDb();
     $db->setQuery("SELECT * FROM {$table_name}");
     $mydata[0] = $db->loadObjectList();
     if (empty($mydata[0])) {
         $this->log[] = "No records found in table: {$table_name}\n";
         return;
     }
     // grab all the params, like GMaps key, field names to use, etc
     $geocode_gmap_key = $params->get('geocode_gmap_key');
     $geocode_is_empty = $params->get('geocode_is_empty');
     $geocode_zoom_level = $params->get('geocode_zoom_level', '4');
     $geocode_map_element_long = $params->get('geocode_map_element');
     $default = $params->get('geocode_default');
     $geocode_map_element = FabrikString::shortColName($geocode_map_element_long);
     $geocode_addr1_element_long = $params->get('geocode_addr1_element');
     $geocode_addr1_element = $geocode_addr1_element_long ? FabrikString::shortColName($geocode_addr1_element_long) : '';
     $geocode_addr2_element_long = $params->get('geocode_addr2_element');
     $geocode_addr2_element = $geocode_addr2_element_long ? FabrikString::shortColName($geocode_addr2_element_long) : '';
     $geocode_city_element_long = $params->get('geocode_city_element');
     $geocode_city_element = $geocode_city_element_long ? FabrikString::shortColName($geocode_city_element_long) : '';
     $geocode_state_element_long = $params->get('geocode_state_element');
     $geocode_state_element = $geocode_state_element_long ? FabrikString::shortColName($geocode_state_element_long) : '';
     $geocode_zip_element_long = $params->get('geocode_zip_element');
     $geocode_zip_element = $geocode_zip_element_long ? FabrikString::shortColName($geocode_zip_element_long) : '';
     $geocode_country_element_long = $params->get('geocode_country_userid_element');
     $geocode_country_element = $geocode_country_element_long ? FabrikString::shortColName($geocode_country_element_long) : '';
     $verbose = (int) $params->get('geocode_verbose_log', 0);
     $app =& JFactory::getApplication();
     // sanity check, make sure required elements have been specified
     if (empty($geocode_gmap_key)) {
         $msg = "no Google Maps API key given!";
         if ($app->isAdmin()) {
             $app->enqueueMessage($msg);
         }
         $this->log[] = $msg . "\n";
         return;
     }
     $gmap = new GMaps($geocode_gmap_key);
     $delay = (int) $params->get('geo_code_delay', 0);
     $max = (int) $params->get('geo_code_batch', 0);
     $counter = 0;
     // run through our table data
     $total_encoded = 0;
     $total_unfound = 0;
     $total_emtypy = 0;
     $total_not_empty = 0;
     $gkeys = array_keys($mydata);
     foreach ($gkeys as $x) {
         $group = $mydata[$x];
         if (is_array($group)) {
             $rkeys = array_keys($group);
             foreach ($rkeys as $y) {
                 if ($max != 0 && $counter > $max) {
                     break 2;
                 }
                 $row = $group[$y];
                 $map_value = "({$default}):{$geocode_zoom_level}";
                 // see if the map element is considered empty
                 if (empty($row->{$geocode_map_element}) || $row->{$geocode_map_element} == $geocode_is_empty) {
                     // it's empty, so lets try and geocode.
                     // first, construct the address
                     // we'll build an array of address components, which we'll explode into a string later
                     $a_full_addr = array();
                     // for each address component element, see if one is specific in the params,
                     // if so, see if it has a value in this row
                     // if so, add it to the address array.
                     if ($geocode_addr1_element) {
                         if ($row->{$geocode_addr1_element}) {
                             $a_full_addr[] = $row->{$geocode_addr1_element};
                         }
                     }
                     if ($geocode_addr2_element) {
                         if ($row->{$geocode_addr2_element}) {
                             $a_full_addr[] = $row->{$geocode_addr2_element};
                         }
                     }
                     if ($geocode_city_element) {
                         if ($row->{$geocode_city_element}) {
                             $a_full_addr[] = $row->{$geocode_city_element};
                         }
                     }
                     if ($geocode_state_element) {
                         if ($row->{$geocode_state_element}) {
                             $a_full_addr[] = $row->{$geocode_state_element};
                         }
                     }
                     if ($geocode_zip_element) {
                         if ($row->{$geocode_zip_element}) {
                             $a_full_addr[] = $row->{$geocode_zip_element};
                         }
                     }
                     if ($geocode_country_element) {
                         if ($row->{$geocode_country_element}) {
                             $a_full_addr[] = $row->{$geocode_zip_element};
                         }
                     }
                     // now explode the address into a string
                     $full_addr = implode(',', $a_full_addr);
                     // Did we actually get an address?
                     if (!empty($full_addr)) {
                         // OK!  Lets try and geocode it ...
                         if ($gmap->getInfoLocation($full_addr)) {
                             $lat = $gmap->getLatitude();
                             $long = $gmap->getLongitude();
                             if (!empty($lat) && !empty($long)) {
                                 $map_value = "({$lat},{$long}):{$geocode_zoom_level}";
                                 $total_encoded++;
                             } else {
                                 $total_unfound++;
                                 $this->log[] = "record unfound: " . $row->{$primary_key_element} . "\n";
                                 continue;
                             }
                         } else {
                             $total_unfound++;
                             $this->log[] = "record unfound: " . $row->{$primary_key_element} . "\n";
                             continue;
                         }
                         if ($gmap->errcode == '620') {
                             //sent too fast
                             $delay += 100000;
                             $this->log[] = "Google API error 620, increasing delay\n";
                         }
                         if (!empty($gmap->err)) {
                             $this->log[] = "Google API error: " . $gmap->err . "\n";
                             continue;
                         }
                     } else {
                         $total_emtypy++;
                         $this->log[] = "No address data for row: " . $row->{$primary_key_element} . "\n";
                         continue;
                     }
                     $db->setQuery("\n\t\t\t\t\t\t\t\t\t\tUPDATE {$table_name}\n\t\t\t\t\t\t\t\t\t\tSET {$geocode_map_element} = " . $db->Quote($map_value) . "\n\t\t\t\t\t\t\t\t\t\tWHERE {$primary_key} = " . $db->Quote($row->{$primary_key_element}));
                     $db->query();
                     if ($verbose) {
                         $this->log[] = "Updated row {$row->{$primary_key_element}}: {$map_value}\n";
                     }
                 } else {
                     $total_not_empty++;
                     if ($verbose) {
                         $this->log[] = "Map element not considered empty for row {$row->{$primary_key_element}}: {$row->{$geocode_map_element}}\n";
                     }
                 }
                 //
                 $counter++;
                 usleep($delay);
             }
         }
     }
     $this->log[] = $msg = "encoded: {$total_encoded}; unfound: {$total_unfound}; empty addresses: {$total_emtypy}; not empty map element: {$total_not_empty}";
     if ($app->isAdmin()) {
         $app->enqueueMessage($msg);
     }
     return $total_encoded + $total_unfound + $total_emtypy;
 }