Skip to content

Commit

Permalink
Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wdda committed Feb 1, 2023
1 parent d5fe381 commit 3bcb9a5
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .bash_history
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ cd packagist/
git clone [email protected]:wdda/laravel-uikit-form.git
cd ..
exit
composer require symfony/filesystem
php artisan tinker
cd packagist/
git clone [email protected]:wdda/laravel-uikit-form.git
cd ..
exit
38 changes: 37 additions & 1 deletion app/Http/Controllers/Resources/FoldersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,55 @@
namespace App\Http\Controllers\Resources;

use App\Http\Controllers\Controller;
use App\Models\Resources\Folders;
use Exception;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Routing\Redirector;
use Illuminate\Validation\ValidationException;

class FoldersController extends Controller
{
public function index(): Factory|View|Application
{
return view('resources.files.index');
return view('resources.files.index', Folders::getListData());
}

public function create(): Factory|View|Application
{
return view('resources.files.create');
}

/**
* @throws ValidationException
* @throws Exception
*/
public function store(Request $request)
{
$this->validate($request, ['name' => 'required|regex:/^[a-z0-9_]*$/']);
$folderName = $request->get('name');

$create = Folders::create($folderName);

if ($create['status']) {
return redirect(route('folders.index'))->with('message', 'Folder ' . $folderName . ' created!');
}

return back()->with('error', $create['error']);
}


public function delete($folder): Redirector|RedirectResponse|Application
{
$delete = Folders::delete($folder);

if(!$delete['error']) {
return redirect(route('folders.index'))->with('message', 'Folder ' . $folder . ' deleted!');
}

return back()->with('error', 'Folder not deleted: ' . $delete['error']);
}
}
74 changes: 72 additions & 2 deletions app/Models/Resources/Folders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models\Resources;

use JetBrains\PhpStorm\ArrayShape;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;

Expand All @@ -19,13 +20,82 @@ public static function install(): bool
return true;
}

public static function getFolders()
public static function getList(): array
{
$path = base_path(config('game.resources_path'));
$dirs = scandir(base_path(config('game.resources_path')));
$result = [];

foreach ($dirs as $dir) {
if ($dir != '.' && $dir != '..') {
if (is_dir($path . '/' . $dir)) {
$result[] = [
'name' => $dir,
'path' => $path
];
}
}
}

return $result;
}

#[ArrayShape(['folders' => "array"])] public static function getListData()
{
return [
'folders' => self::getList()
];
}

public static function createFolder($folderName)
public static function create($folderName): array
{
$path = base_path(config('game.resources_path'));
$folderPath = $path . '/' . $folderName;
$error = null;
$status = null;

if (!is_dir($path)) {
$error = 'Error path, not exist: ' . $path;
}

if (is_dir($folderPath)) {
$error = 'Error folder, is exist ' . $folderPath;
}

if (!$error) {
$status = mkdir($folderPath, 0775);
}

return compact('status', 'error');
}

public static function delete($folderName): array
{
$path = base_path(config('game.resources_path'));
$folderPath = $path . '/' . $folderName;
$status = null;
$error = null;

if (!self::dirIsEmpty($folderPath)) {
$error = 'Directory is not empty';
}

if (!$error) {
$status = rmdir($folderPath);
}

return compact('status', 'error');
}

public static function dirIsEmpty($dir) {
$handle = opendir($dir);
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
closedir($handle);
return false;
}
}
closedir($handle);
return true;
}
}
7 changes: 7 additions & 0 deletions config/game.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php


return [
'resources_path' => env('GAME_PATH_RESOURCES') . '/resources/graphics'
];

5 changes: 5 additions & 0 deletions resources/css/sass/app.sass
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
@import "uikit/src/scss/variables"
@import "uikit/src/scss/uikit"

.uk-table
th, td
border: 1px solid grey
padding: 4px
6 changes: 6 additions & 0 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
<div class="uk-alert-danger" uk-alert>{!! session()->get('error') !!}</div>
@endif

@if ($errors->any())
@foreach ($errors->all() as $error)
<div class="uk-alert-danger" uk-alert>{!! $error !!}</div>
@endforeach
@endif

<!-- Page Content -->
<main>
{{ $slot }}
Expand Down
3 changes: 2 additions & 1 deletion resources/views/layouts/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<nav class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-left">
<ul class="uk-navbar-nav">
<li class="uk-active"><a href="/">Main</a></li>
<li class="uk-active"><a href="">Files</a></li>
<li class="uk-active"><a href="{{ route('folders.index') }}">Folders</a></li>
</ul>
</div>

Expand Down
5 changes: 5 additions & 0 deletions resources/views/resources/files/create.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<x-app-layout>
<x-title title="Create folder" />

<form action="{{ route('folders.store') }}" method="POST">
@include('resources.files.form')
</form>

</x-app-layout>
5 changes: 5 additions & 0 deletions resources/views/resources/files/form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@csrf

{!! formInput('name')->label('Name *')->value(old('name'))->placeholder('example_name_folder')->render() !!}

{!! formButton('create')->render() !!}
31 changes: 31 additions & 0 deletions resources/views/resources/files/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<x-app-layout>
<x-title title="Folders" />

<div class="uk-flex uk-flex-right">
<div>
<a class="uk-button uk-button-primary" href="{{ route('folders.create') }}">
+ create
</a>
</div>
</div>

<table class="uk-table">
<thead>
<tr>
<th>name</th>
<th>action</th>
</tr>
</thead>
<tbody>
@foreach($folders as $folder)
<tr>
<td>{{ $folder['name'] }}</td>
<td>
<a href="{{ route('folders.delete', $folder['name']) }}">delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>

</x-app-layout>
3 changes: 2 additions & 1 deletion routes/resources/folders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
use App\Http\Controllers\Resources\FoldersController;
use Illuminate\Support\Facades\Route;

Route::resource('/resources/folders', FoldersController::class);
Route::resource('/resources/folders', FoldersController::class)->except('destroy');
Route::get('/resources/folders/{folder}/delete', [FoldersController::class, 'delete'])->name('folders.delete');

0 comments on commit 3bcb9a5

Please sign in to comment.