diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index 62a23628e1..770b79c3a5 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -398,6 +398,7 @@ class Application extends SilexApplication $dispatcher->addSubscriber($app['phraseanet.maintenance-subscriber']); $dispatcher->addSubscriber($app['phraseanet.cookie-disabler-subscriber']); $dispatcher->addSubscriber($app['phraseanet.session-manager-subscriber']); + $dispatcher->addSubscriber($app['phraseanet.record-edit-subscriber']); return $dispatcher; }) diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Edit.php b/lib/Alchemy/Phrasea/Controller/Prod/Edit.php index 76e35fa3b0..71d84e4280 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Edit.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Edit.php @@ -11,6 +11,8 @@ namespace Alchemy\Phrasea\Controller\Prod; +use Alchemy\Phrasea\Core\Event\RecordEdit; +use Alchemy\Phrasea\Core\PhraseaEvents; use Alchemy\Phrasea\Vocabulary\Controller as VocabularyController; use Alchemy\Phrasea\Controller\RecordsRequest; use Alchemy\Phrasea\Metadata\Tag\TfEditdate; @@ -311,18 +313,11 @@ class Edit implements ControllerProviderInterface return $app->json(array('message' => '', 'error' => false)); } + $app['dispatcher']->dispatch(PhraseaEvents::RECORD_EDIT, new RecordEdit($records)); + $databoxes = $records->databoxes(); $databox = array_pop($databoxes); - $meta_struct = $databox->get_meta_structure(); - $write_edit_el = false; - $date_obj = new \DateTime(); - foreach ($meta_struct->get_elements() as $meta_struct_el) { - if ($meta_struct_el->get_tag() instanceof TfEditdate) { - $write_edit_el = $meta_struct_el; - } - } - $elements = $records->toArray(); foreach ($request->request->get('mds') as $rec) { @@ -351,31 +346,6 @@ class Edit implements ControllerProviderInterface $record->set_metadatas($rec['metadatas']); } - /** - * todo : this should not work - */ - if ($write_edit_el instanceof \databox_field) { - $fields = $record->get_caption()->get_fields(array($write_edit_el->get_name()), true); - $field = array_pop($fields); - - $meta_id = null; - - if ($field && !$field->is_multi()) { - $values = $field->get_values(); - $meta_id = array_pop($values)->getId(); - } - - $metas = array( - array( - 'meta_struct_id' => $write_edit_el->get_id(), - 'meta_id' => $meta_id, - 'value' => $date_obj->format('Y-m-d h:i:s'), - ) - ); - - $record->set_metadatas($metas, true); - } - $newstat = $record->get_status(); $statbits = ltrim($statbits, 'x'); if (!in_array($statbits, array('', 'null'))) { diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Upload.php b/lib/Alchemy/Phrasea/Controller/Prod/Upload.php index 6d1472b4cc..9a8b7fc4ca 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/Upload.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/Upload.php @@ -13,6 +13,8 @@ namespace Alchemy\Phrasea\Controller\Prod; use Alchemy\Phrasea\Border\File; use Alchemy\Phrasea\Border\Attribute\Status; +use Alchemy\Phrasea\Core\Event\RecordEdit; +use Alchemy\Phrasea\Core\PhraseaEvents; use DataURI\Parser; use DataURI\Exception\Exception as DataUriException; use Entities\LazaretSession; @@ -247,6 +249,8 @@ class Upload implements ControllerProviderInterface $message = _('The record was successfully created'); $app['phraseanet.SE']->addRecord($elementCreated); + $app['dispatcher']->dispatch(PhraseaEvents::RECORD_UPLOAD, new RecordEdit($elementCreated)); + // try to create thumbnail from data URI if ('' !== $b64Image = $request->request->get('b64_image', '')) { try { diff --git a/lib/Alchemy/Phrasea/Core/Event/RecordEdit.php b/lib/Alchemy/Phrasea/Core/Event/RecordEdit.php new file mode 100644 index 0000000000..f26254c99e --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Event/RecordEdit.php @@ -0,0 +1,52 @@ +records = $data->toArray(); + $this->collections = $data->collections(); + $this->databoxes = $data->databoxes(); + } elseif ($data instanceof \record_adapter) { + $this->records[] = $data; + $this->collections[] = $data->get_collection(); + $this->databoxes[] = $data->get_databox(); + } + + } + + public function getRecords() + { + return $this->records; + } + + public function getCollections() + { + return $this->collections; + } + + public function getDataboxes() + { + return $this->databoxes; + } +} diff --git a/lib/Alchemy/Phrasea/Core/Event/Subscriber/RecordEditSubscriber.php b/lib/Alchemy/Phrasea/Core/Event/Subscriber/RecordEditSubscriber.php new file mode 100644 index 0000000000..70614f8f54 --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Event/Subscriber/RecordEditSubscriber.php @@ -0,0 +1,83 @@ +getRecords(); + $databoxes = $event->getDataboxes(); + $databox = array_pop($databoxes); + + $metaStructure = $databox->get_meta_structure(); + $editDateField = false; + foreach ($metaStructure->get_elements() as $meta) { + if ($meta->get_tag() instanceof TfEditdate) { + $editDateField = $meta; + } + } + + if ($editDateField instanceof \databox_field) { + foreach ($records as $record) { + $this->updateRecord($record, $editDateField); + } + } + } + + private function updateRecord($record, $field) + { + if (false === $record->is_grouping()) { + foreach ($record->get_grouping_parents() as $story) { + $this->updateEditField($story, $field); + } + } + $this->updateEditField($record, $field); + } + + + private function updateEditField($record, $editField) + { + $fields = $record->get_caption()->get_fields(array($editField->get_name()), true); + $field = array_pop($fields); + + $metaId = null; + + if ($field && !$field->is_multi()) { + $values = $field->get_values(); + $metaId = array_pop($values)->getId(); + } + + $date = new \DateTime(); + $record->set_metadatas(array( + array( + 'meta_struct_id' => $editField->get_id(), + 'meta_id' => $metaId, + 'value' => $date->format('Y-m-d H:i:s'), + ) + ), true); + } + + public static function getSubscribedEvents() + { + return array( + PhraseaEvents::RECORD_EDIT => 'onEdit', + PhraseaEvents::RECORD_UPLOAD => 'onEdit', + ); + } +} diff --git a/lib/Alchemy/Phrasea/Core/PhraseaEvents.php b/lib/Alchemy/Phrasea/Core/PhraseaEvents.php index 6cff63f9e2..f07001c60f 100644 --- a/lib/Alchemy/Phrasea/Core/PhraseaEvents.php +++ b/lib/Alchemy/Phrasea/Core/PhraseaEvents.php @@ -25,4 +25,7 @@ final class PhraseaEvents const API_RESULT = 'api.result'; const COLLECTION_CREATE = 'collection.create'; + + const RECORD_EDIT = 'record.edit'; + const RECORD_UPLOAD = 'record.upload'; } diff --git a/lib/Alchemy/Phrasea/Core/Provider/PhraseaEventServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/PhraseaEventServiceProvider.php index ece1f1b21e..6a8711d3ca 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/PhraseaEventServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/PhraseaEventServiceProvider.php @@ -16,6 +16,7 @@ use Alchemy\Phrasea\Core\Event\Subscriber\CookiesDisablerSubscriber; use Alchemy\Phrasea\Core\Event\Subscriber\LogoutSubscriber; use Alchemy\Phrasea\Core\Event\Subscriber\MaintenanceSubscriber; use Alchemy\Phrasea\Core\Event\Subscriber\PhraseaLocaleSubscriber; +use Alchemy\Phrasea\Core\Event\Subscriber\RecordEditSubscriber; use Alchemy\Phrasea\Core\Event\Subscriber\SessionManagerSubscriber; use Silex\Application; use Silex\ServiceProviderInterface; @@ -42,6 +43,9 @@ class PhraseaEventServiceProvider implements ServiceProviderInterface $app['phraseanet.content-negotiation-subscriber'] = $app->share(function (Application $app) { return new ContentNegotiationSubscriber($app); }); + $app['phraseanet.record-edit-subscriber'] = $app->share(function (Application $app) { + return new RecordEditSubscriber(); + }); } public function boot(Application $app) diff --git a/lib/classes/API/V1/adapter.php b/lib/classes/API/V1/adapter.php index 9420960d27..c81470510e 100644 --- a/lib/classes/API/V1/adapter.php +++ b/lib/classes/API/V1/adapter.php @@ -17,6 +17,8 @@ use Alchemy\Phrasea\Border\Attribute\Status; use Alchemy\Phrasea\Border\Manager as BorderManager; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Alchemy\Phrasea\Core\PhraseaEvents; +use Alchemy\Phrasea\Core\Event\RecordEdit; class API_V1_adapter extends API_V1_Abstract { @@ -503,6 +505,8 @@ class API_V1_adapter extends API_V1_Abstract $ret['entity'] = '0'; $ret['url'] = '/records/' . $output->get_sbas_id() . '/' . $output->get_record_id() . '/'; $app['phraseanet.SE']->addRecord($output); + + $app['dispatcher']->dispatch(PhraseaEvents::RECORD_UPLOAD, new RecordEdit($output)); } if ($output instanceof \Entities\LazaretFile) { $ret['entity'] = '1'; @@ -806,6 +810,9 @@ class API_V1_adapter extends API_V1_Abstract } $record->set_metadatas($metadatas); + + $this->app['dispatcher']->dispatch(PhraseaEvents::RECORD_EDIT, new RecordEdit($record)); + $result->set_datas(array("record_metadatas" => $this->list_record_caption($record->get_caption()))); } catch (\Exception $e) { $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('An error occurred')); @@ -857,6 +864,8 @@ class API_V1_adapter extends API_V1_Abstract $this->app['phraseanet.SE']->updateRecord($record); + $this->app['dispatcher']->dispatch(PhraseaEvents::RECORD_EDIT, new RecordEdit($record)); + $result->set_datas(array("status" => $this->list_record_status($databox, $record->get_status()))); } catch (\Exception $e) { $result->set_error_message(API_V1_result::ERROR_BAD_REQUEST, _('An error occurred'));