From e7dfa6723522231a613c5a58abfa200903c37cc9 Mon Sep 17 00:00:00 2001 From: mcabrera Date: Wed, 6 Feb 2019 21:22:06 -0300 Subject: [PATCH] fixer.io integration --- .../FeedGenerator/Api/FixerioInterface.php | 16 +++ .../Block/Adminhtml/Config/View.php | 52 ++++++++ .../Controller/Adminhtml/Feed/View.php | 49 +++++++ .../MindArc/FeedGenerator/Cron/Generator.php | 45 +++++++ .../MindArc/FeedGenerator/Helper/Data.php | 75 +++++++++++ .../MindArc/FeedGenerator/Helper/Fixerio.php | 39 ++++++ app/code/MindArc/FeedGenerator/Model/Feed.php | 122 ++++++++++++++++++ .../MindArc/FeedGenerator/Model/Fixerio.php | 42 ++++++ app/code/MindArc/FeedGenerator/README.md | 4 + .../FeedGenerator/etc/adminhtml/routes.xml | 9 ++ .../FeedGenerator/etc/adminhtml/system.xml | 18 +++ .../MindArc/FeedGenerator/etc/crontab.xml | 8 ++ app/code/MindArc/FeedGenerator/etc/di.xml | 4 + app/code/MindArc/FeedGenerator/etc/module.xml | 5 + app/code/MindArc/FeedGenerator/etc/webapi.xml | 9 ++ .../MindArc/FeedGenerator/registration.php | 6 + .../templates/system/config/view.phtml | 7 + 17 files changed, 510 insertions(+) create mode 100644 app/code/MindArc/FeedGenerator/Api/FixerioInterface.php create mode 100644 app/code/MindArc/FeedGenerator/Block/Adminhtml/Config/View.php create mode 100644 app/code/MindArc/FeedGenerator/Controller/Adminhtml/Feed/View.php create mode 100644 app/code/MindArc/FeedGenerator/Cron/Generator.php create mode 100644 app/code/MindArc/FeedGenerator/Helper/Data.php create mode 100644 app/code/MindArc/FeedGenerator/Helper/Fixerio.php create mode 100644 app/code/MindArc/FeedGenerator/Model/Feed.php create mode 100644 app/code/MindArc/FeedGenerator/Model/Fixerio.php create mode 100644 app/code/MindArc/FeedGenerator/README.md create mode 100644 app/code/MindArc/FeedGenerator/etc/adminhtml/routes.xml create mode 100644 app/code/MindArc/FeedGenerator/etc/adminhtml/system.xml create mode 100644 app/code/MindArc/FeedGenerator/etc/crontab.xml create mode 100644 app/code/MindArc/FeedGenerator/etc/di.xml create mode 100644 app/code/MindArc/FeedGenerator/etc/module.xml create mode 100644 app/code/MindArc/FeedGenerator/etc/webapi.xml create mode 100644 app/code/MindArc/FeedGenerator/registration.php create mode 100644 app/code/MindArc/FeedGenerator/view/adminhtml/templates/system/config/view.phtml diff --git a/app/code/MindArc/FeedGenerator/Api/FixerioInterface.php b/app/code/MindArc/FeedGenerator/Api/FixerioInterface.php new file mode 100644 index 0000000..b575531 --- /dev/null +++ b/app/code/MindArc/FeedGenerator/Api/FixerioInterface.php @@ -0,0 +1,16 @@ +getTemplate()) { + $this->setTemplate(static::BUTTON_TEMPLATE); + } + + return $this; + } + + public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) + { + $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue(); + + return parent::render($element); + } + + protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) + { + $this->addData( + [ + 'url' => $this->getUrl(), + 'html_id' => $element->getHtmlId(), + ] + ); + + return $this->_toHtml(); + } + + public function getUrl($route = '', $params = []) + { + return parent::getUrl('generate/feed/view'); + } + +} \ No newline at end of file diff --git a/app/code/MindArc/FeedGenerator/Controller/Adminhtml/Feed/View.php b/app/code/MindArc/FeedGenerator/Controller/Adminhtml/Feed/View.php new file mode 100644 index 0000000..07aa25e --- /dev/null +++ b/app/code/MindArc/FeedGenerator/Controller/Adminhtml/Feed/View.php @@ -0,0 +1,49 @@ +feedHelper = $feedHelper; + $this->feed = $feed; + $this->messageManager = $context->getMessageManager(); + parent::__construct($context); + } + + public function execute() + { + $feedGenerated = $this->feed->generateFeed(); + + $this->showFeedContents($feedGenerated); + + if ($feedGenerated) { + $this->messageManager->addNoticeMessage('Feed has been generated successful'); + } else { + $this->messageManager->addErrorMessage('Error to generated the feed'); + } + } + + protected function showFeedContents($feed) + { + header("Content-Type: application/xml; charset=utf-8"); + echo $feed; + } + + +} \ No newline at end of file diff --git a/app/code/MindArc/FeedGenerator/Cron/Generator.php b/app/code/MindArc/FeedGenerator/Cron/Generator.php new file mode 100644 index 0000000..48cfc83 --- /dev/null +++ b/app/code/MindArc/FeedGenerator/Cron/Generator.php @@ -0,0 +1,45 @@ +feed = $feed; + $this->feedHelper = $feedHelper; + $this->logger = $logger; + $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); + } + + public function execute() + { + $this->logger->info('Feed generation started'); + + $feedGenerated = $this->feed->generateFeed(); + + $filename = $this->feedHelper->getFileName('feed'); + $path = $this->feedHelper->getFeedPath(); + + $result = $this->mediaDirectory->writeFile($path . $filename, $feedGenerated); + + if (!$result) { + $this->logger->error('The feed has not been generated'); + } else { + $this->logger->info('Feed generation finished'); + } + } +} \ No newline at end of file diff --git a/app/code/MindArc/FeedGenerator/Helper/Data.php b/app/code/MindArc/FeedGenerator/Helper/Data.php new file mode 100644 index 0000000..cfda86a --- /dev/null +++ b/app/code/MindArc/FeedGenerator/Helper/Data.php @@ -0,0 +1,75 @@ +dateTime = $dateTime; + $this->storeManager = $storeManager; + } + + public function getImageLink($product) + { + $urlImage = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . + $product->getImage(); + + return $urlImage; + } + + public function getWebLink() + { + return $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); + } + + public function getPrice($product) + { + return $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue(); + } + + /* + * This is could be a custom attribute product. Magento has not condition attribute for products + * so you can write your custom code here + */ + public function getProductAttribute($product, $attributeName = 'condition') + { + if ($attributeName == 'condition') { + return 'new'; + } else { + $attributeValue = $product->getData($attributeName); + if (!empty($attributeValue)) { + return $attributeValue; + } + } + + return ''; + + } + + public function getFileName($filename, $format = 'xml') + { + $filename = $filename . '_'; + $filename .= $this->dateTime->gmtDate('d-m-Y_h:m:i'); + $filename .= '.' . $format; + + return $filename; + } + + public function getFeedPath() + { + return self::XML_TMP_PATH; + } + +} diff --git a/app/code/MindArc/FeedGenerator/Helper/Fixerio.php b/app/code/MindArc/FeedGenerator/Helper/Fixerio.php new file mode 100644 index 0000000..2d4369f --- /dev/null +++ b/app/code/MindArc/FeedGenerator/Helper/Fixerio.php @@ -0,0 +1,39 @@ +api = $api; + $this->response = []; + } + + public function convert($base, $currency, $amount) + { + if (!count($this->response)) {//it avoids always consult the services + $this->response = $this->api->connectService(); + } + $rates = get_object_vars($this->response->rates); + $rate = isset($rates[$currency]) ? $rates[$currency] : 1; + + if ($base == $this->response->base) { + $convertedAmount = $amount; + } else { + $convertedAmount = $amount * $rate; + } + + return $convertedAmount; + } +} diff --git a/app/code/MindArc/FeedGenerator/Model/Feed.php b/app/code/MindArc/FeedGenerator/Model/Feed.php new file mode 100644 index 0000000..4c64bd3 --- /dev/null +++ b/app/code/MindArc/FeedGenerator/Model/Feed.php @@ -0,0 +1,122 @@ +storeManager = $storeManager; + $this->fixerio = $fixerio; + $this->productCollection = $productCollection; + $this->store = $store; + $this->helper = $helper; + $this->title = 'The name of your data feed'; + $this->description = 'A description of your content'; + } + + protected function prepareCollection() + { + return $this->productCollection->create()->addFieldToSelect('*')->setPageSize(6)->load(); + } + + public function generateFeed() + { + $content = $this->generateHeadFeed(); + $dom = $content['dom']; + $rss = $content['rss']; + + $channel = $rss->appendChild($dom->createElement('channel')); + $channel->appendChild($dom->createElement('title', $this->title)); + $channel->appendChild($dom->createElement('link', $this->helper->getWebLink())); + $description = $channel->appendChild($dom->createElement('description')); + $description->appendChild($dom->createCDATASection($this->description)); + + $items = $this->prepareCollection(); + + $this->generateItems($items, $channel, $dom); + + return $dom->saveXML(); + } + + protected function generateHeadFeed() + { + $dom = new \DOMDocument('1.0'); + $dom->formatOutput = true; + $rss = $dom->appendChild($dom->createElement('rss')); + $rss->setAttribute('version', '2.0'); + $rss->setAttribute('xmlns:g', 'http://base.google.com/ns/1.0'); + + return array('dom' => $dom, 'rss' => $rss); + } + + protected function generateItems($items, $channel, $dom) + { + foreach ($items as $product) { + + $item = $channel->appendChild($dom->createElement('item')); + + $item->appendChild($dom->createElement('title', $product->getName())); + $item->appendChild($dom->createElement('link', $product->getProductUrl())); + + $desc = $item->appendChild($dom->createElement('description')); + $desc->appendChild($dom->createCDATASection($product->getDescription())); + + try { + $item->appendChild($dom->createElement('g:image_link', $this->helper->getImageLink($product))); + } catch (\Exception $e) { + $item->appendChild($dom->createElement('g:image_link', __('Product no image'))); + } + $item->appendChild($dom->createElement('g:price', $this->helper->getPrice($product))); + + $item->appendChild($dom->createElement('g:condition', $this->helper->getProductAttribute($product))); + + $item->appendChild($dom->createElement('g:id', $product->getSku())); + + $storeCode = $this->storeManager->getStore()->getCurrentCurrencyCode(); + $priceConverted = $this->getConvertedPrice($storeCode, 'USD', $product->getPrice()); + + $item->appendChild($dom->createElement('g:converted_price', $priceConverted)); + + } + } + + protected function getConvertedPrice($base, $currency, $price) + { + return bcdiv($this->fixerio->convert($base, $currency, $price), 1, 2); + } +} \ No newline at end of file diff --git a/app/code/MindArc/FeedGenerator/Model/Fixerio.php b/app/code/MindArc/FeedGenerator/Model/Fixerio.php new file mode 100644 index 0000000..29ab425 --- /dev/null +++ b/app/code/MindArc/FeedGenerator/Model/Fixerio.php @@ -0,0 +1,42 @@ +curlClient = $curl; + } + + /** + * @throws \Magento\Framework\Exception\NoSuchEntityException + * @return mixed + */ + public function connectService() + { + try { + $this->curlClient->get(self::SERVICE_URL . '?' . build_query(["access_key" => self::ACCESS_KEY])); + if ($this->curlClient->getStatus() == 200) { + $body = $this->curlClient->getBody(); + $response = json_decode($body); + } else { + $response = ['success' => false, 'message' => 'Fixerio connection cannot be established']; + } + } catch (\Exception $e) { + $response = ['success' => false, 'message' => $e->getMessage()]; + } + + return $response; + } +} \ No newline at end of file diff --git a/app/code/MindArc/FeedGenerator/README.md b/app/code/MindArc/FeedGenerator/README.md new file mode 100644 index 0000000..5d41ac9 --- /dev/null +++ b/app/code/MindArc/FeedGenerator/README.md @@ -0,0 +1,4 @@ +Feed Generator functionality. + +To Generate the view, go to Stores->Configuration->General->Feed Generator. +To see the feed's file, go to media/Feed/ \ No newline at end of file diff --git a/app/code/MindArc/FeedGenerator/etc/adminhtml/routes.xml b/app/code/MindArc/FeedGenerator/etc/adminhtml/routes.xml new file mode 100644 index 0000000..2d09af0 --- /dev/null +++ b/app/code/MindArc/FeedGenerator/etc/adminhtml/routes.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/code/MindArc/FeedGenerator/etc/adminhtml/system.xml b/app/code/MindArc/FeedGenerator/etc/adminhtml/system.xml new file mode 100644 index 0000000..1e8773a --- /dev/null +++ b/app/code/MindArc/FeedGenerator/etc/adminhtml/system.xml @@ -0,0 +1,18 @@ + + + + +
+ + general + MindArc_FeedGenerator::FeedGenerator + + + + + MindArc\FeedGenerator\Block\Adminhtml\Config\View + + +
+
+
diff --git a/app/code/MindArc/FeedGenerator/etc/crontab.xml b/app/code/MindArc/FeedGenerator/etc/crontab.xml new file mode 100644 index 0000000..9fdfcf4 --- /dev/null +++ b/app/code/MindArc/FeedGenerator/etc/crontab.xml @@ -0,0 +1,8 @@ + + + + + 0 0 * * * + + + diff --git a/app/code/MindArc/FeedGenerator/etc/di.xml b/app/code/MindArc/FeedGenerator/etc/di.xml new file mode 100644 index 0000000..b68a4c0 --- /dev/null +++ b/app/code/MindArc/FeedGenerator/etc/di.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/app/code/MindArc/FeedGenerator/etc/module.xml b/app/code/MindArc/FeedGenerator/etc/module.xml new file mode 100644 index 0000000..34b59e8 --- /dev/null +++ b/app/code/MindArc/FeedGenerator/etc/module.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/app/code/MindArc/FeedGenerator/etc/webapi.xml b/app/code/MindArc/FeedGenerator/etc/webapi.xml new file mode 100644 index 0000000..3b616de --- /dev/null +++ b/app/code/MindArc/FeedGenerator/etc/webapi.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/app/code/MindArc/FeedGenerator/registration.php b/app/code/MindArc/FeedGenerator/registration.php new file mode 100644 index 0000000..80568d9 --- /dev/null +++ b/app/code/MindArc/FeedGenerator/registration.php @@ -0,0 +1,6 @@ + +

+ +

+ \ No newline at end of file