]> dev.renevier.net Git - syj.git/blob - application/models/PathMapper.php
version 0.1
[syj.git] / application / models / PathMapper.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 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         print $select->assemble();
29         $row = $db->fetchRow($select);
30         return $row['t'];
31     }
32
33     public function find($id, Syj_Model_Path $path) {
34         $select = $this->_select();
35         $select->where('id = ?', (int)$id);
36         return $this->_fetchItem($select, $path);
37     }
38
39     public function findByUrl($url, Syj_Model_Path $path) {
40         $select = $this->_select();
41         $select->where('id = ?', (int)$url)->orWhere('urlcomp = ?', (string)$url);
42         return $this->_fetchItem($select, $path);
43     }
44
45     public function fetchAll() {
46         $select = $this->_select();
47
48         $resultSet = $table->fetchAll($select);
49
50         $entries   = array();
51         foreach ($resultSet as $row) {
52             $entry = new Syj_Model_Path();
53             $entries[] = $this->_itemFromRow($entry, $row);
54         }
55         return $entries;
56     }
57
58     public function save (Syj_Model_Path $path) {
59         $data = array(
60             'geom'=> (string)$path->geom,
61             'owner'=> $path->owner->id,
62             'title'=> $path->title
63         );
64         if (null === ($id = $path->getId())) {
65             $path->id = $this->getDbTable()->insert($data);
66         } else {
67             $this->getDbTable()->update($data, array('id = ?' => $id));
68         }
69     }
70
71     protected function _itemFromRow(Syj_Model_Path $item, Zend_Db_Table_Row $row) {
72         $decoder = new gisconverter\WKT();
73         $geom = $decoder->geomFromText($row->wkt);
74
75         $item->setId($row->id)->
76             setGeom($geom)->
77             setTitle($row->title)->
78             setUrlComp($row->urlcomp);
79
80         if (!$item->getOwner()) {
81             $user = new Syj_Model_User();
82             $userMapper = new Syj_Model_UserMapper();
83             if ($userMapper->find($row->owner, $user)) {
84                 $item->setOwner($user);
85             }
86         }
87         return $item;
88     }
89
90     protected function _fetchItem(Zend_Db_Select $select, Syj_Model_Path $path) {
91         $row = $select->getTable()->fetchRow($select);
92         if ($row) {
93             $this->_itemFromRow($path, $row);
94             return true;
95         }
96         return false;
97     }
98
99     protected function _select() {
100         $table = $this->getDbTable();
101         $select = $table->select();
102         $select->from($table, array('id', 'ST_AsText(geom) AS wkt', 'owner', 'title', 'urlcomp'));
103         return $select;
104     }
105
106 }
107