link api from web interface, make single note fetching work, redo note storage username
[grauphel.git] / controller / guicontroller.php
index 79c6447d8fcbe11cf1978b4e4032f5cf4829f5d8..b92d374e73fb0623d242f63e0f43aef9e06b31e8 100644 (file)
@@ -56,18 +56,67 @@ class GuiController extends Controller
      */
     public function index()
     {
+        $this->checkDeps();
+
         $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;
+    }
+
+    protected function addNavigation(TemplateResponse $res)
+    {
+        $nav = new \OCP\Template('grauphel', 'appnavigation', '');
+        $nav->assign('apiroot', $this->getApiRootUrl());
+
+        $params = $res->getParams();
+        $params['appNavigation'] = $nav;
+        $res->setParams($params);
+    }
+
+    protected function addStats(TemplateResponse $res)
+    {
+        if ($this->user === null) {
+            return;
+        }
+
+        $username = $this->user->getUid();
+        $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
+        $notes->setUsername($username);
+        $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')
+            ),
+            '/'
+        );
     }
 }
 ?>