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\Lib\Converter\Html();
92 $converter->internalLinkHandler = array($this, 'noteLinkHandler');
96 'note-content' => $converter->convert(
97 $note->{'note-content'}
102 $selectedRawtag = null;
103 if (count($note->tags) > 0) {
104 $selectedRawtag = $note->tags[0];
107 $this->addNavigation($res, $selectedRawtag);
111 public function noteLinkHandler($noteTitle)
113 $guid = $this->getNotes()->loadGuidByTitle($noteTitle);
114 if ($guid === null) {
117 return $this->urlGen->linkToRoute(
118 'grauphel.gui.note', array('guid' => $guid)
123 * Show all notes of a tag
128 public function tag($rawtag)
130 $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
133 function($noteA, $noteB) {
134 return strcmp($noteA['title'], $noteB['title']);
138 $res = new TemplateResponse('grauphel', 'tag');
141 'tag' => $this->getPrettyTagName($rawtag),
146 $this->addNavigation($res, $rawtag);
157 public function tokens()
159 $tokens = new TokenStorage();
160 $res = new TemplateResponse('grauphel', 'tokens');
163 'tokens' => $tokens->loadForUser(
164 $this->user->getUid(), 'access'
166 'client' => new Client(),
167 'username' => $this->user->getUid(),
170 $this->addNavigation($res, null);
176 * Allow the user to clear his database
181 public function database($reset = null)
183 $res = new TemplateResponse('grauphel', 'gui-database');
184 $res->setParams(array('reset' => $reset));
185 $this->addNavigation($res, null);
186 $this->addStats($res);
192 * Resets the database by deleting all notes and deleting the user's
197 public function databaseReset()
200 if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
201 $notes = $this->getNotes();
203 $notes->deleteSyncData();
207 return $this->database($reset);
210 protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
212 $nav = new \OCP\Template('grauphel', 'appnavigation', '');
213 $nav->assign('apiroot', $this->getApiRootUrl());
214 $nav->assign('tags', array());
216 $params = $res->getParams();
217 $params['appNavigation'] = $nav;
218 $res->setParams($params);
220 if ($this->user === null) {
224 $rawtags = $this->getNotes()->getTags();
228 'grauphel:special:all', 'grauphel:special:untagged'
232 foreach ($rawtags as $rawtag) {
233 $name = $this->getPrettyTagName($rawtag);
234 if ($name !== false) {
238 'href' => $this->urlGen->linkToRoute(
239 'grauphel.gui.tag', array('rawtag' => $rawtag)
241 'selected' => $rawtag == $selectedRawtag,
245 $nav->assign('tags', $tags);
248 protected function addStats(TemplateResponse $res)
250 if ($this->user === null) {
254 $username = $this->user->getUid();
255 $notes = $this->getNotes();
256 $tokens = new \OCA\Grauphel\Lib\TokenStorage();
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')));
263 $params = $res->getParams();
264 $params['stats'] = $nav;
265 $res->setParams($params);
268 protected function checkDeps()
270 if (!class_exists('OAuthProvider')) {
271 throw new \Exception('PHP extension "oauth" is required');
275 protected function getApiRootUrl()
277 //we need to remove the trailing / for tomdroid and conboy
279 $this->urlGen->getAbsoluteURL(
280 $this->urlGen->linkToRoute('grauphel.gui.index')
286 protected function getNotes()
288 $username = $this->user->getUid();
289 $notes = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
290 $notes->setUsername($username);
294 protected function getPrettyTagName($rawtag)
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) . '*';