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\TokenStorage;
25 * @author Christian Weiske <cweiske@cweiske.de>
26 * @copyright 2014 Christian Weiske
27 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
28 * @version Release: @package_version@
29 * @link http://cweiske.de/grauphel.htm
31 class GuiController extends Controller
34 * constructor of the controller
36 * @param string $appName Name of the app
37 * @param IRequest $request Instance of the request
39 public function __construct($appName, \OCP\IRequest $request, $user, $urlGen)
41 parent::__construct($appName, $request);
43 $this->urlGen = $urlGen;
45 //default http header: we assume something is broken
46 header('HTTP/1.0 500 Internal Server Error');
52 * Tomdroid wants this to be a public page. Sync fails otherwise.
58 public function index()
62 $res = new TemplateResponse('grauphel', 'index');
65 'apiroot' => $this->getApiRootUrl(),
66 'apiurl' => $this->urlGen->linkToRoute('grauphel.api.index')
69 $this->addNavigation($res);
70 $this->addStats($res);
75 * Show all notes of a tag
80 public function tag($rawtag)
82 $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
84 $res = new TemplateResponse('grauphel', 'tag');
87 'tag' => $this->getPrettyTagName($rawtag),
92 $this->addNavigation($res, $rawtag);
103 public function tokens()
105 $tokens = new TokenStorage();
106 $res = new TemplateResponse('grauphel', 'tokens');
109 'tokens' => $tokens->loadForUser(
110 $this->user->getUid(), 'access'
114 $this->addNavigation($res, $rawtag);
119 protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
121 $nav = new \OCP\Template('grauphel', 'appnavigation', '');
122 $nav->assign('apiroot', $this->getApiRootUrl());
124 $params = $res->getParams();
125 $params['appNavigation'] = $nav;
126 $res->setParams($params);
128 if ($this->user === null) {
132 $rawtags = $this->getNotes()->getTags();
136 'grauphel:special:all', 'grauphel:special:untagged'
140 foreach ($rawtags as $rawtag) {
141 $name = $this->getPrettyTagName($rawtag);
142 if ($name !== false) {
146 'href' => $this->urlGen->linkToRoute(
147 'grauphel.gui.tag', array('rawtag' => $rawtag)
152 $nav->assign('tags', $tags);
155 protected function addStats(TemplateResponse $res)
157 if ($this->user === null) {
161 $username = $this->user->getUid();
162 $notes = $this->getNotes();
163 $tokens = new \OCA\Grauphel\Lib\TokenStorage();
165 $nav = new \OCP\Template('grauphel', 'indexStats', '');
166 $nav->assign('notes', count($notes->loadNotesOverview()));
167 $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
168 $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
170 $params = $res->getParams();
171 $params['stats'] = $nav;
172 $res->setParams($params);
175 protected function checkDeps()
177 if (!class_exists('OAuthProvider')) {
178 throw new \Exception('PHP extension "oauth" is required');
182 protected function getApiRootUrl()
184 //we need to remove the trailing / for tomdroid and conboy
186 $this->urlGen->getAbsoluteURL(
187 $this->urlGen->linkToRoute('grauphel.gui.index')
193 protected function getNotes()
195 $username = $this->user->getUid();
196 $notes = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
197 $notes->setUsername($username);
201 protected function getPrettyTagName($rawtag)
203 if (substr($rawtag, 0, 16) == 'system:notebook:') {
204 return substr($rawtag, 16);
205 } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
206 return '*' . substr($rawtag, 17) . '*';