Sitemap.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Backend Model for Currency import options
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Cron\Model\Config\Backend;
  12. /**
  13. * Sitemap configuration
  14. */
  15. class Sitemap extends \Magento\Framework\App\Config\Value
  16. {
  17. /**
  18. * Cron string path
  19. */
  20. const CRON_STRING_PATH = 'crontab/default/jobs/sitemap_generate/schedule/cron_expr';
  21. /**
  22. * Cron mode path
  23. */
  24. const CRON_MODEL_PATH = 'crontab/default/jobs/sitemap_generate/run/model';
  25. /**
  26. * @var \Magento\Framework\App\Config\ValueFactory
  27. */
  28. protected $_configValueFactory;
  29. /**
  30. * @var string
  31. */
  32. protected $_runModelPath = '';
  33. /**
  34. * @param \Magento\Framework\Model\Context $context
  35. * @param \Magento\Framework\Registry $registry
  36. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  37. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  38. * @param \Magento\Framework\App\Config\ValueFactory $configValueFactory
  39. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  40. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  41. * @param string $runModelPath
  42. * @param array $data
  43. */
  44. public function __construct(
  45. \Magento\Framework\Model\Context $context,
  46. \Magento\Framework\Registry $registry,
  47. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  48. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  49. \Magento\Framework\App\Config\ValueFactory $configValueFactory,
  50. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  51. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  52. $runModelPath = '',
  53. array $data = []
  54. ) {
  55. $this->_runModelPath = $runModelPath;
  56. $this->_configValueFactory = $configValueFactory;
  57. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  58. }
  59. /**
  60. * After save handler
  61. *
  62. * @return $this
  63. * @throws \Exception
  64. */
  65. public function afterSave()
  66. {
  67. $time = $this->getData('groups/generate/fields/time/value');
  68. $frequency = $this->getData('groups/generate/fields/frequency/value');
  69. $cronExprArray = [
  70. (int)$time[1], //Minute
  71. (int)$time[0], //Hour
  72. $frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY ? '1' : '*', //Day of the Month
  73. '*', //Month of the Year
  74. $frequency == \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY ? '1' : '*', //# Day of the Week
  75. ];
  76. $cronExprString = join(' ', $cronExprArray);
  77. try {
  78. $this->_configValueFactory->create()->load(
  79. self::CRON_STRING_PATH,
  80. 'path'
  81. )->setValue(
  82. $cronExprString
  83. )->setPath(
  84. self::CRON_STRING_PATH
  85. )->save();
  86. $this->_configValueFactory->create()->load(
  87. self::CRON_MODEL_PATH,
  88. 'path'
  89. )->setValue(
  90. $this->_runModelPath
  91. )->setPath(
  92. self::CRON_MODEL_PATH
  93. )->save();
  94. } catch (\Exception $e) {
  95. throw new \Exception(__('We can\'t save the cron expression.'));
  96. }
  97. return parent::afterSave();
  98. }
  99. }