summaryrefslogtreecommitdiffstats
path: root/controller/pagecontroller.php
blob: 346ef61ef797103889e56466c40cf8eafe122ae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Alessandro Cosentino <cosenal@gmail.com>
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Alessandro Cosentino 2012
 * @copyright Bernhard Posselt 2012, 2014
 */

namespace OCA\News\Controller;

use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\AppFramework\Controller;

use OCA\News\Service\StatusService;
use OCA\News\Config\AppConfig;
use OCA\News\Config\Config;
use OCA\News\Explore\RecommendedSites;
use OCA\News\Db\FeedType;

class PageController extends Controller {

    private $settings;
    private $l10n;
    private $userId;
    private $appConfig;
    private $urlGenerator;
    private $config;
    private $recommendedSites;
    private $statusService;

    public function __construct($AppName,
                                IRequest $request,
                                IConfig $settings,
                                IURLGenerator $urlGenerator,
                                AppConfig $appConfig,
                                Config $config,
                                IL10N $l10n,
                                RecommendedSites $recommendedSites,
                                StatusService $statusService,
                                $UserId){
        parent::__construct($AppName, $request);
        $this->settings = $settings;
        $this->urlGenerator = $urlGenerator;
        $this->appConfig = $appConfig;
        $this->l10n = $l10n;
        $this->userId = $UserId;
        $this->config = $config;
        $this->recommendedSites = $recommendedSites;
        $this->statusService = $statusService;
    }


    /**
     * @NoAdminRequired
     * @NoCSRFRequired
     */
    public function index() {
        $status = $this->statusService->getStatus();

        return new TemplateResponse($this->appName, 'index', [
            'cronWarning' => $status['warnings']['improperlyConfiguredCron']
        ]);
    }


    /**
     * @NoAdminRequired
     */
    public function settings() {
        $settings = [
            'showAll',
            'compact',
            'preventReadOnScroll',
            'oldestFirst',
            'compactExpand'
        ];

        $exploreUrl = $this->config->getExploreUrl();
        if (trim($exploreUrl) === '') {
            $exploreUrl = $this->urlGenerator->getAbsoluteURL(
                '/index.php/apps/news/explore'
            );
        }

        $result = [
            'language' => $this->l10n->getLanguageCode(),
            'exploreUrl' => $exploreUrl
        ];

        foreach ($settings as $setting) {
            $result[$setting] = $this->settings->getUserValue(
                $this->userId, $this->appName, $setting
            ) === '1';
        }
        return ['settings' => $result];
    }


    /**
     * @NoAdminRequired
     *
     * @param bool $showAll
     * @param bool $compact
     * @param bool $preventReadOnScroll
     * @param bool $oldestFirst
     */
    public function updateSettings($showAll, $compact, $preventReadOnScroll,
                                   $oldestFirst, $compactExpand) {
        $settings = ['showAll',
            'compact',
            'preventReadOnScroll',
            'oldestFirst',
            'compactExpand'
        ];

        foreach ($settings as $setting) {
            $this->settings->setUserValue($this->userId, $this->appName,
                                          $setting, ${$setting});
        }
    }


    /**
     * @NoCSRFRequired
     * @PublicPage
     *
     * Generates a web app manifest, according to specs in:
     * https://developer.mozilla.org/en-US/Apps/Build/Manifest
     */
    public function manifest() {
        $config = $this->appConfig->getConfig();

        // size of the icons: 128x128 is required by FxOS for all app manifests
        $iconSizes = ['128', '512'];
        $icons = [];

        $locale = str_replace('_', '-', $this->l10n->getLanguageCode());

        foreach ($iconSizes as $size) {
            $filename = 'app-' . $size . '.png';
            if (file_exists(__DIR__ . '/../img/' . $filename)) {
                $icons[$size] = $this->urlGenerator->imagePath($config['id'],
                    $filename);
            }
        }


        $data = [
            "name" => $config['name'],
            "type" => 'web',
            "default_locale" => $locale,
            "description" => $config['description'],
            "launch_path" => $this->urlGenerator->linkToRoute(
                $config['navigation']['route']),
            "icons" => $icons,
            "developer" => [
                "name" => $config['author'],
                "url" => $config['homepage']
            ]
        ];

        $response = new JSONResponse($data);
        $response->addHeader('Content-Type',
            'application/x-web-app-manifest+json');

        return $response;
    }

    /**
     * @NoAdminRequired
     *
     * @param string $lang
     */
    public function explore($lang='en') {
        $default = 'en';

        $this->settings->setUserValue($this->userId, $this->appName,
            'lastViewedFeedId', 0);
        $this->settings->setUserValue($this->userId, $this->appName,
            'lastViewedFeedType', FeedType::EXPLORE);

        return $this->recommendedSites->forLanguage($lang, $default);
    }


}