aboutsummaryrefslogtreecommitdiff
path: root/classes/Notice_location.php
blob: 1574414e117ac6e9fbcf48f8be2249bf6ca951af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
 * Table Definition for notice_location
 */

class Notice_location extends Managed_DataObject
{
    public $__table = 'notice_location';     // table name
    public $notice_id;                       // int(4)  primary_key not_null
    public $lat;                             // decimal(10,7)
    public $lon;                             // decimal(10,7)
    public $location_id;                     // int(4)
    public $location_ns;                     // int(4)
    public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP

    public static function schemaDef()
    {
        return array(
            'fields' => 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;
    }
}