2 /* This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3 and is published under the AGPL license. */
5 class Syj_Model_PathMapper
8 protected $_tableInfos = array('name' => 'paths');
10 public function getDbTable() {
11 if (null === $this->_dbTable) {
12 $this->_dbTable = new Zend_Db_Table($this->_tableInfos);
14 return $this->_dbTable;
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.
21 public function hasexisted($id) {
22 if (!is_numeric($id)) {
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);
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);
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);
45 public function fetchAll() {
46 $select = $this->_select();
48 $resultSet = $table->fetchAll($select);
51 foreach ($resultSet as $row) {
52 $entry = new Syj_Model_Path();
53 $entries[] = $this->_itemFromRow($entry, $row);
58 public function save (Syj_Model_Path $path) {
60 'geom'=> (string)$path->geom,
61 'owner'=> $path->owner->id,
62 'title'=> $path->title
64 if (null === ($id = $path->getId())) {
65 $path->id = $this->getDbTable()->insert($data);
67 $this->getDbTable()->update($data, array('id = ?' => $id));
71 protected function _itemFromRow(Syj_Model_Path $item, Zend_Db_Table_Row $row) {
72 $decoder = new gisconverter\WKT();
73 $geom = $decoder->geomFromText($row->wkt);
75 $item->setId($row->id)->
77 setTitle($row->title)->
78 setUrlComp($row->urlcomp);
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);
90 protected function _fetchItem(Zend_Db_Select $select, Syj_Model_Path $path) {
91 $row = $select->getTable()->fetchRow($select);
93 $this->_itemFromRow($path, $row);
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'));