]> dev.renevier.net Git - syj.git/blob - application/models/PathMapper.php
routes profile
[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             $path->invalidateCache();
84         }
85     }
86
87     public function delete (Syj_Model_Path $path) {
88         $this->getDbTable()->delete(array('id = ?' => $path->getId()));
89     }
90
91     protected function _itemFromRow(Syj_Model_Path $item, Zend_Db_Table_Row $row) {
92         $decoder = new gisconverter\WKT();
93         $geom = $decoder->geomFromText($row->wkt);
94
95         $item->setId($row->id)->
96             setGeom($geom)->
97             setTitle($row->title)->
98             setUrlComp($row->urlcomp)->
99             setCreatorIp($row->creator_ip);
100
101         if (!$item->getCreator()) {
102             $user = new Syj_Model_User();
103             $userMapper = new Syj_Model_UserMapper();
104             if ($userMapper->find($row->creator, $user)) {
105                 $item->setCreator($user);
106             }
107         }
108         return $item;
109     }
110
111     protected function _fetchItem(Zend_Db_Select $select, Syj_Model_Path $path) {
112         $row = $select->getTable()->fetchRow($select);
113         if ($row) {
114             $this->_itemFromRow($path, $row);
115             return true;
116         }
117         return false;
118     }
119
120     protected function _select() {
121         $table = $this->getDbTable();
122         $select = $table->select();
123         $select->from($table, array('id', 'ST_AsText(geom) AS wkt', 'creator', 'title', 'urlcomp', 'creator_ip'));
124         return $select;
125     }
126
127 }
128