Skip to content

Commit

Permalink
Update: gitlab files moved to github
Browse files Browse the repository at this point in the history
  • Loading branch information
codename-niels committed Apr 11, 2023
1 parent 933040c commit 5eca0d0
Show file tree
Hide file tree
Showing 12 changed files with 435 additions and 0 deletions.
1 change: 1 addition & 0 deletions .craftplugin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"pluginName":"Restrict Volumes","pluginDescription":"Decide what filetypes are allowed per volume.","pluginVersion":"1.0.0","pluginAuthorName":"Born05","pluginVendorName":"born05","pluginAuthorUrl":"https://born05.com","pluginAuthorGithub":"","codeComments":"","pluginComponents":["services","settings"],"consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"test","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# CRAFT ENVIRONMENT
.env.php
.env.sh
.env

# COMPOSER
/vendor

# BUILD FILES
/bower_components/*
/node_modules/*
/build/*
/yarn-error.log

# MISC FILES
.cache
.DS_Store
.idea
.project
.settings
*.esproj
*.sublime-workspace
*.sublime-project
*.tmproj
*.tmproject
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
config.codekit3
prepros-6.config
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Craft volume filetypes Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.0 - 2021-05-28
### Added
- Initial release
## 1.0.2 - 2021-06-01
### Fixed
- Settings error after adding a new volume
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2021 Born05

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "born05/restrict-volumes",
"description": "Decide what filetypes are allowed per volume.",
"type": "craft-plugin",
"version": "1.0.0",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"restrict volumes"
],
"license": "MIT",
"authors": [
{
"name": "Born05",
"homepage": "https://born05.com"
}
],
"require": {
"craftcms/cms": "^3.0.0 || ^4.0.0"
},
"autoload": {
"psr-4": {
"born05\\restrictvolumes\\": "src/"
}
},
"extra": {
"name": "Restrict Volumes",
"handle": "restrict-volumes",
"developer": "Born05",
"developerUrl": "https://born05.com",
"class": "born05\\restrictvolumes\\Plugin"
}
}
160 changes: 160 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

/**
* Craft restrict volumes plugin for Craft CMS 3.x
*
* Decide what filetypes are allowed per volume.
*
* @link https://born05.com
* @copyright Copyright (c) 2021 Born05
*/

namespace born05\restrictvolumes;

use born05\restrictvolumes\assetbundle\AssetBundle;
use born05\restrictvolumes\models\Settings;

use Craft;
use craft\base\Model;
use craft\elements\Asset;
use craft\events\AssetEvent;
use craft\helpers\Assets as AssetsHelper;
use craft\i18n\PhpMessageSource;

use yii\base\Event;

/**
* Class Plugin
*
* @author Born05
* @package RestrictVolumes
* @since 1.0.0
*/
class Plugin extends \craft\base\Plugin
{
// Static Properties
// =========================================================================

/**
* @var Plugin
*/
public static $plugin;

// Public Properties
// =========================================================================

/**
* @inheritdoc
*/
public string $schemaVersion = '1.0.0';

/**
* @var bool
*/
public bool $hasCpSettings = true;

/**
* @var bool
*/
public bool $hasCpSection = false;

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function __construct($id, $parent = null, array $config = [])
{
Craft::setAlias('@born05/restrictvolumes', $this->getBasePath());

static::setInstance($this);

parent::__construct($id, $parent, $config);
}

/**
* @inheritdoc
*/
public function init()
{
parent::init();
self::$plugin = $this;

Craft::$app->i18n->translations['restrict-volumes'] = [
'class' => PhpMessageSource::class,
'sourceLanguage' => 'en',
'basePath' => __DIR__ . '/translations',
'allowOverrides' => true,
];

if (Craft::$app->getRequest()->getIsCpRequest()) {
Craft::$app->getView()->registerAssetBundle(AssetBundle::class);
}

Event::on(
Asset::class,
Asset::EVENT_BEFORE_HANDLE_FILE,
function (AssetEvent $e) {
$asset = $e->asset;
$allowedKinds = $this->getSettings()->allowedKinds ?: [];

if (!$allowedKinds || count($allowedKinds) === 0) {
return;
}

[$folderId] = AssetsHelper::parseFileLocation($asset->newLocation);
if (!$folderId) {
return;
}

$targetVolume = Craft::$app->volumes->getVolumeById($folderId);
if (!$targetVolume) {
return;
}

$targetVolumeHandle = $targetVolume->handle;
if (!array_key_exists($targetVolumeHandle, $allowedKinds)) {
return;
}

$allowedVolumeFileKinds = $allowedKinds[$targetVolumeHandle];

$kind = $asset->kind;
if ($kind === null && $asset->filename !== null) {
$kind = AssetsHelper::getFileKindByExtension($asset->filename);
} else if ($asset->kind === null) {
return;
}

if (array_key_exists($kind, $allowedVolumeFileKinds) && !$allowedVolumeFileKinds[$kind]) {
throw new \Exception(Craft::t('restrict-volumes', 'unallowed-error', ['kind' => $kind, 'volume' => $targetVolume->name]));
}
}
);
}

// Protected Methods
// =========================================================================

/**
* @inheritdoc
*/
protected function createSettingsModel(): ?Model
{
return new Settings();
}

/**
* @inheritdoc
*/
protected function settingsHtml(): string
{
return Craft::$app->view->renderTemplate(
'restrict-volumes/settings',
[
'settings' => $this->getSettings()
]
);
}
}
37 changes: 37 additions & 0 deletions src/assetbundle/AssetBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Craft restrict volumes plugin for Craft CMS 3.x
*
* Decide what filetypes are allowed per volume.
*
* @link https://born05.com
* @copyright Copyright (c) 2021 Born05
*/

namespace born05\restrictvolumes\assetbundle;

use craft\web\assets\cp\CpAsset;

/**
* @author Born05
* @package RestrictVolumes
* @since 1.0.0
*/
class AssetBundle extends \craft\web\AssetBundle
{
/**
* @inheritdoc
*/
public $sourcePath = __DIR__ . '/dist';

/**
* @inheritdoc
*/
public $depends = [CpAsset::class];

/**
* @inheritdoc
*/
public $css = ['css/restrict-volumes.css'];
}
27 changes: 27 additions & 0 deletions src/assetbundle/dist/css/restrict-volumes.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Craft volume filetypes plugin for Craft CMS
*
* Craft volume filetypes CSS
*
* @author Born05
* @copyright Copyright (c) 2021 Born05
* @link https://born05.com
* @package RestrictVolumes
* @since 1.0.0
*/

.allowed-kinds .checkboxfield {
margin: 0;
}

.allowed-kinds {
display: grid;
grid-template-columns: repeat(auto-fit, 230px);
grid-gap: 35px;
}

.allowed-kinds-volume {
padding: 22px 30px;
background-color: var(--gray-100);
border-radius: 5px;
}
41 changes: 41 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Craft restrict volumes plugin for Craft CMS 3.x
*
* Decide what filetypes are allowed per volume.
*
* @link https://born05.com
* @copyright Copyright (c) 2021 Born05
*/

namespace born05\restrictvolumes\models;

/**
* @author Born05
* @package RestrictVolumes
* @since 1.0.0
*/
class Settings extends \craft\base\Model
{
// Public Properties
// =========================================================================

/**
* @var string
*/
public $allowedKinds = [];

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function rules(): array
{
return [
['allowedKinds', 'default', 'value' => []],
];
}
}
Loading

0 comments on commit 5eca0d0

Please sign in to comment.