Skip to content

Commit

Permalink
Add date added field (#74)
Browse files Browse the repository at this point in the history
* add date added field

* add test

* fixed type exception

* fixed tests

* increase test coverage, some code adjustments

* increase coverage

* fixed issue with static variables
  • Loading branch information
koertho committed Mar 1, 2024
1 parent c6e6005 commit 37bdb6a
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 47 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -52,7 +52,8 @@
"preferred-install": "dist",
"allow-plugins": {
"contao-components/installer": true,
"contao/manager-plugin": true
"contao/manager-plugin": true,
"php-http/discovery": false
}
},
"extra": {
Expand Down
33 changes: 33 additions & 0 deletions src/Dca/AbstractDcaField.php
@@ -0,0 +1,33 @@
<?php

namespace HeimrichHannot\UtilsBundle\Dca;

abstract class AbstractDcaField
{
/**
* Register a dca to have an author field and update logic added.
*/
public static function register(string $table): DcaFieldConfiguration
{
$config = static::createOptionObject($table);
static::storeConfig($config);
return $config;
}

abstract protected static function storeConfig(DcaFieldConfiguration $config): void;

abstract protected static function loadConfig(): array;

/**
* @return array<DcaFieldConfiguration>
*/
public static function getRegistrations(): array
{
return static::loadConfig();
}

protected static function createOptionObject(string $table): DcaFieldConfiguration
{
return new DcaFieldConfiguration($table);
}
}
28 changes: 17 additions & 11 deletions src/Dca/AuthorField.php
Expand Up @@ -2,30 +2,36 @@

namespace HeimrichHannot\UtilsBundle\Dca;

class AuthorField
class AuthorField extends AbstractDcaField
{
public const TYPE_USER = 'user';
public const TYPE_MEMBER = 'member';

/** @var array */
protected static $tables = [];

/**
* Register a dca to have an author field and update logic added.
* @return array<AuthorFieldConfiguration>
*/
public static function register(string $table): AuthorFieldOptions
public static function getRegistrations(): array
{
$config = new AuthorFieldOptions($table);

static::$tables[$table] = $config;

return $config;
return parent::getRegistrations();
}

/**
* @return array<AuthorFieldOptions>
* @param string $table
* @return AuthorFieldConfiguration
*/
public static function getRegistrations(): array
protected static function createOptionObject(string $table): DcaFieldConfiguration
{
return new AuthorFieldConfiguration($table);
}

protected static function storeConfig(DcaFieldConfiguration $config): void
{
static::$tables[$config->getTable()] = $config;
}

protected static function loadConfig(): array
{
return static::$tables;
}
Expand Down
Expand Up @@ -2,10 +2,8 @@

namespace HeimrichHannot\UtilsBundle\Dca;

class AuthorFieldOptions
class AuthorFieldConfiguration extends DcaFieldConfiguration
{
/** @var string */
protected $table;
/** @var string */
protected $type = AuthorField::TYPE_USER;
/** @var string */
Expand All @@ -19,26 +17,7 @@ class AuthorFieldOptions
/** @var bool */
protected $filter = true;

/**
* @param string $table
*/
public function __construct(string $table)
{
$this->table = $table;
}


public function getTable(): string
{
return $this->table;
}

public function getType(): string
{
return $this->type;
}

public function setType(string $type): AuthorFieldOptions
public function setType(string $type): AuthorFieldConfiguration
{
$this->type = $type;
return $this;
Expand All @@ -54,7 +33,7 @@ public function getFieldNamePrefix(): string
return $this->fieldNamePrefix;
}

public function setFieldNamePrefix(string $fieldNamePrefix): AuthorFieldOptions
public function setFieldNamePrefix(string $fieldNamePrefix): AuthorFieldConfiguration
{
$this->fieldNamePrefix = $fieldNamePrefix;
return $this;
Expand All @@ -65,7 +44,7 @@ public function isUseDefaultLabel(): bool
return $this->useDefaultLabel;
}

public function setUseDefaultLabel(bool $useDefaultLabel): AuthorFieldOptions
public function setUseDefaultLabel(bool $useDefaultLabel): AuthorFieldConfiguration
{
$this->useDefaultLabel = $useDefaultLabel;
return $this;
Expand All @@ -76,7 +55,7 @@ public function isExclude(): bool
return $this->exclude;
}

public function setExclude(bool $exclude): AuthorFieldOptions
public function setExclude(bool $exclude): AuthorFieldConfiguration
{
$this->exclude = $exclude;
return $this;
Expand All @@ -87,7 +66,7 @@ public function isSearch(): bool
return $this->search;
}

public function setSearch(bool $search): AuthorFieldOptions
public function setSearch(bool $search): AuthorFieldConfiguration
{
$this->search = $search;
return $this;
Expand All @@ -98,9 +77,14 @@ public function isFilter(): bool
return $this->filter;
}

public function setFilter(bool $filter): AuthorFieldOptions
public function setFilter(bool $filter): AuthorFieldConfiguration
{
$this->filter = $filter;
return $this;
}

public function getType(): string
{
return $this->type;
}
}
18 changes: 18 additions & 0 deletions src/Dca/DateAddedField.php
@@ -0,0 +1,18 @@
<?php

namespace HeimrichHannot\UtilsBundle\Dca;

class DateAddedField extends AbstractDcaField
{
private static $tables = [];

protected static function storeConfig(DcaFieldConfiguration $config): void
{
static::$tables[$config->getTable()] = $config;
}

protected static function loadConfig(): array
{
return static::$tables;
}
}
24 changes: 24 additions & 0 deletions src/Dca/DcaFieldConfiguration.php
@@ -0,0 +1,24 @@
<?php

namespace HeimrichHannot\UtilsBundle\Dca;

class DcaFieldConfiguration
{
/**
* @var string
*/
private $table;

/**
* @param string $table
*/
public function __construct(string $table)
{
$this->table = $table;
}

public function getTable(): string
{
return $this->table;
}
}
37 changes: 37 additions & 0 deletions src/EventListener/DcaField/AbstractDcaFieldListener.php
@@ -0,0 +1,37 @@
<?php

namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;

use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\Model;
use Psr\Container\ContainerInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

abstract class AbstractDcaFieldListener implements ServiceSubscriberInterface
{
/**
* @var ContainerInterface
*/
private $container;

protected function getModelInstance(string $table, int $id): ?Model
{
$framework = $this->container->get('contao.framework');
$modelClass = $framework->getAdapter(Model::class)->getClassFromTable($table);
return $framework->getAdapter($modelClass)->findByPk($id);
}

public static function getSubscribedServices()
{
return [
'contao.framework' => ContaoFramework::class,
];
}

public function setContainer(ContainerInterface $container): ?ContainerInterface
{
$this->container = $container;
}


}
60 changes: 60 additions & 0 deletions src/EventListener/DcaField/DateAddedFieldListener.php
@@ -0,0 +1,60 @@
<?php

namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;

use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\DataContainer;
use HeimrichHannot\UtilsBundle\Dca\DateAddedField;

class DateAddedFieldListener extends AbstractDcaFieldListener
{
/**
* @Hook("loadDataContainer")
*/
public function onLoadDataContainer(string $table): void
{
if (!isset(DateAddedField::getRegistrations()[$table])) {
return;
}

$GLOBALS['TL_DCA'][$table]['config']['onload_callback'][] = [self::class, 'onLoadCallback'];
$GLOBALS['TL_DCA'][$table]['config']['oncopy_callback'][] = [self::class, 'onCopyCallback'];

$GLOBALS['TL_DCA'][$table]['fields']['dateAdded'] = [
'label' => &$GLOBALS['TL_LANG']['MSC']['dateAdded'],
'sorting' => true,
'eval' => ['rgxp' => 'datim', 'doNotCopy' => true],
'sql' => "int(10) unsigned NOT NULL default '0'",
];
}

public function onLoadCallback(DataContainer $dc = null): void
{
if (!$dc || !$dc->id) {
return;
}

$model = $this->getModelInstance($dc->table, (int)$dc->id);
if (!$model || $model->dateAdded > 0) {
return;
}

$model->dateAdded = time();
$model->save();
}

public function onCopyCallback(int $insertId, DataContainer $dc): void
{
if (!$dc->id) {
return;
}

$model = $this->getModelInstance($dc->table, $insertId);
if (!$model || $model->dateAdded > 0) {
return;
}

$model->dateAdded = time();
$model->save();
}
}
@@ -1,6 +1,6 @@
<?php

namespace HeimrichHannot\UtilsBundle\EventListener;
namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;

use Contao\BackendUser;
use Contao\CoreBundle\Framework\ContaoFramework;
Expand All @@ -9,7 +9,7 @@
use Contao\FrontendUser;
use Contao\Model;
use HeimrichHannot\UtilsBundle\Dca\AuthorField;
use HeimrichHannot\UtilsBundle\Dca\AuthorFieldOptions;
use HeimrichHannot\UtilsBundle\Dca\AuthorFieldConfiguration;
use Symfony\Component\Security\Core\Security;

class DcaAuthorListener
Expand Down Expand Up @@ -103,10 +103,10 @@ public function onConfigCopyCallback(int $insertId, DataContainer $dc): void
}

/**
* @param AuthorFieldOptions $options
* @param AuthorFieldConfiguration $options
* @return string
*/
protected function getAuthorFieldName(AuthorFieldOptions $options): string
protected function getAuthorFieldName(AuthorFieldConfiguration $options): string
{
if (!$options->hasFieldNamePrefix()) {
return 'author';
Expand Down
16 changes: 16 additions & 0 deletions tests/Dca/DcaFieldOptionsTest.php
@@ -0,0 +1,16 @@
<?php

namespace HeimrichHannot\UtilsBundle\Tests\Dca;


use HeimrichHannot\UtilsBundle\Dca\DcaFieldConfiguration;
use PHPUnit\Framework\TestCase;

class DcaFieldOptionsTest extends TestCase
{
public function testGetTable()
{
$dcaFieldOptions = new DcaFieldConfiguration('test_table');
$this->assertEquals('test_table', $dcaFieldOptions->getTable());
}
}

0 comments on commit 37bdb6a

Please sign in to comment.