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'}
100 'json' => $this->urlGen->linkToRoute(
101 'grauphel.api.note', array(
102 'guid' => $guid, 'username' => $this->user->getUid()
105 'xml' => $this->urlGen->linkToRoute(
106 'grauphel.notes.xml', array('guid' => $guid)
112 $selectedRawtag = null;
113 if (count($note->tags) > 0) {
114 $selectedRawtag = $note->tags[0];
117 $this->addNavigation($res, $selectedRawtag);
121 public function noteLinkHandler($noteTitle)
123 $guid = $this->getNotes()->loadGuidByTitle($noteTitle);
124 if ($guid === null) {
127 return $this->urlGen->linkToRoute(
128 'grauphel.gui.note', array('guid' => $guid)
133 * Show all notes of a tag
138 public function tag($rawtag)
140 $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
143 function($noteA, $noteB) {
144 return strcmp($noteA['title'], $noteB['title']);
148 $res = new TemplateResponse('grauphel', 'tag');
151 'tag' => $this->getPrettyTagName($rawtag),
156 $this->addNavigation($res, $rawtag);
167 public function tokens()
169 $tokens = new TokenStorage();
170 $res = new TemplateResponse('grauphel', 'tokens');
173 'tokens' => $tokens->loadForUser(
174 $this->user->getUid(), 'access'
176 'client' => new Client(),
177 'username' => $this->user->getUid(),
180 $this->addNavigation($res, null);
186 * Allow the user to clear his database
191 public function database($reset = null)
193 $res = new TemplateResponse('grauphel', 'gui-database');
194 $res->setParams(array('reset' => $reset));
195 $this->addNavigation($res, null);
196 $this->addStats($res);
202 * Resets the database by deleting all notes and deleting the user's
207 public function databaseReset()
210 if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
211 $notes = $this->getNotes();
213 $notes->deleteSyncData();
217 return $this->database($reset);
220 protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
222 $nav = new \OCP\Template('grauphel', 'appnavigation', '');
223 $nav->assign('apiroot', $this->getApiRootUrl());
224 $nav->assign('tags', array());
226 $params = $res->getParams();
227 $params['appNavigation'] = $nav;
228 $res->setParams($params);
230 if ($this->user === null) {
234 $rawtags = $this->getNotes()->getTags();
238 'grauphel:special:all', 'grauphel:special:untagged'
242 foreach ($rawtags as $rawtag) {
243 $name = $this->getPrettyTagName($rawtag);
244 if ($name !== false) {
248 'href' => $this->urlGen->linkToRoute(
249 'grauphel.gui.tag', array('rawtag' => $rawtag)
251 'selected' => $rawtag == $selectedRawtag,
255 $nav->assign('tags', $tags);
258 protected function addStats(TemplateResponse $res)
260 if ($this->user === null) {
264 $username = $this->user->getUid();
265 $notes = $this->getNotes();
266 $tokens = new \OCA\Grauphel\Lib\TokenStorage();
268 $nav = new \OCP\Template('grauphel', 'indexStats', '');
269 $nav->assign('notes', count($notes->loadNotesOverview()));
270 $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
271 $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
273 $params = $res->getParams();
274 $params['stats'] = $nav;
275 $res->setParams($params);
278 protected function checkDeps()
280 if (!class_exists('OAuthProvider')) {
281 throw new \Exception('PHP extension "oauth" is required');
285 protected function getApiRootUrl()
287 //we need to remove the trailing / for tomdroid and conboy
289 $this->urlGen->getAbsoluteURL(
290 $this->urlGen->linkToRoute('grauphel.gui.index')
296 protected function getNotes()
298 $username = $this->user->getUid();
299 $notes = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
300 $notes->setUsername($username);
304 protected function getPrettyTagName($rawtag)
306 if (substr($rawtag, 0, 16) == 'system:notebook:') {
307 return substr($rawtag, 16);
308 } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
309 return '*' . substr($rawtag, 17) . '*';