Skip to content

Commit

Permalink
Improve UI to enable/disable cronjobs (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjaap authored Dec 28, 2023
1 parent da5a94c commit bac46aa
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
62 changes: 62 additions & 0 deletions Controller/Adminhtml/Config/Job/MassDisableJobs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace EthanYehuda\CronjobManager\Controller\Adminhtml\Config\Job;

use EthanYehuda\CronjobManager\Helper\JobConfig;
use Exception;
use Magento\Backend\App\Action\Context;
use Magento\Backend\App\Action;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config as ConfigCache;

class MassDisableJobs extends Action
{
public const ADMIN_RESOURCE = "EthanYehuda_CronjobManager::cronjobmanager";

/**
* @param Context $context
* @param JobConfig $helper
* @param CacheInterface $cache
*/
public function __construct(
Context $context,
private readonly JobConfig $helper,
private readonly CacheInterface $cache,
) {
parent::__construct($context);
}

/**
* Save cronjob
*
* @return void
*/
public function execute()
{
$params = $this->getRequest()->getParam('selected');
if (!isset($params)) {
$this->getMessageManager()->addErrorMessage("Something went wrong when receiving the request");
$this->_redirect('*/config/index');
return;
}

try {
foreach ($params as $jobCode) {
$path = $this->helper->constructFrequencyPath($jobCode);
// Empty frequency disables the job
$this->helper->saveJobFrequencyConfig($path, '');
}

// Clear the config cache
$this->cache->clean([ConfigCache::CACHE_TAG]);
} catch (Exception $e) {
$this->getMessageManager()->addErrorMessage($e->getMessage());
$this->_redirect('*/config/index/');

return;
}

$this->getMessageManager()->addSuccessMessage("Successfully disabled " . count($params) . " jobs");
$this->_redirect("*/config/index/");
}
}
29 changes: 29 additions & 0 deletions Ui/Component/Listing/Column/Frequency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace EthanYehuda\CronjobManager\Ui\Component\Listing\Column;

use Magento\Ui\Component\Listing\Columns\Column;
use EthanYehuda\CronjobManager\Api\Data\ScheduleInterface;

class Frequency extends Column
{
/**
* Prepare Data Source
*
* @param array $dataSource
*
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
if (empty($item['frequency'])) {
$item['frequency'] = __('Disabled');
}
}
}

return $dataSource;
}
}
15 changes: 14 additions & 1 deletion view/adminhtml/ui_component/cronjobmanager_config_grid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@
</item>
</argument>
</action>
<action name="disable_jobs">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Disable Jobs</item>
<item name="message" xsi:type="string" translate="true">Are you sure you want to disable these cron jobs (use Restore to re-enable them)?</item>
</item>
<item name="type" xsi:type="string">disable_jobs</item>
<item name="label" xsi:type="string" translate="true">Disable Jobs</item>
<item name="url" xsi:type="url" path="cronjobmanager/config_job/massDisableJobs" />
</item>
</argument>
</action>
<action name="restore">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
Expand Down Expand Up @@ -138,7 +151,7 @@
</item>
</argument>
</column>
<column name="frequency">
<column name="frequency" class="EthanYehuda\CronjobManager\Ui\Component\Listing\Column\Frequency">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
Expand Down

0 comments on commit bac46aa

Please sign in to comment.