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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
<?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 which handles all ajax calls
*/
class NewsAjaxController extends Controller {
private $feedMapper;
private $folderMapper;
private $itemMapper;
/**
* @param Request $request: the object with the request instance
* @param string $api: an instance of the api wrapper
* @param FeedMapper $feedMapepr an instance of the feed mapper
* @param FolderMapper $folderMapper an instance of the folder mapper
* @param ItemMapper $itemMapper an instance of the item mapper
*/
public function __construct($request, $api, $feedMapper, $folderMapper,
$itemMapper){
parent::__construct($request, $api);
$this->feedMapper = $feedMapper;
$this->folderMapper = $folderMapper;
$this->itemMapper = $itemMapper;
}
/**
* @brief turns a post parameter which got a boolean from javascript to
* a boolean in PHP
* @param string $param the post parameter that should be turned into a bool
* @return a PHP boolean
*/
public function postParamToBool($param){
if($param === 'false') {
return false;
} else {
return true;
}
}
/**
* This turns a folder result into an array which can be sent to the client
* as JSON
* @param array $folders the database query result for folders
* @return an array ready for sending as JSON
*/
private function foldersToArray($folders){
$foldersArray = array();
foreach($folders as $folder){
if($folder instanceof \OCA\News\Folder){
array_push($foldersArray, array(
'id' => (int)$folder->getId(),
'name' => $folder->getName(),
'open' => $folder->getOpened()==="1",
'hasChildren' => count($folder->getChildren()) > 0,
'show' => true
)
);
}
}
return $foldersArray;
}
/**
* This turns a feed result into an array which can be sent to the client
* as JSON
* @param array $feeds the database query result for feeds
* @return an array ready for sending as JSON
*/
private function feedsToArray($feeds){
$feedsArray = array();
foreach($feeds as $feed){
array_push($feedsArray, array(
'id' => (int)$feed->getId(),
'name' => $feed->getTitle(),
'unreadCount' => (int)$this->itemMapper->getUnreadCount(FeedType::FEED,
$feed->getId()),
'folderId' => (int)$feed->getFolderId(),
'show' => true,
'icon' => 'url(' . $feed->getFavicon() .')',
'url' => $feed->getUrl()
)
);
}
return $feedsArray;
}
/**
* This turns an items result into an array which can be sent to the client
* as JSON
* @param array $items the database query result for items
* @return an array ready for sending as JSON
*/
private function itemsToArray($items){
$itemsArray = array();
foreach($items as $item){
$enclosure = $item->getEnclosure();
if($enclosure){
$enclosure = array(
'link' => $enclosure->getLink(),
'type' => $enclosure->getMimeType()
);
}
array_push($itemsArray, array(
'id' => (int)$item->getId(),
'title' => $item->getTitle(),
'isRead' => (bool)$item->isRead(),
'isImportant' => (bool)$item->isImportant(),
'feedId' => (int)$item->getFeedId(),
'feedTitle' => $item->getFeedTitle(),
'date' => (int)$item->getDate(),
'body' => $item->getBody(),
'author' => $item->getAuthor(),
'url' => $item->getUrl(),
'enclosure' => $enclosure
)
);
}
return $itemsArray;
}
/**
* This is being called when the app starts and all feeds
* and folders are requested
*/
public function init(){
$folders = $this->folderMapper->childrenOfWithFeeds(0);
$foldersArray = $this->foldersToArray($folders);
$feeds = $this->feedMapper->findAll();
$feedsArray = $this->feedsToArray($feeds);
$activeFeed = array();
$activeFeed['id'] = (int)$this->api->getUserValue('lastViewedFeed');
$activeFeed['type'] = (int)$this->api->getUserValue('lastViewedFeedType');
$showAll = $this->api->getUserValue('showAll') === "1";
$starredCount = $this->itemMapper->getUnreadCount(\OCA\News\FeedType::STARRED, 0);
$result = array(
'folders' => $foldersArray,
'feeds' => $feedsArray,
'activeFeed' => $activeFeed,
'showAll' => $showAll,
'userId' => $this->userId,
'starredCount' => $starredCount
);
return $this->renderJSON($result);
}
/**
* loads the next X feeds from the server
*/
public function loadFeed(){
$feedType = (int)$this->params('type');
$feedId = (int)$this->params('id');
$latestFeedId = (int)$this->params('latestFeedId');
$latestTimestamp = (int)$this->params('latestTimestamp');
$limit = (int)$this->params('limit');
// FIXME: integrate latestFeedId, latestTimestamp and limit
$this->api->setUserValue('lastViewedFeed', $feedId);
$this->api->setUserValue('lastViewedFeedType', $feedType);
$showAll = $this->api->getUserValue('showAll');
$items = $this->itemMapper->getItems($feedType, $feedId, $showAll);
$itemsArray = $this->itemsToArray($items);
// update unread count of all feeds
$feeds = $this->feedMapper->findAll();
$feedsArray = array();
foreach($feeds as $feed){
$unreadCount = $this->itemMapper->countAllStatus($feed->getId(), StatusFlag::UNREAD);
$unreadArray = array(
'id' => (int)$feed->getId(),
'unreadCount' => (int)$unreadCount
);
array_push($feedsArray, $unreadArray);
}
$result = array(
'items' => $itemsArray,
'feeds' => $feedsArray
);
return $this->renderJSON($result);
}
/**
* Used for setting the showAll value from a post request
*/
public function setShowAll(){
$showAll = $this->postParamToBool($this->params('showAll'));
$this->api->setUserValue('showAll', $showAll);
return $this->renderJSON();
}
/**
* Used for setting the showAll value from a post request
*/
public function collapseFolder(){
$folderId = (int)$this->params('folderId');
$opened = $this->postParamToBool($this->params('opened'));
$folder = $this->folderMapper->find($folderId);
$folder->setOpened($opened);
$this->folderMapper->update($folder);
return $this->renderJSON();
}
/**
* Deletes a feed
*/
public function deleteFeed(){
$feedId = (int)$this->params('feedId');
$this->feedMapper->deleteById($feedId);
return $this->renderJSON();
}
/**
* Deletes a folder
*/
public function deleteFolder(){
$folderId = (int)$this->params('folderId');
$this->folderMapper->deleteById($folderId);
return $this->renderJSON();
}
/**
* Sets the status of an item
*/
public function setItemStatus(){
$itemId = (int)$this->params('itemId');
$status = $this->params('status');
$item = $this->itemMapper->findById($itemId);
switch ($status) {
case 'read':
$item->setRead();
break;
case 'unread':
$item->setUnread();
break;
case 'important':
$item->setImportant();
break;
case 'unimportant':
$item->setUnimportant();
break;
default:
exit();
break;
}
$this->itemMapper->update($item);
return
|