]> _ Git - cubist_cms-back.git/commitdiff
wip #5700 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 26 Jan 2023 14:26:00 +0000 (15:26 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 26 Jan 2023 14:26:00 +0000 (15:26 +0100)
composer.json
src/app/Magic/Fields/ExternalPath.php
src/app/Magic/Models/ExternalServer.php
src/resources/views/fields/external_path.blade.php [new file with mode: 0644]

index d87d1940f00237efd39c026101601d99433e743e..6778e0ca29cefb591b00c697b69b3667cd4d9302 100644 (file)
@@ -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",
         "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",
index f55eae723de8213242aa9557d2a680e1d6ef3763..e17855603d0351b9fe492b7469d7d90cb7479d26 100644 (file)
@@ -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]);
+    }
 }
index f3972453cfb1accb74e6ff3aeb2c7b6ea6b9a275..0a33f07482153e575531b94d57ec369fd310f372 100644 (file)
@@ -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 (file)
index 0000000..c9f6afa
--- /dev/null
@@ -0,0 +1,124 @@
+<!-- range of values -->
+<?php
+$empty = ['server' => '', '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')
+<label>{!! $field['label'] !!}</label>
+<div class="external_path__wrapper">
+    <div class="external_path__field server">
+        <label>{!! __('Serveur') !!}</label>
+        <select data-class="external_path_server_select" @include('crud::fields.inc.attributes')
+        name="{{ $field['name'] }}[server]"
+        >
+            <option value="" @if(!$value['server']) selected @endif>--</option>
+            @foreach($servers as $i=>$server)
+                <option value="{{$i}}" @if($value['server']==$i) selected
+                        @endif data-prefix="{{$server['base_url']}}">{!! $server['name'] !!}</option>
+            @endforeach
+        </select>
+    </div>
+    <div class="external_path__field path">
+        <label>{!! __('Chemin') !!}</label>
+        <div class="input-group">
+            <div class="input-group-prepend"><span class="input-group-text prefix"></span></div>
+            <input @include('crud::fields.inc.attributes')
+                   value="{{ $value['path'] }}"
+                   type="text"
+                   name="{{ $field['name'] }}[path]"
+            ></div>
+    </div>
+</div>
+
+{{-- HINT --}}
+@if (isset($field['hint']))
+    <p class="help-block">{!! $field['hint'] !!}</p>
+@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')
+        <style type="text/css">
+            .external_path__wrapper {
+                width: 100%;
+                white-space: nowrap;
+            }
+
+            .external_path__field {
+                display: inline-block;
+                padding: 0 15px;
+                box-sizing: border-box;
+                vertical-align: top;
+            }
+
+            .external_path__field.server {
+                width: 30%;
+            }
+
+            .external_path__field.path {
+                width: 70%;
+            }
+
+
+            .external_path__field label {
+                font-weight: 400;
+                margin: 0;
+            }
+
+            .external_path__field:first-child {
+                padding-left: 0;
+            }
+
+            .external_path__field:last-child {
+                padding-right: 0;
+            }
+        </style>
+    @endpush
+
+    {{-- FIELD EXTRA JS --}}
+    {{-- push things in the after_scripts section --}}
+
+    @push('crud_fields_scripts')
+        <script>
+            $(function () {
+                function updateServer(s) {
+                    var prefix = $(s).find('option[value="' + $(s).val() + '"]').data('prefix');
+                    console.log($(s).val(), prefix,(s).find('option[value="' + $(s).val() + '"]'));
+                    $(s).closest('.external_path__wrapper').find('.prefix').text(prefix);
+                }
+
+                $(document).on('change', '[data-class="external_path_server_select"]', function () {
+                    updateServer($(this));
+                });
+
+                $('[data-class="external_path_server_select"]').each(function () {
+                    updateServer($(this));
+                });
+            });
+        </script>
+    @endpush
+@endif