Refactor move collection

This commit is contained in:
Romain Neutron
2012-07-24 19:51:14 +02:00
parent 58e5053a8c
commit 8bebadffad

View File

@@ -14,8 +14,8 @@ namespace Alchemy\Phrasea\Controller\Prod;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Alchemy\Phrasea\Helper\Record\MoveCollection as Helper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Alchemy\Phrasea\Helper\Record as RecordHelper;
/**
@@ -25,42 +25,55 @@ use Alchemy\Phrasea\Helper\Record as RecordHelper;
*/
class MoveCollection implements ControllerProviderInterface
{
public function connect(Application $app)
{
$controllers = $app['controllers_factory'];
$controllers->post('/', function(Application $app, Request $request) {
$request = $app['request'];
$move = new RecordHelper\MoveCollection($app['phraseanet.core'], $app['request']);
$move->propose();
return $app['twig']->render('prod/actions/collection_default.html.twig', array('action' => $move, 'message' => ''));
}
);
$controllers->post('/apply/', function(Application $app, Request $request) {
$move = new RecordHelper\MoveCollection($app['phraseanet.core'], $request);
$success = false;
try {
$move->execute();
$success = true;
$msg = _('Records have been successfuly moved');
} catch (\Exception_Unauthorized $e) {
$msg = sprintf(_("You do not have the permission to move records to %s"), \phrasea::bas_names($move->getBaseIdDestination()));
} catch (\Exception $e) {
$msg = _('An error occured');
}
$datas = array(
'success' => $success,
'message' => $msg
);
return new JsonResponse($datas);
});
$controllers->post('/', $this->call('displayForm'));
$controllers->post('/apply/', $this->call('apply'));
return $controllers;
}
public function displayForm(Application $app, Request $request)
{
$move = new Helper($app['phraseanet.core'], $request);
$move->propose();
return $app['twig']->render('prod/actions/collection_default.html.twig', array('action' => $move, 'message' => ''));
}
public function apply(Application $app, Request $request)
{
$move = new Helper($app['phraseanet.core'], $request);
$success = false;
try {
$move->execute();
$success = true;
$msg = _('Records have been successfuly moved');
} catch (\Exception_Unauthorized $e) {
$msg = sprintf(_("You do not have the permission to move records to %s"), \phrasea::bas_names($move->getBaseIdDestination()));
} catch (\Exception $e) {
$msg = _('An error occured');
}
$datas = array(
'success' => $success,
'message' => $msg
);
return $app->json($datas);
}
/**
* Prefix the method to call with the controller class name
*
* @param string $method The method to call
* @return string
*/
private function call($method)
{
return sprintf('%s::%s', __CLASS__, $method);
}
}