summaryrefslogtreecommitdiffstats
path: root/controller/news.controller.php
blob: e13cfe979ae03a0234ccc831386b05a28f88bde0 (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
<?php
/**
* ownCloud - News app
*
* @author Bernhard Posselt
* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com>
*
* This file is licensed under the Affero General Public License version 3 or later.
* See the COPYING-README file
*
*/

namespace OCA\News;


class NewsController extends Controller {

        private $feedMapper;
        private $folderMapper;

        /**
         * @param Request $request: the object with the request instance
         * @param string $api: an instance of the api wrapper object
         * @param FolderMapper $folderMapper: an instance of the folder mapper
         * @param FeedMapper $feedMapper: an instance of the feed mapper
         */
        public function __construct($request, $api, $feedMapper, $folderMapper){
                parent::__construct($request, $api);
                $this->feedMapper = $feedMapper;
                $this->folderMapper = $folderMapper;
                $this->api->activateNavigationEntry();
        }


        /**
         * OPML export download page
         */
        public function exportOPML($urlParams=array()){
                $opmlExporter = new OPMLExporter($this->api);

                $allFeeds = $this->folderMapper->childrenOfWithFeeds(0);
                $opml = $opmlExporter->buildOPML($allFeeds);

                $fileName = 'ownCloud ' . $this->trans->t('News') . ' ' . $this->userId . '.opml';
                $contentType = 'application/x.opml+xml';
                $response = new TextDownloadResponse($opml, $fileName, $contentType);

                return $response;
        }


        /**
         * Decides wether to show the feedpage or the firstrun page
         */
        public function index($urlParams=array()){
                $this->api->add3rdPartyScript('angular-1.0.2/angular.min');
                $this->api->add3rdPartyScript('moment.min');
                $this->api->addScript('app');
                $this->api->addStyle('news');


                if($urlParams['feedid']){
                        $this->api->setUserValue('lastViewedFeed', $urlParams['feedid']);
                        $this->api->setUserValue('lastViewedFeedType', FeedType::FEED);
                }

                $lastViewedFeedId = $this->api->getUserValue('lastViewedFeed');
                $lastViewedFeedType = $this->api->getUserValue('lastViewedFeedType');

                if( $lastViewedFeedId === null || $lastViewedFeedType === null) {
                        $this->api->setUserValue('lastViewedFeed', $this->feedMapper->mostRecent());;
                        $this->api->setUserValue('lastViewedFeedType', FeedType::FEED);

                } else {
                        // check if the last selected feed or folder exists
                        if(($lastViewedFeedType === FeedType::FEED &&
                                $this->feedMapper->findById($lastViewedFeedId) === null) ||
                                ($lastViewedFeedType === FeedType::FOLDER &&
                                        $this->folderMapper->findById($lastViewedFeedId) === null)){
                                $this->api->setUserValue('lastViewedFeed', $this->feedMapper->mostRecent());;
                                $this->api->setUserValue('lastViewedFeedType', FeedType::FEED);
                        }
                }

                return $this->render('main');
        }


}