aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/feedController.php
blob: 6dad1ac0827b0d43c2030248d39b5a11811164b9 (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
<?php

class feedController extends ActionController {
	public function addAction () {
		if (Request::isPost ()) {
			$url = Request::param ('url_rss');
			
			try {
				$feed = new Feed ($url);
				$feed->load ();
				$entries = $feed->entries ();
				
				$entryDAO = new EntryDAO ();
				
				foreach ($entries as $entry) {
					$values = array (
						'id' => $entry->id (),
						'guid' => $entry->guid (),
						'title' => $entry->title (),
						'author' => $entry->author (),
						'content' => $entry->content (),
						'link' => $entry->link (),
						'date' => $entry->date (true),
						'is_read' => $entry->isRead (),
						'is_favorite' => $entry->isFavorite (),
						'id_feed' => $feed->id ()
					);
					$entryDAO->addEntry ($values);
				}
				
				$feedDAO = new FeedDAO ();
				$values = array (
					'id' => $feed->id (),
					'url' => $feed->url (),
					'category' => $feed->category (),
					'name' => $feed->name (),
					'website' => $feed->website (),
					'description' => $feed->description (),
				);
				$feedDAO->addFeed ($values);
			} catch (Exception $e) {
				// TODO ajouter une erreur : url non valide
			}
			
			Request::forward (array (), true);
		}
	}
	
	public function actualizeAction () {
		$feedDAO = new FeedDAO ();
		$entryDAO = new EntryDAO ();
		
		$feeds = $feedDAO->listFeeds ();
		
		foreach ($feeds as $feed) {
			$feed->load ();
			$entries = $feed->entries ();
			
			foreach ($entries as $entry) {
				$values = array (
					'id' => $entry->id (),
					'guid' => $entry->guid (),
					'title' => $entry->title (),
					'author' => $entry->author (),
					'content' => $entry->content (),
					'link' => $entry->link (),
					'date' => $entry->date (true),
					'is_read' => $entry->isRead (),
					'is_favorite' => $entry->isFavorite (),
					'id_feed' => $feed->id ()
				);
				$entryDAO->addEntry ($values);
				
				// TODO gérer suppression des articles trop vieux (à paramétrer)
			}
		}
		
		Request::forward (array (), true);
	}
	
	public function massiveImportAction () {
		$entryDAO = new EntryDAO ();
		$feedDAO = new FeedDAO ();
		$catDAO = new CategoryDAO ();
		
		$categories = Request::param ('categories', array ());
		$feeds = Request::param ('feeds', array ());
		
		foreach ($categories as $cat) {
			$values = array (
				'id' => $cat->id (),
				'name' => $cat->name (),
				'color' => $cat->color ()
			);
			$catDAO->addCategory ($values);
		}
		
		foreach ($feeds as $feed) {
			$feed->load ();
			$entries = $feed->entries ();
			
			// Chargement du flux
			foreach ($entries as $entry) {
				$values = array (
					'id' => $entry->id (),
					'guid' => $entry->guid (),
					'title' => $entry->title (),
					'author' => $entry->author (),
					'content' => $entry->content (),
					'link' => $entry->link (),
					'date' => $entry->date (true),
					'is_read' => $entry->isRead (),
					'is_favorite' => $entry->isFavorite (),
					'id_feed' => $feed->id ()
				);
				$entryDAO->addEntry ($values);
			}
			
			// Enregistrement du flux
			$values = array (
				'id' => $feed->id (),
				'url' => $feed->url (),
				'category' => $feed->category (),
				'name' => $feed->name (),
				'website' => $feed->website (),
				'description' => $feed->description (),
			);
			$feedDAO->addFeed ($values);
		}
	
		Request::forward (array ('c' => 'configure', 'a' => 'importExport'));
	}
}