Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
12 changed files
with
402 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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->id) { | ||
return; | ||
} | ||
|
||
$model = $this->getModelInstance($dc->table, $insertId); | ||
if (!$model || $model->dateAdded > 0) { | ||
return; | ||
} | ||
|
||
$model->dateAdded = time(); | ||
$model->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
Oops, something went wrong.