Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/controllers/FeedsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function actionEditFeed($feedId = null, $feed = null)
$variables['feed'] = $feed;
}

$variables['feedMethods'] = array('GET' => 'GET', 'POST' => 'POST');
$variables['dataTypes'] = Plugin::$plugin->data->dataTypesList();
$variables['elements'] = Plugin::$plugin->elements->getRegisteredElements();

Expand Down Expand Up @@ -317,6 +318,7 @@ private function _getModelFromPost()
$feed->name = $request->getBodyParam('name', $feed->name);
$feed->feedUrl = $request->getBodyParam('feedUrl', $feed->feedUrl);
$feed->feedType = $request->getBodyParam('feedType', $feed->feedType);
$feed->feedMethod = $request->getBodyParam('feedMethod', $feed->feedMethod);
$feed->primaryElement = $request->getBodyParam('primaryElement', $feed->primaryElement);
$feed->elementType = $request->getBodyParam('elementType', $feed->elementType);
$feed->elementGroup = $request->getBodyParam('elementGroup', $feed->elementGroup);
Expand Down
3 changes: 3 additions & 0 deletions src/datatypes/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function getFeed($url, $settings, $usePrimaryElement = true)

$data = $response['data'];

// Remove BOM from UTF-8 if exists
$data = str_replace("\xEF\xBB\xBF",'',$data);

// Parse the JSON string
try {
$array = JsonHelper::decode($data);
Expand Down
50 changes: 50 additions & 0 deletions src/migrations/m201104_181117_add_feed_method.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace craft\feedme\migrations;

use Craft;
use craft\db\Query;
use craft\db\Migration;

/**
* m201104_181117_add_feed_method migration.
*/
class m201104_181117_add_feed_method extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{


if (!$this->db->columnExists('{{%feedme_feeds}}', 'feedMethod')) {
$this->addColumn('{{%feedme_feeds}}', 'feedMethod', $this->string()->after('feedType'));

$feeds = (new Query())
->select(['*'])
->from(['{{%feedme_feeds}}'])
->all();

foreach ($feeds as $i => $feed) {
$this->update('{{%feedme_feeds}}', ['feedMethod' => 'GET'], ['id' => $feed['id']]);
}
}

// Place migration code here...
}

/**
* @inheritdoc
*/
public function safeDown()
{
if ($this->db->columnExists('{{%feedme_feeds}}', 'feedMethod')) {
$this->dropColumn('{{%feedme_feeds}}', 'feedMethod');
return true;
} else {
echo "m201104_181117_add_feed_method cannot be reverted.\n";
return false;
}
}
}
3 changes: 2 additions & 1 deletion src/models/FeedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class FeedModel extends Model
public $name;
public $feedUrl;
public $feedType;
public $feedMethod;
public $primaryElement;
public $elementType;
public $elementGroup;
Expand Down Expand Up @@ -121,7 +122,7 @@ public function getNextPagination()
public function rules()
{
return [
[['name', 'feedUrl', 'feedType', 'elementType', 'duplicateHandle', 'passkey'], 'required'],
[['name', 'feedUrl', 'feedType', 'feedMethod', 'elementType', 'duplicateHandle', 'passkey'], 'required'],
[['backup'], 'boolean'],
];
}
Expand Down
6 changes: 5 additions & 1 deletion src/services/DataTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ public function getRawData($url, $feedId = null)
try {
$client = Plugin::$plugin->service->createGuzzleClient($feedId);
$options = Plugin::$plugin->service->getRequestOptions($feedId);
$method = Plugin::$plugin->service->getRequestMethod($feedId);

$resp = $client->request('GET', $url, $options);
$resp = $client->request($method, $url, $options);
$data = (string)$resp->getBody();

// Save headers for later
Expand Down Expand Up @@ -288,6 +289,7 @@ public function getFeedForTemplate($options = [])

$url = Hash::get($options, 'url');
$type = Hash::get($options, 'type');
$method = Hash::get($options, 'method');
$element = Hash::get($options, 'element');
$cache = Hash::get($options, 'cache', true);

Expand All @@ -307,6 +309,8 @@ public function getFeedForTemplate($options = [])
$feed = new FeedModel();
$feed->feedUrl = $url;
$feed->feedType = $type;
$feed->feedMethod = $method;


if ($element) {
$feed->primaryElement = $element;
Expand Down
2 changes: 2 additions & 0 deletions src/services/Feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function saveFeed(FeedModel $model, bool $runValidation = true): bool
$record->name = $model->name;
$record->feedUrl = $model->feedUrl;
$record->feedType = $model->feedType;
$record->feedMethod = $model->feedMethod;
$record->primaryElement = $model->primaryElement;
$record->elementType = $model->elementType;
$record->siteId = $model->siteId;
Expand Down Expand Up @@ -193,6 +194,7 @@ private function _getQuery()
'name',
'feedUrl',
'feedType',
'feedMethod',
'primaryElement',
'elementType',
'elementGroup',
Expand Down
16 changes: 15 additions & 1 deletion src/services/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,32 @@ public function getConfig($key, $feedId = null)

return $configItem;
}
public function getFeedConfig($key, $feedId = null)
{
$configItem = false;
if ($feedId) {
$feed = Plugin::$plugin->feeds->getFeedById($feedId);
$configItem = Hash::get($feed, $key);
}
return $configItem;
}


public function createGuzzleClient($feedId = null)
{
$options = $this->getConfig('clientOptions', $feedId);

return Craft::createGuzzleClient($options);
}

public function getRequestOptions($feedId = null)
{
$options = $this->getConfig('requestOptions', $feedId);
return $options;
}

public function getRequestMethod($feedId = null)
{
$options = $this->getFeedConfig('feedMethod', $feedId);
return $options;
}

Expand Down
11 changes: 11 additions & 0 deletions src/templates/feeds/_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@
required: true,
}) }}

{{ forms.selectField({
label: "Feed Method" | t('feed-me'),
instructions: 'Choose the HTTP request method to use.' | t('feed-me'),
id: 'feedMethod',
name: 'feedMethod',
options: feedMethods,
value: feed.feedMethod,
errors: feed.getErrors('feedMethod'),
required: true,
}) }}

<hr>

{% set elementsList = [] %}
Expand Down