FIX: fix file manager
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace App\CMS\Helpers;
|
||||
|
||||
use App\Models\FileManager\Directory;
|
||||
use App\Models\FileManager\File;
|
||||
|
||||
class RootDirectory
|
||||
{
|
||||
protected static $directory;
|
||||
|
||||
/**
|
||||
* Create root directory
|
||||
*
|
||||
* @return Directory
|
||||
*/
|
||||
protected static function create()
|
||||
{
|
||||
$directory = new Directory();
|
||||
$directory->name = 'Root';
|
||||
$directory->is_root = true;
|
||||
$directory->save();
|
||||
|
||||
Directory::whereNull('parent_id')
|
||||
->where('is_root', false)
|
||||
->update(['parent_id' => $directory->id]);
|
||||
|
||||
File::where('directory_id', null)
|
||||
->update(['directory_id' => $directory->id]);
|
||||
|
||||
return $directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get root directory
|
||||
*
|
||||
* @return Directory
|
||||
*/
|
||||
public static function get()
|
||||
{
|
||||
self::$directory = Directory::where('is_root', true)
|
||||
->first();
|
||||
|
||||
if (is_null(self::$directory)) {
|
||||
self::$directory = self::create();
|
||||
}
|
||||
|
||||
return self::$directory;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
<?php
|
||||
namespace App\CMS\Repositories\FileManager;
|
||||
|
||||
use App\CMS\Helpers\RootDirectory;
|
||||
use App\Models\FileManager\Directory as DirectoryModel;
|
||||
use App\Models\FileManager\File;
|
||||
|
||||
class Directory
|
||||
{
|
||||
@@ -11,4 +13,25 @@ class Directory
|
||||
->where('parent_id', $directoryId)
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DirectoryModel $directory
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function getAllSubdirectories(DirectoryModel &$directory)
|
||||
{
|
||||
if (!$directory->exists) {
|
||||
$directory = DirectoryModel::where('is_root', true)
|
||||
->first();
|
||||
|
||||
if (is_null($directory)) {
|
||||
$directory = RootDirectory::get();
|
||||
}
|
||||
}
|
||||
|
||||
$subdirectories = $directory->allSubdirectories();
|
||||
|
||||
return $subdirectories;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user