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;
26 * @author Christian Weiske <cweiske@cweiske.de>
27 * @copyright 2014 Christian Weiske
28 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
29 * @version Release: @package_version@
30 * @link http://cweiske.de/grauphel.htm
32 class GuiController extends Controller
35 * constructor of the controller
37 * @param string $appName Name of the app
38 * @param IRequest $request Instance of the request
40 public function __construct($appName, \OCP\IRequest $request, $user, $urlGen)
42 parent::__construct($appName, $request);
44 $this->urlGen = $urlGen;
46 //default http header: we assume something is broken
47 header('HTTP/1.0 500 Internal Server Error');
53 * Tomdroid wants this to be a public page. Sync fails otherwise.
59 public function index()
63 $res = new TemplateResponse('grauphel', 'index');
66 'apiroot' => $this->getApiRootUrl(),
67 'apiurl' => $this->urlGen->linkToRoute('grauphel.api.index')
70 $this->addNavigation($res);
71 $this->addStats($res);
76 * Show all notes of a tag
81 public function tag($rawtag)
83 $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
85 $res = new TemplateResponse('grauphel', 'tag');
88 'tag' => $this->getPrettyTagName($rawtag),
93 $this->addNavigation($res, $rawtag);
104 public function tokens()
106 $tokens = new TokenStorage();
107 $res = new TemplateResponse('grauphel', 'tokens');
110 'tokens' => $tokens->loadForUser(
111 $this->user->getUid(), 'access'
113 'client' => new Client(),
114 'username' => $this->user->getUid(),
117 $this->addNavigation($res, null);
123 * Allow the user to clear his database
128 public function database($reset = null)
130 $res = new TemplateResponse('grauphel', 'gui-database');
131 $res->setParams(array('reset' => $reset));
132 $this->addNavigation($res, null);
133 $this->addStats($res);
139 * Resets the database by deleting all notes and deleting the user's
144 public function databaseReset()
147 if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
148 $notes = $this->getNotes();
150 $notes->deleteSyncData();
154 return $this->database($reset);
157 protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
159 $nav = new \OCP\Template('grauphel', 'appnavigation', '');
160 $nav->assign('apiroot', $this->getApiRootUrl());
161 $nav->assign('tags', array());
163 $params = $res->getParams();
164 $params['appNavigation'] = $nav;
165 $res->setParams($params);
167 if ($this->user === null) {
171 $rawtags = $this->getNotes()->getTags();
175 'grauphel:special:all', 'grauphel:special:untagged'
179 foreach ($rawtags as $rawtag) {
180 $name = $this->getPrettyTagName($rawtag);
181 if ($name !== false) {
185 'href' => $this->urlGen->linkToRoute(
186 'grauphel.gui.tag', array('rawtag' => $rawtag)
191 $nav->assign('tags', $tags);
194 protected function addStats(TemplateResponse $res)
196 if ($this->user === null) {
200 $username = $this->user->getUid();
201 $notes = $this->getNotes();
202 $tokens = new \OCA\Grauphel\Lib\TokenStorage();
204 $nav = new \OCP\Template('grauphel', 'indexStats', '');
205 $nav->assign('notes', count($notes->loadNotesOverview()));
206 $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
207 $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
209 $params = $res->getParams();
210 $params['stats'] = $nav;
211 $res->setParams($params);
214 protected function checkDeps()
216 if (!class_exists('OAuthProvider')) {
217 throw new \Exception('PHP extension "oauth" is required');
221 protected function getApiRootUrl()
223 //we need to remove the trailing / for tomdroid and conboy
225 $this->urlGen->getAbsoluteURL(
226 $this->urlGen->linkToRoute('grauphel.gui.index')
232 protected function getNotes()
234 $username = $this->user->getUid();
235 $notes = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
236 $notes->setUsername($username);
240 protected function getPrettyTagName($rawtag)
242 if (substr($rawtag, 0, 16) == 'system:notebook:') {
243 return substr($rawtag, 16);
244 } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
245 return '*' . substr($rawtag, 17) . '*';