Adding lat lon from address
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 3 May 2016 17:27:42 +0000 (13:27 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 3 May 2016 17:27:42 +0000 (13:27 -0400)
Use curl to get geolocation data for the event

models/front/events/frontAdd.php
views/front/events/frontAdd.html

index 3ba7b57..a21cc51 100644 (file)
@@ -9,7 +9,7 @@
 require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH . '/data/dataEvents.php';
 require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH . '/data/dataCategories.php';
 require_once GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/models/front/events/baseAction.php';
-require_once(GLM_MEMBERS_PLUGIN_PATH.'/models/admin/ajax/imageUpload.php');
+require_once GLM_MEMBERS_PLUGIN_PATH.'/models/admin/ajax/imageUpload.php';
 require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH.'/data/dataRecurrences.php';
 
 /**
@@ -20,12 +20,12 @@ require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH.'/data/dataRecurrences.php';
  * @version   0.0.1
  * @copyright Copyright (c) 2010 All rights reserved.
  * @author    Steve Sutton <steve@gaslightmedia.com>
- * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
+ * @license   PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
  */
 class GLmMembersFront_events_frontAdd extends GlmDataEvents
 {
-    
-     /* getCityId
+
+    /* getCityId
      *
      * Given a city name find and return the id of the city.
      * If a city cannot be found then create city with that name.
@@ -54,6 +54,30 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
             return $cityId['id'];
         }
     }
+
+    public function getGeoLocation($address)
+    {
+        foreach ($address as $key => &$part) {
+            $part = urlencode($part);
+        }
+        $addressString = implode( ',', $address);
+        $url = "http://maps.googleapis.com/maps/api/geocode/json?address={$addressString}";
+        $ch = curl_init($url);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        $return = json_decode(curl_exec($ch));
+        curl_close($ch);
+        if (isset($return->results)) {
+            $location = $return->results[0]->geometry->location;
+
+            return array(
+                $location->lat,
+                $location->lng
+            );;
+        } else {
+            return false;
+        }
+    }
+
     /**
      * modelAction
      *
@@ -74,53 +98,53 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
 
         // Load recurrences class
         $Recurrences = new GlmDataEventsRecurrences($this->wpdb, $this->config);
-        
+
         //Load Events class
         $events = new GlmDataEvents($this->wpdb, $this->config );
         $newEvent = $events->newEntry();
-        
+
         // if form is submitted
          if($_REQUEST['newEvent'] == "Add Event"){
-             
+
              // parse dates and times
              date_default_timezone_set('America/New_York');
              $date = date('y/m/d H:i:s a', time());
              $starting = trim(filter_var($_REQUEST['starting'], FILTER_SANITIZE_STRING));
              $ending = trim(filter_var($_REQUEST['ending'], FILTER_SANITIZE_STRING));
-             
-             
-             
+
+
+
              // if there are no times selected, default the start time to midnight or get the starting / ending times
              if(empty($_REQUEST['shour']) && strlen($_REQUEST['smin']) === 0){
                  $smilitaryTime = "00:00:00";
              } else {
-                         
+
                  $startTime = trim(filter_var($_REQUEST['shour'],FILTER_SANITIZE_STRING)) . ':' . trim(filter_var($_REQUEST['smin'],FILTER_SANITIZE_STRING)) . " " . trim(filter_var($_REQUEST['sampm'],FILTER_SANITIZE_STRING));
 
                  $smilitaryTime = date("H:i:s", strtotime($startTime));
              }
-             
+
              // Get the ending time
              if(empty($_REQUEST['ehour']) && strlen($_REQUEST['emin']) === 0){
                  $startTimeOnly = 1;
                  $emilitaryTime = $smilitaryTime;
              } else {
-            
+
                  $endTime = trim(filter_var($_REQUEST['ehour'],FILTER_SANITIZE_STRING)) . ':' . trim(filter_var($_REQUEST['emin'],FILTER_SANITIZE_STRING)) . " " . trim(filter_var($_REQUEST['eampm'],FILTER_SANITIZE_STRING));
                  $emilitaryTime = date("H:i:s", strtotime($endTime));
              }
-             
+
              if(empty($_REQUEST['shour'])  && empty($_REQUEST['ehour'])){
                  $allDay = true;
                  $startTimeOnly = 0;
              }
-                         
+
              $starting_date = strtotime($starting . " " . $smilitaryTime);
              $mysqlStart = date("Y-m-d H:i:s",$starting_date);
-             
+
              $ending_date = strtotime($ending . " " . $emilitaryTime);
              $mysqlEnd = date("Y-m-d H:i:s",$ending_date);
-        
+
              // events table data
              $name = trim(filter_var($_REQUEST['event_name'],FILTER_SANITIZE_STRING));
              $intro = trim(filter_var($_REQUEST['intro'],FILTER_SANITIZE_STRING));
@@ -133,7 +157,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
              $adminEmail = trim(filter_var($_REQUEST['admin_email'],FILTER_SANITIZE_STRING));
              $facebook = trim(filter_var($_REQUEST['facebook'],FILTER_SANITIZE_STRING));
              $twitter = trim(filter_var($_REQUEST['twitter'],FILTER_SANITIZE_STRING));
-             
+
              $contactFirst = trim(filter_var($_REQUEST['contact_fname'],FILTER_SANITIZE_STRING));
              $contactLast = trim(filter_var($_REQUEST['contact_lname'],FILTER_SANITIZE_STRING));
              $place = trim(filter_var($_REQUEST['place'],FILTER_SANITIZE_STRING));
@@ -145,6 +169,13 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
              $contactPhone = trim(filter_var($_REQUEST['contact_phone'],FILTER_SANITIZE_STRING));
              $fileDescr = trim(filter_var($_REQUEST['fileDescription'],FILTER_SANITIZE_STRING));
 
+             // get lat lon
+             $latLon = $this->getGeoLocation( array($address, $city, $state) );
+             if ($latLon) {
+                $lat = $latLon[0];
+                $lon = $latLon[1];
+             } 
+
              $weekOfMonth = $_REQUEST['weekom'];
              if(strlen($weekOfMonth) !== 0){
                  $weekOfMonth = pow(2, $weekOfMonth);
@@ -158,7 +189,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
              } else {
                  $byDayOfMonth = false;
              }
-             
+
              if(empty($_REQUEST['free'])){
                  $free = false;
              } else {
@@ -174,7 +205,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
              } else {
                  $allDay = $_REQUEST['allday'];
              }
-             
+
              //get days of the week values for recurrences
              foreach($_REQUEST['daysow'] as $check){
                  $daysOfWeek[] = $check;
@@ -189,7 +220,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
                      $days += $value;
                  }
              }
-             
+
              $city =  $this->getCityId($city);
              // image upload settings
              $image = $_FILES['image_new'];
@@ -202,7 +233,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
             $imageExt  = array_pop($temp);
             $imageExt = strtolower($imageExt);
             $tmpName = implode('.', $temp);
-            
+
              $allowed = array('jpeg', 'png', 'jpg', 'bmp', 'gif','svg');
              if(in_array($imageExt, $allowed)){
                  if($imageError === 0){
@@ -211,7 +242,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
                          $imgUrl = GLM_MEMBERS_PLUGIN_MEDIA_PATH . "/images/small/" . $imageName;
                          if(!file_exists($imgUrl)){
                             if(move_uploaded_file($imageTmp, $imgUrl)){
-                                
+
                             }
                         } else {
                              echo "Image Already Exists";
@@ -225,7 +256,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
                 $fileTmp = $file['tmp_name'];
                 $fileSize = $file['size'];
                 $fileError = $file['error'];
-                 
+
                 $temp = explode('.', $fileName);
                 $fileExt  = array_pop($temp);
                 $fileExt = strtolower($fileExt);
@@ -238,7 +269,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
                          $fileUrl = GLM_MEMBERS_PLUGIN_MEDIA_PATH . "/files/" .$fileName;
                          if(!file_exists($fileUrl)){
                             if(move_uploaded_file($fileTmp, $fileUrl)){
-                                
+
                             }
                         } else {
                              echo "File Already Exists";
@@ -248,53 +279,54 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
              }
           // mySql transactions
              $eventData = array(
-                            'status'            => '20',
-                            'created'           => $date,
-                            'updated'           => $date,
-                            'name'              => $name,
-                            'intro'             => $intro,
-                            'descr'             => $descr,
-                            'image'             => $imageName,
-                            'file1'             => $fileName,
-                            'file1_descr'       => $fileDescr,
-                            'cost'              => $cost,
-                            'free'              => $free,
-                            'url'               => $url,
-                            'contact_phone'     => $contactPhone,
-                            'admin_name'        => $admin_name,
-                            'admin_phone'       => $adminPhone,
-                            'admin_email'       => $adminEmail,
-                            'contact_name'      => $contactFirst . " " . $contactLast,
-                            'contact_email'     => $contactEmail,
-                            'admin_org'         => $admin_org
-                        );
+                'status'            => '20',
+                'created'           => $date,
+                'updated'           => $date,
+                'name'              => $name,
+                'intro'             => $intro,
+                'descr'             => $descr,
+                'image'             => $imageName,
+                'file1'             => $fileName,
+                'file1_descr'       => $fileDescr,
+                'cost'              => $cost,
+                'free'              => $free,
+                'url'               => $url,
+                'contact_phone'     => $contactPhone,
+                'admin_name'        => $admin_name,
+                'admin_phone'       => $adminPhone,
+                'admin_email'       => $adminEmail,
+                'contact_name'      => $contactFirst . " " . $contactLast,
+                'contact_email'     => $contactEmail,
+                'admin_org'         => $admin_org
+            );
+            $eventDataFormat = array(
+                 '%d',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s'
+            );
             $this->wpdb->insert(
-                        GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'events',
-                        $eventData,
-                        array(
-                             '%d',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s'
-                        )
-                    );   
+                GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'events',
+                $eventData,
+                $eventDataFormat
+            );   
             $eventID = $this->wpdb->insert_id;
-            
+
             $locationData = array(
                             'event'         => $eventID,
                             'name'          => $place,
@@ -306,24 +338,31 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
                             'contact_fname' => $contactFirst,
                             'contact_lname' => $contactLast,
                             'email'         => $contactEmail
-                            
+
             );
-             $this->wpdb->insert(
-                        GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'locations',
-                        $locationData,
-                        array(
-                             '%d',
-                             '%s',
-                             '%s',
-                             '%d',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s',
-                             '%s'
-                        )
-                    );
+            $locationDataFormat = array(
+                 '%d',
+                 '%s',
+                 '%s',
+                 '%d',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s',
+                 '%s'
+            );
+            if ($lat && $lon) {
+                $locationData['lat'] = $lat;
+                $locationData['lon'] = $lon;
+                $locationDataFormat[] = '%s';
+                $locationDataFormat[] = '%s';
+            }
+                $this->wpdb->insert(
+                    GLM_MEMBERS_EVENTS_PLUGIN_DB_PREFIX . 'locations',
+                    $locationData,
+                    $locationDataFormat 
+                );
              $ending = date("Y-m-d", strtotime($ending));
              $starting = date("Y-m-d", strtotime($starting));
              $recurData =array(
@@ -367,7 +406,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
               $recurID = $this->wpdb->insert_id;
               $Recurrences->optionIncludeSelectListData = false;
               $times = $Recurrences->createRecurrenceTimesEntries($recurID, true, true);
-              
+
               // event category data
               $categoryData = array(
                     'event'      => $eventID,
@@ -381,8 +420,8 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
                              '%d'
                         )
                     );
-              
-              
+
+
             // Send confirmation email
 //            $to      = 'anthony@localhost';
 //            $subject = 'Event Form Submission';
@@ -395,7 +434,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
 //            mail($to, $subject, $message, $headers);
               $view = 'confirmation';
          }
-        
+
 //        echo '<pre>', print_r($cats), '</pre>';
         // Compile template data
         $templateData = array(
index fe266d3..24f746d 100644 (file)
@@ -32,7 +32,7 @@
         <tr class='timeRow'>
             <th> Start Time </th>
         <td>
-               <select class="timeDropDown glm-required" name="shour">
+        <select class="timeDropDown glm-required" name="shour">
                 <option value="">Hour</option>
                 <option value="1">01</option>
                 <option value="2">02</option>
                 <option value="AM">AM</option>
                 <option value="PM">PM</option>
         </select>
-       </td>
+    </td>
         </tr>
           <tr class="timeRow">
             <th> End Time </th>
         <td>
-               <select class="timeDropDown" name="ehour">
+        <select class="timeDropDown" name="ehour">
                 <option value="">Hour</option>
                 <option value="1">01</option>
                 <option value="2">02</option>
@@ -87,7 +87,7 @@
                 <option value="AM">AM</option>
                 <option value="PM">PM</option>
         </select>
-       </td>
+    </td>
         </tr>
         <tr>
             <th>Repeating Event</th>
         </tr>
         <tr>
             <td>
-               <label style="color: red;">Place</label>
+        <label style="color: red;">Place</label>
+        <input type="hidden" id="glm-add-event-lat" name="lat">
+        <input type="hidden" id="glm-add-event-lon" name="lon">
             </td>
             <td>
-               <input class="glm-required" name="place" maxlength="100" type="text" required>
+        <input class="glm-required" name="place" maxlength="100" type="text" required>
             </td>
         </tr>
         <tr>
             <td>
-               <label >Address</label>
+        <label >Address</label>
             </td>
             <td>
-               <input name="address" maxlength="100" type="text">
+        <input name="address" maxlength="100" type="text">
             </td>
         </tr>
         <tr>
             <td>
-               <label style="color: red;">City</label>
+        <label style="color: red;">City</label>
             </td>
             <td>
-               <input name="city" maxlength="100" type="text" class="glm-required" required>
+        <input name="city" maxlength="100" type="text" class="glm-required" required>
             </td>
         </tr>
         <tr>
                 Minnesota
             </option>
             <option value="MS">
-                Mississppi
+                Mississippi
             </option>
             <option value="MO">
                 Missouri
 </tr>
         <tr>
             <td>
-               <label >ZIP</label>
+        <label >ZIP</label>
             </td>
             <td>
-               <input name="zip" maxlength="100" type="text">
+        <input name="zip" maxlength="100" type="text">
             </td>
         </tr>
         <tr>
             <td>
-               <label>Event Contact Person<br>(published on Web site)</label>
+        <label>Event Contact Person<br>(published on Web site)</label>
             </td>
             <td class="contactName">
-               <input id="contactFirstName" name="contact_fname" maxlength="100" type="text" placeholder="First Name">
+        <input id="contactFirstName" name="contact_fname" maxlength="100" type="text" placeholder="First Name">
       
-               <input id="contactLastName" name="contact_lname" maxlength="100" type="text" placeholder="Last Name">
+        <input id="contactLastName" name="contact_lname" maxlength="100" type="text" placeholder="Last Name">
             </td>
         </tr>
         <tr>
             <td>
-               <label>Contact Email<br>(published on Web site)</label>
+        <label>Contact Email<br>(published on Web site)</label>
             </td>
             <td>
-               <input name="contact_email" maxlength="100" type="text">
+        <input name="contact_email" maxlength="100" type="text">
             </td>
         </tr>
         <tr>
             <td>
-               <label style="color: red;">Contact Phone<br>(published on Web site)</label>
+        <label style="color: red;">Contact Phone<br>(published on Web site)</label>
             </td>
             <td>
-               <input name="contact_phone" maxlength="100" type="text" class="glm-required" required>
+        <input name="contact_phone" maxlength="100" type="text" class="glm-required" required>
             </td>
         </tr>
         <tr>
             <td>
-               <label style="color: red;">Organization Contact Name Submitting Event</label>
+        <label style="color: red;">Organization Contact Name Submitting Event</label>
             </td>
             <td>
-               <input name="admin_name" maxlength="100" type="text" class="glm-required">
+        <input name="admin_name" maxlength="100" type="text" class="glm-required">
             </td>
         </tr>
         <tr>
             <td>
-               <label style="color: red;"> Name of Organization </label>
+        <label style="color: red;"> Name of Organization </label>
             </td>
             <td>
-               <input name="admin_org" maxlength="100" type="text" class="glm-required">
+        <input name="admin_org" maxlength="100" type="text" class="glm-required">
             </td>
         </tr>
         <tr>
                 <label style="color: red;">Organization Phone</label>
             </td>
             <td>
-               <input name="admin_phone" maxlength="100" type="text" class="glm-required" required>
+        <input name="admin_phone" maxlength="100" type="text" class="glm-required" required>
             </td>
         </tr>
         <tr>
             <td>
 
-               <label style="color: red;">Organization Email Address</label>
+        <label style="color: red;">Organization Email Address</label>
             </td>
             <td>
-               <input name="admin_email" maxlength="100" type="text" class="glm-required" required>
+        <input name="admin_email" maxlength="100" type="text" class="glm-required" required>
             </td>
         </tr>
         <tr>
             <td>
-               <label>Event File</label>
+        <label>Event File</label>
             </td>
             <td>
-               <input name="file_rmv" type="file">
+        <input name="file_rmv" type="file">
             </td>
         </tr>
         <tr>
             <td>
-               <label>File Description</label>
+        <label>File Description</label>
             </td>
             <td>
-               <input name="fileDescription" maxlength="100" type="text">
+        <input name="fileDescription" maxlength="100" type="text">
             </td>
         </tr>