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

class feedController extends ActionController {
	public function addAction () {
		if (login_is_conf ($this->view->conf) && !is_logged ()) {
			Error::error (
				403,
				array ('error' => array ('Vous n\'avez pas le droit d\'accéder à cette page'))
			);
		} else {
			if (Request::isPost ()) {
				$url = Request::param ('url_rss');
			
				try {
					$feed = new Feed ($url);
					$feed->load ();
					
					$feedDAO = new FeedDAO ();
					$values = array (
						'id' => $feed->id (),
						'url' => $feed->url (),
						'category' => null,
						'name' => $feed->name (),
						'website' => $feed->website (),
						'description' => $feed->description (),
					);
					$feedDAO->addFeed ($values);
				
					$entryDAO = new EntryDAO ();
					$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);
					}
					
					// notif
					$notif = array (
						'type' => 'good',
						'content' => 'Le flux <em>' . $feed->url () . '</em> a bien été ajouté'
					);
					Session::_param ('notification', $notif);
				} catch (Exception $e) {
					// notif
					$notif = array (
						'type' => 'bad',
						'content' => 'L\'url <em>' . $url . '</em> est invalide'
					);
					Session::_param ('notification', $notif);
				}
			
				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);
			}
		}
		
		$entryDAO->cleanOldEntries ($this->view->conf->oldEntries ());
		
		// notif
		$notif = array (
			'type' => 'good',
			'content' => 'Les flux ont été mis à jour'
		);
		Session::_param ('notification', $notif);
		
		Request::forward (array (), true);
	}
	
	public function massiveImportAction () {
		if (login_is_conf ($this->view->conf) && !is_logged ()) {
			Error::error (
				403,
				array ('error' => array ('Vous n\'avez pas le droit d\'accéder à cette page'))
			);
		} else {
			$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);
			}
			
			// notif
			$notif = array (
				'type' => 'good',
				'content' => 'Les flux ont été importés'
			);
			Session::_param ('notification', $notif);
	
			Request::forward (array ('c' => 'configure', 'a' => 'importExport'));
		}
	}
	
	public function deleteAction () {
		if (login_is_conf ($this->view->conf) && !is_logged ()) {
			Error::error (
				403,
				array ('error' => array ('Vous n\'avez pas le droit d\'accéder à cette page'))
			);
		} else {
			$id = Request::param ('id');
		
			$feedDAO = new FeedDAO ();
			$feedDAO->deleteFeed ($id);
			
			// notif
			$notif = array (
				'type' => 'good',
				'content' => 'Le flux a été supprimé'
			);
			Session::_param ('notification', $notif);
		
			Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
		}
	}
}