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 $this->addNavigation($res);
106 public function noteLinkHandler($noteTitle)
108 $guid = $this->getNotes()->loadGuidByTitle($noteTitle);
109 if ($guid === null) {
112 return $this->urlGen->linkToRoute(
113 'grauphel.gui.note', array('guid' => $guid)
118 * Show all notes of a tag
123 public function tag($rawtag)
125 $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
128 function($noteA, $noteB) {
129 return strcmp($noteA['title'], $noteB['title']);
133 $res = new TemplateResponse('grauphel', 'tag');
136 'tag' => $this->getPrettyTagName($rawtag),
141 $this->addNavigation($res, $rawtag);
152 public function tokens()
154 $tokens = new TokenStorage();
155 $res = new TemplateResponse('grauphel', 'tokens');
158 'tokens' => $tokens->loadForUser(
159 $this->user->getUid(), 'access'
161 'client' => new Client(),
162 'username' => $this->user->getUid(),
165 $this->addNavigation($res, null);
171 * Allow the user to clear his database
176 public function database($reset = null)
178 $res = new TemplateResponse('grauphel', 'gui-database');
179 $res->setParams(array('reset' => $reset));
180 $this->addNavigation($res, null);
181 $this->addStats($res);
187 * Resets the database by deleting all notes and deleting the user's
192 public function databaseReset()
195 if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
196 $notes = $this->getNotes();
198 $notes->deleteSyncData();
202 return $this->database($reset);
205 protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
207 $nav = new \OCP\Template('grauphel', 'appnavigation', '');
208 $nav->assign('apiroot', $this->getApiRootUrl());
209 $nav->assign('tags', array());
211 $params = $res->getParams();
212 $params['appNavigation'] = $nav;
213 $res->setParams($params);
215 if ($this->user === null) {
219 $rawtags = $this->getNotes()->getTags();
223 'grauphel:special:all', 'grauphel:special:untagged'
227 foreach ($rawtags as $rawtag) {
228 $name = $this->getPrettyTagName($rawtag);
229 if ($name !== false) {
233 'href' => $this->urlGen->linkToRoute(
234 'grauphel.gui.tag', array('rawtag' => $rawtag)
239 $nav->assign('tags', $tags);
242 protected function addStats(TemplateResponse $res)
244 if ($this->user === null) {
248 $username = $this->user->getUid();
249 $notes = $this->getNotes();
250 $tokens = new \OCA\Grauphel\Lib\TokenStorage();
252 $nav = new \OCP\Template('grauphel', 'indexStats', '');
253 $nav->assign('notes', count($notes->loadNotesOverview()));
254 $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
255 $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
257 $params = $res->getParams();
258 $params['stats'] = $nav;
259 $res->setParams($params);
262 protected function checkDeps()
264 if (!class_exists('OAuthProvider')) {
265 throw new \Exception('PHP extension "oauth" is required');
269 protected function getApiRootUrl()
271 //we need to remove the trailing / for tomdroid and conboy
273 $this->urlGen->getAbsoluteURL(
274 $this->urlGen->linkToRoute('grauphel.gui.index')
280 protected function getNotes()
282 $username = $this->user->getUid();
283 $notes = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
284 $notes->setUsername($username);
288 protected function getPrettyTagName($rawtag)
290 if (substr($rawtag, 0, 16) == 'system:notebook:') {
291 return substr($rawtag, 16);
292 } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
293 return '*' . substr($rawtag, 17) . '*';