Link to maintainership/funding post
[grauphel.git] / controller / guicontroller.php
index b07ba45679051911653d9845fe9ce28b1de4161d..cac2f290c88aea7171bab2c9bc0f0c58ac957fe6 100644 (file)
@@ -15,6 +15,9 @@ namespace OCA\Grauphel\Controller;
 
 use \OCP\AppFramework\Controller;
 use \OCP\AppFramework\Http\TemplateResponse;
+use \OCA\Grauphel\Lib\Client;
+use \OCA\Grauphel\Lib\TokenStorage;
+use \OCA\Grauphel\Lib\Response\ErrorResponse;
 
 /**
  * Owncloud frontend
@@ -56,7 +59,18 @@ class GuiController extends Controller
      */
     public function index()
     {
-        $this->checkDeps();
+        try {
+            $this->checkDeps();
+        } catch (\Exception $e) {
+            $res = new TemplateResponse('grauphel', 'error');
+            $res->setParams(
+                array(
+                    'message' => $e->getMessage(),
+                    'code' => $e->getCode(),
+                )
+            );
+            return $res;
+        }
 
         $res = new TemplateResponse('grauphel', 'index');
         $res->setParams(
@@ -70,6 +84,83 @@ class GuiController extends Controller
         return $res;
     }
 
+    /**
+     * Show contents of a note
+     *
+     * @NoAdminRequired
+     * @NoCSRFRequired
+     */
+    public function note($guid)
+    {
+        $res = new TemplateResponse('grauphel', 'gui-note');
+
+        $note = $this->getNotes()->load($guid, false);
+        if ($note === null) {
+            $res = new ErrorResponse('Note does not exist');
+            $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
+            return $res;
+        }
+
+        $converter = new \OCA\Grauphel\Converter\Html();
+        $converter->internalLinkHandler = array($this, 'noteLinkHandler');
+
+        try {
+            $contentHtml = $converter->convert($note->{'note-content'});
+        } catch (\OCA\Grauphel\Converter\Exception $e) {
+            $contentHtml = '<div class="error">'
+                . '<p>There was an error converting the note to HTML:</p>'
+                . '<blockquote><tt>' . htmlspecialchars($e->getMessage()) . '</tt></blockquote>'
+                . '<p>Please open a bug report at'
+                . ' <a class="lined" href="http://github.com/cweiske/grauphel/issues">'
+                . 'github.com/cweiske/grauphel/issues</a>'
+                . ' and attach the XML version of the note.'
+                . '</div>';
+        }
+
+        $res->setParams(
+            array(
+                'note' => $note,
+                'note-content' => $contentHtml,
+                'links' => array(
+                    'html' => $this->urlGen->linkToRoute(
+                        'grauphel.notes.html', array('guid' => $guid)
+                    ),
+                    'json' => $this->urlGen->linkToRoute(
+                        'grauphel.api.note', array(
+                            'guid' => $guid, 'username' => $this->user->getUid()
+                        )
+                    ),
+                    'text' => $this->urlGen->linkToRoute(
+                        'grauphel.notes.text', array('guid' => $guid)
+                    ),
+                    'xml' => $this->urlGen->linkToRoute(
+                        'grauphel.notes.xml', array('guid' => $guid)
+                    ),
+                )
+            )
+        );
+
+        $selectedRawtag = 'grauphel:special:untagged';
+        if (count($note->tags) > 0) {
+            $selectedRawtag = $note->tags[0];
+        }
+
+        $this->addNavigation($res, $selectedRawtag);
+        $this->addGlobalVars($res);
+        return $res;
+    }
+
+    public function noteLinkHandler($noteTitle)
+    {
+        $guid = $this->getNotes()->loadGuidByTitle($noteTitle);
+        if ($guid === null) {
+            return '#';
+        }
+        return $this->urlGen->linkToRoute(
+            'grauphel.gui.note', array('guid' => $guid)
+        );
+    }
+
     /**
      * Show all notes of a tag
      *
@@ -78,25 +169,139 @@ class GuiController extends Controller
      */
     public function tag($rawtag)
     {
-        $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
+        $rawtag = $this->unescapeTagFromUrl($rawtag);
+        $notes = $this->getNotes()->loadNotesOverview(null, $rawtag, true);
+
+        if (!isset($_GET['sortby'])) {
+            $_GET['sortby'] = 'title';
+        }
+
+        switch ($_GET['sortby']) {
+        case 'title':
+            usort(
+                $notes,
+                function($noteA, $noteB) {
+                    return strcasecmp($noteA['title'], $noteB['title']);
+                }
+            );
+            break;
+        case 'date':
+            usort(
+                $notes,
+                function($noteA, $noteB) {
+                    return strcmp($noteB['last-change-date'], $noteA['last-change-date']);
+                }
+            );
+            break;
+        }
+
+        foreach ($notes as &$note) {
+            $diffInDays = intval(
+                (time() - strtotime($note['last-change-date'])) / 86400
+            );
+            $value = 0 + $diffInDays;
+            if ($value > 160) {
+                $value = 160;
+            }
+            $note['dateColor'] = '#' . str_repeat(sprintf('%02X', $value), 3);
+        }
 
         $res = new TemplateResponse('grauphel', 'tag');
         $res->setParams(
             array(
-                'tag'    => substr($rawtag, 16),
+                'tag'    => $this->getPrettyTagName($rawtag),
                 'rawtag' => $rawtag,
                 'notes'  => $notes,
+                'tagUrl' => $this->urlGen->linkToRoute(
+                    'grauphel.gui.tag',
+                    array('rawtag' => $this->escapeTagForUrl($rawtag))
+                ),
             )
         );
+        $this->addGlobalVars($res);
         $this->addNavigation($res, $rawtag);
 
         return $res;
     }
 
+    /**
+     * Show access tokens
+     *
+     * @NoAdminRequired
+     * @NoCSRFRequired
+     */
+    public function tokens()
+    {
+        $tokens = new TokenStorage();
+        $res = new TemplateResponse('grauphel', 'tokens');
+        $res->setParams(
+            array(
+                'tokens' => $tokens->loadForUser(
+                    $this->user->getUid(), 'access'
+                ),
+                'client' => new Client(),
+                'username' => $this->user->getUid(),
+            )
+        );
+        $this->addGlobalVars($res);
+        $this->addNavigation($res, null);
+
+        return $res;
+    }
+
+    /**
+     * Allow the user to clear his database
+     *
+     * @NoAdminRequired
+     * @NoCSRFRequired
+     */
+    public function database($reset = null)
+    {
+        $res = new TemplateResponse('grauphel', 'gui-database');
+        $res->setParams(array('reset' => $reset));
+        $this->addNavigation($res, null);
+        $this->addStats($res);
+
+        return $res;
+    }
+
+    /**
+     * Resets the database by deleting all notes and deleting the user's
+     * sync data.
+     *
+     * @NoAdminRequired
+     */
+    public function databaseReset()
+    {
+        $reset = false;
+        if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
+            $notes = $this->getNotes();
+            $notes->deleteAll();
+            $notes->deleteSyncData();
+            $reset = true;
+        }
+
+        return $this->database($reset);
+    }
+
+    /**
+     * Register some variables that templates will probably need.
+     *
+     * @return void
+     */
+    protected function addGlobalVars(TemplateResponse $res)
+    {
+        $params = $res->getParams();
+        $params['date']   = \OC::$server->getDateTimeFormatter();
+        $params['urlGen'] = \OC::$server->getURLGenerator();
+        $res->setParams($params);
+    }
+
     protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
     {
         $nav = new \OCP\Template('grauphel', 'appnavigation', '');
         $nav->assign('apiroot', $this->getApiRootUrl());
+        $nav->assign('tags', array());
 
         $params = $res->getParams();
         $params['appNavigation'] = $nav;
@@ -108,15 +313,23 @@ class GuiController extends Controller
 
         $rawtags = $this->getNotes()->getTags();
         sort($rawtags);
+        array_unshift(
+            $rawtags,
+            'grauphel:special:all', 'grauphel:special:untagged'
+        );
+
         $tags = array();
         foreach ($rawtags as $rawtag) {
-            if (substr($rawtag, 0, 16) == 'system:notebook:') {
+            $name = $this->getPrettyTagName($rawtag);
+            if ($name !== false) {
                 $tags[] = array(
-                    'name' => substr($rawtag, 16),
+                    'name' => $name,
                     'id'   => $rawtag,
                     'href' => $this->urlGen->linkToRoute(
-                        'grauphel.gui.tag', array('rawtag' => $rawtag)
+                        'grauphel.gui.tag',
+                        array('rawtag' => $this->escapeTagForUrl($rawtag))
                     ),
+                    'selected' => $rawtag == $selectedRawtag,
                 );
             }
         }
@@ -146,7 +359,7 @@ class GuiController extends Controller
     protected function checkDeps()
     {
         if (!class_exists('OAuthProvider')) {
-            throw new \Exception('PHP extension "oauth" is required');
+            throw new \Exception('PHP extension "oauth" is required', 1001);
         }
     }
 
@@ -168,5 +381,25 @@ class GuiController extends Controller
         $notes->setUsername($username);
         return $notes;
     }
+
+    protected function getPrettyTagName($rawtag)
+    {
+        if (substr($rawtag, 0, 16) == 'system:notebook:') {
+            return substr($rawtag, 16);
+        } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
+            return '*' . substr($rawtag, 17) . '*';
+        }
+        return false;
+    }
+
+    protected function escapeTagForUrl($rawtag)
+    {
+        return str_replace('/', '%2F', $rawtag);
+    }
+
+    protected function unescapeTagFromUrl($rawtag)
+    {
+        return str_replace('%2F', '/', $rawtag);
+    }
 }
 ?>