]> dev.renevier.net Git - syj.git/blob - application/models/PathMapper.php
7d514e9f655a9ffac7c746f11cef451838468a52
[syj.git] / application / models / PathMapper.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 class Syj_Model_PathMapper
6 {
7     protected $_dbTable;
8     protected $_tableInfos = array('name' => 'paths');
9
10     public function getDbTable() {
11         if (null === $this->_dbTable) {
12             $this->_dbTable = new Zend_Db_Table($this->_tableInfos);
13         }
14         return $this->_dbTable;
15     }
16
17     /*
18      * return true if a path with id $id has been created one day, even if the
19      * path has been deleted since. Returns false if path has never existed.
20      */
21     public function hasexisted($id) {
22         if (!is_numeric($id)) {
23             return false;
24         }
25         $db = $this->getDbTable()->getAdapter();
26         $expr = $db->quoteInto('seq_attained_value(?)', array('paths_id_seq', (int)$id));
27         $select = $db->select()->from(new Zend_Db_Expr($expr));
28         $row = $db->fetchRow($select);
29         return $row['t'];
30     }
31
32     public function find($id, Syj_Model_Path $path) {
33         $select = $this->_select();
34         $select->where('id = ?', (int)$id);
35         return $this->_fetchItem($select, $path);
36     }
37
38     public function findByTitle($title, Syj_Model_Path $path) {
39         $select = $this->_select();
40         $select->where('title = ?', (string)$title);
41         return $this->_fetchItem($select, $path);
42     }
43
44     public function findByUrl($url, Syj_Model_Path $path) {
45         $select = $this->_select();
46         $select->where('id = ?', (int)$url)->orWhere('urlcomp = ?', (string)$url);
47         return $this->_fetchItem($select, $path);
48     }
49
50     public function fetchByCreator(Syj_Model_User $user) {
51         $select = $this->_select();
52         $select->where('creator = ?', (int)$user->id)->order('id');
53         return $this->fetchAll($select);
54     }
55
56     public function fetchAll(Zend_Db_Table_Select $select) {
57         if (!isset($select)) {
58             $select = $this->_select();
59         }
60
61         $table = $this->getDbTable();
62         $resultSet = $table->fetchAll($select);
63
64         $entries   = array();
65         foreach ($resultSet as $row) {
66             $entry = new Syj_Model_Path();
67             $entries[] = $this->_itemFromRow($entry, $row);
68         }
69         return $entries;
70     }
71
72     public function save (Syj_Model_Path $path) {
73         $data = array(
74             'geom'=> (string)$path->geom,
75             'creator'=> $path->creator? $path->creator->id: null,
76             'title'=> $path->title,
77             'creator_ip'=> $path->creatorIp
78         );
79         if (null === ($id = $path->getId())) {
80             $path->id = $this->getDbTable()->insert($data);
81         } else {
82             $this->getDbTable()->update($data, array('id = ?' => $id));
83         }
84     }
85
86     public function delete (Syj_Model_Path $path) {
87         $this->getDbTable()->delete(array('id = ?' => $path->getId()));
88     }
89
90     protected function _itemFromRow(Syj_Model_Path $item, Zend_Db_Table_Row $row) {
91         $decoder = new gisconverter\WKT();
92         $geom = $decoder->geomFromText($row->wkt);
93
94         $item->setId($row->id)->
95             setGeom($geom)->
96             setTitle($row->title)->
97             setUrlComp($row->urlcomp)->
98             setCreatorIp($row->creator_ip);
99
100         if (!$item->getCreator()) {
101             $user = new Syj_Model_User();
102             $userMapper = new Syj_Model_UserMapper();
103             if ($userMapper->find($row->creator, $user)) {
104                 $item->setCreator($user);
105             }
106         }
107         return $item;
108     }
109
110     protected function _fetchItem(Zend_Db_Select $select, Syj_Model_Path $path) {
111         $row = $select->getTable()->fetchRow($select);
112         if ($row) {
113             $this->_itemFromRow($path, $row);
114             return true;
115         }
116         return false;
117     }
118
119     protected function _select() {
120         $table = $this->getDbTable();
121         $select = $table->select();
122         $select->from($table, array('id', 'ST_AsText(geom) AS wkt', 'creator', 'title', 'urlcomp', 'creator_ip'));
123         return $select;
124     }
125
126 }
127