Skip to content

Commit

Permalink
add date added field
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Feb 29, 2024
1 parent c6e6005 commit 5e2f039
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 38 deletions.
33 changes: 33 additions & 0 deletions src/Dca/AbstractDcaField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace HeimrichHannot\UtilsBundle\Dca;

abstract class AbstractDcaField
{
protected static $tables = [];

/**
* Register a dca to have an author field and update logic added.
*/
public static function register(string $table): DcaFieldOptions
{
$config = self::createOptionObject($table);

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

return $config;
}

/**
* @return array<DcaFieldOptions>
*/
public static function getRegistrations(): array
{
return static::$tables;
}

protected static function createOptionObject(string $table): DcaFieldOptions
{
return new DcaFieldOptions($table);
}
}
24 changes: 10 additions & 14 deletions src/Dca/AuthorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,27 @@

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<AuthorFieldOptions>
*/
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 AuthorFieldOptions
*/
public static function getRegistrations(): array
protected static function createOptionObject(string $table): DcaFieldOptions
{
return static::$tables;
return new AuthorFieldOptions($table);
}


}
23 changes: 1 addition & 22 deletions src/Dca/AuthorFieldOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace HeimrichHannot\UtilsBundle\Dca;

class AuthorFieldOptions
class AuthorFieldOptions extends DcaFieldOptions
{
/** @var string */
protected $table;
/** @var string */
protected $type = AuthorField::TYPE_USER;
/** @var string */
Expand All @@ -19,25 +17,6 @@ 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
{
$this->type = $type;
Expand Down
7 changes: 7 additions & 0 deletions src/Dca/DateAddedField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace HeimrichHannot\UtilsBundle\Dca;

class DateAddedField extends AbstractDcaField
{
}
29 changes: 29 additions & 0 deletions src/Dca/DcaFieldOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace HeimrichHannot\UtilsBundle\Dca;

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

/**
* @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;
}
}
37 changes: 37 additions & 0 deletions src/EventListener/DcaField/AbstractDcaFieldListener.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 || !$dc->id) {
return;
}

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

$model->dateAdded = time();
$model->save();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

use Contao\BackendUser;
use Contao\CoreBundle\Framework\ContaoFramework;
Expand Down
2 changes: 1 addition & 1 deletion tests/EventListener/DcaAuthorListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Contao\FrontendUser;
use Contao\Model;
use HeimrichHannot\UtilsBundle\Dca\AuthorField;
use HeimrichHannot\UtilsBundle\EventListener\DcaAuthorListener;
use HeimrichHannot\UtilsBundle\EventListener\DcaField\DcaAuthorListener;
use HeimrichHannot\UtilsBundle\Tests\AbstractUtilsTestCase;
use PHPUnit\Framework\MockObject\MockBuilder;
use Symfony\Component\Security\Core\Security;
Expand Down

0 comments on commit 5e2f039

Please sign in to comment.