50 lines
1.0 KiB
PHP
50 lines
1.0 KiB
PHP
<?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;
|
|
}
|
|
}
|