-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
179 additions
and
38 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
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); | ||
} | ||
} |
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,7 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\UtilsBundle\Dca; | ||
|
||
class DateAddedField extends AbstractDcaField | ||
{ | ||
} |
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,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; | ||
} | ||
} |
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 || !$dc->id) { | ||
return; | ||
} | ||
|
||
$model = $this->getModelInstance($dc->table, $insertId); | ||
if (!$model || $model->dateAdded > 0) { | ||
return; | ||
} | ||
|
||
$model->dateAdded = time(); | ||
$model->save(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/EventListener/DcaAuthorListener.php → ...ntListener/DcaField/DcaAuthorListener.php
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