]> dev.renevier.net Git - syj.git/blob - application/models/PathMapper.php
fixes php warning when uploading a file without being logged
[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 findByUrl($url, Syj_Model_Path $path) {
39         $select = $this->_select();
40         $select->where('id = ?', (int)$url)->orWhere('urlcomp = ?', (string)$url);
41         return $this->_fetchItem($select, $path);
42     }
43
44     public function fetchByCreator(Syj_Model_User $user) {
45         $select = $this->_select();
46         $select->where('creator = ?', (int)$user->id)->order('id');
47         return $this->fetchAll($select);
48     }
49
50     public function fetchAll(Zend_Db_Table_Select $select) {
51         if (!isset($select)) {
52             $select = $this->_select();
53         }
54
55         $table = $this->getDbTable();
56         $resultSet = $table->fetchAll($select);
57
58         $entries   = array();
59         foreach ($resultSet as $row) {
60             $entry = new Syj_Model_Path();
61             $entries[] = $this->_itemFromRow($entry, $row);
62         }
63         return $entries;
64     }
65
66     public function save (Syj_Model_Path $path) {
67         $data = array(
68             'geom'=> (string)$path->geom,
69             'creator'=> $path->creator? $path->creator->id: null,
70             'title'=> $path->title,
71             'creator_ip'=> $path->creatorIp
72         );
73         if (null === ($id = $path->getId())) {
74             $path->id = $this->getDbTable()->insert($data);
75         } else {
76             $this->getDbTable()->update($data, array('id = ?' => $id));
77         }
78     }
79
80     public function delete (Syj_Model_Path $path) {
81         $this->getDbTable()->delete(array('id = ?' => $path->getId()));
82     }
83
84     protected function _itemFromRow(Syj_Model_Path $item, Zend_Db_Table_Row $row) {
85         $decoder = new gisconverter\WKT();
86         $geom = $decoder->geomFromText($row->wkt);
87
88         $item->setId($row->id)->
89             setGeom($geom)->
90             setTitle($row->title)->
91             setUrlComp($row->urlcomp)->
92             setCreatorIp($row->creator_ip);
93
94         if (!$item->getCreator()) {
95             $user = new Syj_Model_User();
96             $userMapper = new Syj_Model_UserMapper();
97             if ($userMapper->find($row->creator, $user)) {
98                 $item->setCreator($user);
99             }
100         }
101         return $item;
102     }
103
104     protected function _fetchItem(Zend_Db_Select $select, Syj_Model_Path $path) {
105         $row = $select->getTable()->fetchRow($select);
106         if ($row) {
107             $this->_itemFromRow($path, $row);
108             return true;
109         }
110         return false;
111     }
112
113     protected function _select() {
114         $table = $this->getDbTable();
115         $select = $table->select();
116         $select->from($table, array('id', 'ST_AsText(geom) AS wkt', 'creator', 'title', 'urlcomp', 'creator_ip'));
117         return $select;
118     }
119
120 }
121