]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Move location-argument-handling code into a single function
authorEvan Prodromou <evan@status.net>
Mon, 28 Dec 2009 23:13:15 +0000 (15:13 -0800)
committerEvan Prodromou <evan@status.net>
Mon, 28 Dec 2009 23:13:15 +0000 (15:13 -0800)
Moved the important parts of the location-argument-handling stuff
to a single function. Handles defaults and overrides correctly, and
easy to use. Changed Web and API channels to use it.

actions/apistatusesupdate.php
actions/newnotice.php
classes/Notice.php

index dabbea92f5246c747b913a16bd70a8c3308c7a9d..f594bbf393626ffa67cccc8d5806ea9629df864a 100644 (file)
@@ -203,12 +203,6 @@ class ApiStatusesUpdateAction extends ApiAuthAction
                 }
             }
 
-            $location = null;
-
-            if (!empty($this->lat) && !empty($this->lon)) {
-                $location = Location::fromLatLon($this->lat, $this->lon);
-            }
-
             $upload = null;
 
             try {
@@ -235,11 +229,15 @@ class ApiStatusesUpdateAction extends ApiAuthAction
 
             $options = array('reply_to' => $reply_to);
 
-            if (!empty($location)) {
-                $options['lat'] = $location->lat;
-                $options['lon'] = $location->lon;
-                $options['location_id'] = $location->location_id;
-                $options['location_ns'] = $location->location_ns;
+            if ($this->user->shareLocation()) {
+
+                $locOptions = Notice::locationOptions($this->lat,
+                                                      $this->lon,
+                                                      null,
+                                                      null,
+                                                      $this->user->getProfile());
+
+                $options = array_merge($options, $locOptions);
             }
 
             $this->notice =
index 8591522dc55e9c8f9476a42aab52a0cc3e672989..2d9f0ff797d973fc31e5970a28061859fc7054d7 100644 (file)
@@ -186,23 +186,13 @@ class NewnoticeAction extends Action
 
         if ($user->shareLocation()) {
 
-            $lat = $this->trimmed('lat');
-            $lon = $this->trimmed('lon');
-            $location_id = $this->trimmed('location_id');
-            $location_ns = $this->trimmed('location_ns');
-
-            if (!empty($lat) && !empty($lon) && empty($location_id)) {
-                $location = Location::fromLatLon($lat, $lon);
-                if (!empty($location)) {
-                    $location_id = $location->location_id;
-                    $location_ns = $location->location_ns;
-                }
-            }
+            $locOptions = Notice::locationOptions($this->trimmed('lat'),
+                                                  $this->trimmed('lon'),
+                                                  $this->trimmed('location_id'),
+                                                  $this->trimmed('location_ns'),
+                                                  $user->getProfile());
 
-            $options['lat'] = $lat;
-            $options['lon'] = $lon;
-            $options['location_id'] = $location_id;
-            $options['location_ns'] = $location_ns;
+            $options = array_merge($options, $locOptions);
         }
 
         $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options);
index 7651d8bd50e4bd4fbc80022ea170d90551388cb9..9f68c52553d0647ae382685f2ec0ba50c6924ba0 100644 (file)
@@ -289,21 +289,11 @@ class Notice extends Memcached_DataObject
         if (!empty($lat) && !empty($lon)) {
             $notice->lat = $lat;
             $notice->lon = $lon;
+        }
+
+        if (!empty($location_ns) && !empty($location_id)) {
             $notice->location_id = $location_id;
             $notice->location_ns = $location_ns;
-        } else if (!empty($location_ns) && !empty($location_id)) {
-            $location = Location::fromId($location_id, $location_ns);
-            if (!empty($location)) {
-                $notice->lat = $location->lat;
-                $notice->lon = $location->lon;
-                $notice->location_id = $location_id;
-                $notice->location_ns = $location_ns;
-            }
-        } else {
-            $notice->lat         = $profile->lat;
-            $notice->lon         = $profile->lon;
-            $notice->location_id = $profile->location_id;
-            $notice->location_ns = $profile->location_ns;
         }
 
         if (Event::handle('StartNoticeSave', array(&$notice))) {
@@ -1429,4 +1419,47 @@ class Notice extends Memcached_DataObject
 
         return $ids;
     }
+
+    function locationOptions($lat, $lon, $location_id, $location_ns, $profile = null)
+    {
+        $options = array();
+
+        if (!empty($location_id) && !empty($location_ns)) {
+
+            $options['location_id'] = $location_id;
+            $options['location_ns'] = $location_ns;
+
+            $location = Location::fromId($location_id, $location_ns);
+
+            if (!empty($location)) {
+                $options['lat'] = $location->lat;
+                $options['lon'] = $location->lon;
+            }
+
+        } else if (!empty($lat) && !empty($lon)) {
+
+            $options['lat'] = $lat;
+            $options['lon'] = $lon;
+
+            $location = Location::fromLatLon($lat, $lon);
+
+            if (!empty($location)) {
+                $options['location_id'] = $location->location_id;
+                $options['location_ns'] = $location->location_ns;
+            }
+        } else if (!empty($profile)) {
+
+            if (isset($profile->lat) && isset($profile->lon)) {
+                $options['lat'] = $profile->lat;
+                $options['lon'] = $profile->lon;
+            }
+
+            if (isset($profile->location_id) && isset($profile->location_ns)) {
+                $options['location_id'] = $profile->location_id;
+                $options['location_ns'] = $profile->location_ns;
+            }
+        }
+
+        return $options;
+    }
 }