FieldsController refactor

This commit is contained in:
Benoît Burnichon
2015-04-02 14:33:12 +02:00
parent 5f8dab86fd
commit bdec67aac7
2 changed files with 76 additions and 149 deletions

View File

@@ -14,25 +14,27 @@ use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Controller\Controller; use Alchemy\Phrasea\Controller\Controller;
use Alchemy\Phrasea\Metadata\TagProvider; use Alchemy\Phrasea\Metadata\TagProvider;
use Alchemy\Phrasea\Vocabulary\Controller as VocabularyController; use Alchemy\Phrasea\Vocabulary\Controller as VocabularyController;
use Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
class FieldsController extends Controller class FieldsController extends Controller
{ {
public function updateFields(Application $app, Request $request, $sbas_id) public function updateFields(Request $request, $sbas_id)
{ {
$fields = []; $fields = [];
$databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $databox = $this->findDataboxById((int) $sbas_id);
$metaStructure = $databox->get_meta_structure(); $metaStructure = $databox->get_meta_structure();
$connection = $databox->get_connection(); $connection = $databox->get_connection();
$data = $this->getFieldsJsonFromRequest($app, $request); $data = $this->getFieldsJsonFromRequest($request);
$connection->beginTransaction(); $connection->beginTransaction();
foreach ($data as $jsonField) { foreach ($data as $jsonField) {
try { try {
$field = \databox_field::get_instance($app, $databox, $jsonField['id']); $field = \databox_field::get_instance($this->app, $databox, $jsonField['id']);
if ($field->get_name() !== $jsonField['name']) { if ($field->get_name() !== $jsonField['name']) {
$this->validateNameField($metaStructure, $jsonField); $this->validateNameField($metaStructure, $jsonField);
@@ -40,64 +42,64 @@ class FieldsController extends Controller
$this->validateTagField($jsonField); $this->validateTagField($jsonField);
$this->updateFieldWithData($app, $field, $jsonField); $this->updateFieldWithData($field, $jsonField);
$field->save(); $field->save();
$fields[] = $field->toArray(); $fields[] = $field->toArray();
} catch (\Exception $e) { } catch (\Exception $e) {
$connection->rollback(); $connection->rollback();
$app->abort(500, $app->trans('Field %name% could not be saved, please try again or contact an admin.', ['%name%' => $jsonField['name']])); $this->app->abort(500, $this->app->trans('Field %name% could not be saved, please try again or contact an admin.', ['%name%' => $jsonField['name']]));
break; break;
} }
} }
$connection->commit(); $connection->commit();
return $app->json($fields); return $this->app->json($fields);
} }
public function getLanguage(Application $app, Request $request) public function getLanguage()
{ {
return $app->json([ return $this->app->json([
'something_wrong' => $app->trans('Something wrong happened, please try again or contact an admin.'), 'something_wrong' => $this->app->trans('Something wrong happened, please try again or contact an admin.'),
'created_success' => $app->trans('%s field has been created with success.'), 'created_success' => $this->app->trans('%s field has been created with success.'),
'deleted_success' => $app->trans('%s field has been deleted with success.'), 'deleted_success' => $this->app->trans('%s field has been deleted with success.'),
'are_you_sure_delete' => $app->trans('Do you really want to delete the field %s ?'), 'are_you_sure_delete' => $this->app->trans('Do you really want to delete the field %s ?'),
'validation_blank' => $app->trans('Field can not be blank.'), 'validation_blank' => $this->app->trans('Field can not be blank.'),
'validation_name_exists' => $app->trans('Field name already exists.'), 'validation_name_exists' => $this->app->trans('Field name already exists.'),
'validation_name_invalid' => $app->trans('Field name is not valid.'), 'validation_name_invalid' => $this->app->trans('Field name is not valid.'),
'validation_tag_invalid' => $app->trans('Field source is not valid.'), 'validation_tag_invalid' => $this->app->trans('Field source is not valid.'),
'field_error' => $app->trans('Field %s contains errors.'), 'field_error' => $this->app->trans('Field %s contains errors.'),
'fields_save' => $app->trans('Your configuration has been successfuly saved.'), 'fields_save' => $this->app->trans('Your configuration has been successfuly saved.'),
]); ]);
} }
public function displayApp(Application $app, Request $request, $sbas_id) public function displayApp($sbas_id)
{ {
$languages = []; $languages = [];
foreach ($app['locales.available'] as $code => $language) { foreach ($this->app['locales.available'] as $code => $language) {
$data = explode('_', $code); $data = explode('_', $code);
$languages[$data[0]] = $language; $languages[$data[0]] = $language;
} }
return $app['twig']->render('/admin/fields/index.html.twig', [ return $this->render('/admin/fields/index.html.twig', [
'sbas_id' => $sbas_id, 'sbas_id' => $sbas_id,
'languages' => $languages, 'languages' => $languages,
]); ]);
} }
public function listDcFields(Application $app, Request $request) public function listDcFields()
{ {
$data = $app['serializer']->serialize(array_values(\databox::get_available_dcfields()), 'json'); $data = $this->app['serializer']->serialize(array_values(\databox::get_available_dcfields()), 'json');
return new Response($data, 200, ['content-type' => 'application/json']); return new Response($data, 200, ['content-type' => 'application/json']);
} }
public function listVocabularies(Application $app, Request $request) public function listVocabularies()
{ {
$vocabularies = VocabularyController::getAvailable($app); $vocabularies = VocabularyController::getAvailable($this->app);
return $app->json(array_map(function ($vocabulary) { return $this->app->json(array_map(function (ControlProviderInterface $vocabulary) {
return [ return [
'type' => $vocabulary->getType(), 'type' => $vocabulary->getType(),
'name' => $vocabulary->getName(), 'name' => $vocabulary->getName(),
@@ -105,17 +107,17 @@ class FieldsController extends Controller
}, $vocabularies)); }, $vocabularies));
} }
public function getVocabulary(Application $app, Request $request, $type) public function getVocabulary($type)
{ {
$vocabulary = VocabularyController::get($app, $type); $vocabulary = VocabularyController::get($this->app, $type);
return $app->json([ return $this->app->json([
'type' => $vocabulary->getType(), 'type' => $vocabulary->getType(),
'name' => $vocabulary->getName(), 'name' => $vocabulary->getName(),
]); ]);
} }
public function searchTag(Application $app, Request $request) public function searchTag(Request $request)
{ {
$term = trim(strtolower($request->query->get('term'))); $term = trim(strtolower($request->query->get('term')));
$res = []; $res = [];
@@ -141,61 +143,64 @@ class FieldsController extends Controller
} }
} }
return $app->json($res); return $this->app->json($res);
} }
public function getTag(Application $app, Request $request, $tagname) public function getTag($tagname)
{ {
$tag = \databox_field::loadClassFromTagName($tagname); $tag = \databox_field::loadClassFromTagName($tagname);
$json = $app['serializer']->serialize($tag, 'json'); $json = $this->app['serializer']->serialize($tag, 'json');
return new Response($json, 200, ['Content-Type' => 'application/json']); return new Response($json, 200, ['Content-Type' => 'application/json']);
} }
public function createField(Application $app, Request $request, $sbas_id) public function createField(Request $request, $sbas_id)
{ {
$databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $databox = $this->findDataboxById((int) $sbas_id);
$data = $this->getFieldJsonFromRequest($app, $request); $data = $this->getFieldJsonFromRequest($request);
$metaStructure = $databox->get_meta_structure(); $metaStructure = $databox->get_meta_structure();
$this->validateNameField($metaStructure, $data); $this->validateNameField($metaStructure, $data);
$this->validateTagField($data); $this->validateTagField($data);
try { try {
$field = \databox_field::create($app, $databox, $data['name'], $data['multi']); $field = \databox_field::create($this->app, $databox, $data['name'], $data['multi']);
$this->updateFieldWithData($app, $field, $data); $this->updateFieldWithData($field, $data);
$field->save(); $field->save();
} catch (\Exception $e) { } catch (\Exception $e) {
$app->abort(500, $app->trans('Field %name% could not be created, please try again or contact an admin.', ['%name%' => $data['name']])); throw new HttpException(500, $this->app->trans(
'Field %name% could not be created, please try again or contact an admin.',
['%name%' => $data['name']]
));
} }
return $app->json($field->toArray(), 201, [ return $this->app->json($field->toArray(), 201, [
'location' => $app->path('admin_fields_show_field', [ 'location' => $this->app->path('admin_fields_show_field', [
'sbas_id' => $sbas_id, 'sbas_id' => $sbas_id,
'id' => $field->get_id() 'id' => $field->get_id(),
])]); ])]);
} }
public function listFields(Application $app, $sbas_id) public function listFields($sbas_id)
{ {
$databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $databox = $this->findDataboxById((int) $sbas_id);
return $app->json($databox->get_meta_structure()->toArray()); return $this->app->json($databox->get_meta_structure()->toArray());
} }
public function getField(Application $app, $sbas_id, $id) public function getField($sbas_id, $id)
{ {
$databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $databox = $this->findDataboxById((int) $sbas_id);
$field = \databox_field::get_instance($app, $databox, $id); $field = \databox_field::get_instance($this->app, $databox, $id);
return $app->json($field->toArray()); return $this->app->json($field->toArray());
} }
public function updateField(Application $app, Request $request, $sbas_id, $id) public function updateField(Request $request, $sbas_id, $id)
{ {
$databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $databox = $this->findDataboxById((int) $sbas_id);
$field = \databox_field::get_instance($app, $databox, $id); $field = \databox_field::get_instance($this->app, $databox, $id);
$data = $this->getFieldJsonFromRequest($app, $request); $data = $this->getFieldJsonFromRequest($request);
$this->validateTagField($data); $this->validateTagField($data);
@@ -204,35 +209,35 @@ class FieldsController extends Controller
$this->validateNameField($metaStructure, $data); $this->validateNameField($metaStructure, $data);
} }
$this->updateFieldWithData($app, $field, $data); $this->updateFieldWithData($field, $data);
$field->save(); $field->save();
return $app->json($field->toArray()); return $this->app->json($field->toArray());
} }
public function deleteField(Application $app, $sbas_id, $id) public function deleteField($sbas_id, $id)
{ {
$databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $databox = $this->findDataboxById((int) $sbas_id);
\databox_field::get_instance($app, $databox, $id)->delete(); \databox_field::get_instance($this->app, $databox, $id)->delete();
return new Response('', 204); return new Response('', 204);
} }
private function getFieldJsonFromRequest(Application $app, Request $request) private function getFieldJsonFromRequest(Request $request)
{ {
$data = $this->requestBodyToJson($request); $data = $this->requestBodyToJson($request);
$required = $this->getMandatoryFieldProperties(); $required = $this->getMandatoryFieldProperties();
foreach ($required as $key) { foreach ($required as $key) {
if (false === array_key_exists($key, $data)) { if (false === array_key_exists($key, $data)) {
$app->abort(400, sprintf('The entity must contain a key `%s`', $key)); $this->app->abort(400, sprintf('The entity must contain a key `%s`', $key));
} }
} }
return $data; return $data;
} }
private function getFieldsJsonFromRequest(Application $app, Request $request) private function getFieldsJsonFromRequest(Request $request)
{ {
$data = $this->requestBodyToJson($request); $data = $this->requestBodyToJson($request);
$required = $this->getMandatoryFieldProperties(); $required = $this->getMandatoryFieldProperties();
@@ -240,7 +245,7 @@ class FieldsController extends Controller
foreach ($data as $field) { foreach ($data as $field) {
foreach ($required as $key) { foreach ($required as $key) {
if (false === array_key_exists($key, $field)) { if (false === array_key_exists($key, $field)) {
$app->abort(400, sprintf('The entity must contain a key `%s`', $key)); $this->app->abort(400, sprintf('The entity must contain a key `%s`', $key));
} }
} }
} }
@@ -248,7 +253,7 @@ class FieldsController extends Controller
return $data; return $data;
} }
private function updateFieldWithData(Application $app, \databox_field $field, array $data) private function updateFieldWithData(\databox_field $field, array $data)
{ {
$field $field
->set_name($data['name']) ->set_name($data['name'])
@@ -275,7 +280,7 @@ class FieldsController extends Controller
} }
try { try {
$vocabulary = VocabularyController::get($app, $data['vocabulary-type']); $vocabulary = VocabularyController::get($this->app, $data['vocabulary-type']);
$field->setVocabularyControl($vocabulary); $field->setVocabularyControl($vocabulary);
$field->setVocabularyRestricted($data['vocabulary-restricted']); $field->setVocabularyRestricted($data['vocabulary-restricted']);
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {

View File

@@ -26,98 +26,36 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class databox_field implements cache_cacheableInterface class databox_field implements cache_cacheableInterface
{ {
/**
*
* @var <type>
*/
protected $id; protected $id;
/** /**
*
* @var databox * @var databox
*/ */
protected $databox; protected $databox;
/** /**
* * @var TagInterface
* @var \PHPExiftool\Driver\Tag
*/ */
protected $tag; protected $tag;
/**
*
* @var <type>
*/
protected $name; protected $name;
/**
*
* @var <type>
*/
protected $indexable; protected $indexable;
/**
*
* @var <type>
*/
protected $readonly; protected $readonly;
/**
*
* @var <type>
*/
protected $position; protected $position;
/**
*
* @var <type>
*/
protected $required; protected $required;
/**
*
* @var <type>
*/
protected $multi; protected $multi;
/**
*
* @var <type>
*/
protected $report; protected $report;
/**
*
* @var <type>
*/
protected $type; protected $type;
/**
*
* @var <type>
*/
protected $tbranch; protected $tbranch;
/**
*
* @var <type>
*/
protected $separator; protected $separator;
/**
*
* @var <type>
*/
protected $thumbtitle; protected $thumbtitle;
/** /**
*
* @var array * @var array
*/ */
protected $labels = []; protected $labels = [];
/** /**
*
* @var boolean * @var boolean
*/ */
protected $Business; protected $Business;
@@ -125,15 +63,6 @@ class databox_field implements cache_cacheableInterface
protected $renamed = false; protected $renamed = false;
/** /**
*
*
* To implement : change multi
* Change vocab Id
*
*/
/**
*
* @var int * @var int
*/ */
protected $sbas_id; protected $sbas_id;
@@ -173,7 +102,7 @@ class databox_field implements cache_cacheableInterface
/** /**
* *
* @param databox $databox * @param databox $databox
* @param <type> $id * @param int $id
* @return databox_field * @return databox_field
*/ */
protected function __construct(Application $app, databox $databox, $id) protected function __construct(Application $app, databox $databox, $id)
@@ -242,7 +171,6 @@ class databox_field implements cache_cacheableInterface
} }
/** /**
*
* @return type \Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface * @return type \Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface
*/ */
public function getVocabularyControl() public function getVocabularyControl()
@@ -251,7 +179,6 @@ class databox_field implements cache_cacheableInterface
} }
/** /**
*
* @return boolean * @return boolean
*/ */
public function isVocabularyRestricted() public function isVocabularyRestricted()
@@ -260,7 +187,6 @@ class databox_field implements cache_cacheableInterface
} }
/** /**
*
* @return boolean * @return boolean
*/ */
public function isBusiness() public function isBusiness()
@@ -274,7 +200,6 @@ class databox_field implements cache_cacheableInterface
} }
/** /**
*
* @param Application $app * @param Application $app
* @param \databox $databox * @param \databox $databox
* @param int $id * @param int $id
@@ -306,7 +231,6 @@ class databox_field implements cache_cacheableInterface
} }
/** /**
*
* @param databox $databox * @param databox $databox
*/ */
public function set_databox(databox $databox) public function set_databox(databox $databox)
@@ -315,7 +239,6 @@ class databox_field implements cache_cacheableInterface
} }
/** /**
*
* @return Connection * @return Connection
*/ */
public function get_connection() public function get_connection()
@@ -329,7 +252,6 @@ class databox_field implements cache_cacheableInterface
} }
/** /**
*
* @return databox * @return databox
*/ */
public function get_databox() public function get_databox()
@@ -548,8 +470,8 @@ class databox_field implements cache_cacheableInterface
/** /**
* Get a PHPExiftool Tag from tagName * Get a PHPExiftool Tag from tagName
* *
* @param type $tagName * @param string $tagName
* @return \PHPExiftool\Driver\Tag * @return \PHPExiftool\Driver\TagInterface
* @throws Exception_Databox_metadataDescriptionNotFound * @throws Exception_Databox_metadataDescriptionNotFound
*/ */
public static function loadClassFromTagName($tagName, $throwException = true) public static function loadClassFromTagName($tagName, $throwException = true)
@@ -592,7 +514,7 @@ class databox_field implements cache_cacheableInterface
return $tag; return $tag;
} }
public function set_tag(TagInterface $tag = null) public function set_tag($tag = null)
{ {
if ($tag === null) { if ($tag === null) {
$tag = new Nosource(); $tag = new Nosource();
@@ -989,7 +911,7 @@ class databox_field implements cache_cacheableInterface
* @param type $name * @param type $name
* @param type $multi * @param type $multi
* *
* @return databox_field * @return self
* *
* @throws \Exception_InvalidArgument * @throws \Exception_InvalidArgument
*/ */