Skip to content

Commit

Permalink
Merge pull request #1 from FriendsOfAkeneo/develop
Browse files Browse the repository at this point in the history
Merge develop with master
  • Loading branch information
nowiko committed Feb 1, 2016
2 parents f012f5c + 85a197b commit 95b2913
Show file tree
Hide file tree
Showing 24 changed files with 1,522 additions and 26 deletions.
215 changes: 215 additions & 0 deletions Controller/DashboardController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php

namespace FOA\CronBundle\Controller;

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\HttpFoundation\Response;
use FOA\CronBundle\Form\Type\CronType;
use FOA\CronBundle\Manager\Cron;
use FOA\CronBundle\Manager\CronManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
* Display dashboard and manage CRUD operations
*/
class DashboardController extends Controller
{
/**
* Displays the current crontab and a form to add a new one.
*
* @AclAncestor("foa_cron_management_index")
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction()
{
$cronManager = new CronManager();
$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());

$form = $this->createForm(new CronType(), new Cron());

return $this->render('FOACronBundle:Dashboard:index.html.twig', [
'crons' => $cronManager->get(),
'raw' => $cronManager->getRaw(),
'form' => $form->createView()
]);
}

/**
* Add a cron to the cron table
*
* @param Request $request
* @return RedirectResponse|Response
*/
public function addAction(Request $request)
{
$cronManager = new CronManager();
$cron = new Cron();
$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());
$form = $this->createForm(new CronType(), $cron);

$form->handleRequest($request);
if ($form->isValid()) {
$cronManager->add($cron);
$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());

return $this->redirect($this->generateUrl('foa_cron_index'));
}

return $this->render('FOACronBundle:Dashboard:index.html.twig', [
'crons' => $cronManager->get(),
'raw' => $cronManager->getRaw(),
'form' => $form->createView()
]);
}

/**
* Edit a cron
*
* @param $id - the line of the cron in the cron table
* @return RedirectResponse|Response
*/
public function editAction($id)
{
$cronManager = new CronManager();
$cronList = $cronManager->get();
$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());
$form = $this->createForm(new CronType(), $cronList[$id]);

$request = $this->get('request');
$form->handleRequest($request);
if ($form->isValid()) {
$cronManager->write();

$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());

return $this->redirect($this->generateUrl('foa_cron_index'));
}

return $this->render('FOACronBundle:Dashboard:edit.html.twig', [
'form' => $form->createView()
]);
}

/**
* Wake up a cron from the cron table
*
* @param $id - the line of the cron in the cron table
* @return RedirectResponse
*/
public function wakeupAction($id)
{
$cronManager = new CronManager();
$cronList = $cronManager->get();
$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());

/**
* @var Cron $cron
*/
$cron = $cronList[$id];
$cron->setSuspended(false);

$cronManager->write();
$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());

return $this->redirect($this->generateUrl('foa_cron_index'));
}

/**
* Suspend a cron from the cron table
*
* @param $id - the line of the cron in the cron table
* @return RedirectResponse
*/
public function suspendAction($id)
{
$cronManager = new CronManager();
$cronList = $cronManager->get();
$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());

/**
* @var Cron $cron
*/
$cron = $cronList[$id];
$cron->setSuspended(true);

$cronManager->write();
$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());

return $this->redirect($this->generateUrl('foa_cron_index'));
}

/**
* Remove a cron from the cron table
*
* @param $id - the line of the cron in the cron table
* @return RedirectResponse
*/
public function removeAction($id)
{
$cronManager = new CronManager();
$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());
$cronManager->remove($id);
$this->addFlash('message', $cronManager->getOutput());
$this->addFlash('error', $cronManager->getError());

return $this->redirect($this->generateUrl('foa_cron_index'));
}

/**
* Gets a log file
*
* @param $id - the line of the cron in the cron table
* @param $type - the type of file, log or error
* @return Response
*/
public function fileAction($id, $type)
{
$cronManager = new CronManager();
$cronList = $cronManager->get();

/**
* @var Cron $cron
*/
$cron = $cronList[$id];

$data = [];
$data['file'] = ($type == 'log') ? $cron->getLogFile(): $cron->getErrorFile();
$data['content'] = file_get_contents($data['file']);

$serializer = new Serializer([], ['json' => new JsonEncoder()]);

return new Response($serializer->serialize($data, 'json'));
}

/**
* Adds a flash to the flash bag where flashes are array of messages
*
* @param $type
* @param $message
* @return mixed
*/
protected function addFlash($type, $message)
{
if (empty($message)) {
return;
}

$session = $this->get('session');
$session->getFlashBag()->add($type, $message);
}
}
22 changes: 0 additions & 22 deletions DependencyInjection/Configuration.php

This file was deleted.

4 changes: 1 addition & 3 deletions DependencyInjection/FOACronExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ class FOACronExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
}
}
3 changes: 3 additions & 0 deletions FOACronBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Integration of Cron management board and Akeneo
*/
class FOACronBundle extends Bundle
{
}
50 changes: 50 additions & 0 deletions Form/Type/CronType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace FOA\CronBundle\Form\Type;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Cron job form type
*/
class CronType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('minute')
->add('hour')
->add('dayOfMonth')
->add('month')
->add('dayOfWeek')
->add('command')
->add('logFile', 'text', [
'required' => false,
])
->add('errorFile', 'text', [
'required' => false,
])
->add('comment', 'text', [
'required' => false,
]);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'FOA\CronBundle\Manager\Cron'
]);
}

/**
* Returns the name of this type.
*
* @return string The name of this type
*/
function getName()
{
return 'cron';
}
}
Loading

0 comments on commit 95b2913

Please sign in to comment.