diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 02921ad38..0adcb9ed9 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -53,6 +53,10 @@ class Application extends App { }); $container->registerMiddleware('SharingMiddleware'); + $container->registerService('databaseType', function($container) { + return $container->getServer()->getConfig()->getSystemValue('dbtype', 'sqlite'); + }); + // Delete user/group acl entries when they get deleted /** @var IUserManager $userManager */ $userManager = $server->getUserManager(); diff --git a/lib/Db/Card.php b/lib/Db/Card.php index 2c2ba7a39..a93309f1c 100644 --- a/lib/Db/Card.php +++ b/lib/Db/Card.php @@ -42,6 +42,8 @@ class Card extends RelationalEntity implements JsonSerializable { protected $archived = false; protected $duedate = null; + private $databaseType = 'sqlite'; + const DUEDATE_FUTURE = 0; const DUEDATE_NEXT = 1; const DUEDATE_NOW = 2; @@ -58,10 +60,17 @@ class Card extends RelationalEntity implements JsonSerializable { $this->addResolvable('owner'); } - public function getDuedate() { + public function setDatabaseType($type) { + $this->databaseType = $type; + } + + public function getDuedate($isoFormat = false) { if($this->duedate === null) return null; $dt = new DateTime($this->duedate); + if (!$isoFormat && $this->databaseType === 'mysql') { + return $dt->format('Y-m-d H:i:s'); + } return $dt->format('c'); } @@ -91,7 +100,7 @@ class Card extends RelationalEntity implements JsonSerializable { $json['overdue'] = self::DUEDATE_OVERDUE; } } - $json['duedate'] = $this->getDuedate(); + $json['duedate'] = $this->getDuedate(true); return $json; } diff --git a/lib/Db/CardMapper.php b/lib/Db/CardMapper.php index e5f4fa284..2d633f21b 100644 --- a/lib/Db/CardMapper.php +++ b/lib/Db/CardMapper.php @@ -32,14 +32,22 @@ class CardMapper extends DeckMapper implements IPermissionMapper { private $labelMapper; private $userManager; + private $databaseType; - public function __construct(IDBConnection $db, LabelMapper $labelMapper, IUserManager $userManager) { + public function __construct( + IDBConnection $db, + LabelMapper $labelMapper, + IUserManager $userManager, + $databaseType + ) { parent::__construct($db, 'deck_cards', '\OCA\Deck\Db\Card'); $this->labelMapper = $labelMapper; $this->userManager = $userManager; + $this->databaseType = $databaseType; } public function insert(Entity $entity) { + $entity->setDatabaseType($this->databaseType); $entity->setCreatedAt(time()); $entity->setLastModified(time()); return parent::insert($entity); @@ -141,4 +149,4 @@ class CardMapper extends DeckMapper implements IPermissionMapper { } -} \ No newline at end of file +} diff --git a/tests/unit/Db/CardTest.php b/tests/unit/Db/CardTest.php index 184c5b569..a81df4c2c 100644 --- a/tests/unit/Db/CardTest.php +++ b/tests/unit/Db/CardTest.php @@ -99,4 +99,11 @@ class CardTest extends \PHPUnit_Framework_TestCase { ], $card->jsonSerialize()); } + public function testMysqlDateFallback() { + $date = new DateTime(); + $card = new Card(); + $card->setDuedate($date->format('c')); + $card->setDatabaseType('mysql'); + $this->assertEquals($date->format('Y-m-d H:i:s'), $card->getDuedate(false)); + } } \ No newline at end of file