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