InlineEdit.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Smartwave\Dailydeals\Controller\Adminhtml\Dailydeal;
  3. abstract class InlineEdit extends \Magento\Backend\App\Action
  4. {
  5. /**
  6. * JSON Factory
  7. *
  8. * @var \Magento\Framework\Controller\Result\JsonFactory
  9. */
  10. protected $jsonFactory;
  11. /**
  12. * Dailydeal Factory
  13. *
  14. * @var \Smartwave\Dailydeals\Model\DailydealFactory
  15. */
  16. protected $dailydealFactory;
  17. /**
  18. * constructor
  19. *
  20. * @param \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
  21. * @param \Smartwave\Dailydeals\Model\DailydealFactory $dailydealFactory
  22. * @param \Magento\Backend\App\Action\Context $context
  23. */
  24. public function __construct(
  25. \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
  26. \Smartwave\Dailydeals\Model\DailydealFactory $dailydealFactory,
  27. \Magento\Backend\App\Action\Context $context
  28. ) {
  29. $this->jsonFactory = $jsonFactory;
  30. $this->dailydealFactory = $dailydealFactory;
  31. parent::__construct($context);
  32. }
  33. /**
  34. * @return \Magento\Framework\Controller\ResultInterface
  35. */
  36. public function execute()
  37. {
  38. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  39. $resultJson = $this->jsonFactory->create();
  40. $error = false;
  41. $messages = [];
  42. $postItems = $this->getRequest()->getParam('items', []);
  43. if (!($this->getRequest()->getParam('isAjax') && count($postItems))) {
  44. return $resultJson->setData([
  45. 'messages' => [__('Please correct the data sent.')],
  46. 'error' => true,
  47. ]);
  48. }
  49. foreach (array_keys($postItems) as $dailydealId) {
  50. /** @var \Smartwave\Dailydeals\Model\Dailydeal $dailydeal */
  51. $dailydeal = $this->dailydealFactory->create()->load($dailydealId);
  52. try {
  53. $dailydealData = $postItems[$dailydealId];//todo: handle dates
  54. $dailydeal->addData($dailydealData);
  55. $dailydeal->save();
  56. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  57. $messages[] = $this->getErrorWithDailydealId($dailydeal, $e->getMessage());
  58. $error = true;
  59. } catch (\RuntimeException $e) {
  60. $messages[] = $this->getErrorWithDailydealId($dailydeal, $e->getMessage());
  61. $error = true;
  62. } catch (\Exception $e) {
  63. $messages[] = $this->getErrorWithDailydealId(
  64. $dailydeal,
  65. __('Something went wrong while saving the Dailydeal.')
  66. );
  67. $error = true;
  68. }
  69. }
  70. return $resultJson->setData([
  71. 'messages' => $messages,
  72. 'error' => $error
  73. ]);
  74. }
  75. /**
  76. * Add Dailydeal id to error message
  77. *
  78. * @param \Smartwave\Dailydeals\Model\Dailydeal $dailydeal
  79. * @param string $errorText
  80. * @return string
  81. */
  82. protected function getErrorWithDailydealId(\Smartwave\Dailydeals\Model\Dailydeal $dailydeal, $errorText)
  83. {
  84. return '[Dailydeal ID: ' . $dailydeal->getId() . '] ' . $errorText;
  85. }
  86. }