Skip to content

Commit

Permalink
added entity finter
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Mar 22, 2022
1 parent 482bda9 commit 4e917de
Show file tree
Hide file tree
Showing 8 changed files with 519 additions and 80 deletions.
142 changes: 80 additions & 62 deletions src/Command/CreateImageSizeItemsCommand.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
<?php

/*
* Copyright (c) 2021 Heimrich & Hannot GmbH
* Copyright (c) 2022 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\UtilsBundle\Command;

use Contao\CoreBundle\Command\AbstractLockedCommand;
use Contao\CoreBundle\Framework\FrameworkAwareInterface;
use Contao\CoreBundle\Framework\FrameworkAwareTrait;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\ImageSizeItemModel;
use Contao\ImageSizeModel;
use Contao\Model;
use Contao\System;
use HeimrichHannot\UtilsBundle\Util\Utils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class CreateImageSizeItemsCommand extends AbstractLockedCommand implements FrameworkAwareInterface
class CreateImageSizeItemsCommand extends Command
{
use FrameworkAwareTrait;

const MODE_FIRST = 1;
const MODE_INTERMEDIATE = 2;
const MODE_LAST = 3;
Expand All @@ -36,77 +33,48 @@ class CreateImageSizeItemsCommand extends AbstractLockedCommand implements Frame
1400,
];

protected static $defaultName = 'huh:utils:create-image-size-items';
/**
* @var ContaoFramework
*/
private $contaoFramework;
/**
* @var SymfonyStyle
* @var Utils
*/
private $io;
private $utils;

public function createImageSizes(SymfonyStyle $io, array $breakpoints, array $imageSizeIds = [])
public function __construct(ContaoFramework $contaoFramework, Utils $utils)
{
sort($breakpoints);

$creationCount = 0;
$columns = (empty($imageSizeIds) ? [] : ['tl_image_size.id IN ('.implode(',', $imageSizeIds).')']);

if (null === ($imageSizes = System::getContainer()->get('huh.utils.model')->findModelInstancesBy('tl_image_size', $columns, []))) {
$io->error('No image sizes found for the given ids.');

return false;
}

/** @var ImageSizeModel $imageSize */
foreach ($imageSizes as $imageSize) {
$existingItems = System::getContainer()->get('huh.utils.model')->findModelInstancesBy('tl_image_size_item', ['tl_image_size_item.pid=?'], [$imageSize->id]);

if (null !== $existingItems) {
$io->warning('Skipping image size ID '.$imageSize->id.' because it already has existing image size items.');

continue;
}

$j = 0;

// first
$this->createItem($imageSize, $j++, $breakpoints[0], null, static::MODE_FIRST);
++$creationCount;

// intermediates
foreach ($breakpoints as $i => $breakpoint) {
if ($i === \count($breakpoints) - 1) {
continue;
}

$this->createItem($imageSize, $j++, $breakpoint, $breakpoints[$i + 1], static::MODE_INTERMEDIATE);

++$creationCount;
}

// last
$this->createItem($imageSize, $j++, $breakpoints[\count($breakpoints) - 1], null, static::MODE_LAST);
++$creationCount;
}

$io->success($creationCount.' image size items have been created.');

return true;
parent::__construct();
$this->contaoFramework = $contaoFramework;
$this->utils = $utils;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('huh:utils:create-image-size-items')->setDescription('Creates image size items for a given image size entity. Image size entities with existing image size items will be skipped.');
$this->addArgument('image-size-ids', InputArgument::OPTIONAL, 'The comma separated ids of the image size. Skip the parameter in order to create image size items for all image size entities.');
$this->addArgument('breakpoints', InputArgument::OPTIONAL, 'The comma separated breakpoints as pixel amounts (defaults to "576,768,992,1200,1400").', implode(',', static::DEFAULT_BREAKPOINTS));
$this
->setDescription('Creates image size items for a given image size entity. Image size entities with existing image size items will be skipped.')
->addArgument(
'image-size-ids',
InputArgument::OPTIONAL,
'The comma separated ids of the image size. Skip the parameter in order to create image size items for all image size entities.'
)
->addArgument(
'breakpoints',
InputArgument::OPTIONAL,
'The comma separated breakpoints as pixel amounts (defaults to "576,768,992,1200,1400").', implode(',', static::DEFAULT_BREAKPOINTS)
);
}

/**
* {@inheritdoc}
*/
protected function executeLocked(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->framework->initialize();
$this->contaoFramework->initialize();

$io = new SymfonyStyle($input, $output);

Expand Down Expand Up @@ -158,4 +126,54 @@ protected function createItem(Model $imageSize, int $index, int $breakpoint, ?in

$item->save();
}

private function createImageSizes(SymfonyStyle $io, array $breakpoints, array $imageSizeIds = [])
{
sort($breakpoints);

$creationCount = 0;
$columns = (empty($imageSizeIds) ? [] : ['tl_image_size.id IN ('.implode(',', $imageSizeIds).')']);

if (null === ($imageSizes = $this->utils->model()->findModelInstancesBy('tl_image_size', $columns, []))) {
$io->error('No image sizes found for the given ids.');

return false;
}

/** @var ImageSizeModel $imageSize */
foreach ($imageSizes as $imageSize) {
$existingItems = $this->utils->model()->findModelInstancesBy('tl_image_size_item', ['tl_image_size_item.pid=?'], [$imageSize->id]);

if (null !== $existingItems) {
$io->warning('Skipping image size ID '.$imageSize->id.' because it already has existing image size items.');

continue;
}

$j = 0;

// first
$this->createItem($imageSize, $j++, $breakpoints[0], null, static::MODE_FIRST);
++$creationCount;

// intermediates
foreach ($breakpoints as $i => $breakpoint) {
if ($i === \count($breakpoints) - 1) {
continue;
}

$this->createItem($imageSize, $j++, $breakpoint, $breakpoints[$i + 1], static::MODE_INTERMEDIATE);

++$creationCount;
}

// last
$this->createItem($imageSize, $j++, $breakpoints[\count($breakpoints) - 1], null, static::MODE_LAST);
++$creationCount;
}

$io->success($creationCount.' image size items have been created.');

return true;
}
}
Loading

0 comments on commit 4e917de

Please sign in to comment.