Skip to content

Commit 4b8554c

Browse files
authored
Add oneshot v5 (#3)
* Added possibility to create one shot job from scheduled job
1 parent 4b8403b commit 4b8554c

File tree

7 files changed

+92
-9
lines changed

7 files changed

+92
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Version 5.1.0
2+
* Added possibility to create one shot job from scheduled job
3+
14
# Version 5.0.0
25

36
* Renamed bundle to IbexaDataflowBundle

src/Controller/JobController.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
use CodeRhapsodie\DataflowBundle\Entity\Job;
88
use CodeRhapsodie\IbexaDataflowBundle\Form\CreateOneshotType;
99
use CodeRhapsodie\IbexaDataflowBundle\Gateway\JobGateway;
10+
use CodeRhapsodie\IbexaDataflowBundle\Gateway\ScheduledDataflowGateway;
1011
use Ibexa\Contracts\AdminUi\Controller\Controller;
1112
use Ibexa\Contracts\AdminUi\Notification\NotificationHandlerInterface;
1213
use Ibexa\Core\MVC\Symfony\Security\Authorization\Attribute;
1314
use Symfony\Component\HttpFoundation\JsonResponse;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1618
use Symfony\Component\Routing\Annotation\Route;
1719
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1820
use Symfony\Contracts\Translation\TranslatorInterface;
1921

2022
#[Route(path: '/ibexa_dataflow/job')]
2123
class JobController extends Controller
2224
{
23-
public function __construct(private readonly JobGateway $jobGateway, private readonly NotificationHandlerInterface $notificationHandler, private readonly TranslatorInterface $translator)
25+
public function __construct(private readonly JobGateway $jobGateway, private readonly NotificationHandlerInterface $notificationHandler, private readonly TranslatorInterface $translator, private readonly ScheduledDataflowGateway $scheduledDataflowGateway)
2426
{
2527
}
2628

@@ -81,4 +83,35 @@ public function create(Request $request): Response
8183
]),
8284
]);
8385
}
86+
87+
#[Route(path: '/run-oneshot/{id}', name: 'coderhapsodie.ibexa_dataflow.job.run-oneshot', methods: ['GET'])]
88+
public function runOneShot(int $id): Response
89+
{
90+
$this->denyAccessUnlessGranted(new Attribute('ibexa_dataflow', 'view'));
91+
92+
$scheduledDataflow = $this->scheduledDataflowGateway->find($id);
93+
94+
if ($scheduledDataflow === null) {
95+
throw new NotFoundHttpException();
96+
}
97+
98+
$newOneshotJob = new Job();
99+
$newOneshotJob->setOptions($scheduledDataflow->getOptions());
100+
$newOneshotJob->setLabel("Manual " . $scheduledDataflow->getLabel());
101+
$newOneshotJob->setScheduledDataflowId($scheduledDataflow->getId());
102+
$newOneshotJob->setRequestedDate((new \DateTime())->add(new \DateInterval('PT1H')));
103+
$newOneshotJob->setDataflowType($scheduledDataflow->getDataflowType());
104+
105+
$form = $this->createForm(CreateOneshotType::class, $newOneshotJob, [
106+
'action' => $this->generateUrl('coderhapsodie.ibexa_dataflow.job.create'),
107+
]);
108+
109+
return new JsonResponse([
110+
'form' => $this->renderView('@ibexadesign/ibexa_dataflow/parts/form_modal.html.twig', [
111+
'form' => $form->createView(),
112+
'id' => 'modal-new-oneshot',
113+
'mode' => 'oneshot',
114+
]),
115+
]);
116+
}
84117
}

src/Resources/config/services.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ services:
3838
$jobGateway: '@CodeRhapsodie\IbexaDataflowBundle\Gateway\JobGateway'
3939
$notificationHandler: '@Ibexa\Contracts\AdminUi\Notification\NotificationHandlerInterface'
4040
$translator: '@translator'
41+
$scheduledDataflowGateway: '@CodeRhapsodie\IbexaDataflowBundle\Gateway\ScheduledDataflowGateway'
4142
calls:
4243
- [ 'setContainer', [ '@service_container' ] ]
4344
- [ 'performAccessCheck', [ ] ]

src/Resources/translations/messages.en.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ coderhapsodie.ibexa_dataflow.workflow.list.history: History
1616
coderhapsodie.ibexa_dataflow.workflow.list.edit: Edit
1717
coderhapsodie.ibexa_dataflow.workflow.list.disable: Disable
1818
coderhapsodie.ibexa_dataflow.workflow.list.enable: Enable
19+
coderhapsodie.ibexa_dataflow.workflow.list.runonce: Run now
1920
coderhapsodie.ibexa_dataflow.history.title: History
2021
coderhapsodie.ibexa_dataflow.history.list.title: 'Executions list'
2122
coderhapsodie.ibexa_dataflow.history.list.name: Name

src/Resources/translations/messages.fr.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ coderhapsodie.ibexa_dataflow.workflow.list.history: Historique
1616
coderhapsodie.ibexa_dataflow.workflow.list.edit: Éditer
1717
coderhapsodie.ibexa_dataflow.workflow.list.disable: Désactiver
1818
coderhapsodie.ibexa_dataflow.workflow.list.enable: Activer
19+
coderhapsodie.ibexa_dataflow.workflow.list.runonce: Lancer maintenant
1920
coderhapsodie.ibexa_dataflow.history.title: Historique
2021
coderhapsodie.ibexa_dataflow.history.list.title: 'Liste des exécutions'
2122
coderhapsodie.ibexa_dataflow.history.list.name: Nom

src/Resources/views/themes/admin/ibexa_dataflow/Dashboard/oneshot.html.twig

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{%- block content -%}
22
{% set actions %}
33
<button
4-
type="button"
5-
class="btn ibexa-btn ibexa-btn--tertiary ibexa-btn--small"
6-
data-bs-toggle="modal"
7-
data-bs-target="#modal-new-oneshot"
4+
id="create-oneshot-button"
5+
type="button"
6+
class="btn ibexa-btn ibexa-btn--tertiary ibexa-btn--small"
7+
data-bs-toggle="modal"
8+
data-bs-target="#modal-new-oneshot"
89
>
910
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--create">
1011
<use xlink:href="{{ ibexa_icon_path('create') }}"></use>
@@ -53,6 +54,16 @@
5354
})
5455
;
5556
});
57+
const createButton = document.getElementById('create-oneshot-button')
58+
59+
if (createButton) {
60+
createButton.addEventListener('click', () => {
61+
const oneShotModal = document.getElementById('modal-new-oneshot');
62+
oneShotModal.querySelector('#create_oneshot_label').value = '';
63+
oneShotModal.querySelector('#create_oneshot_options').value = '';
64+
oneShotModal.querySelector('.flatpickr.flatpickr-input').parentNode.parentNode.ibexaInstance.flatpickrInstance.setDate(new Date(), true);
65+
});
66+
}
5667
});
5768
</script>
5869
{%- endblock -%}

src/Resources/views/themes/admin/ibexa_dataflow/parts/tab/schedule_list.html.twig

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040
<use xlink:href="{{ ibexa_icon_path('edit') }}"></use>
4141
</svg>
4242
</a>
43+
<button
44+
class="btn ibexa-btn ibexa-btn--ghost run-oneshot ibexa-btn--no-text"
45+
data-url="{{ path('coderhapsodie.ibexa_dataflow.job.run-oneshot', {id: item.id}) }}"
46+
title="{{ 'coderhapsodie.ibexa_dataflow.workflow.list.runonce'|trans }}"
47+
>
48+
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--create">
49+
<use xlink:href="{{ ibexa_icon_path('create') }}"></use>
50+
</svg>
51+
</button>
4352
{% if item.enabled %}
4453
<a href="{{ path('coderhapsodie.ibexa_dataflow.workflow.disable', {id: item.id}) }}"
4554
class="btn ibexa-btn ibexa-btn--ghost ibexa-btn--no-text"
@@ -96,10 +105,10 @@
96105
{% embed '@ibexadesign/ui/component/table/table_header.html.twig' %}
97106
{% block actions %}
98107
<button
99-
type="button"
100-
class="btn ibexa-btn ibexa-btn--tertiary ibexa-btn--small"
101-
data-bs-toggle="modal"
102-
data-bs-target="#modal-new-scheduled"
108+
type="button"
109+
class="btn ibexa-btn ibexa-btn--tertiary ibexa-btn--small"
110+
data-bs-toggle="modal"
111+
data-bs-target="#modal-new-scheduled"
103112
>
104113
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--create">
105114
<use xlink:href="{{ ibexa_icon_path('create') }}"></use>
@@ -125,3 +134,27 @@
125134
{% endif %}
126135
</div>
127136
</div>
137+
138+
<script>
139+
document.querySelectorAll(".run-oneshot").forEach((el) => {
140+
el.addEventListener('click', () => {
141+
const oneShotModal = document.getElementById('modal-new-oneshot');
142+
143+
fetch(el.getAttribute('data-url'))
144+
.then(response => response.json())
145+
.then(result => {
146+
const node = document.createElement('div');
147+
node.innerHTML = result.form;
148+
oneShotModal.querySelector('#create_oneshot_label').value = node.querySelector('#create_oneshot_label').value;
149+
oneShotModal.querySelector('#create_oneshot_options').value = node.querySelector('#create_oneshot_options').value;
150+
oneShotModal.querySelector('.ibexa-dropdown').ibexaInstance.selectOption(node.querySelector('#create_oneshot_dataflowType').value)
151+
oneShotModal.querySelector('.flatpickr.flatpickr-input').parentNode.parentNode.ibexaInstance.flatpickrInstance.setDate(new Date(), true);
152+
})
153+
.then(() => {
154+
bootstrap.Tab.getOrCreateInstance(document.querySelector('#ibexa-tab-label-coderhapsodie-ibexa_dataflow-code-rhapsodie-ibexa_dataflow-oneshot')).show()
155+
bootstrap.Modal.getOrCreateInstance(oneShotModal).show()
156+
})
157+
158+
});
159+
});
160+
</script>

0 commit comments

Comments
 (0)