From c46e3799eaa1b6eea7920e2e3f7e29e001c65b69 Mon Sep 17 00:00:00 2001 From: Ingolf Steinhardt Date: Tue, 26 Jul 2022 11:47:52 +0200 Subject: [PATCH 1/6] Fix different bugs at file upload --- .../widgets/form_upload-on-steroids.html5 | 50 +++++----- src/Widgets/UploadOnSteroids.php | 93 ++++++++++--------- 2 files changed, 74 insertions(+), 69 deletions(-) diff --git a/src/Resources/contao/templates/widgets/form_upload-on-steroids.html5 b/src/Resources/contao/templates/widgets/form_upload-on-steroids.html5 index 9d13cca..044c9b0 100644 --- a/src/Resources/contao/templates/widgets/form_upload-on-steroids.html5 +++ b/src/Resources/contao/templates/widgets/form_upload-on-steroids.html5 @@ -15,32 +15,32 @@ files): ?>
diff --git a/src/Widgets/UploadOnSteroids.php b/src/Widgets/UploadOnSteroids.php index 36f9509..f606ce8 100644 --- a/src/Widgets/UploadOnSteroids.php +++ b/src/Widgets/UploadOnSteroids.php @@ -3,7 +3,7 @@ /** * This file is part of contao-community-alliance/dc-general-contao-frontend. * - * (c) 2016-2019 Contao Community Alliance. + * (c) 2016-2022 Contao Community Alliance. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -12,7 +12,8 @@ * * @package contao-community-alliance/dc-general-contao-frontend * @author Sven Baumann - * @copyright 2016-2019 Contao Community Alliance. + * @author Ingolf Steinhardt + * @copyright 2016-2022 Contao Community Alliance. * @license https://github.com/contao-community-alliance/dc-general-contao-frontend/blob/master/LICENSE * LGPL-3.0-or-later * @filesource @@ -20,12 +21,14 @@ namespace ContaoCommunityAlliance\DcGeneral\ContaoFrontend\Widgets; +use Contao\CoreBundle\Slug\Slug as SlugGenerator; use Contao\CoreBundle\Framework\Adapter; use Contao\Dbafs; use Contao\FilesModel; use Contao\FormFileUpload; use Contao\Input; use Contao\StringUtil; +use Contao\System; use Doctrine\DBAL\Connection; use Symfony\Component\Filesystem\Filesystem; use Symfony\Contracts\Translation\TranslatorInterface; @@ -39,7 +42,7 @@ * - Can add a default image * - Can add a default image * - Output the Image as Thumbnail - * - Normalize the extend folder (StringUtil::generateAlias) + * - Normalize the extent folder * - Can prefix and postfix the filename. * * @property boolean deselect @@ -81,7 +84,7 @@ class UploadOnSteroids extends FormFileUpload * * @var TranslatorInterface */ - protected $translator; + protected TranslatorInterface $translator; /** * The input provider; @@ -103,12 +106,13 @@ class UploadOnSteroids extends FormFileUpload * @var Filesystem */ private $filesystem; + /** - * The string util. + * The slug generator. * - * @var StringUtil + * @var SlugGenerator */ - private $stringUtil; + private $slugGenerator; public function __construct($attributes = null) { @@ -172,7 +176,7 @@ public function parseFilename(string $filename): string return $filename; } - return $this->normalizeFilename($this->preOrPostFixFilename($filename)); + return $this->normalizeFilename($filename); } /** @@ -183,7 +187,7 @@ public function validate() $inputName = $this->name; if ($this->normalizeExtendFolder) { - $this->extendFolder = $this->str()->generateAlias($this->extendFolder); + $this->extendFolder = $this->slugGenerator()->generate($this->extendFolder, $this->getSlugOptions()); } if ($this->extendFolder) { @@ -221,7 +225,7 @@ private function validateSingleUpload(): void return; } - $inputName = $this->name; + $inputName = $this->name; $_FILES[$inputName]['name'] = $this->parseFilename($_FILES[$inputName]['name']); parent::validate(); @@ -337,7 +341,7 @@ private function deselectFile(string $inputName) /** * Delete the file, if is mark for delete. * - * @param string $inputName The input nanme. + * @param string $inputName The input name. * * @return void */ @@ -360,6 +364,8 @@ private function deleteFile(string $inputName) $file->delete(); } + Dbafs::deleteResource($file->path); + return; } @@ -374,18 +380,12 @@ private function deleteFile(string $inputName) $this->filesystem()->remove($file->path); $file->delete(); + Dbafs::deleteResource($file->path); } $this->value = \array_map('\Contao\StringUtil::uuidToBin', $diffValues); } - /** - * Normalize the filename. - * - * @param array $file The file information. - * - * @return void - */ /** * Normalize the filename. * @@ -402,12 +402,12 @@ private function normalizeFilename(string $filename): string $fileInfo = \pathinfo($filename); $currentExtension = $fileInfo['extension']; - $normalizeExtension = $this->stringUtil()->generateAlias($currentExtension); + $normalizeExtension = $this->slugGenerator()->generate($currentExtension, $this->getSlugOptions()); $currentFilename = $fileInfo['filename']; - $normalizeFilename = $this->stringUtil()->generateAlias($currentFilename); + $normalizeFilename = $this->slugGenerator()->generate($currentFilename, $this->getSlugOptions()); - return $normalizeFilename . '.' . $normalizeExtension; + return $this->preOrPostFixFilename($normalizeFilename) . '.' . $normalizeExtension; } /** @@ -423,20 +423,14 @@ private function preOrPostFixFilename(string $filename): string return $filename; } - $fileInfo = \pathinfo($filename); + $prefix = $this->prefixFilename + ? $this->slugGenerator()->generate($this->prefixFilename, $this->getSlugOptions()) + : ''; + $postfix = $this->postfixFilename + ? $this->slugGenerator()->generate($this->postfixFilename, $this->getSlugOptions()) + : ''; - $extension = $fileInfo['extension']; - - $currentFilename = $fileInfo['filename']; - $extendFilename = ($this->prefixFilename ?: '') . - 'place-holder-extend-filename' . - ($this->postfixFilename ?: ''); - if ($this->normalizeFilename) { - $extendFilename = $this->stringUtil()->generateAlias($extendFilename); - } - $extendFilename = \str_replace('place-holder-extend-filename', $currentFilename, $extendFilename); - - return $extendFilename . '.' . $extension; + return $prefix . $filename . $postfix; } /** @@ -476,7 +470,7 @@ private function addFiles(): void ) ->from($platform->quoteIdentifier('tl_files')) ->where($builder->expr()->in($platform->quoteIdentifier('uuid'), ':uuids')) - ->setParameter(':uuids', (array) $this->value, Connection::PARAM_STR_ARRAY); + ->setParameter('uuids', (array) $this->value, Connection::PARAM_STR_ARRAY); $statement = $builder->execute(); if (!$statement->rowCount()) { @@ -501,7 +495,7 @@ public function trans( array $parameters = [], ?string $domain = 'contao_default', ?string $locale = null - ) { + ): string { return $this->translator()->trans($transId, $parameters, $domain, $locale); } @@ -516,7 +510,7 @@ private function addIsDeletable(): void } /** - * Add file is deselectable. + * Add file is deselect able. * * @return void */ @@ -584,7 +578,7 @@ private function filesModel(): Adapter * * @return Filesystem */ - private function filesystem(): Filesystem + private function filesystem() { if (!$this->filesystem) { $this->filesystem = self::getContainer()->get('filesystem'); @@ -594,17 +588,28 @@ private function filesystem(): Filesystem } /** - * Get the string util. + * Get the slug generator. * - * @return Adapter|StringUtil + * @return SlugGenerator */ - private function stringUtil(): Adapter + private function slugGenerator() { - if (!$this->stringUtil) { - $this->stringUtil = self::getContainer()->get('contao.framework')->getAdapter(StringUtil::class); + if (!$this->slugGenerator) { + $this->slugGenerator = System::getContainer()->get('contao.slug'); } - return $this->stringUtil; + return $this->slugGenerator; + } + + /** + * Get the slug options. + * + * @return array + */ + protected function getSlugOptions(): array + { + // TODO: make configurable. + return ['locale' => 'de', 'validChars' => '0-9a-z_-']; } /** From 44b124920da84a371e88efb73a86d3464e35848c Mon Sep 17 00:00:00 2001 From: Ingolf Steinhardt Date: Wed, 27 Jul 2022 11:51:48 +0200 Subject: [PATCH 2/6] Add github actions --- .github/CONTRIBUTING.md | 66 +++++++++++++++++++++++++++++++ .github/ISSUE_TEMPLATE.md | 39 ++++++++++++++++++ .github/PULL_REQUEST_TEMPLATE.md | 12 ++++++ .github/workflows/diagnostics.yml | 49 +++++++++++++++++++++++ .travis.yml | 59 --------------------------- README.md | 2 +- composer.json | 2 +- 7 files changed, 168 insertions(+), 61 deletions(-) create mode 100644 .github/CONTRIBUTING.md create mode 100644 .github/ISSUE_TEMPLATE.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/workflows/diagnostics.yml delete mode 100644 .travis.yml diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..0c2013e --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,66 @@ +# How to contribute + +We are really glad you're reading this, because we need volunteer developers to +help this project come to fruition. + +If you haven't already, come find us in IRC (#contao.cca on freenode). +We want you working on things you're excited about. + +The following is a set of guidelines for contributing to DC_General, which +is hosted in the [Contao Community Alliance organization][1] on GitHub. These +are just guidelines, not rules, use your best judgement and feel free to +propose changes to this document in a pull request. + +## Submitting issues + +* Use the search function to see if a similar issue has already been submitted. +* Describe the issue in detail and include all the steps to follow in order to + reproduce the bug. +* Include the version of Contao, PHP and DC_General you are using (if possible + with a detailed list of other installed extensions that might be related). +* Include screenshots or screencasts if possible; they are immensely helpful. +* If you are reporting a bug, please include any related error message you are + seeing and also check the `system/logs/error.log` file. The error message is + not just the message but also the complete(!) stack trace below the message. + This trace is a long list of function calls which helps us to diagnose the + problem en detail. + +## Submitting changes + +* Please send a [GitHub Pull Request to Contao Community Alliance][1] with a + clear list of what you've done (read more about [pull requests][2]). +* When you send a pull request, we will love you forever if you include + phpunit tests. We can always use more test coverage. +* Please follow the [phpcq 2.0][3] coding standards. +* Please make sure all of your commits are atomic (only one feature or fix per + commit). +* We use phpcq/all-tasks in these projects, so please check your changes + using phpcq when submitting a pull request. +* Create your pull request against the [`master`][4] branch for bug fixes or the + [`develop`][5] branch for new features. +* Include screenshots in your pull request whenever possible. + +Always write a clear log message for your commits. +One-line messages are fine for small changes, but bigger changes should look +like this: + + $ git commit -m "A brief summary of the commit + > + > A paragraph describing what changed and its impact." + +## Testing + +We have a handful of unit tests. Please write unit tests for new code you +create. + +## Git commit messages + +* Use the present tense ("Add feature" not "Added feature"). +* Use the imperative mood ("Move cursor to …" not "Moves cursor to …"). +* Reference issues and pull requests liberally. + +[1]: https://github.com/contao-community-alliance +[2]: http://help.github.com/pull-requests/ +[3]: https://github.com/phpcq/coding-standard +[4]: https://github.com/contao-community-alliance/dc-general-contao-frontend/pull/new/master +[5]: https://github.com/contao-community-alliance/dc-general-contao-frontend/pull/new/develop diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..5e4e4cc --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,39 @@ +## Checklist before I submit this issue report + +I confirm that: +- [ ] I have tested this with the latest version available +- [ ] I have checked the Contao community forums for references https://community.contao.org/ +- [ ] I have checked existing issues for duplicates and found none @ https://github.com/contao-community-alliance/dc-general-contao-frontend/issues?q=is%3Aissue + +## My environment is: + +(Please fill in the actual values from your environment) + +| Key | Value | Comments | +| ----------------------------------- | ---------| ---------------------------------| +| PHP version: | | | +| Contao version: | | | +| DC_General version: | | | +| Installation via composer: | (yes/no) | | + +## Issue description + +(Describe the problem you are having) + +## Steps to reproduce + +1. [First Step] +2. [Second Step] +3. [and so on…] + +## Describe the behaviour of the application + +(Tell us what happens) + +## Describe the expected behaviour of the application + +(Tell us what should have happened) + +## Screenshots + +(Add some visual love or even a video if you can) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..ac28856 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,12 @@ +## Description + +Please explain the detailed changes you made here. +Reference any issue number this pull request fixes. + +## Checklist +- [ ] Read and understood the [CONTRIBUTING guidelines](CONTRIBUTING.md) +- [ ] Created tests, if possible +- [ ] All tests passing +- [ ] Extended the README / documentation, if necessary +- [ ] Added myself to the `@authors` in touched PHP files +- [ ] Checked the changes with phpcq and introduced no new issues diff --git a/.github/workflows/diagnostics.yml b/.github/workflows/diagnostics.yml new file mode 100644 index 0000000..8208b32 --- /dev/null +++ b/.github/workflows/diagnostics.yml @@ -0,0 +1,49 @@ +name: DC General Contao Frontend + +on: + push: + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + php: [7.4] + contao: [~4.9.0] + + steps: + - name: PHP ${{ matrix.php }} ${{ matrix.contao }} Pull source + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + # see https://github.com/shivammathur/setup-php + - name: PHP ${{ matrix.php }} ${{ matrix.contao }} Setup PHP. + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + + - name: PHP ${{ matrix.php }} ${{ matrix.contao }} Cache composer cache directory + uses: actions/cache@v1 + env: + cache-name: composer-cache-dir + with: + path: ~/.cache/composer + key: ${{ runner.os }}-build-${{ env.cache-name }} + + - name: PHP ${{ matrix.php }} ${{ matrix.contao }} Cache vendor directory + uses: actions/cache@v1 + env: + cache-name: composer-vendor + with: + path: vendor + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-build-${{ env.cache-name }}- + - name: PHP ${{ matrix.php }} ${{ matrix.contao }} Install composer dependencies + run: composer update --prefer-dist --no-interaction --no-suggest + + - name: PHP ${{ matrix.php }} ${{ matrix.contao }} Run tests + run: ant -keep-going diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ed27399..0000000 --- a/.travis.yml +++ /dev/null @@ -1,59 +0,0 @@ -dist: xenial - -addons: - apt: - packages: - - ant-optional - -language: php - -php: - - "7.4" - - "7.3" - - "7.2" - -env: - - CONTAO_VERSION='contao/core-bundle ~4.9.0' - - CONTAO_VERSION='contao/core-bundle ~4.10.0@dev' - -# Exclude impossible Contao Version combinations. -matrix: - fast_finish: true - allow_failures: - - php: "7.4" - - env: CONTAO_VERSION='contao/core-bundle ~4.10.0@dev' - -before_script: - - echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini - - travis_retry composer self-update && composer --version - - travis_retry composer require contao/core-bundle $CONTAO_VERSION --no-update - - > - if [ "x${TRAVIS_TAG}" != "x" ]; then - export COMPOSER_ROOT_VERSION=${TRAVIS_TAG} - else - export COMPOSER_ROOT_VERSION=$([[ ${TRAVIS_BRANCH} =~ (hotfix|release)/([0-9.]*(-(alpha|beta|rc)[0-9]+)?) ]] \ - && echo ${BASH_REMATCH[2]} \ - || echo dev-${TRAVIS_BRANCH}) - fi - - echo "Using root version ${COMPOSER_ROOT_VERSION}" - - > - echo "PHP version: ${TRAVIS_PHP_VERSION}"; - if [ "x${TRAVIS_PHP_VERSION}" == "xnightly" ]; then - travis_retry composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-suggest - else - travis_retry composer update --prefer-dist --no-interaction --no-suggest - fi - -script: ant -keep-going - -# Hack to make things work again - we can not use a shallow repository. -git: - depth: 2147483647 - -branches: - except: - - /.*-translation/ - -cache: - directories: - - vendor diff --git a/README.md b/README.md index 913ef0f..3f00d88 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.org/contao-community-alliance/dc-general-contao-frontend.png)](https://travis-ci.org/contao-community-alliance/dc-general-contao-frontend) +[![Build Status](https://github.com/contao-community-alliance/dc-general-contao-frontend/actions/workflows/diagnostics.yml/badge.svg)](https://github.com/contao-community-alliance/dc-general-contao-frontend/actions) [![Latest Version tagged](http://img.shields.io/github/tag/contao-community-alliance/dc-general-contao-frontend.svg)](https://github.com/contao-community-alliance/dc-general-contao-frontend/tags) [![Latest Version on Packagist](http://img.shields.io/packagist/v/contao-community-alliance/dc-general-contao-frontend.svg)](https://packagist.org/packages/contao-community-alliance/dc-general-contao-frontend) [![Installations via composer per month](http://img.shields.io/packagist/dm/contao-community-alliance/dc-general-contao-frontend.svg)](https://packagist.org/packages/contao-community-alliance/dc-general-contao-frontend) diff --git a/composer.json b/composer.json index 8bef536..757e13c 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "source":"https://github.com/contao-community-alliance/dc-general-contao-frontend" }, "require": { - "php":"^7.2", + "php":"^7.4", "contao-community-alliance/dc-general": "^2.2", "contao-community-alliance/url-builder": "~1.1", "contao-community-alliance/translator": "^2.2", From f34aaf169951919db9bab0b8ff1ac04068936952 Mon Sep 17 00:00:00 2001 From: Ingolf Steinhardt Date: Wed, 27 Jul 2022 20:08:35 +0200 Subject: [PATCH 3/6] Add sorting --- src/Widgets/UploadOnSteroids.php | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Widgets/UploadOnSteroids.php b/src/Widgets/UploadOnSteroids.php index f606ce8..9908851 100644 --- a/src/Widgets/UploadOnSteroids.php +++ b/src/Widgets/UploadOnSteroids.php @@ -136,7 +136,9 @@ public function __set($key, $value) 'postfixFilename', 'files', 'showThumbnail', - 'multiple' + 'multiple', + 'imageSize', + 'sortBy' ] )) { $this->arrConfiguration[$key] = $value; @@ -156,7 +158,7 @@ public function parse($attributes = null) $this->addIsDeselectable(); $this->addIsMultiple(); $this->addShowThumbnail(); - $this->addFiles(); + $this->addFiles($this->sortBy); $this->value = \implode(',', \array_map('\Contao\StringUtil::binToUuid', (array) $this->value)); @@ -436,9 +438,11 @@ private function preOrPostFixFilename(string $filename): string /** * Add the files from the value. * + * @param string $sortBy The file sorting + * * @return void */ - private function addFiles(): void + private function addFiles($sortBy) { if (empty($this->value)) { $this->files = null; @@ -451,10 +455,30 @@ private function addFiles(): void $platform = $connection->getDatabasePlatform(); $builder = $connection->createQueryBuilder(); + + switch ($sortBy) { + case 'name_desc': + $builder->orderBy('name', 'DESC'); + break; + case 'date_asc': + $builder->orderBy('tstamp', 'ASC'); + break; + case 'date_desc': + $builder->orderBy('tstamp', 'DESC'); + break; + case 'random': + $builder->orderBy('RAND()'); + break; + default: + case 'name_asc': + $builder->orderBy('name', 'ASC'); + } + $builder ->select( $platform->quoteIdentifier('id'), $platform->quoteIdentifier('pid'), + $platform->quoteIdentifier('tstamp'), $platform->quoteIdentifier('uuid'), $platform->quoteIdentifier('type'), $platform->quoteIdentifier('path'), From ea47ef225cd9ea1f98955e1caa632c1d0efa9a1d Mon Sep 17 00:00:00 2001 From: Ingolf Steinhardt Date: Thu, 28 Jul 2022 23:32:37 +0200 Subject: [PATCH 4/6] Add thumbnail --- .../widgets/form_upload-on-steroids.html5 | 52 ++++++++++++------- src/Widgets/UploadOnSteroids.php | 44 +++++++++++++++- 2 files changed, 76 insertions(+), 20 deletions(-) diff --git a/src/Resources/contao/templates/widgets/form_upload-on-steroids.html5 b/src/Resources/contao/templates/widgets/form_upload-on-steroids.html5 index 044c9b0..9caf3d0 100644 --- a/src/Resources/contao/templates/widgets/form_upload-on-steroids.html5 +++ b/src/Resources/contao/templates/widgets/form_upload-on-steroids.html5 @@ -5,7 +5,8 @@ label): ?>