new: Auto load the default feeds from file

pull/2232/head
iglocska 2017-05-30 11:42:57 +02:00
parent aa0703de45
commit 9f8954999a
2 changed files with 55 additions and 29 deletions

View File

@ -25,6 +25,7 @@ class FeedsController extends AppController {
}
public function index() {
$this->Feed->load_default_feeds();
$scope = isset($this->passedArgs['scope']) ? $this->passedArgs['scope'] : 'all';
if ($scope !== 'all') {
if ($scope == 'enabled') {
@ -82,36 +83,14 @@ class FeedsController extends AppController {
public function importFeeds() {
if ($this->request->is('post')) {
$feeds = json_decode($this->request->data['Feed']['json'], true);
if (!isset($feeds[0])) {
$feeds = array($feeds);
$results = $this->Feed->importFeeds($this->request->data['Feed']['json'], $this->Auth->user());
if ($results['successes'] > 0) {
$message = $results['successes'] . ' new feeds added.';
} else {
$message = 'No new feeds to add.';
}
if (empty($feeds)) throw new NotFoundException('No valid ');
$existingFeeds = $this->Feed->find('all', array());
$fail = $success = 0;
foreach ($feeds as $feed) {
if (isset($feed['Feed']['id'])) {
unset($feed['Feed']['id']);
}
$found = false;
foreach ($existingFeeds as $existingFeed) {
if ($existingFeed['Feed']['url'] == $feed['Feed']['url']) {
$found = true;
}
}
if (!$found) {
$this->Feed->create();
if (!$this->Feed->save($feed, true, array('name', 'provider', 'url', 'rules', 'source_format', 'fixed_event', 'delta_merge', 'override_ids', 'publish', 'settings'))) {
$fail++;
$this->Session->setFlash('Could not save feeds. Reason: ' . json_encode($this->Feed->validationErros));
} else {
$success++;
}
}
}
$message = $success . ' new feeds added.';
if ($fail) {
$message .= ' ' . $fail . ' feeds could not be added (possibly because they already exist)';
if ($results['fails']) {
$message .= ' ' . $results['fails'] . ' feeds could not be added (possibly because they already exist)';
}
if ($this->_isRest()) {
return $this->RestResponse->saveSuccessResponse('Feed', 'importFeeds', false, $this->response->type(), $message);

View File

@ -905,4 +905,51 @@ class Feed extends AppModel {
}
return $feeds;
}
public function importFeeds($feeds, $user, $default = false) {
$feeds = json_decode($feeds, true);
if (!isset($feeds[0])) {
$feeds = array($feeds);
}
$results = array('successes' => 0, 'fails' => 0);
if (empty($feeds)) return $results;
$existingFeeds = $this->find('all', array());
foreach ($feeds as $feed) {
if ($default) {
$feed['Feed']['default'] = 1;
} else {
$feed['Feed']['default'] = 0;
}
if (isset($feed['Feed']['id'])) {
unset($feed['Feed']['id']);
}
$found = false;
foreach ($existingFeeds as $existingFeed) {
if ($existingFeed['Feed']['url'] == $feed['Feed']['url']) {
$found = true;
}
}
if (!$found) {
$feed['Feed']['tag_id'] = 0;
if (isset($feed['Tag'])) {
$tag_id = $this->Tag->captureTag($feed['Tag'], $user);
if ($tag_id) $feed['Feed']['tag_id'] = $tag_id;
}
$this->create();
if (!$this->save($feed, true, array('name', 'provider', 'url', 'rules', 'source_format', 'fixed_event', 'delta_merge', 'override_ids', 'publish', 'settings', 'tag_id', 'default'))) {
$results['fails']++;
} else {
$results['successes']++;
}
}
}
return $results;
}
public function load_default_feeds() {
$user = array('Role' => array('perm_tag_editor' => 1, 'perm_site_admin' => 1));
$json = file_get_contents(APP . 'files/feed-metadata/defaults.json');
$this->importFeeds($json, $user, true);
return true;
}
}