note preview
[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         $this->checkDeps();
63
64         $res = new TemplateResponse('grauphel', 'index');
65         $res->setParams(
66             array(
67                 'apiroot' => $this->getApiRootUrl(),
68                 'apiurl'  => $this->urlGen->linkToRoute('grauphel.api.index')
69             )
70         );
71         $this->addNavigation($res);
72         $this->addStats($res);
73         return $res;
74     }
75
76     /**
77      * Show contents of a note
78      *
79      * @NoAdminRequired
80      * @NoCSRFRequired
81      */
82     public function note($guid)
83     {
84         $res = new TemplateResponse('grauphel', 'gui-note');
85
86         $note = $this->getNotes()->load($guid, false);
87         if ($note === null) {
88             return new ErrorResponse('Note does not exist');
89         }
90
91         $converter = new \OCA\Grauphel\Lib\Converter\Html();
92         $converter->internalLinkHandler = array($this, 'noteLinkHandler');
93         $res->setParams(
94             array(
95                 'note' => $note,
96                 'note-content' => $converter->convert(
97                     $note->{'note-content'}
98                 ),
99             )
100         );
101
102         $this->addNavigation($res);
103         return $res;
104     }
105
106     public function noteLinkHandler($noteTitle)
107     {
108         $guid = $this->getNotes()->loadGuidByTitle($noteTitle);
109         if ($guid === null) {
110             return '#';
111         }
112         return $this->urlGen->linkToRoute(
113             'grauphel.gui.note', array('guid' => $guid)
114         );
115     }
116
117     /**
118      * Show all notes of a tag
119      *
120      * @NoAdminRequired
121      * @NoCSRFRequired
122      */
123     public function tag($rawtag)
124     {
125         $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
126         usort(
127             $notes,
128             function($noteA, $noteB) {
129                 return strcmp($noteA['title'], $noteB['title']);
130             }
131         );
132
133         $res = new TemplateResponse('grauphel', 'tag');
134         $res->setParams(
135             array(
136                 'tag'    => $this->getPrettyTagName($rawtag),
137                 'rawtag' => $rawtag,
138                 'notes'  => $notes,
139             )
140         );
141         $this->addNavigation($res, $rawtag);
142
143         return $res;
144     }
145
146     /**
147      * Show access tokens
148      *
149      * @NoAdminRequired
150      * @NoCSRFRequired
151      */
152     public function tokens()
153     {
154         $tokens = new TokenStorage();
155         $res = new TemplateResponse('grauphel', 'tokens');
156         $res->setParams(
157             array(
158                 'tokens' => $tokens->loadForUser(
159                     $this->user->getUid(), 'access'
160                 ),
161                 'client' => new Client(),
162                 'username' => $this->user->getUid(),
163             )
164         );
165         $this->addNavigation($res, null);
166
167         return $res;
168     }
169
170     /**
171      * Allow the user to clear his database
172      *
173      * @NoAdminRequired
174      * @NoCSRFRequired
175      */
176     public function database($reset = null)
177     {
178         $res = new TemplateResponse('grauphel', 'gui-database');
179         $res->setParams(array('reset' => $reset));
180         $this->addNavigation($res, null);
181         $this->addStats($res);
182
183         return $res;
184     }
185
186     /**
187      * Resets the database by deleting all notes and deleting the user's
188      * sync data.
189      *
190      * @NoAdminRequired
191      */
192     public function databaseReset()
193     {
194         $reset = false;
195         if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
196             $notes = $this->getNotes();
197             $notes->deleteAll();
198             $notes->deleteSyncData();
199             $reset = true;
200         }
201
202         return $this->database($reset);
203     }
204
205     protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
206     {
207         $nav = new \OCP\Template('grauphel', 'appnavigation', '');
208         $nav->assign('apiroot', $this->getApiRootUrl());
209         $nav->assign('tags', array());
210
211         $params = $res->getParams();
212         $params['appNavigation'] = $nav;
213         $res->setParams($params);
214
215         if ($this->user === null) {
216             return;
217         }
218
219         $rawtags = $this->getNotes()->getTags();
220         sort($rawtags);
221         array_unshift(
222             $rawtags,
223             'grauphel:special:all', 'grauphel:special:untagged'
224         );
225
226         $tags = array();
227         foreach ($rawtags as $rawtag) {
228             $name = $this->getPrettyTagName($rawtag);
229             if ($name !== false) {
230                 $tags[] = array(
231                     'name' => $name,
232                     'id'   => $rawtag,
233                     'href' => $this->urlGen->linkToRoute(
234                         'grauphel.gui.tag', array('rawtag' => $rawtag)
235                     ),
236                 );
237             }
238         }
239         $nav->assign('tags', $tags);
240     }
241
242     protected function addStats(TemplateResponse $res)
243     {
244         if ($this->user === null) {
245             return;
246         }
247
248         $username = $this->user->getUid();
249         $notes  = $this->getNotes();
250         $tokens = new \OCA\Grauphel\Lib\TokenStorage();
251
252         $nav = new \OCP\Template('grauphel', 'indexStats', '');
253         $nav->assign('notes', count($notes->loadNotesOverview()));
254         $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
255         $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
256
257         $params = $res->getParams();
258         $params['stats'] = $nav;
259         $res->setParams($params);
260     }
261
262     protected function checkDeps()
263     {
264         if (!class_exists('OAuthProvider')) {
265             throw new \Exception('PHP extension "oauth" is required');
266         }
267     }
268
269     protected function getApiRootUrl()
270     {
271         //we need to remove the trailing / for tomdroid and conboy
272         return rtrim(
273             $this->urlGen->getAbsoluteURL(
274                 $this->urlGen->linkToRoute('grauphel.gui.index')
275             ),
276             '/'
277         );
278     }
279
280     protected function getNotes()
281     {
282         $username = $this->user->getUid();
283         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
284         $notes->setUsername($username);
285         return $notes;
286     }
287
288     protected function getPrettyTagName($rawtag)
289     {
290         if (substr($rawtag, 0, 16) == 'system:notebook:') {
291             return substr($rawtag, 16);
292         } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
293             return '*' . substr($rawtag, 17) . '*';
294         }
295         return false;
296     }
297 }
298 ?>