CUS-120 #fix editon date for records

This commit is contained in:
Nicolas Le Goff
2014-11-18 17:47:33 +01:00
parent 52b9850add
commit 7d44a356e3
8 changed files with 160 additions and 34 deletions

View File

@@ -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;
})

View File

@@ -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'))) {

View File

@@ -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 {

View File

@@ -0,0 +1,52 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Event;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Controller\RecordsRequest;
use Symfony\Component\EventDispatcher\Event as SfEvent;
class RecordEdit extends SfEvent
{
private $records = array();
private $collections = array();
private $databoxes = array();
public function __construct($data)
{
if ($data instanceof RecordsRequest) {
$this->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;
}
}

View File

@@ -0,0 +1,83 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Event\Subscriber;
use Alchemy\Phrasea\Core\Event\RecordEdit;
use Alchemy\Phrasea\Core\PhraseaEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Alchemy\Phrasea\Metadata\Tag\TfEditdate;
class RecordEditSubscriber implements EventSubscriberInterface
{
public function onEdit(RecordEdit $event)
{
$records = $event->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',
);
}
}

View File

@@ -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';
}

View File

@@ -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)

View File

@@ -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'));