Lists can be sorted by title and date now
[grauphel.git] / controller / guicontroller.php
1 <?php
2 /**
3  * Part of grauphel
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Grauphel
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/grauphel.htm
13  */
14 namespace OCA\Grauphel\Controller;
15
16 use \OCP\AppFramework\Controller;
17 use \OCP\AppFramework\Http\TemplateResponse;
18 use \OCA\Grauphel\Lib\Client;
19 use \OCA\Grauphel\Lib\TokenStorage;
20 use \OCA\Grauphel\Lib\Response\ErrorResponse;
21
22 /**
23  * Owncloud frontend
24  *
25  * @category  Tools
26  * @package   Grauphel
27  * @author    Christian Weiske <cweiske@cweiske.de>
28  * @copyright 2014 Christian Weiske
29  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
30  * @version   Release: @package_version@
31  * @link      http://cweiske.de/grauphel.htm
32  */
33 class GuiController extends Controller
34 {
35     /**
36      * constructor of the controller
37      *
38      * @param string   $appName Name of the app
39      * @param IRequest $request Instance of the request
40      */
41     public function __construct($appName, \OCP\IRequest $request, $user, $urlGen)
42     {
43         parent::__construct($appName, $request);
44         $this->user   = $user;
45         $this->urlGen = $urlGen;
46
47         //default http header: we assume something is broken
48         header('HTTP/1.0 500 Internal Server Error');
49     }
50
51     /**
52      * Main page /
53      *
54      * Tomdroid wants this to be a public page. Sync fails otherwise.
55      *
56      * @NoAdminRequired
57      * @NoCSRFRequired
58      * @PublicPage
59      */
60     public function index()
61     {
62         try {
63             $this->checkDeps();
64         } catch (\Exception $e) {
65             $res = new TemplateResponse('grauphel', 'error');
66             $res->setParams(
67                 array(
68                     'message' => $e->getMessage(),
69                     'code' => $e->getCode(),
70                 )
71             );
72             return $res;
73         }
74
75         $res = new TemplateResponse('grauphel', 'index');
76         $res->setParams(
77             array(
78                 'apiroot' => $this->getApiRootUrl(),
79                 'apiurl'  => $this->urlGen->linkToRoute('grauphel.api.index')
80             )
81         );
82         $this->addNavigation($res);
83         $this->addStats($res);
84         return $res;
85     }
86
87     /**
88      * Show contents of a note
89      *
90      * @NoAdminRequired
91      * @NoCSRFRequired
92      */
93     public function note($guid)
94     {
95         $res = new TemplateResponse('grauphel', 'gui-note');
96
97         $note = $this->getNotes()->load($guid, false);
98         if ($note === null) {
99             $res = new ErrorResponse('Note does not exist');
100             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
101             return $res;
102         }
103
104         $converter = new \OCA\Grauphel\Converter\Html();
105         $converter->internalLinkHandler = array($this, 'noteLinkHandler');
106
107         try {
108             $contentHtml = $converter->convert($note->{'note-content'});
109         } catch (\OCA\Grauphel\Converter\Exception $e) {
110             $contentHtml = '<div class="error">'
111                 . '<p>There was an error converting the note to HTML:</p>'
112                 . '<blockquote><tt>' . htmlspecialchars($e->getMessage()) . '</tt></blockquote>'
113                 . '<p>Please open a bug report at'
114                 . ' <a class="lined" href="http://github.com/cweiske/grauphel/issues">'
115                 . 'github.com/cweiske/grauphel/issues</a>'
116                 . ' and attach the XML version of the note.'
117                 . '</div>';
118         }
119
120         $res->setParams(
121             array(
122                 'note' => $note,
123                 'note-content' => $contentHtml,
124                 'links' => array(
125                     'html' => $this->urlGen->linkToRoute(
126                         'grauphel.notes.html', array('guid' => $guid)
127                     ),
128                     'json' => $this->urlGen->linkToRoute(
129                         'grauphel.api.note', array(
130                             'guid' => $guid, 'username' => $this->user->getUid()
131                         )
132                     ),
133                     'text' => $this->urlGen->linkToRoute(
134                         'grauphel.notes.text', array('guid' => $guid)
135                     ),
136                     'xml' => $this->urlGen->linkToRoute(
137                         'grauphel.notes.xml', array('guid' => $guid)
138                     ),
139                 )
140             )
141         );
142
143         $selectedRawtag = 'grauphel:special:untagged';
144         if (count($note->tags) > 0) {
145             $selectedRawtag = $note->tags[0];
146         }
147
148         $this->addNavigation($res, $selectedRawtag);
149         $this->addGlobalVars($res);
150         return $res;
151     }
152
153     public function noteLinkHandler($noteTitle)
154     {
155         $guid = $this->getNotes()->loadGuidByTitle($noteTitle);
156         if ($guid === null) {
157             return '#';
158         }
159         return $this->urlGen->linkToRoute(
160             'grauphel.gui.note', array('guid' => $guid)
161         );
162     }
163
164     /**
165      * Show all notes of a tag
166      *
167      * @NoAdminRequired
168      * @NoCSRFRequired
169      */
170     public function tag($rawtag)
171     {
172         $rawtag = $this->unescapeTagFromUrl($rawtag);
173         $notes = $this->getNotes()->loadNotesOverview(null, $rawtag, true);
174
175         if (!isset($_GET['sortby'])) {
176             $_GET['sortby'] = 'title';
177         }
178
179         switch ($_GET['sortby']) {
180         case 'title':
181             usort(
182                 $notes,
183                 function($noteA, $noteB) {
184                     return strcasecmp($noteA['title'], $noteB['title']);
185                 }
186             );
187             break;
188         case 'date':
189             usort(
190                 $notes,
191                 function($noteA, $noteB) {
192                     return strcmp($noteB['last-change-date'], $noteA['last-change-date']);
193                 }
194             );
195             break;
196         }
197
198         foreach ($notes as &$note) {
199             $diffInDays = intval(
200                 (time() - strtotime($note['last-change-date'])) / 86400
201             );
202             $value = 0 + $diffInDays;
203             if ($value > 160) {
204                 $value = 160;
205             }
206             $note['dateColor'] = '#' . str_repeat(sprintf('%02X', $value), 3);
207         }
208
209         $res = new TemplateResponse('grauphel', 'tag');
210         $res->setParams(
211             array(
212                 'tag'    => $this->getPrettyTagName($rawtag),
213                 'rawtag' => $rawtag,
214                 'notes'  => $notes,
215                 'tagUrl' => $this->urlGen->linkToRoute(
216                     'grauphel.gui.tag',
217                     array('rawtag' => $this->escapeTagForUrl($rawtag))
218                 ),
219             )
220         );
221         $this->addGlobalVars($res);
222         $this->addNavigation($res, $rawtag);
223
224         return $res;
225     }
226
227     /**
228      * Show access tokens
229      *
230      * @NoAdminRequired
231      * @NoCSRFRequired
232      */
233     public function tokens()
234     {
235         $tokens = new TokenStorage();
236         $res = new TemplateResponse('grauphel', 'tokens');
237         $res->setParams(
238             array(
239                 'tokens' => $tokens->loadForUser(
240                     $this->user->getUid(), 'access'
241                 ),
242                 'client' => new Client(),
243                 'username' => $this->user->getUid(),
244             )
245         );
246         $this->addGlobalVars($res);
247         $this->addNavigation($res, null);
248
249         return $res;
250     }
251
252     /**
253      * Allow the user to clear his database
254      *
255      * @NoAdminRequired
256      * @NoCSRFRequired
257      */
258     public function database($reset = null)
259     {
260         $res = new TemplateResponse('grauphel', 'gui-database');
261         $res->setParams(array('reset' => $reset));
262         $this->addNavigation($res, null);
263         $this->addStats($res);
264
265         return $res;
266     }
267
268     /**
269      * Resets the database by deleting all notes and deleting the user's
270      * sync data.
271      *
272      * @NoAdminRequired
273      */
274     public function databaseReset()
275     {
276         $reset = false;
277         if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
278             $notes = $this->getNotes();
279             $notes->deleteAll();
280             $notes->deleteSyncData();
281             $reset = true;
282         }
283
284         return $this->database($reset);
285     }
286
287     /**
288      * Register some variables that templates will probably need.
289      *
290      * @return void
291      */
292     protected function addGlobalVars(TemplateResponse $res)
293     {
294         $params = $res->getParams();
295         $params['date']   = \OC::$server->getDateTimeFormatter();
296         $params['urlGen'] = \OC::$server->getURLGenerator();
297         $res->setParams($params);
298     }
299
300     protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
301     {
302         $nav = new \OCP\Template('grauphel', 'appnavigation', '');
303         $nav->assign('apiroot', $this->getApiRootUrl());
304         $nav->assign('tags', array());
305
306         $params = $res->getParams();
307         $params['appNavigation'] = $nav;
308         $res->setParams($params);
309
310         if ($this->user === null) {
311             return;
312         }
313
314         $rawtags = $this->getNotes()->getTags();
315         sort($rawtags);
316         array_unshift(
317             $rawtags,
318             'grauphel:special:all', 'grauphel:special:untagged'
319         );
320
321         $tags = array();
322         foreach ($rawtags as $rawtag) {
323             $name = $this->getPrettyTagName($rawtag);
324             if ($name !== false) {
325                 $tags[] = array(
326                     'name' => $name,
327                     'id'   => $rawtag,
328                     'href' => $this->urlGen->linkToRoute(
329                         'grauphel.gui.tag',
330                         array('rawtag' => $this->escapeTagForUrl($rawtag))
331                     ),
332                     'selected' => $rawtag == $selectedRawtag,
333                 );
334             }
335         }
336         $nav->assign('tags', $tags);
337     }
338
339     protected function addStats(TemplateResponse $res)
340     {
341         if ($this->user === null) {
342             return;
343         }
344
345         $username = $this->user->getUid();
346         $notes  = $this->getNotes();
347         $tokens = new \OCA\Grauphel\Lib\TokenStorage();
348
349         $nav = new \OCP\Template('grauphel', 'indexStats', '');
350         $nav->assign('notes', count($notes->loadNotesOverview()));
351         $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
352         $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
353
354         $params = $res->getParams();
355         $params['stats'] = $nav;
356         $res->setParams($params);
357     }
358
359     protected function checkDeps()
360     {
361         if (!class_exists('OAuthProvider')) {
362             throw new \Exception('PHP extension "oauth" is required', 1001);
363         }
364     }
365
366     protected function getApiRootUrl()
367     {
368         //we need to remove the trailing / for tomdroid and conboy
369         return rtrim(
370             $this->urlGen->getAbsoluteURL(
371                 $this->urlGen->linkToRoute('grauphel.gui.index')
372             ),
373             '/'
374         );
375     }
376
377     protected function getNotes()
378     {
379         $username = $this->user->getUid();
380         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
381         $notes->setUsername($username);
382         return $notes;
383     }
384
385     protected function getPrettyTagName($rawtag)
386     {
387         if (substr($rawtag, 0, 16) == 'system:notebook:') {
388             return substr($rawtag, 16);
389         } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
390             return '*' . substr($rawtag, 17) . '*';
391         }
392         return false;
393     }
394
395     protected function escapeTagForUrl($rawtag)
396     {
397         return str_replace('/', '%2F', $rawtag);
398     }
399
400     protected function unescapeTagFromUrl($rawtag)
401     {
402         return str_replace('%2F', '/', $rawtag);
403     }
404 }
405 ?>