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 } catch (\Exception $e) {
65 $res = new TemplateResponse('grauphel', 'error');
68 'message' => $e->getMessage(),
69 'code' => $e->getCode(),
75 $res = new TemplateResponse('grauphel', 'index');
78 'apiroot' => $this->getApiRootUrl(),
79 'apiurl' => $this->urlGen->linkToRoute('grauphel.api.index')
82 $this->addNavigation($res);
83 $this->addStats($res);
88 * Show contents of a note
93 public function note($guid)
95 $res = new TemplateResponse('grauphel', 'gui-note');
97 $note = $this->getNotes()->load($guid, false);
99 $res = new ErrorResponse('Note does not exist');
100 $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
104 $converter = new \OCA\Grauphel\Converter\Html();
105 $converter->internalLinkHandler = array($this, 'noteLinkHandler');
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.'
123 'note-content' => $contentHtml,
125 'html' => $this->urlGen->linkToRoute(
126 'grauphel.notes.html', array('guid' => $guid)
128 'json' => $this->urlGen->linkToRoute(
129 'grauphel.api.note', array(
130 'guid' => $guid, 'username' => $this->user->getUid()
133 'text' => $this->urlGen->linkToRoute(
134 'grauphel.notes.text', array('guid' => $guid)
136 'xml' => $this->urlGen->linkToRoute(
137 'grauphel.notes.xml', array('guid' => $guid)
143 $selectedRawtag = 'grauphel:special:untagged';
144 if (count($note->tags) > 0) {
145 $selectedRawtag = $note->tags[0];
148 $this->addNavigation($res, $selectedRawtag);
149 $this->addGlobalVars($res);
153 public function noteLinkHandler($noteTitle)
155 $guid = $this->getNotes()->loadGuidByTitle($noteTitle);
156 if ($guid === null) {
159 return $this->urlGen->linkToRoute(
160 'grauphel.gui.note', array('guid' => $guid)
165 * Show all notes of a tag
170 public function tag($rawtag)
172 $rawtag = $this->unescapeTagFromUrl($rawtag);
173 $notes = $this->getNotes()->loadNotesOverview(null, $rawtag, true);
176 function($noteA, $noteB) {
177 return strcmp($noteA['title'], $noteB['title']);
181 foreach ($notes as &$note) {
182 $diffInDays = intval(
183 (time() - strtotime($note['last-change-date'])) / 86400
185 $value = 0 + $diffInDays;
189 $note['dateColor'] = '#' . str_repeat(sprintf('%02X', $value), 3);
192 $res = new TemplateResponse('grauphel', 'tag');
195 'tag' => $this->getPrettyTagName($rawtag),
200 $this->addGlobalVars($res);
201 $this->addNavigation($res, $rawtag);
212 public function tokens()
214 $tokens = new TokenStorage();
215 $res = new TemplateResponse('grauphel', 'tokens');
218 'tokens' => $tokens->loadForUser(
219 $this->user->getUid(), 'access'
221 'client' => new Client(),
222 'username' => $this->user->getUid(),
225 $this->addGlobalVars($res);
226 $this->addNavigation($res, null);
232 * Allow the user to clear his database
237 public function database($reset = null)
239 $res = new TemplateResponse('grauphel', 'gui-database');
240 $res->setParams(array('reset' => $reset));
241 $this->addNavigation($res, null);
242 $this->addStats($res);
248 * Resets the database by deleting all notes and deleting the user's
253 public function databaseReset()
256 if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
257 $notes = $this->getNotes();
259 $notes->deleteSyncData();
263 return $this->database($reset);
267 * Register some variables that templates will probably need.
271 protected function addGlobalVars(TemplateResponse $res)
273 $params = $res->getParams();
274 $params['date'] = \OC::$server->getDateTimeFormatter();
275 $params['urlGen'] = \OC::$server->getURLGenerator();
276 $res->setParams($params);
279 protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
281 $nav = new \OCP\Template('grauphel', 'appnavigation', '');
282 $nav->assign('apiroot', $this->getApiRootUrl());
283 $nav->assign('tags', array());
285 $params = $res->getParams();
286 $params['appNavigation'] = $nav;
287 $res->setParams($params);
289 if ($this->user === null) {
293 $rawtags = $this->getNotes()->getTags();
297 'grauphel:special:all', 'grauphel:special:untagged'
301 foreach ($rawtags as $rawtag) {
302 $name = $this->getPrettyTagName($rawtag);
303 if ($name !== false) {
307 'href' => $this->urlGen->linkToRoute(
309 array('rawtag' => $this->escapeTagForUrl($rawtag))
311 'selected' => $rawtag == $selectedRawtag,
315 $nav->assign('tags', $tags);
318 protected function addStats(TemplateResponse $res)
320 if ($this->user === null) {
324 $username = $this->user->getUid();
325 $notes = $this->getNotes();
326 $tokens = new \OCA\Grauphel\Lib\TokenStorage();
328 $nav = new \OCP\Template('grauphel', 'indexStats', '');
329 $nav->assign('notes', count($notes->loadNotesOverview()));
330 $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
331 $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
333 $params = $res->getParams();
334 $params['stats'] = $nav;
335 $res->setParams($params);
338 protected function checkDeps()
340 if (!class_exists('OAuthProvider')) {
341 throw new \Exception('PHP extension "oauth" is required', 1001);
345 protected function getApiRootUrl()
347 //we need to remove the trailing / for tomdroid and conboy
349 $this->urlGen->getAbsoluteURL(
350 $this->urlGen->linkToRoute('grauphel.gui.index')
356 protected function getNotes()
358 $username = $this->user->getUid();
359 $notes = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
360 $notes->setUsername($username);
364 protected function getPrettyTagName($rawtag)
366 if (substr($rawtag, 0, 16) == 'system:notebook:') {
367 return substr($rawtag, 16);
368 } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
369 return '*' . substr($rawtag, 17) . '*';
374 protected function escapeTagForUrl($rawtag)
376 return str_replace('/', '%2F', $rawtag);
379 protected function unescapeTagFromUrl($rawtag)
381 return str_replace('%2F', '/', $rawtag);