Notice_location.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Table Definition for notice_location
  4. */
  5. class Notice_location extends Managed_DataObject
  6. {
  7. public $__table = 'notice_location'; // table name
  8. public $notice_id; // int(4) primary_key not_null
  9. public $lat; // decimal(10,7)
  10. public $lon; // decimal(10,7)
  11. public $location_id; // int(4)
  12. public $location_ns; // int(4)
  13. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  14. public static function schemaDef()
  15. {
  16. return array(
  17. 'fields' => array(
  18. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice that is the reply'),
  19. 'lat' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude'),
  20. 'lon' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'),
  21. 'location_id' => array('type' => 'int', 'description' => 'location id if possible'),
  22. 'location_ns' => array('type' => 'int', 'description' => 'namespace for location'),
  23. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  24. ),
  25. 'primary key' => array('notice_id'),
  26. 'foreign keys' => array(
  27. 'notice_location_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  28. ),
  29. 'indexes' => array(
  30. 'notice_location_location_id_idx' => array('location_id'),
  31. ),
  32. );
  33. }
  34. static function locFromStored(Notice $stored)
  35. {
  36. $loc = new Notice_location();
  37. $loc->notice_id = $stored->getID();
  38. if (!$loc->find(true)) {
  39. throw new NoResultException($loc);
  40. }
  41. return $loc->asLocation();
  42. }
  43. static function fromLocation(Location $location)
  44. {
  45. $notloc = new Notice_location();
  46. $notloc->lat = $location->lat;
  47. $notloc->lon = $location->lon;
  48. $notloc->location_ns = $location->location_ns;
  49. $notloc->location_id = $location->location_id;
  50. return $notloc;
  51. }
  52. public function asLocation()
  53. {
  54. $location = null;
  55. if (!empty($this->location_id) && !empty($this->location_ns)) {
  56. $location = Location::fromId($this->location_id, $this->location_ns);
  57. }
  58. if (is_null($location)) { // no ID, or Location::fromId() failed
  59. $location = Location::fromLatLon($this->lat, $this->lon);
  60. }
  61. if (is_null($location)) {
  62. throw new ServerException('Location could not be looked up from existing data.');
  63. }
  64. return $location;
  65. }
  66. }