Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: CI

on:
pull_request: null

jobs:
Silverstripe:
name: "Silverstripe (bundle)"
uses: nswdpc/ci-files/.github/workflows/silverstripe_53_83.yml@v-4
PHPStan:
name: "PHPStan (analyse)"
uses: nswdpc/ci-files/.github/workflows/phpstan.silverstripe_83.yml@v-4
needs: Silverstripe
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/client/node_modules
/vendor/
.DS_Store
/.php-cs-fixer.cache
/app/
/public/
/composer.lock
21 changes: 0 additions & 21 deletions .php-cs-fixer.dist.php

This file was deleted.

33 changes: 25 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,31 @@
"license": "BSD-3-Clause",
"authors": [
{
"name": "Mark Taylor",
"homepage": "https://dpc.nsw.gov.au",
"role": "Bon Vivant"
"name": "Mark Taylor"
},
{
"name": "James Ellis",
"homepage": "https://dpc.nsw.gov.au",
"role": "Developer"
"name": "James Ellis"
}
],
"require": {
"dnadesign/silverstripe-elemental": "^5"
"dnadesign/silverstripe-elemental": "^5",
"silverstripe/asset-admin": "^2.4"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^3"
"friendsofphp/php-cs-fixer": "^3",
"cambis/silverstripe-rector": "^2",
"cambis/silverstan": "^2",
"nswdpc/ci-files": "dev-v-4",
"phpstan/phpstan": "^2",
"phpstan/phpstan-phpunit": "^2",
"rector/rector": "^2"
},
"scripts": {
"phpstan-analyse": "./vendor/bin/phpstan analyse --ansi --no-progress --no-interaction --configuration vendor/nswdpc/ci-files/phpstan/.phpstan.silverstripe.neon src/ tests/",
"rector-dryrun": "./vendor/bin/rector process --dry-run --ansi --config vendor/nswdpc/ci-files/rector/.rector.silverstripe_53_83.php src/ tests/",
"rector-process": "./vendor/bin/rector process --no-diffs --ansi --config vendor/nswdpc/ci-files/rector/.rector.silverstripe_53_83.php src/ tests/",
"phpcsfixer-fix": "./vendor/bin/php-cs-fixer fix --ansi --no-interaction --config vendor/nswdpc/ci-files/php-cs-fixer/.php-cs-fixer.php src/ tests/"
},
"autoload": {
"psr-4": {
Expand All @@ -37,5 +46,13 @@
"src/Models/Elements/"
]
}
},
"config": {
"allow-plugins": {
"composer/installers": true,
"silverstripe/vendor-plugin": true,
"silverstripe/recipe-plugin": true,
"phpstan/extension-installer": true
}
}
}
25 changes: 13 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="nswdpc/silverstripe-elemental-image">
<directory>tests/</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</coverage>
<testsuite name="nswdpc/silverstripe-elemental-image">
<directory>tests/</directory>
</testsuite>
</phpunit>
96 changes: 63 additions & 33 deletions src/Models/Elements/ElementImage.php
Original file line number Diff line number Diff line change
@@ -1,101 +1,127 @@
<?php

namespace NSWDPC\Elemental\Models\Image;

use DNADesign\Elemental\Models\BaseElement;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\CheckboxField;

/**
* ElementImage adds an image with some config
* @property ?string $Width
* @property ?string $Height
* @property ?string $Caption
* @property int $ImageID
* @method \SilverStripe\Assets\Image Image()
* @property bool $HideCaption
*/
class ElementImage extends BaseElement
{
private static $icon = "font-icon-image";
private static string $icon = "font-icon-image";

private static string $table_name = "ElementImage";

private static string $title = "Image";

private static string $description = "Display an image";

private static string $singular_name = "Image";

private static string $plural_name = "Images";

private static array $allowed_file_types = ["jpg", "jpeg", "gif", "png", "webp"];

private static $table_name = "ElementImage";
public const WIDTH_FULL = 'full';

private static $title = "Image";
private static $description = "Display an image";
public const WIDTH_CONTAINER = 'container';

private static $singular_name = "Image";
private static $plural_name = "Images";
public const HEIGHT_SMALL = 'small';

private static $allowed_file_types = ["jpg", "jpeg", "gif", "png", "webp"];
public const HEIGHT_MEDIUM = 'medium';

const WIDTH_FULL = 'full';
const WIDTH_CONTAINER = 'container';
public const HEIGHT_LARGE = 'large';

const HEIGHT_SMALL = 'small';
const HEIGHT_MEDIUM = 'medium';
const HEIGHT_LARGE = 'large';
const HEIGHT_ORIGINAL = 'original';
public const HEIGHT_ORIGINAL = 'original';

#[\Override]
public function getType()
{
return _t(__CLASS__ . ".BlockType", "Image");
return _t(self::class . ".BlockType", "Image");
}

private static $db = [
private static array $db = [
"Width" => "Varchar",
"Height" => "Varchar",
'Caption' => 'Text',
"HideCaption" => "Boolean"
];

private static $has_one = [
private static array $has_one = [
"Image" => Image::class,
];

private static $summary_fields = [
private static array $defaults = [
"HideCaption" => 0
];

private static array $summary_fields = [
"Image.CMSThumbnail" => "Image",
"Title" => "Title",
];

private static $owns = ["Image"];
private static array $owns = ["Image"];

public function getAllowedFileTypes()
public function ShowCaption(): bool
{
return $this->HideCaption == 0;
}

public function getAllowedFileTypes(): array
{
$types = $this->config()->get("allowed_file_types");
if (empty($types)) {
$types = ['jpg', 'jpeg', 'gif', 'png', 'webp'];
}
$types = array_unique($types);
return $types;

return array_unique($types);
}

#[\Override]
public function getCMSFields()
{

$this->beforeUpdateCMSFields(function ($fields) {
$this->beforeUpdateCMSFields(function ($fields): void {
$fields->addFieldsToTab("Root.Main", [
DropdownField::create(
"Width",
_t(__CLASS__ . ".WIDTH", "Width"),
_t(self::class . ".WIDTH", "Width"),
[
self::WIDTH_CONTAINER => _t(__CLASS__ . ".CONTAINER_WIDTH", "Content width"),
self::WIDTH_FULL => _t(__CLASS__ . ".BROWSER_WIDTH", "Browser width")
self::WIDTH_CONTAINER => _t(self::class . ".CONTAINER_WIDTH", "Content width"),
self::WIDTH_FULL => _t(self::class . ".BROWSER_WIDTH", "Browser width")
]
),
DropdownField::create(
"Height",
_t(__CLASS__ . ".HEIGHT", "Height"),
_t(self::class . ".HEIGHT", "Height"),
[
self::HEIGHT_SMALL => _t(__CLASS__ . ".HEIGHT_SMALL", "Small"),
self::HEIGHT_MEDIUM => _t(__CLASS__ . ".HEIGHT_MEDIUM", "Medium"),
self::HEIGHT_LARGE => _t(__CLASS__ . ".HEIGHT_LARGE", "Large"),
self::HEIGHT_ORIGINAL => _t(__CLASS__ . ".HEIGHT_ORIGINAL", "Original")
self::HEIGHT_SMALL => _t(self::class . ".HEIGHT_SMALL", "Small"),
self::HEIGHT_MEDIUM => _t(self::class . ".HEIGHT_MEDIUM", "Medium"),
self::HEIGHT_LARGE => _t(self::class . ".HEIGHT_LARGE", "Large"),
self::HEIGHT_ORIGINAL => _t(self::class . ".HEIGHT_ORIGINAL", "Original")
]
),
UploadField::create(
"Image",
_t(__CLASS__ . ".SLIDE_IMAGE", "Image")
_t(self::class . ".SLIDE_IMAGE", "Image")
)
->setAllowedExtensions($this->getAllowedFileTypes())
->setIsMultiUpload(false)
->setDescription(
_t(
__CLASS__ . "ALLOWED_FILE_TYPES",
self::class . "ALLOWED_FILE_TYPES",
"Allowed file types: {types}",
[
'types' => implode(",", $this->getAllowedFileTypes())
Expand All @@ -104,7 +130,11 @@ public function getCMSFields()
),
TextareaField::create(
'Caption',
_t(__CLASS__ . ".CAPTION", "Caption")
_t(self::class . ".CAPTION", "Caption")
),
CheckboxField::create(
"HideCaption",
_t(self::class . ".HIDE_CAPTION", "Hide Caption")
)
]);
});
Expand Down
2 changes: 1 addition & 1 deletion templates/NSWDPC/Elemental/Models/Image/ElementImage.ss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="image">
{$Image}
</div>
<% if $Caption %>
<% if $Caption && $ShowCaption %>
<p class="caption">{$Caption}</p>
<% end_if %>
<% end_if %>
17 changes: 9 additions & 8 deletions tests/ElementImageTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace NSWDPC\Elemental\Models\Image\Tests;

use SilverStripe\Dev\SapphireTest;
Expand All @@ -8,10 +10,9 @@

class ElementImageTest extends SapphireTest
{

protected $usesDatabase = true;

public function testAllowedFileTypes()
public function testAllowedFileTypes(): void
{
$allowed = ["jpg", "jpeg"];
Config::modify()->set(
Expand All @@ -20,28 +21,28 @@ public function testAllowedFileTypes()
$allowed
);
$element = ElementImage::create();
$this->assertEquals( $allowed, $element->getAllowedFileTypes() );
$this->assertEquals($allowed, $element->getAllowedFileTypes());
}

public function testDimensions()
public function testDimensions(): void
{
$width = ElementImage::WIDTH_CONTAINER;
$height = ElementImage::HEIGHT_LARGE;
$element = ElementImage::create([
'Width' => $width,
'Height' => $height
]);
$this->assertEquals( $width, $element->Width );
$this->assertEquals( $height, $element->Height );
$this->assertEquals($width, $element->Width);
$this->assertEquals($height, $element->Height);
}

public function testCaption()
public function testCaption(): void
{
$caption = 'Test caption';
$element = ElementImage::create([
'Caption' => $caption
]);
$this->assertEquals( $caption, $element->Caption );
$this->assertEquals($caption, $element->Caption);
}

}