Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tastaturberuf committed May 30, 2020
0 parents commit c3f3e2d
Show file tree
Hide file tree
Showing 22 changed files with 809 additions and 0 deletions.
38 changes: 38 additions & 0 deletions ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* ImageCopyright for Contao Open Source CMS
*
* @copyright 2016 – 2020 Tastaturberuf <tastaturberuf.de>
* @author Daniel Jahnsmüller <tastaturberuf.de>
* @license LGPL-3.0-or-later
*/

declare(strict_types=1);


namespace Tastaturberuf\ContaoImageCopyrightBundle\ContaoManager;


use Contao\CoreBundle\ContaoCoreBundle;
use Contao\ManagerPlugin\Bundle\BundlePluginInterface;
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
use Tastaturberuf\ContaoImageCopyrightBundle\TastaturberufContaoImageCopyrightBundle;


class Plugin implements BundlePluginInterface
{

/**
* @inheritDoc
*/
public function getBundles(ParserInterface $parser)
{
return [
BundleConfig::create(TastaturberufContaoImageCopyrightBundle::class)
->setLoadAfter([ContaoCoreBundle::class])
];
}

}
111 changes: 111 additions & 0 deletions Controller/ImageCopyrightListController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/**
* ImageCopyright for Contao Open Source CMS
*
* @copyright 2016 – 2020 Tastaturberuf <tastaturberuf.de>
* @author Daniel Jahnsmüller <tastaturberuf.de>
* @license LGPL-3.0-or-later
*/

declare(strict_types=1);


namespace Tastaturberuf\ContaoImageCopyrightBundle\Controller;


use Contao\Config;
use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
use Contao\CoreBundle\Image\ImageFactoryInterface;
use Contao\CoreBundle\ServiceAnnotation\FrontendModule;
use Contao\FilesModel;
use Contao\Model\Collection;
use Contao\ModuleModel;
use Contao\StringUtil;
use Contao\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;


/**
* @FrontendModule(category="miscellaneous")
*/
class ImageCopyrightListController extends AbstractFrontendModuleController
{

public const TYPE = 'image_copyright_list';


/**
* @var ImageFactoryInterface
*/
private $imageFactory;

/**
* @var string
*/
private $rootDir;

/**
* @var array
*/
private $validImageExtensions;


public function __construct(ImageFactoryInterface $imageFactory, string $rootDir, array $validImageExtensions)
{
$this->imageFactory = $imageFactory;
$this->rootDir = $rootDir;
$this->validImageExtensions = $validImageExtensions;
}


protected function getResponse(Template $template, ModuleModel $model, Request $request): ?Response
{
if ( null !== $files = $this->getImages() )
{
$imgSize = StringUtil::deserialize($model->imgSize);

foreach ($files as $file)
{
$image = $this->imageFactory->create($this->rootDir.DIRECTORY_SEPARATOR.$file->path, $imgSize);

$file->src = $image->getUrl($this->rootDir);
$file->dimensions = $image->getDimensions();
}

$template->files = $files;
}

return $template->getResponse();
}


private function getImages(): ?Collection
{
// mask all strings to e.g. 'jpeg', 'webp', ...
$extensions = array_map(function (string $extension): string {
return "'$extension'";
}, $this->validImageExtensions);



$options =
[
'column' =>
[
// text is set
"ic_copyright != ''",
// type is file
"type = 'file'",
// is not hidden
"ic_hide = ''",
// is valid image type
sprintf("extension IN (%s)", join(',', $extensions))
]
];

return FilesModel::findAll($options);
}

}
39 changes: 39 additions & 0 deletions DependencyInjection/TastaturberufContaoImageCopyrightExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* ImageCopyright for Contao Open Source CMS
*
* @copyright 2016 – 2020 Tastaturberuf <tastaturberuf.de>
* @author Daniel Jahnsmüller <tastaturberuf.de>
* @license LGPL-3.0-or-later
*/

declare(strict_types=1);


namespace Tastaturberuf\ContaoImageCopyrightBundle\DependencyInjection;


use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;


class TastaturberufContaoImageCopyrightExtension extends Extension
{

/**
* @inheritdoc
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);

$loader->load('services.yaml');
}

}
91 changes: 91 additions & 0 deletions EventListener/DcaFilesListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* ImageCopyright for Contao Open Source CMS
*
* @copyright 2016 – 2020 Tastaturberuf <tastaturberuf.de>
* @author Daniel Jahnsmüller <tastaturberuf.de>
* @license LGPL-3.0-or-later
*/

declare(strict_types=1);


namespace Tastaturberuf\ContaoImageCopyrightBundle\EventListener;


use Contao\CoreBundle\DataContainer\PaletteManipulator;
use Contao\CoreBundle\ServiceAnnotation\Hook;
use Terminal42\ServiceAnnotationBundle\ServiceAnnotationInterface;


class DcaFilesListener implements ServiceAnnotationInterface
{

/**
* @Hook("loadDataContainer", priority=1)
*/
public function onLoadCallback(string $table): void
{
if ( 'tl_files' !== $table )
{
return;
}

$this->extendPalette($table);
$this->addFields($table);
}


private function extendPalette(string $table): void
{
PaletteManipulator::create()
->addLegend('tastaturberuf_image_copyright_legend', 'meta')
->addField(['ic_copyright', 'ic_href', 'ic_hide'], 'tastaturberuf_image_copyright_legend')
->applyToPalette('default', $table)
;
}


private function addFields(string $table): void
{
$GLOBALS['TL_DCA'][$table]['fields'] = array_replace_recursive($GLOBALS['TL_DCA'][$table]['fields'],
[
'ic_copyright' =>
[
'exclude' => true,
'inputType' => 'text',
'eval' =>
[
'maxlength' => 128,
'tl_class' => 'w50'
],
'sql' => "varchar(128) NOT NULL default ''"
],
'ic_href' =>
[
'exclude' => true,
'inputType' => 'text',
'eval' =>
[
'rgxp' => 'url',
'maxlength' => 255,
'tl_class' => 'w50'
],
'sql' => "varchar(255) NOT NULL default ''"
],
'ic_hide' =>
[
'exclude' => true,
'inputType' => 'checkbox',
'eval' =>
[
'tl_class' => 'clear m12 w50'
],
'sql' => "char(1) NOT NULL default ''"
],

]);
}

}
41 changes: 41 additions & 0 deletions EventListener/DcaModuleListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* ImageCopyright for Contao Open Source CMS
*
* @copyright 2016 – 2020 Tastaturberuf <tastaturberuf.de>
* @author Daniel Jahnsmüller <tastaturberuf.de>
* @license LGPL-3.0-or-later
*/

declare(strict_types=1);


namespace Tastaturberuf\ContaoImageCopyrightBundle\EventListener;


use Contao\CoreBundle\ServiceAnnotation\Callback;
use Contao\DataContainer;
use Tastaturberuf\ContaoImageCopyrightBundle\Controller\ImageCopyrightListController;
use Terminal42\ServiceAnnotationBundle\ServiceAnnotationInterface;


class DcaModuleListener implements ServiceAnnotationInterface
{

/**
* @Callback(table="tl_module", target="config.onload")
*/
public function onLoadCallback(DataContainer $dc): void
{
$GLOBALS['TL_DCA'][$dc->table]['palettes'][ImageCopyrightListController::TYPE] =
'
{title_legend},name,headline,type;
{config_legend},imgSize;
{template_legend:hide},customTpl;
{protected_legend:hide},protected;
{expert_legend:hide},guests,cssID
';
}

}
Loading

0 comments on commit c3f3e2d

Please sign in to comment.