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
14 namespace OCA\Grauphel\Controller;
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;
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
33 class GuiController extends Controller
36 * constructor of the controller
38 * @param string $appName Name of the app
39 * @param IRequest $request Instance of the request
41 public function __construct($appName, \OCP\IRequest $request, $user, $urlGen)
43 parent::__construct($appName, $request);
45 $this->urlGen = $urlGen;
47 //default http header: we assume something is broken
48 header('HTTP/1.0 500 Internal Server Error');
54 * Tomdroid wants this to be a public page. Sync fails otherwise.
60 public function index()
64 $res = new TemplateResponse('grauphel', 'index');
67 'apiroot' => $this->getApiRootUrl(),
68 'apiurl' => $this->urlGen->linkToRoute('grauphel.api.index')
71 $this->addNavigation($res);
72 $this->addStats($res);
77 * Show contents of a note
82 public function note($guid)
84 $res = new TemplateResponse('grauphel', 'gui-note');
86 $note = $this->getNotes()->load($guid, false);
88 return new ErrorResponse('Note does not exist');
91 $converter = new \OCA\Grauphel\Converter\Html();
92 $converter->internalLinkHandler = array($this, 'noteLinkHandler');
95 $contentHtml = $converter->convert($note->{'note-content'});
96 } catch (\OCA\Grauphel\Converter\Exception $e) {
97 $contentHtml = '<div class="error">'
98 . '<p>There was an error converting the note to HTML:</p>'
99 . '<blockquote><tt>' . htmlspecialchars($e->getMessage()) . '</tt></blockquote>'
100 . '<p>Please open a bug report at'
101 . ' <a class="lined" href="http://github.com/cweiske/grauphel/issues">'
102 . 'github.com/cweiske/grauphel/issues</a>'
103 . ' and attach the XML version of the note.'
110 'note-content' => $contentHtml,
112 'json' => $this->urlGen->linkToRoute(
113 'grauphel.api.note', array(
114 'guid' => $guid, 'username' => $this->user->getUid()
117 'xml' => $this->urlGen->linkToRoute(
118 'grauphel.notes.xml', array('guid' => $guid)
124 $selectedRawtag = 'grauphel:special:untagged';
125 if (count($note->tags) > 0) {
126 $selectedRawtag = $note->tags[0];
129 $this->addNavigation($res, $selectedRawtag);
133 public function noteLinkHandler($noteTitle)
135 $guid = $this->getNotes()->loadGuidByTitle($noteTitle);
136 if ($guid === null) {
139 return $this->urlGen->linkToRoute(
140 'grauphel.gui.note', array('guid' => $guid)
145 * Show all notes of a tag
150 public function tag($rawtag)
152 $notes = $this->getNotes()->loadNotesOverview(null, $rawtag, true);
155 function($noteA, $noteB) {
156 return strcmp($noteA['title'], $noteB['title']);
160 foreach ($notes as &$note) {
161 $diffInDays = intval(
162 (time() - strtotime($note['last-change-date'])) / 86400
164 $value = 0 + $diffInDays;
168 $note['dateColor'] = '#' . str_repeat(sprintf('%02X', $value), 3);
171 $res = new TemplateResponse('grauphel', 'tag');
174 'tag' => $this->getPrettyTagName($rawtag),
179 $this->addNavigation($res, $rawtag);
190 public function tokens()
192 $tokens = new TokenStorage();
193 $res = new TemplateResponse('grauphel', 'tokens');
196 'tokens' => $tokens->loadForUser(
197 $this->user->getUid(), 'access'
199 'client' => new Client(),
200 'username' => $this->user->getUid(),
203 $this->addNavigation($res, null);
209 * Allow the user to clear his database
214 public function database($reset = null)
216 $res = new TemplateResponse('grauphel', 'gui-database');
217 $res->setParams(array('reset' => $reset));
218 $this->addNavigation($res, null);
219 $this->addStats($res);
225 * Resets the database by deleting all notes and deleting the user's
230 public function databaseReset()
233 if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
234 $notes = $this->getNotes();
236 $notes->deleteSyncData();
240 return $this->database($reset);
243 protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
245 $nav = new \OCP\Template('grauphel', 'appnavigation', '');
246 $nav->assign('apiroot', $this->getApiRootUrl());
247 $nav->assign('tags', array());
249 $params = $res->getParams();
250 $params['appNavigation'] = $nav;
251 $res->setParams($params);
253 if ($this->user === null) {
257 $rawtags = $this->getNotes()->getTags();
261 'grauphel:special:all', 'grauphel:special:untagged'
265 foreach ($rawtags as $rawtag) {
266 $name = $this->getPrettyTagName($rawtag);
267 if ($name !== false) {
271 'href' => $this->urlGen->linkToRoute(
272 'grauphel.gui.tag', array('rawtag' => $rawtag)
274 'selected' => $rawtag == $selectedRawtag,
278 $nav->assign('tags', $tags);
281 protected function addStats(TemplateResponse $res)
283 if ($this->user === null) {
287 $username = $this->user->getUid();
288 $notes = $this->getNotes();
289 $tokens = new \OCA\Grauphel\Lib\TokenStorage();
291 $nav = new \OCP\Template('grauphel', 'indexStats', '');
292 $nav->assign('notes', count($notes->loadNotesOverview()));
293 $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
294 $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
296 $params = $res->getParams();
297 $params['stats'] = $nav;
298 $res->setParams($params);
301 protected function checkDeps()
303 if (!class_exists('OAuthProvider')) {
304 throw new \Exception('PHP extension "oauth" is required');
308 protected function getApiRootUrl()
310 //we need to remove the trailing / for tomdroid and conboy
312 $this->urlGen->getAbsoluteURL(
313 $this->urlGen->linkToRoute('grauphel.gui.index')
319 protected function getNotes()
321 $username = $this->user->getUid();
322 $notes = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
323 $notes->setUsername($username);
327 protected function getPrettyTagName($rawtag)
329 if (substr($rawtag, 0, 16) == 'system:notebook:') {
330 return substr($rawtag, 16);
331 } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
332 return '*' . substr($rawtag, 17) . '*';