From: Mikael Nordfeldth Date: Fri, 4 Sep 2015 20:30:33 +0000 (+0200) Subject: Oops, forgot Notice_location PHP file X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7f30e614027fa27e83ecc32eb5443ee82d03f0f7;p=quix0rs-gnu-social.git Oops, forgot Notice_location PHP file --- diff --git a/classes/Notice_location.php b/classes/Notice_location.php new file mode 100644 index 0000000000..1574414e11 --- /dev/null +++ b/classes/Notice_location.php @@ -0,0 +1,75 @@ + array( + 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice that is the reply'), + 'lat' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude'), + 'lon' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'), + 'location_id' => array('type' => 'int', 'description' => 'location id if possible'), + 'location_ns' => array('type' => 'int', 'description' => 'namespace for location'), + 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'), + ), + 'primary key' => array('notice_id'), + 'foreign keys' => array( + 'notice_location_notice_id_fkey' => array('notice', array('notice_id' => 'id')), + ), + 'indexes' => array( + 'notice_location_location_id_idx' => array('location_id'), + ), + ); + } + + static function locFromStored(Notice $stored) + { + $loc = new Notice_location(); + $loc->notice_id = $stored->getID(); + if (!$loc->find(true)) { + throw new NoResultException($loc); + } + return $loc->asLocation(); + } + + static function fromLocation(Location $location) + { + $notloc = new Notice_location(); + $notloc->lat = $location->lat; + $notloc->lon = $location->lon; + $notloc->location_ns = $location->location_ns; + $notloc->location_id = $location->location_id; + return $notloc; + } + + public function asLocation() + { + $location = null; + + if (!empty($this->location_id) && !empty($this->location_ns)) { + $location = Location::fromId($this->location_id, $this->location_ns); + } + + if (is_null($location)) { // no ID, or Location::fromId() failed + $location = Location::fromLatLon($this->lat, $this->lon); + } + + if (is_null($location)) { + throw new ServerException('Location could not be looked up from existing data.'); + } + + return $location; + } +}