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