show some statistics on the index page
[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(array('apiurl' => $this->getApiUrl()));
63         $this->addNavigation($res);
64         $this->addStats($res);
65         return $res;
66     }
67
68     protected function addNavigation(TemplateResponse $res)
69     {
70         $nav = new \OCP\Template('grauphel', 'appnavigation', '');
71         $nav->assign('apiurl', $this->getApiUrl());
72
73         $params = $res->getParams();
74         $params['appNavigation'] = $nav;
75         $res->setParams($params);
76     }
77
78     protected function addStats(TemplateResponse $res)
79     {
80         if ($this->user === null) {
81             return;
82         }
83
84         $username = $this->user->getUid();
85         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
86         $tokens = new \OCA\Grauphel\Lib\TokenStorage();
87
88         $nav = new \OCP\Template('grauphel', 'indexStats', '');
89         $nav->assign('notes', count($notes->loadNotesOverview($username)));
90         $nav->assign('syncrev', $notes->loadSyncData($username)->latestSyncRevision);
91         $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
92
93         $params = $res->getParams();
94         $params['stats'] = $nav;
95         $res->setParams($params);
96     }
97
98     protected function checkDeps()
99     {
100         if (!class_exists('OAuthProvider')) {
101             throw new \Exception('PHP extension "oauth" is required');
102         }
103     }
104
105     protected function getApiUrl()
106     {
107         //we need to remove the trailing / for tomdroid and conboy
108         return rtrim(
109             $this->urlGen->getAbsoluteURL(
110                 $this->urlGen->linkToRoute('grauphel.gui.index')
111             ),
112             '/'
113         );
114     }
115 }
116 ?>