Skip to content

Commit

Permalink
Fix mysql datetime format
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Oct 10, 2017
1 parent 1b0ac8c commit 2cd7159
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function __construct(array $urlParams = array()) {
});
$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();
Expand Down
13 changes: 11 additions & 2 deletions lib/Db/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Card extends RelationalEntity implements JsonSerializable {
protected $duedate = null;
protected $notified = false;

private $databaseType = 'sqlite';

const DUEDATE_FUTURE = 0;
const DUEDATE_NEXT = 1;
const DUEDATE_NOW = 2;
Expand All @@ -60,10 +62,17 @@ public function __construct() {
$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');
}

Expand Down Expand Up @@ -93,7 +102,7 @@ public function jsonSerialize() {
$json['overdue'] = self::DUEDATE_OVERDUE;
}
}
$json['duedate'] = $this->getDuedate();
$json['duedate'] = $this->getDuedate(true);
unset($json['notified']);
return $json;
}
Expand Down
8 changes: 7 additions & 1 deletion lib/Db/CardMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,32 @@ class CardMapper extends DeckMapper implements IPermissionMapper {
private $userManager;
/** @var IManager */
private $notificationManager;
private $databaseType;

public function __construct(
IDBConnection $db,
LabelMapper $labelMapper,
IUserManager $userManager,
IManager $notificationManager
IManager $notificationManager,
$databaseType
) {
parent::__construct($db, 'deck_cards', '\OCA\Deck\Db\Card');
$this->labelMapper = $labelMapper;
$this->userManager = $userManager;
$this->notificationManager = $notificationManager;
$this->databaseType = $databaseType;
}

public function insert(Entity $entity) {
$entity->setDatabaseType($this->databaseType);
$entity->setCreatedAt(time());
$entity->setLastModified(time());
return parent::insert($entity);
}

public function update(Entity $entity, $updateModified = true) {
$entity->setDatabaseType($this->databaseType);

if ($updateModified)
$entity->setLastModified(time());

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/Db/CardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,11 @@ public function testJsonSerializeLabels() {
], $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));
}
}

0 comments on commit 2cd7159

Please sign in to comment.