From 1890d5c11b12b6524c892e81f9b128f8163003eb Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 26 Jan 2023 15:26:00 +0100 Subject: [PATCH] wip #5700 @0.25 --- composer.json | 10 +- src/app/Magic/Fields/ExternalPath.php | 13 +- src/app/Magic/Models/ExternalServer.php | 78 ++++++++++- .../views/fields/external_path.blade.php | 124 ++++++++++++++++++ 4 files changed, 218 insertions(+), 7 deletions(-) create mode 100644 src/resources/views/fields/external_path.blade.php diff --git a/composer.json b/composer.json index d87d194..6778e0c 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "ext-libxml": "*", "ext-json": "*", "ext-redis": "*", - "backpack/crud": "^4.1.69", + "backpack/crud": "^4.1.71", "backpack/backupmanager": "^3.0", "backpack/logmanager": "^4.0", "backpack/permissionmanager": "^6.0", @@ -38,7 +38,7 @@ "cviebrock/eloquent-sluggable": "^8.0", "lavary/laravel-menu": "^v1.8", "graham-campbell/markdown": "^v13.1", - "barryvdh/laravel-debugbar": "^v3.6", + "barryvdh/laravel-debugbar": "^v3.7", "league/commonmark": "^1.6", "cviebrock/laravel-elasticsearch": "^8.0", "spatie/laravel-honeypot": "^4.2", @@ -50,10 +50,10 @@ "digitallyhappy/toggle-field-for-backpack": "^2.0", "calebporzio/parental": "^v0.11", "cache/filesystem-adapter": "^1.2", - "laravel/framework": "^v8.83", - "laravel-lang/lang": "^10.1", + "laravel/framework": "^v8.83.27", + "laravel-lang/lang": "^10.9", "laravel-lang/publisher": "^10.3", - "spatie/laravel-permission": "^4.4|^5.5" + "spatie/laravel-permission": "^4.4|^5.8" }, "require-dev": { "filp/whoops": "^2.14", diff --git a/src/app/Magic/Fields/ExternalPath.php b/src/app/Magic/Fields/ExternalPath.php index f55eae7..e178556 100644 --- a/src/app/Magic/Fields/ExternalPath.php +++ b/src/app/Magic/Fields/ExternalPath.php @@ -2,7 +2,18 @@ namespace Cubist\Backpack\Magic\Fields; -class ExternalPath +use Cubist\Backpack\CubistBackpackServiceProvider; +use Cubist\Backpack\Magic\Models\ExternalServer; + +class ExternalPath extends Field { + protected $_adminType = 'external_path'; + protected $_cast = 'array'; + protected $_viewNamespace = CubistBackpackServiceProvider::NAMESPACE . '::fields'; + + public function getDefaultAttributes() + { + return array_merge(parent::getDefaultAttributes(), ['servers_model' => ExternalServer::class]); + } } diff --git a/src/app/Magic/Models/ExternalServer.php b/src/app/Magic/Models/ExternalServer.php index f397245..0a33f07 100644 --- a/src/app/Magic/Models/ExternalServer.php +++ b/src/app/Magic/Models/ExternalServer.php @@ -7,9 +7,18 @@ use Cubist\Backpack\Magic\Fields\Password; use Cubist\Backpack\Magic\Fields\SelectFromArray; use Cubist\Backpack\Magic\Fields\Text; use Cubist\Backpack\Magic\Fields\URL; +use Cubist\Net\Transfer\Driver; +use Cubist\Net\Transfer\FTP; +use Cubist\Net\Transfer\IServer; +use Cubist\Net\Transfer\SFTP; -class ExternalServer extends CubistMagicAbstractModel +class ExternalServer extends CubistMagicAbstractModel implements IServer { + /** + * @var array[]|null + */ + public static $_servers = null; + public function setFields() { parent::setFields(); @@ -26,6 +35,18 @@ class ExternalServer extends CubistMagicAbstractModel $this->addField('base_url', URL::class, __('URL de base'), ['default' => 'https://']); } + public static function getAllServers() + { + + if (null === static::$_servers) { + static::$_servers = []; + foreach (static::where('created_ok', '1')->get() as $server) { + static::$_servers[$server->id] = ['name' => $server->name, 'base_url' => $server->base_url]; + } + } + return static::$_servers; + } + protected function getProtocols() { return ['FTP' => __('FTP non sécurisé'), 'FTPS' => __('FTP sécurisé explicite sur TLS'), 'FTPES' => __('FTP sécurisé implicite sur TLS'), 'SFTP' => 'SFTP']; @@ -55,4 +76,59 @@ class ExternalServer extends CubistMagicAbstractModel { return $this->getUsernameProtocols(); } + + + /** + * @param $protocol + * @return Driver + */ + public function getTransferDriver($protocol) + { + if (in_array($this->getProtocol(), $this->getFTPProtocols())) { + return new FTP($this); + } else if ($this->getProtocol() === 'sftp') { + return new SFTP($this); + } + return null; + } + + public function getProtocol() + { + return $this->protocol; + } + + public function getHost() + { + return $this->host; + } + + public function getPort() + { + return $this->port; + } + + public function getUsername() + { + return $this->username; + } + + public function getPassword() + { + return $this->password; + } + + public function getBasePath() + { + return $this->base_path; + } + + public function getBaseURL() + { + return $this->base_url; + } + + public function getSettings() + { + return ['ftp_port' => $this->ftp_port]; + } } diff --git a/src/resources/views/fields/external_path.blade.php b/src/resources/views/fields/external_path.blade.php new file mode 100644 index 0000000..c9f6afa --- /dev/null +++ b/src/resources/views/fields/external_path.blade.php @@ -0,0 +1,124 @@ + + '', 'path' => '']; +$value = old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? $field['default'] ?? $empty; +if ($value == '') { + $value = $empty; +} + +if (is_string($value)) { + $value = json_decode($value, true); +} +if (is_object($value)) { + $value = Cubist\Util\ArrayUtil::asArray($value); +} + +if (!isset($value['server'])) { + $value['server'] = ''; +} +if (!isset($value['path'])) { + $value['path'] = ''; +} + +$serversClass = $field['servers_model']; +$servers = $serversClass::getAllServers(); + +?> +@include('crud::fields.inc.wrapper_start') + +
+
+ + +
+
+ +
+
+
+
+
+ +{{-- HINT --}} +@if (isset($field['hint'])) +

{!! $field['hint'] !!}

+@endif +@include('crud::fields.inc.wrapper_end') + +@if ($crud->checkIfFieldIsFirstOfItsType($field)) + {{-- FIELD EXTRA CSS --}} + {{-- push things in the after_styles section --}} + + @push('crud_fields_styles') + + @endpush + + {{-- FIELD EXTRA JS --}} + {{-- push things in the after_scripts section --}} + + @push('crud_fields_scripts') + + @endpush +@endif -- 2.39.5