Add missing standard fields (Tomboy legacy):
[grauphel.git] / lib / notestorage.php
index d1fe7e3b8f99c5d5a723728811f7310063052005..5dc503b2a286ccee3f55665c8d0a1ac9f8a6800f 100644 (file)
@@ -26,12 +26,18 @@ namespace OCA\Grauphel\Lib;
  */
 class NoteStorage
 {
+    /**
+     * @var \OCP\IDBConnection
+     */
+    protected $db;
+
     protected $urlGen;
     protected $username;
 
     public function __construct($urlGen)
     {
-        $this->urlGen   = $urlGen;
+        $this->urlGen = $urlGen;
+        $this->db     = \OC::$server->getDatabaseConnection();
     }
 
     public function setUsername($username)
@@ -54,14 +60,14 @@ class NoteStorage
 
     public function getTags()
     {
-        $result = \OC_DB::executeAudited(
+        $result = $this->db->executeQuery(
             'SELECT `note_tags` FROM `*PREFIX*grauphel_notes`'
             . ' WHERE note_user = ?',
             array($this->username)
         );
 
         $tags = array();
-        while ($row = $result->fetchRow()) {
+        while ($row = $result->fetch()) {
             $tags = array_merge($tags, json_decode($row['note_tags']));
         }
         return array_unique($tags);
@@ -89,6 +95,9 @@ class NoteStorage
             'pinned',
             'tags',
             'title',
+            'x', 'y', 'width', 'height',
+            'selection-bound-position',
+            'cursor-position',
         );
 
         $changed = array();
@@ -114,6 +123,11 @@ class NoteStorage
             $note->{'last-metadata-change-date'} = date('c');
         }
 
+        if (!isset($note->{'create-date'})) {
+            //no idea how to get the microseconds in there
+            $note->{'create-date'} = date('c');
+        }
+
         if (isset($noteUpdate->{'node-content'})
             && $note->{'note-content-version'} == 0
         ) {
@@ -131,11 +145,11 @@ class NoteStorage
      */
     public function loadSyncData()
     {
-        $row = \OC_DB::executeAudited(
+        $row = $this->db->executeQuery(
             'SELECT * FROM `*PREFIX*grauphel_syncdata`'
             . ' WHERE `syncdata_user` = ?',
             array($this->username)
-        )->fetchRow();
+        )->fetch();
 
         if ($row === false) {
             $syncdata = $this->getNewSyncData();
@@ -158,11 +172,11 @@ class NoteStorage
      */
     public function saveSyncData(SyncData $syncdata)
     {
-        $row = \OC_DB::executeAudited(
+        $row = $this->db->executeQuery(
             'SELECT * FROM `*PREFIX*grauphel_syncdata`'
             . ' WHERE `syncdata_user` = ?',
             array($this->username)
-        )->fetchRow();
+        )->fetch();
 
         if ($row === false) {
             //INSERT
@@ -186,7 +200,7 @@ class NoteStorage
             $params = array_values($data);
             $params[] = $this->username;
         }
-        \OC_DB::executeAudited($sql, $params);
+        $this->db->executeQuery($sql, $params);
     }
 
     /**
@@ -198,7 +212,7 @@ class NoteStorage
      */
     public function deleteSyncData()
     {
-        \OC_DB::executeAudited(
+        $this->db->executeQuery(
             'DELETE FROM `*PREFIX*grauphel_syncdata`'
             . ' WHERE `syncdata_user` = ?',
             array($this->username)
@@ -215,30 +229,38 @@ class NoteStorage
      */
     public function load($guid, $createNew = true)
     {
-        $row = \OC_DB::executeAudited(
+        $row = $this->db->executeQuery(
             'SELECT * FROM `*PREFIX*grauphel_notes`'
             . ' WHERE `note_user` = ? AND `note_guid` = ?',
             array($this->username, $guid)
-        )->fetchRow();
+        )->fetch();
 
         if ($row === false) {
             if (!$createNew) {
                 return null;
             }
             return (object) array(
-                'guid' => $guid,
+                'guid'                      => $guid,
 
                 'create-date'               => null,
                 'last-change-date'          => null,
                 'last-metadata-change-date' => null,
 
-                'title'                => null,
-                'note-content'         => null,
-                'note-content-version' => 0.3,
+                'title'                     => null,
+                'note-content'              => null,
+                'note-content-version'      => 0.3,
+
+                'open-on-startup'           => false,
+                'pinned'                    => false,
+                'tags'                      => array(),
 
-                'open-on-startup' => false,
-                'pinned'          => false,
-                'tags'            => array(),
+                'x'                         => 20,
+                'y'                         => 20,
+                'width'                     => -1,
+                'height'                    => -1,
+
+                'cursor-position'           => 0,
+                'selection-bound-position'  => 0,
             );
         }
 
@@ -258,11 +280,11 @@ class NoteStorage
      */
     public function loadGuidByTitle($title)
     {
-        $row = \OC_DB::executeAudited(
+        $row = $this->db->executeQuery(
             'SELECT note_guid FROM `*PREFIX*grauphel_notes`'
             . ' WHERE `note_user` = ? AND `note_title` = ?',
             array($this->username, htmlspecialchars($title))
-        )->fetchRow();
+        )->fetch();
 
         if ($row === false) {
             return null;
@@ -287,8 +309,8 @@ class NoteStorage
             $keywordGroups['NOT'] = array();
         }
 
-        $sqlTplAnd = ' AND (note_title LIKE ? OR note_tags LIKE ? OR note_content LIKE ?)';
-        $sqlTplNot = ' AND NOT (note_title LIKE ? OR note_tags LIKE ? OR note_content LIKE ?)';
+        $sqlTplAnd = ' AND (`note_title` ILIKE ? OR `note_tags` ILIKE ? OR `note_content` ILIKE ?)';
+        $sqlTplNot = ' AND NOT (`note_title` ILIKE ? OR `note_tags` ILIKE ? OR `note_content` ILIKE ?)';
         $arData = array(
             $this->username
         );
@@ -301,7 +323,7 @@ class NoteStorage
             }
         }
 
-        $result = \OC_DB::executeAudited(
+        $result = $this->db->executeQuery(
             'SELECT `note_guid`, `note_title`'
             . ' FROM `*PREFIX*grauphel_notes`'
             . ' WHERE note_user = ?'
@@ -311,7 +333,7 @@ class NoteStorage
         );
 
         $notes = array();
-        while ($row = $result->fetchRow()) {
+        while ($row = $result->fetch()) {
             $notes[] = $row;
         }
         return $notes;
@@ -326,11 +348,11 @@ class NoteStorage
      */
     public function save($note)
     {
-        $row = \OC_DB::executeAudited(
+        $row = $this->db->executeQuery(
             'SELECT * FROM `*PREFIX*grauphel_notes`'
             . ' WHERE `note_user` = ? AND `note_guid` = ?',
             array($this->username, $note->guid)
-        )->fetchRow();
+        )->fetch();
 
         $data = $this->rowFromNote($note);
         if ($row === false) {
@@ -349,7 +371,7 @@ class NoteStorage
             $params[] = $this->username;
             $params[] = $note->guid;
         }
-        \OC_DB::executeAudited($sql, $params);
+        $this->db->executeQuery($sql, $params);
     }
 
     /**
@@ -361,7 +383,7 @@ class NoteStorage
      */
     public function delete($guid)
     {
-        \OC_DB::executeAudited(
+        $this->db->executeQuery(
             'DELETE FROM `*PREFIX*grauphel_notes`'
             . ' WHERE `note_user` = ? AND `note_guid` = ?',
             array($this->username, $guid)
@@ -375,7 +397,7 @@ class NoteStorage
      */
     public function deleteAll()
     {
-        \OC_DB::executeAudited(
+        $this->db->executeQuery(
             'DELETE FROM `*PREFIX*grauphel_notes`'
             . ' WHERE `note_user` = ?',
             array($this->username)
@@ -397,15 +419,18 @@ class NoteStorage
     public function loadNotesOverview(
         $since = null, $rawtag = null, $includeDate = false
     ) {
-        $result = \OC_DB::executeAudited(
-            'SELECT `note_guid`, `note_title`, `note_last_sync_revision`, `note_tags`'
+        $sql = 'SELECT `note_guid`, `note_title`'
+            . ', `note_last_sync_revision`, `note_tags`'
             . ', `note_last_change_date`'
             . ' FROM `*PREFIX*grauphel_notes`'
-            . ' WHERE note_user = ?',
-            array($this->username)
-        );
+            . ' WHERE note_user = ?';
+        $sqlData = array($this->username);
+
+        if ($since !== null) {
+            $sqlData[] = $since;
+            $sql .= ' AND note_last_sync_revision > ?';
+        }
 
-        $notes = array();
         if ($rawtag == 'grauphel:special:all') {
             $rawtag = null;
         } else if ($rawtag == 'grauphel:special:untagged') {
@@ -413,13 +438,14 @@ class NoteStorage
         } else {
             $jsRawtag = json_encode($rawtag);
         }
-        while ($row = $result->fetchRow()) {
-            if ($since !== null && $row['note_last_sync_revision'] <= $since) {
-                continue;
-            }
-            if ($rawtag !== null && strpos($row['note_tags'], $jsRawtag) === false) {
-                continue;
-            }
+        if ($rawtag !== null) {
+            $sqlData[] = '%' . $jsRawtag . '%';
+            $sql .= ' AND note_tags LIKE ?';
+        }
+
+        $result = $this->db->executeQuery($sql, $sqlData);
+        $notes = array();
+        while ($row = $result->fetch()) {
             $note = array(
                 'guid' => $row['note_guid'],
                 'ref'  => array(
@@ -462,14 +488,14 @@ class NoteStorage
      */
     public function loadNotesFull($since = null)
     {
-        $result = \OC_DB::executeAudited(
+        $result = $this->db->executeQuery(
             'SELECT * FROM `*PREFIX*grauphel_notes`'
             . ' WHERE note_user = ?',
             array($this->username)
         );
 
         $notes = array();
-        while ($row = $result->fetchRow()) {
+        while ($row = $result->fetch()) {
             if ($since !== null && $row['note_last_sync_revision'] <= $since) {
                 continue;
             }
@@ -498,37 +524,53 @@ class NoteStorage
             'last-change-date'          => $this->fixDate($row['note_last_change_date']),
             'last-metadata-change-date' => $this->fixDate($row['note_last_metadata_change_date']),
 
-            'title'                => $row['note_title'],
-            'note-content'         => $row['note_content'],
-            'note-content-version' => $row['note_content_version'],
+            'title'                     => $row['note_title'],
+            'note-content'              => $row['note_content'],
+            'note-content-version'      => $row['note_content_version'],
+
+            'open-on-startup'           => (bool) $row['note_open_on_startup'],
+            'pinned'                    => (bool) $row['note_pinned'],
+            'tags'                      => json_decode($row['note_tags']),
 
-            'open-on-startup' => (bool) $row['note_open_on_startup'],
-            'pinned'          => (bool) $row['note_pinned'],
-            'tags'            => json_decode($row['note_tags']),
+            'x'                         => (int) $row['note_x'],
+            'y'                         => (int) $row['note_y'],
+            'height'                    => (int) $row['note_height'],
+            'width'                     => (int) $row['note_width'],
 
-            'last-sync-revision' => (int) $row['note_last_sync_revision'],
+            'selection-bound-position'  => (int) $row['note_selection_bound_position'],
+            'cursor-position'           => (int) $row['note_cursor_position'],
+
+            'last-sync-revision'        => (int) $row['note_last_sync_revision'],
         );
     }
 
     protected function rowFromNote($note)
     {
         return array(
-            'note_guid'  => $note->guid,
-            'note_title' => (string) $note->title,
+            'note_guid'                      => $note->guid,
+            'note_title'                     => (string) $note->title,
 
-            'note_content'         => (string) $note->{'note-content'},
-            'note_content_version' => (string) $note->{'note-content-version'},
+            'note_content'                   => (string) $note->{'note-content'},
+            'note_content_version'           => (string) $note->{'note-content-version'},
 
             'note_create_date'               => $note->{'create-date'},
             'note_last_change_date'          => $note->{'last-change-date'},
             'note_last_metadata_change_date' => $note->{'last-metadata-change-date'},
 
-            'note_open_on_startup' => (int) $note->{'open-on-startup'},
-            'note_pinned'          => (int) $note->pinned,
-            'note_tags'            => json_encode($note->tags),
+            'note_open_on_startup'           => (int) $note->{'open-on-startup'},
+            'note_pinned'                    => (int) $note->pinned,
+            'note_tags'                      => json_encode($note->tags),
+
+            'note_x'                         => (int) $note->{'x'},
+            'note_y'                         => (int) $note->{'y'},
+            'note_height'                    => (int) $note->{'height'},
+            'note_width'                     => (int) $note->{'width'},
+
+            'note_selection_bound_position'  => (int) $note->{'selection-bound-position'},
+            'note_cursor_position'           => (int) $note->{'cursor-position'},
 
-            'note_last_sync_revision' => $note->{'last-sync-revision'},
+            'note_last_sync_revision'        => $note->{'last-sync-revision'},
         );
     }
 }
-?>
\ No newline at end of file
+?>