Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #8 from tuyennn/feature/cron-auto-post
Browse files Browse the repository at this point in the history
Feature/cron auto post
  • Loading branch information
tuyennn authored Mar 29, 2019
2 parents 5515d91 + 551964b commit ab245b3
Show file tree
Hide file tree
Showing 19 changed files with 1,367 additions and 837 deletions.
20 changes: 20 additions & 0 deletions Api/WorkerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace GhoSter\AutoInstagramPost\Api;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;

interface WorkerInterface {

/**
* Post to Instagram by Product
* @param Product $product
*/
public function postInstagramByProduct(Product $product);

/**
* @param ProductCollection $collection
* @return mixed
*/
public function postInstagramByProductCollection(ProductCollection $collection);
}
19 changes: 14 additions & 5 deletions Block/Adminhtml/System/Config/Hashtag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@

namespace GhoSter\AutoInstagramPost\Block\Adminhtml\System\Config;

class Hashtag extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use GhoSter\AutoInstagramPost\Block\Adminhtml\System\Config\Hashtag\Activation;

class Hashtag extends AbstractFieldArray
{

/**
* @var $_attributesRenderer \GhoSter\AutoInstagramPost\Block\Adminhtml\System\Config\Hashtag\Activation
* @var $_attributesRenderer Activation
*/
protected $_activation;

/**
* Get activation options.
*
* @return \GhoSter\AutoInstagramPost\Block\Adminhtml\System\Config\Hashtag\Activation
* @return Activation
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _getActivationRenderer()
{
if (!$this->_activation) {
$this->_activation = $this->getLayout()->createBlock(
'\GhoSter\AutoInstagramPost\Block\Adminhtml\System\Config\Hashtag\Activation',
Activation::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
Expand All @@ -34,7 +37,13 @@ protected function _getActivationRenderer()
*/
protected function _prepareToRender()
{
$this->addColumn('hashtag', ['label' => __('Hashtag Value'), 'renderer' => false]);
$this->addColumn(
'hashtag',
[
'label' => __('Hashtag Value'),
'renderer' => false
]
);
$this->addColumn(
'status',
[
Expand Down
136 changes: 32 additions & 104 deletions Controller/Adminhtml/Manage/MassPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,33 @@
use Magento\Backend\App\Action;
use Magento\Framework\Exception\LocalizedException;
use GhoSter\AutoInstagramPost\Model\Instagram;
use GhoSter\AutoInstagramPost\Model\Instagram\Worker as InstagramWorker;
use GhoSter\AutoInstagramPost\Model\Item as InstagramItem;
use Magento\Catalog\Model\ProductFactory;
use GhoSter\AutoInstagramPost\Model\ImageProcessor;
use Magento\Framework\Exception\NotFoundException;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use GhoSter\AutoInstagramPost\Model\Logger;
use GhoSter\AutoInstagramPost\Model\Logger as InstagramLogger;
use GhoSter\AutoInstagramPost\Model\Config as InstagramConfig;
use GhoSter\AutoInstagramPost\Helper\Data as InstagramHelper;


class MassPost extends Action
{
/**
* @var \GhoSter\AutoInstagramPost\Helper\Data
*/
protected $helper;

/**
* @var Logger
*/
protected $_logger;

/** @var InstagramConfig */
protected $config;

/**
* @var array
* @var InstagramWorker
*/
protected $_account;

/**
* @var Instagram
*/
protected $_instagram;

/**
* @var \Magento\Framework\App\Filesystem\DirectoryList
*/
protected $_directoryList;

/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
protected $instagramWorker;

/**
* @var ProductFactory
*/
protected $productFactory;

/**
* @var ImageProcessor
*/
protected $imageProcessor;

/**
* MassActions filter
*
Expand All @@ -72,28 +45,34 @@ class MassPost extends Action
protected $collectionFactory;


/**
* MassPost constructor.
*
* @param Action\Context $context
* @param ProductFactory $productFactory
* @param InstagramWorker $instagramWorker
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
public function __construct(
Action\Context $context,
\GhoSter\AutoInstagramPost\Helper\Data $helper,
ProductFactory $productFactory,
Instagram $instagram,
ImageProcessor $imageProcessor,
InstagramWorker $instagramWorker,
Filter $filter,
CollectionFactory $collectionFactory,
Logger $logger
)
CollectionFactory $collectionFactory
)
{
$this->helper = $helper;
$this->productFactory = $productFactory;
$this->_instagram = $instagram;
$this->imageProcessor = $imageProcessor;
$this->_account = $this->helper->getAccountInformation();
$this->instagramWorker = $instagramWorker;
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
$this->_logger = $logger;
parent::__construct($context);
}

/**
* @return \Magento\Backend\Model\View\Result\Redirect|\Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface
* @throws NotFoundException
*/
public function execute()
{
if (!$this->getRequest()->isPost()) {
Expand All @@ -105,58 +84,15 @@ public function execute()
$productIds = $this->getRequest()->getParam('selected');

if (!empty($productIds)) {
$storeId = (int)$this->getRequest()->getParam('store', 0);

$productCollection = $this->collectionFactory->create()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', ['in' => $productIds]);
try {

if (!empty($this->_account)
&& isset($this->_account['username'])
&& isset($this->_account['password'])) {
$this->_getInstagram()->setUser(
$this->_account['username'],
$this->_account['password']
);
}
$result = $this->instagramWorker->postInstagramByProductCollection($productCollection);


if (!$this->_getInstagram()->login()) {
$this->messageManager->addErrorMessage(__('Unauthorized Instagram Account, check your user/password setting'));
}

$productUploaded = $errorNumber = 0;

foreach ($productCollection as $product) {

$image = $this->imageProcessor->processBaseImage($product);

if ($image) {
$caption = $this->helper->getInstagramPostDescription($product);
$result = $this->_getInstagram()->uploadPhoto($image, $caption);

if (empty($result)) {
$errorNumber++;
$this->_logger->recordInstagramLog(
$product,
[],
InstagramItem::TYPE_ERROR
);
}

if (isset($result['status'])
&& $result['status'] === Instagram::STATUS_OK
) {
$productUploaded++;
$this->_logger->recordInstagramLog(
$product,
$result,
InstagramItem::TYPE_SUCCESS
);
}
}
}
$productUploaded = count($result);
$errorNumber = $productCollection->count() - $productUploaded;

if ($errorNumber) {
$this->messageManager->addComplexErrorMessage(
Expand All @@ -178,7 +114,10 @@ public function execute()
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while posting to Instagram.'));
$this->messageManager->addExceptionMessage(
$e,
__('Something went wrong while posting to Instagram.')
);
}

return $resultRedirect->setPath('*/*/');
Expand All @@ -187,17 +126,6 @@ public function execute()
return $resultRedirect->setPath('*/*/');
}

/**
* Get Instagram Client
*
* @return \GhoSter\AutoInstagramPost\Model\Instagram
*/
private function _getInstagram()
{
return $this->_instagram;
}


/**
* Is the user allowed to view the blog post grid.
*
Expand Down
Loading

0 comments on commit ab245b3

Please sign in to comment.