Add NullableDateTime formatting class

This commit is contained in:
Benoît Burnichon
2016-04-05 18:52:17 +02:00
parent aeac1a7141
commit d0dcdf6e6f
9 changed files with 49 additions and 36 deletions

View File

@@ -61,6 +61,7 @@ use Alchemy\Phrasea\SearchEngine\SearchEngineResult;
use Alchemy\Phrasea\SearchEngine\SearchEngineSuggestion; use Alchemy\Phrasea\SearchEngine\SearchEngineSuggestion;
use Alchemy\Phrasea\Status\StatusStructure; use Alchemy\Phrasea\Status\StatusStructure;
use Alchemy\Phrasea\TaskManager\LiveInformation; use Alchemy\Phrasea\TaskManager\LiveInformation;
use Alchemy\Phrasea\Utilities\NullableDateTime;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\Form; use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -138,10 +139,10 @@ class V1Controller extends Controller
'pid' => $data['process-id'], 'pid' => $data['process-id'],
'jobId' => $task->getJobId(), 'jobId' => $task->getJobId(),
'period' => $task->getPeriod(), 'period' => $task->getPeriod(),
'last_exec_time' => $task->getLastExecution() ? $task->getLastExecution()->format(DATE_ATOM) : null, 'last_exec_time' => NullableDateTime::format($task->getLastExecution()),
'last_execution' => $task->getLastExecution() ? $task->getLastExecution()->format(DATE_ATOM) : null, 'last_execution' => NullableDateTime::format($task->getLastExecution()),
'updated' => $task->getUpdated() ? $task->getUpdated()->format(DATE_ATOM) : null, 'updated' => NullableDateTime::format($task->getUpdated()),
'created' => $task->getCreated() ? $task->getCreated()->format(DATE_ATOM) : null, 'created' => NullableDateTime::format($task->getCreated()),
'auto_start' => $task->getStatus() === Task::STATUS_STARTED, 'auto_start' => $task->getStatus() === Task::STATUS_STARTED,
'crashed' => $task->getCrashed(), 'crashed' => $task->getCrashed(),
]; ];
@@ -709,9 +710,9 @@ class V1Controller extends Controller
'position' => $user->getActivity() ?: null, 'position' => $user->getActivity() ?: null,
'company' => $user->getCompany() ?: null, 'company' => $user->getCompany() ?: null,
'geoname_id' => $user->getGeonameId() ?: null, 'geoname_id' => $user->getGeonameId() ?: null,
'last_connection' => $user->getLastConnection() ? $user->getLastConnection()->format(DATE_ATOM) : null, 'last_connection' => NullableDateTime::format($user->getLastConnection()),
'created_on' => $user->getCreated() ? $user->getCreated()->format(DATE_ATOM) : null, 'created_on' => NullableDateTime::format($user->getCreated()),
'updated_on' => $user->getUpdated() ? $user->getUpdated()->format(DATE_ATOM) : null, 'updated_on' => NullableDateTime::format($user->getUpdated()),
'locale' => $user->getLocale() ?: null, 'locale' => $user->getLocale() ?: null,
]; ];
} }
@@ -1466,11 +1467,7 @@ class V1Controller extends Controller
]; ];
}, iterator_to_array($basket->getValidation()->getParticipants())); }, iterator_to_array($basket->getValidation()->getParticipants()));
$expires_on_atom = $basket->getValidation()->getExpires(); $expires_on_atom = NullableDateTime::format($basket->getValidation()->getExpires());
if ($expires_on_atom instanceof \DateTime) {
$expires_on_atom = $expires_on_atom->format(DATE_ATOM);
}
$ret = array_merge([ $ret = array_merge([
'validation_users' => $users, 'validation_users' => $users,

View File

@@ -17,6 +17,7 @@ use Alchemy\Phrasea\Feed\FeedInterface;
use Alchemy\Phrasea\Feed\Link\FeedLink; use Alchemy\Phrasea\Feed\Link\FeedLink;
use Alchemy\Phrasea\Feed\Link\LinkGeneratorCollection; use Alchemy\Phrasea\Feed\Link\LinkGeneratorCollection;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Utilities\NullableDateTime;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
class AtomFormatter extends FeedFormatterAbstract implements FeedFormatterInterface class AtomFormatter extends FeedFormatterAbstract implements FeedFormatterInterface
@@ -48,8 +49,6 @@ class AtomFormatter extends FeedFormatterAbstract implements FeedFormatterInterf
*/ */
public function format(FeedInterface $feed, $page, User $user = null, $generator = 'Phraseanet', Application $app = null) public function format(FeedInterface $feed, $page, User $user = null, $generator = 'Phraseanet', Application $app = null)
{ {
$updated_on = $feed->getUpdatedOn();
$document = new \DOMDocument('1.0', 'UTF-8'); $document = new \DOMDocument('1.0', 'UTF-8');
$document->formatOutput = true; $document->formatOutput = true;
$document->standalone = true; $document->standalone = true;
@@ -59,8 +58,7 @@ class AtomFormatter extends FeedFormatterAbstract implements FeedFormatterInterf
$root->setAttribute('xmlns:media', 'http://search.yahoo.com/mrss/'); $root->setAttribute('xmlns:media', 'http://search.yahoo.com/mrss/');
$this->addTag($document, $root, 'title', $feed->getTitle()); $this->addTag($document, $root, 'title', $feed->getTitle());
if ($updated_on instanceof \DateTime) { if (null !== $updated_on = NullableDateTime::format($feed->getUpdatedOn())) {
$updated_on = $updated_on->format(DATE_ATOM);
$this->addTag($document, $root, 'updated', $updated_on); $this->addTag($document, $root, 'updated', $updated_on);
} }

View File

@@ -19,6 +19,7 @@ use Alchemy\Phrasea\Model\Entities\FeedEntry;
use Alchemy\Phrasea\Model\Entities\FeedItem; use Alchemy\Phrasea\Model\Entities\FeedItem;
use Alchemy\Phrasea\Feed\Link\LinkGeneratorCollection; use Alchemy\Phrasea\Feed\Link\LinkGeneratorCollection;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Utilities\NullableDateTime;
use DateTime; use DateTime;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -52,8 +53,6 @@ class CoolirisFormatter extends FeedFormatterAbstract implements FeedFormatterIn
*/ */
public function format(FeedInterface $feed, $page, User $user = null, $generator = 'Phraseanet', Application $app = null) public function format(FeedInterface $feed, $page, User $user = null, $generator = 'Phraseanet', Application $app = null)
{ {
$updated_on = $feed->getUpdatedOn();
$doc = new \DOMDocument('1.0', 'UTF-8'); $doc = new \DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true; $doc->formatOutput = true;
$doc->standalone = true; $doc->standalone = true;
@@ -89,8 +88,7 @@ class CoolirisFormatter extends FeedFormatterAbstract implements FeedFormatterIn
$this->addTag($doc, $channel, 'managingEditor', $this->managingEditor); $this->addTag($doc, $channel, 'managingEditor', $this->managingEditor);
if (isset($this->webMaster)) if (isset($this->webMaster))
$this->addTag($doc, $channel, 'webMaster', $this->webMaster); $this->addTag($doc, $channel, 'webMaster', $this->webMaster);
if ($updated_on instanceof DateTime) { if (null !== $updated_on = NullableDateTime::format($feed->getUpdatedOn(), DATE_RFC2822)) {
$updated_on = $updated_on->format(DATE_RFC2822);
$this->addTag($doc, $channel, 'pubDate', $updated_on); $this->addTag($doc, $channel, 'pubDate', $updated_on);
} }
if (isset($this->lastBuildDate) && $this->lastBuildDate instanceof DateTime) { if (isset($this->lastBuildDate) && $this->lastBuildDate instanceof DateTime) {

View File

@@ -17,6 +17,7 @@ use Alchemy\Phrasea\Feed\Link\FeedLink;
use Alchemy\Phrasea\Feed\Link\LinkGeneratorCollection; use Alchemy\Phrasea\Feed\Link\LinkGeneratorCollection;
use Alchemy\Phrasea\Feed\RSS\FeedRSSImage; use Alchemy\Phrasea\Feed\RSS\FeedRSSImage;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Utilities\NullableDateTime;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Alchemy\Phrasea\Model\Entities\FeedEntry; use Alchemy\Phrasea\Model\Entities\FeedEntry;
use Alchemy\Phrasea\Feed\Link\FeedLinkGenerator; use Alchemy\Phrasea\Feed\Link\FeedLinkGenerator;
@@ -51,8 +52,6 @@ class RssFormatter extends FeedFormatterAbstract implements FeedFormatterInterfa
*/ */
public function format(FeedInterface $feed, $page, User $user = null, $generator = 'Phraseanet', Application $app = null) public function format(FeedInterface $feed, $page, User $user = null, $generator = 'Phraseanet', Application $app = null)
{ {
$updated_on = $feed->getUpdatedOn();
$next = $prev = null; $next = $prev = null;
if ($feed->hasPage($page + 1, self::PAGE_SIZE)) { if ($feed->hasPage($page + 1, self::PAGE_SIZE)) {
@@ -104,11 +103,10 @@ class RssFormatter extends FeedFormatterAbstract implements FeedFormatterInterfa
$this->addTag($doc, $channel, 'managingEditor', $this->managingEditor); $this->addTag($doc, $channel, 'managingEditor', $this->managingEditor);
if (isset($this->webMaster)) if (isset($this->webMaster))
$this->addTag($doc, $channel, 'webMaster', $this->webMaster); $this->addTag($doc, $channel, 'webMaster', $this->webMaster);
if ($updated_on instanceof \DateTime) { if (null !== $updated_on = NullableDateTime::format($feed->getUpdatedOn(), DATE_RFC2822)) {
$updated_on = $updated_on->format(DATE_RFC2822);
$this->addTag($doc, $channel, 'pubDate', $updated_on); $this->addTag($doc, $channel, 'pubDate', $updated_on);
} }
if (isset($this->lastBuildDate) && $this->lastBuildDate instanceof DateTime) { if (isset($this->lastBuildDate) && $this->lastBuildDate instanceof \DateTime) {
$last_build = $this->lastBuildDate->format(DATE_RFC2822); $last_build = $this->lastBuildDate->format(DATE_RFC2822);
$this->addTag($doc, $channel, 'lastBuildDate', $last_build); $this->addTag($doc, $channel, 'lastBuildDate', $last_build);
} }

View File

@@ -94,8 +94,8 @@ class ApiOrderController extends BaseOrderController
$pager = new Pagerfanta(new DoctrineORMAdapter($builder, false)); $pager = new Pagerfanta(new DoctrineORMAdapter($builder, false));
$pager->setCurrentPage($page); $pager->setCurrentPage($page);
$pager->setMaxPerPage($perPage); $pager->setMaxPerPage($perPage);
$paginator = new PagerfantaPaginatorAdapter($pager, $routeGenerator);
$resource->setPaginator($paginator); $resource->setPaginator(new PagerfantaPaginatorAdapter($pager, $routeGenerator));
return $this->returnResourceResponse($request, $fractal, $resource); return $this->returnResourceResponse($request, $fractal, $resource);
} }

View File

@@ -0,0 +1,19 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2016 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Utilities;
final class NullableDateTime
{
public static function format(\DateTime $dateTime = null, $format = DATE_ATOM, $default = null)
{
return $dateTime ? $dateTime->format($format) : $default;
}
}

View File

@@ -26,6 +26,7 @@ use Alchemy\Phrasea\Core\Event\Acl\SysadminChangedEvent;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Model\RecordInterface; use Alchemy\Phrasea\Model\RecordInterface;
use Alchemy\Phrasea\Model\RecordReferenceInterface; use Alchemy\Phrasea\Model\RecordReferenceInterface;
use Alchemy\Phrasea\Utilities\NullableDateTime;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
@@ -1706,10 +1707,10 @@ class ACL implements cache_cacheableInterface
} }
$params = [ $params = [
':usr_id' => $this->user->getId() ':usr_id' => $this->user->getId(),
, ':base_id' => $base_id ':base_id' => $base_id,
, 'limited_from' => ($limit_from ? $limit_from->format(DATE_ISO8601) : null) 'limited_from' => NullableDateTime::format($limit_from, DATE_ISO8601),
, 'limited_to' => ($limit_to ? $limit_to->format(DATE_ISO8601) : null) 'limited_to' => NullableDateTime::format($limit_to, DATE_ISO8601),
]; ];
$stmt = $this->app->getApplicationBox()->get_connection()->prepare($sql); $stmt = $this->app->getApplicationBox()->get_connection()->prepare($sql);

View File

@@ -10,6 +10,7 @@
*/ */
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Utilities\NullableDateTime;
class Bridge_Element class Bridge_Element
{ {
@@ -383,9 +384,9 @@ class Bridge_Element
SET uploaded_on = :uploaded_on, updated_on = :update WHERE id = :id'; SET uploaded_on = :uploaded_on, updated_on = :update WHERE id = :id';
$params = [ $params = [
':uploaded_on' => $this->uploaded_on ? $this->uploaded_on->format(DATE_ISO8601) : null ':uploaded_on' => NullableDateTime::format($this->uploaded_on, DATE_ISO8601),
, ':id' => $this->id ':id' => $this->id,
, ':update' => $this->updated_on->format(DATE_ISO8601) ':update' => $this->updated_on->format(DATE_ISO8601),
]; ];
$stmt = $this->app->getApplicationBox()->get_connection()->prepare($sql); $stmt = $this->app->getApplicationBox()->get_connection()->prepare($sql);

View File

@@ -11,6 +11,7 @@
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Http\StaticFile\Symlink\SymLinker; use Alchemy\Phrasea\Http\StaticFile\Symlink\SymLinker;
use Alchemy\Phrasea\Utilities\NullableDateTime;
use Guzzle\Http\Url; use Guzzle\Http\Url;
use MediaAlchemyst\Alchemyst; use MediaAlchemyst\Alchemyst;
use MediaVorus\Media\MediaInterface; use MediaVorus\Media\MediaInterface;
@@ -206,8 +207,8 @@ SQL;
'physically_present' => $this->is_physically_present, 'physically_present' => $this->is_physically_present,
'is_substituted' => $this->is_substituted, 'is_substituted' => $this->is_substituted,
'subdef_id' => $this->subdef_id, 'subdef_id' => $this->subdef_id,
'updated_on' => $this->modification_date ? $this->modification_date->format(DATE_ATOM) : '', 'updated_on' => NullableDateTime::format($this->modification_date),
'created_on' => $this->creation_date ? $this->creation_date->format(DATE_ATOM) : '', 'created_on' => NullableDateTime::format($this->creation_date),
'etag' => $this->etag, 'etag' => $this->etag,
'url' => (string)$this->url, 'url' => (string)$this->url,
]; ];