Run.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Controller\Adminhtml\Import;
  9. /**
  10. * Run import controller
  11. */
  12. class Run extends \Magento\Backend\App\Action
  13. {
  14. /**
  15. * Run import
  16. * @return \Magento\Framework\Controller\ResultInterface
  17. */
  18. public function execute()
  19. {
  20. set_time_limit(0);
  21. $data = $this->getRequest()->getPost();
  22. $type = '';
  23. try {
  24. if (empty($data['type'])) {
  25. throw new \Exception(__('Blog import type is not specified.'), 1);
  26. }
  27. $_type = ucfirst($data['type']);
  28. $import = $this->_objectManager->create('\Magefan\Blog\Model\Import\\'.$_type);
  29. $type = $data['type'];
  30. $import->prepareData($data)->execute();
  31. $stats = $import->getImportStatistic();
  32. if ($stats->getData('imported_count')) {
  33. if (!$stats->getData('skipped_count')) {
  34. $this->messageManager->addSuccess(__(
  35. 'The import process was completed successfully. %1 posts and %2 categories where imported.',
  36. $stats->getData('imported_posts_count'),
  37. $stats->getData('imported_categories_count')
  38. ));
  39. } else {
  40. $this->messageManager->addNotice(__(
  41. 'The import process completed. %1 posts and %2 categories and %3 tags where imported. Some posts or categories or tags where skipped.<br/> %3 %4',
  42. $stats->getData('imported_posts_count'),
  43. $stats->getData('imported_categories_count'),
  44. $stats->getData('imported_tags_count'),
  45. $stats->getData('skipped_posts') ? __('Skipped Posts') . ': '. implode(', ', $stats->getData('skipped_posts')) . '.<br/>' : '',
  46. $stats->getData('skipped_posts') ? __('Skipped Categories') . ': '. implode(', ', $stats->getData('skipped_categories')) . '. ' : '',
  47. $stats->getData('skipped_posts') ? __('Skipped Tags') . ': '. implode(', ', $stats->getData('skipped_tags')) . '. ' : ''
  48. ));
  49. }
  50. } else {
  51. if (!$stats->getData('skipped_count')) {
  52. $this->messageManager->addNotice(__('Nothing to import.'));
  53. } else {
  54. throw new \Exception(__('Can not make import.'), 1);
  55. }
  56. }
  57. $this->_getSession()->setData('import_'.$type.'_form_data', null);
  58. $this->_redirect('*/*/');
  59. } catch (\Exception $e) {
  60. $this->messageManager->addException($e, __('Something went wrong: ').' '.$e->getMessage());
  61. $this->_getSession()->setData('import_'.$type.'_form_data', $data);
  62. $this->_redirect('*/*/'.$type);
  63. }
  64. }
  65. /**
  66. * Check is allowed access
  67. *
  68. * @return bool
  69. */
  70. protected function _isAllowed()
  71. {
  72. return $this->_authorization->isAllowed('Magefan_Blog::import');
  73. }
  74. }