show tags and note titles in the notebook
[grauphel.git] / controller / guicontroller.php
index 8e260bc0a269795a414bcc2b1e310a3a9912b0c4..92cdf9c6588184666694ff76f43b57c1307a6a91 100644 (file)
@@ -61,21 +61,112 @@ class GuiController extends Controller
         $res = new TemplateResponse('grauphel', 'index');
         $res->setParams(
             array(
-                'apiurl' => $this->urlGen->getAbsoluteURL(
-                    $this->urlGen->linkToRoute(
-                        'grauphel.gui.index'
-                    )
-                ),
+                'apiroot' => $this->getApiRootUrl(),
+                'apiurl'  => $this->urlGen->linkToRoute('grauphel.api.index')
             )
         );
+        $this->addNavigation($res);
+        $this->addStats($res);
         return $res;
     }
 
+    /**
+     * Show all notes of a tag
+     *
+     * @NoAdminRequired
+     * @NoCSRFRequired
+     */
+    public function tag($rawtag)
+    {
+        $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
+
+        $res = new TemplateResponse('grauphel', 'tag');
+        $res->setParams(
+            array(
+                'tag'    => substr($rawtag, 16),
+                'rawtag' => $rawtag,
+                'notes'  => $notes,
+            )
+        );
+        $this->addNavigation($res, $rawtag);
+
+        return $res;
+    }
+
+    protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
+    {
+        $nav = new \OCP\Template('grauphel', 'appnavigation', '');
+        $nav->assign('apiroot', $this->getApiRootUrl());
+
+        $params = $res->getParams();
+        $params['appNavigation'] = $nav;
+        $res->setParams($params);
+
+        if ($this->user === null) {
+            return;
+        }
+
+        $rawtags = $this->getNotes()->getTags();
+        sort($rawtags);
+        $tags = array();
+        foreach ($rawtags as $rawtag) {
+            if (substr($rawtag, 0, 16) == 'system:notebook:') {
+                $tags[] = array(
+                    'name' => substr($rawtag, 16),
+                    'id'   => $rawtag,
+                    'href' => $this->urlGen->linkToRoute(
+                        'grauphel.gui.tag', array('tag' => $rawtag)
+                    ),
+                );
+            }
+        }
+        $nav->assign('tags', $tags);
+    }
+
+    protected function addStats(TemplateResponse $res)
+    {
+        if ($this->user === null) {
+            return;
+        }
+
+        $username = $this->user->getUid();
+        $notes  = $this->getNotes();
+        $tokens = new \OCA\Grauphel\Lib\TokenStorage();
+
+        $nav = new \OCP\Template('grauphel', 'indexStats', '');
+        $nav->assign('notes', count($notes->loadNotesOverview()));
+        $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
+        $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
+
+        $params = $res->getParams();
+        $params['stats'] = $nav;
+        $res->setParams($params);
+    }
+
     protected function checkDeps()
     {
         if (!class_exists('OAuthProvider')) {
             throw new \Exception('PHP extension "oauth" is required');
         }
     }
+
+    protected function getApiRootUrl()
+    {
+        //we need to remove the trailing / for tomdroid and conboy
+        return rtrim(
+            $this->urlGen->getAbsoluteURL(
+                $this->urlGen->linkToRoute('grauphel.gui.index')
+            ),
+            '/'
+        );
+    }
+
+    protected function getNotes()
+    {
+        $username = $this->user->getUid();
+        $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
+        $notes->setUsername($username);
+        return $notes;
+    }
 }
 ?>