]> dev.renevier.net Git - syj.git/blobdiff - application/models/PathMapper.php
routes profile
[syj.git] / application / models / PathMapper.php
index c89936455905e9c060110665e9d194b864d6adb2..2bbeabb7a334d1dbff3e75be7f15d34cc81408d6 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-/*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
+/*  This file is part of Syj, Copyright (c) 2010-2011 Arnaud Renevier,
     and is published under the AGPL license. */
 
 class Syj_Model_PathMapper
@@ -25,7 +25,6 @@ class Syj_Model_PathMapper
         $db = $this->getDbTable()->getAdapter();
         $expr = $db->quoteInto('seq_attained_value(?)', array('paths_id_seq', (int)$id));
         $select = $db->select()->from(new Zend_Db_Expr($expr));
-        print $select->assemble();
         $row = $db->fetchRow($select);
         return $row['t'];
     }
@@ -36,15 +35,30 @@ class Syj_Model_PathMapper
         return $this->_fetchItem($select, $path);
     }
 
+    public function findByTitle($title, Syj_Model_Path $path) {
+        $select = $this->_select();
+        $select->where('title = ?', (string)$title);
+        return $this->_fetchItem($select, $path);
+    }
+
     public function findByUrl($url, Syj_Model_Path $path) {
         $select = $this->_select();
         $select->where('id = ?', (int)$url)->orWhere('urlcomp = ?', (string)$url);
         return $this->_fetchItem($select, $path);
     }
 
-    public function fetchAll() {
+    public function fetchByCreator(Syj_Model_User $user) {
         $select = $this->_select();
+        $select->where('creator = ?', (int)$user->id)->order('id');
+        return $this->fetchAll($select);
+    }
 
+    public function fetchAll(Zend_Db_Table_Select $select) {
+        if (!isset($select)) {
+            $select = $this->_select();
+        }
+
+        $table = $this->getDbTable();
         $resultSet = $table->fetchAll($select);
 
         $entries   = array();
@@ -58,7 +72,7 @@ class Syj_Model_PathMapper
     public function save (Syj_Model_Path $path) {
         $data = array(
             'geom'=> (string)$path->geom,
-            'creator'=> $path->creator->id,
+            'creator'=> $path->creator? $path->creator->id: null,
             'title'=> $path->title,
             'creator_ip'=> $path->creatorIp
         );
@@ -66,9 +80,14 @@ class Syj_Model_PathMapper
             $path->id = $this->getDbTable()->insert($data);
         } else {
             $this->getDbTable()->update($data, array('id = ?' => $id));
+            $path->invalidateCache();
         }
     }
 
+    public function delete (Syj_Model_Path $path) {
+        $this->getDbTable()->delete(array('id = ?' => $path->getId()));
+    }
+
     protected function _itemFromRow(Syj_Model_Path $item, Zend_Db_Table_Row $row) {
         $decoder = new gisconverter\WKT();
         $geom = $decoder->geomFromText($row->wkt);