phing build file to create releases
[grauphel.git] / controller / guicontroller.php
1 <?php
2 /**
3  * Part of grauphel
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Grauphel
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
13  */
14 namespace OCA\Grauphel\Controller;
15
16 use \OCP\AppFramework\Controller;
17 use \OCP\AppFramework\Http\TemplateResponse;
18 use \OCA\Grauphel\Lib\TokenStorage;
19
20 /**
21  * Owncloud frontend
22  *
23  * @category  Tools
24  * @package   Grauphel
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
30  */
31 class GuiController extends Controller
32 {
33     /**
34      * constructor of the controller
35      *
36      * @param string   $appName Name of the app
37      * @param IRequest $request Instance of the request
38      */
39     public function __construct($appName, \OCP\IRequest $request, $user, $urlGen)
40     {
41         parent::__construct($appName, $request);
42         $this->user   = $user;
43         $this->urlGen = $urlGen;
44
45         //default http header: we assume something is broken
46         header('HTTP/1.0 500 Internal Server Error');
47     }
48
49     /**
50      * Main page /
51      *
52      * Tomdroid wants this to be a public page. Sync fails otherwise.
53      *
54      * @NoAdminRequired
55      * @NoCSRFRequired
56      * @PublicPage
57      */
58     public function index()
59     {
60         $this->checkDeps();
61
62         $res = new TemplateResponse('grauphel', 'index');
63         $res->setParams(
64             array(
65                 'apiroot' => $this->getApiRootUrl(),
66                 'apiurl'  => $this->urlGen->linkToRoute('grauphel.api.index')
67             )
68         );
69         $this->addNavigation($res);
70         $this->addStats($res);
71         return $res;
72     }
73
74     /**
75      * Show all notes of a tag
76      *
77      * @NoAdminRequired
78      * @NoCSRFRequired
79      */
80     public function tag($rawtag)
81     {
82         $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
83
84         $res = new TemplateResponse('grauphel', 'tag');
85         $res->setParams(
86             array(
87                 'tag'    => $this->getPrettyTagName($rawtag),
88                 'rawtag' => $rawtag,
89                 'notes'  => $notes,
90             )
91         );
92         $this->addNavigation($res, $rawtag);
93
94         return $res;
95     }
96
97     /**
98      * Show access tokens
99      *
100      * @NoAdminRequired
101      * @NoCSRFRequired
102      */
103     public function tokens()
104     {
105         $tokens = new TokenStorage();
106         $res = new TemplateResponse('grauphel', 'tokens');
107         $res->setParams(
108             array(
109                 'tokens' => $tokens->loadForUser(
110                     $this->user->getUid(), 'access'
111                 )
112             )
113         );
114         $this->addNavigation($res, $rawtag);
115
116         return $res;
117     }
118
119     protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
120     {
121         $nav = new \OCP\Template('grauphel', 'appnavigation', '');
122         $nav->assign('apiroot', $this->getApiRootUrl());
123
124         $params = $res->getParams();
125         $params['appNavigation'] = $nav;
126         $res->setParams($params);
127
128         if ($this->user === null) {
129             return;
130         }
131
132         $rawtags = $this->getNotes()->getTags();
133         sort($rawtags);
134         array_unshift(
135             $rawtags,
136             'grauphel:special:all', 'grauphel:special:untagged'
137         );
138
139         $tags = array();
140         foreach ($rawtags as $rawtag) {
141             $name = $this->getPrettyTagName($rawtag);
142             if ($name !== false) {
143                 $tags[] = array(
144                     'name' => $name,
145                     'id'   => $rawtag,
146                     'href' => $this->urlGen->linkToRoute(
147                         'grauphel.gui.tag', array('rawtag' => $rawtag)
148                     ),
149                 );
150             }
151         }
152         $nav->assign('tags', $tags);
153     }
154
155     protected function addStats(TemplateResponse $res)
156     {
157         if ($this->user === null) {
158             return;
159         }
160
161         $username = $this->user->getUid();
162         $notes  = $this->getNotes();
163         $tokens = new \OCA\Grauphel\Lib\TokenStorage();
164
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')));
169
170         $params = $res->getParams();
171         $params['stats'] = $nav;
172         $res->setParams($params);
173     }
174
175     protected function checkDeps()
176     {
177         if (!class_exists('OAuthProvider')) {
178             throw new \Exception('PHP extension "oauth" is required');
179         }
180     }
181
182     protected function getApiRootUrl()
183     {
184         //we need to remove the trailing / for tomdroid and conboy
185         return rtrim(
186             $this->urlGen->getAbsoluteURL(
187                 $this->urlGen->linkToRoute('grauphel.gui.index')
188             ),
189             '/'
190         );
191     }
192
193     protected function getNotes()
194     {
195         $username = $this->user->getUid();
196         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
197         $notes->setUsername($username);
198         return $notes;
199     }
200
201     protected function getPrettyTagName($rawtag)
202     {
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) . '*';
207         }
208         return false;
209     }
210 }
211 ?>