Fix length of date fields in database.
[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
19 /**
20  * Owncloud frontend
21  *
22  * @category  Tools
23  * @package   Grauphel
24  * @author    Christian Weiske <cweiske@cweiske.de>
25  * @copyright 2014 Christian Weiske
26  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
27  * @version   Release: @package_version@
28  * @link      http://cweiske.de/grauphel.htm
29  */
30 class GuiController extends Controller
31 {
32     /**
33      * constructor of the controller
34      *
35      * @param string   $appName Name of the app
36      * @param IRequest $request Instance of the request
37      */
38     public function __construct($appName, \OCP\IRequest $request, $user, $urlGen)
39     {
40         parent::__construct($appName, $request);
41         $this->user   = $user;
42         $this->urlGen = $urlGen;
43
44         //default http header: we assume something is broken
45         header('HTTP/1.0 500 Internal Server Error');
46     }
47
48     /**
49      * Main page /
50      *
51      * Tomdroid wants this to be a public page. Sync fails otherwise.
52      *
53      * @NoAdminRequired
54      * @NoCSRFRequired
55      * @PublicPage
56      */
57     public function index()
58     {
59         $this->checkDeps();
60
61         $res = new TemplateResponse('grauphel', 'index');
62         $res->setParams(
63             array(
64                 'apiroot' => $this->getApiRootUrl(),
65                 'apiurl'  => $this->urlGen->linkToRoute('grauphel.api.index')
66             )
67         );
68         $this->addNavigation($res);
69         $this->addStats($res);
70         return $res;
71     }
72
73     /**
74      * Show all notes of a tag
75      *
76      * @NoAdminRequired
77      * @NoCSRFRequired
78      */
79     public function tag($rawtag)
80     {
81         $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
82
83         $res = new TemplateResponse('grauphel', 'tag');
84         $res->setParams(
85             array(
86                 'tag'    => $this->getPrettyTagName($rawtag),
87                 'rawtag' => $rawtag,
88                 'notes'  => $notes,
89             )
90         );
91         $this->addNavigation($res, $rawtag);
92
93         return $res;
94     }
95
96     protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
97     {
98         $nav = new \OCP\Template('grauphel', 'appnavigation', '');
99         $nav->assign('apiroot', $this->getApiRootUrl());
100
101         $params = $res->getParams();
102         $params['appNavigation'] = $nav;
103         $res->setParams($params);
104
105         if ($this->user === null) {
106             return;
107         }
108
109         $rawtags = $this->getNotes()->getTags();
110         sort($rawtags);
111         array_unshift(
112             $rawtags,
113             'grauphel:special:all', 'grauphel:special:untagged'
114         );
115
116         $tags = array();
117         foreach ($rawtags as $rawtag) {
118             $name = $this->getPrettyTagName($rawtag);
119             if ($name !== false) {
120                 $tags[] = array(
121                     'name' => $name,
122                     'id'   => $rawtag,
123                     'href' => $this->urlGen->linkToRoute(
124                         'grauphel.gui.tag', array('rawtag' => $rawtag)
125                     ),
126                 );
127             }
128         }
129         $nav->assign('tags', $tags);
130     }
131
132     protected function addStats(TemplateResponse $res)
133     {
134         if ($this->user === null) {
135             return;
136         }
137
138         $username = $this->user->getUid();
139         $notes  = $this->getNotes();
140         $tokens = new \OCA\Grauphel\Lib\TokenStorage();
141
142         $nav = new \OCP\Template('grauphel', 'indexStats', '');
143         $nav->assign('notes', count($notes->loadNotesOverview()));
144         $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
145         $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
146
147         $params = $res->getParams();
148         $params['stats'] = $nav;
149         $res->setParams($params);
150     }
151
152     protected function checkDeps()
153     {
154         if (!class_exists('OAuthProvider')) {
155             throw new \Exception('PHP extension "oauth" is required');
156         }
157     }
158
159     protected function getApiRootUrl()
160     {
161         //we need to remove the trailing / for tomdroid and conboy
162         return rtrim(
163             $this->urlGen->getAbsoluteURL(
164                 $this->urlGen->linkToRoute('grauphel.gui.index')
165             ),
166             '/'
167         );
168     }
169
170     protected function getNotes()
171     {
172         $username = $this->user->getUid();
173         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
174         $notes->setUsername($username);
175         return $notes;
176     }
177
178     protected function getPrettyTagName($rawtag)
179     {
180         if (substr($rawtag, 0, 16) == 'system:notebook:') {
181             return substr($rawtag, 16);
182         } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
183             return '*' . substr($rawtag, 17) . '*';
184         }
185         return false;
186     }
187 }
188 ?>