Notice_location.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Table Definition for notice_location
  18. */
  19. defined('GNUSOCIAL') || die();
  20. class Notice_location extends Managed_DataObject
  21. {
  22. public $__table = 'notice_location'; // table name
  23. public $notice_id; // int(4) primary_key not_null
  24. public $lat; // decimal(10,7)
  25. public $lon; // decimal(10,7)
  26. public $location_id; // int(4)
  27. public $location_ns; // int(4)
  28. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  29. public static function schemaDef()
  30. {
  31. return array(
  32. 'fields' => array(
  33. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice that is the reply'),
  34. 'lat' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude'),
  35. 'lon' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'),
  36. 'location_id' => array('type' => 'int', 'description' => 'location id if possible'),
  37. 'location_ns' => array('type' => 'int', 'description' => 'namespace for location'),
  38. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  39. ),
  40. 'primary key' => array('notice_id'),
  41. 'foreign keys' => array(
  42. 'notice_location_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  43. ),
  44. 'indexes' => array(
  45. 'notice_location_location_id_idx' => array('location_id'),
  46. ),
  47. );
  48. }
  49. public static function locFromStored(Notice $stored)
  50. {
  51. $loc = new Notice_location();
  52. $loc->notice_id = $stored->getID();
  53. if (!$loc->find(true)) {
  54. throw new NoResultException($loc);
  55. }
  56. return $loc->asLocation();
  57. }
  58. public static function fromLocation(Location $location)
  59. {
  60. $notloc = new Notice_location();
  61. $notloc->lat = $location->lat;
  62. $notloc->lon = $location->lon;
  63. $notloc->location_ns = $location->location_ns;
  64. $notloc->location_id = $location->location_id;
  65. return $notloc;
  66. }
  67. public function asLocation()
  68. {
  69. $location = null;
  70. if (!empty($this->location_id) && !empty($this->location_ns)) {
  71. $location = Location::fromId($this->location_id, $this->location_ns);
  72. }
  73. if (is_null($location)) { // no ID, or Location::fromId() failed
  74. $location = Location::fromLatLon($this->lat, $this->lon);
  75. }
  76. if (is_null($location)) {
  77. throw new ServerException('Location could not be looked up from existing data.');
  78. }
  79. return $location;
  80. }
  81. }