summaryrefslogtreecommitdiffstats
path: root/controllers/news.controller.php
blob: 9695e85d1499fa42fd2f35fd2fc624b591bdd51f (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
<?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 FeedType {
    const FEED          = 0;
    const FOLDER        = 1;
    const STARRED       = 2;
    const SUBSCRIPTIONS = 3;
}


class NewsController extends Controller {


    /**
     * Decides wether to show the feedpage or the firstrun page
     */
    public function index(){
        $feedMapper = new FeedMapper($this->userId);

        if($feedMapper->feedCount() > 0){
            $this->feedPage();
        } else {
            $this->firstRun();
        }
    }


    public function firstRun(){
        $this->addScript('news');
        $this->addScript('firstrun');
        $this->addStyle('firstrun');
        $this->render('firstrun');
    }


    public function feedPage(){
        $this->addScript('main');
        $this->addScript('news');
        $this->addScript('menu');
        $this->addScript('items');
        $this->add3rdPartyScript('jquery.timeago');

        $this->addStyle('news');
        $this->addStyle('settings');

        $folderMapper = new FolderMapper($this->userId);
        $feedMapper = new FeedMapper($this->userId);
        $itemMapper = new ItemMapper($this->userId);

        // always show the last viewed feed on reload
        $lastViewedFeedId = $this->getUserValue('lastViewedFeed');
        $lastViewedFeedType = $this->getUserValue('lastViewedFeedType');
        $showAll = $this->getUserValue('showAll'); 

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

        $feeds = $folderMapper->childrenOfWithFeeds(0);
        $folderForest = $folderMapper->childrenOf(0); //retrieve all the folders
        $starredCount = $itemMapper->countEveryItemByStatus(StatusFlag::IMPORTANT);

        $params = array(
            'allfeeds' => $feeds,
            'folderforest' => $folderForest,
            'showAll' => $showAll,
            'lastViewedFeedId' => $lastViewedFeedId,
            'lastViewedFeedType' => $lastViewedFeedType,
            'starredCount' => $starredCount,
        );

        $this->render('main', $params);
    }


    public function javascriptTests(){
        $this->add3rdPartyScript('jasmine-1.2.0/jasmine.js');
        $this->add3rdPartyStyle('jasmine-1.2.0/jasmine.css');
        $this->render('javascript.tests');
    }


}

?>