2 /* This file is part of Syj, Copyright (c) 2010-2011 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 $row = $db->fetchRow($select);
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);
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);
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);
50 public function fetchAll(Zend_Db_Table_Select $select) {
51 if (!isset($select)) {
52 $select = $this->_select();
55 $table = $this->getDbTable();
56 $resultSet = $table->fetchAll($select);
59 foreach ($resultSet as $row) {
60 $entry = new Syj_Model_Path();
61 $entries[] = $this->_itemFromRow($entry, $row);
66 public function save (Syj_Model_Path $path) {
68 'geom'=> (string)$path->geom,
69 'creator'=> $path->creator? $path->creator->id: null,
70 'title'=> $path->title,
71 'creator_ip'=> $path->creatorIp
73 if (null === ($id = $path->getId())) {
74 $path->id = $this->getDbTable()->insert($data);
76 $this->getDbTable()->update($data, array('id = ?' => $id));
80 public function delete (Syj_Model_Path $path) {
81 $this->getDbTable()->delete(array('id = ?' => $path->getId()));
84 protected function _itemFromRow(Syj_Model_Path $item, Zend_Db_Table_Row $row) {
85 $decoder = new gisconverter\WKT();
86 $geom = $decoder->geomFromText($row->wkt);
88 $item->setId($row->id)->
90 setTitle($row->title)->
91 setUrlComp($row->urlcomp)->
92 setCreatorIp($row->creator_ip);
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);
104 protected function _fetchItem(Zend_Db_Select $select, Syj_Model_Path $path) {
105 $row = $select->getTable()->fetchRow($select);
107 $this->_itemFromRow($path, $row);
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'));