112 lines
2.7 KiB
PHP
112 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class MyFile extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'my_files';
|
|
protected $fillable = [
|
|
'name', 'file_type', 'parent_id', 'year', 'description', 'tags', 'name_org', 'place_of_the_file', 'is_temp', 'file_size', 'user_id', 'shared_user_id', 'path_to_root'
|
|
];
|
|
|
|
public static function getPathToRoot($did)
|
|
{
|
|
if(!isset($did) || $did==null)
|
|
return [];
|
|
|
|
$directory = MyFile::find($did);
|
|
if($directory)
|
|
{
|
|
if($directory->parent_id == 0)
|
|
return [$directory->id];
|
|
else
|
|
return array_merge(MyFile::getPathToRoot($directory->parent_id), [$directory->id]);
|
|
}
|
|
return [];
|
|
}
|
|
|
|
public static function getPathToRootString($did)
|
|
{
|
|
$a = MyFile::getPathToRoot($did);
|
|
|
|
return (count($a)>0) ? ";" . implode($a, ";") . ";" : '';
|
|
}
|
|
|
|
public static function getPathArray($did)
|
|
{
|
|
if(!isset($did) || $did==null)
|
|
return [];
|
|
|
|
$directory = MyFile::find($did);
|
|
if($directory)
|
|
{
|
|
if($directory->parent_id == 0)
|
|
return [$directory->id => $directory->name];
|
|
else
|
|
return MyFile::getPathArray($directory->parent_id) + [$directory->id => $directory->name];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
public function getName()
|
|
{
|
|
if($this->file_type == 1 && $this->name_org != null)
|
|
return $this->name_org;
|
|
else
|
|
return $this->name;
|
|
}
|
|
|
|
public function getFileType()
|
|
{
|
|
if($this->file_type == 1)
|
|
return pathinfo($this->name, PATHINFO_EXTENSION);
|
|
|
|
return '';
|
|
}
|
|
|
|
public function getPathOrg()
|
|
{
|
|
return $this->place_of_the_file . '/' . $this->name;
|
|
}
|
|
|
|
public function getPathPdf()
|
|
{
|
|
if($this->file_type == 1)
|
|
return $this->place_of_the_file . '/' . pathinfo($this->name, PATHINFO_FILENAME) . '.pdf';
|
|
|
|
return '';
|
|
}
|
|
public function getReadableName()
|
|
{
|
|
if($this->file_type == 1)
|
|
return pathinfo($this->name_org, PATHINFO_FILENAME) . '.' . pathinfo($this->name, PATHINFO_EXTENSION);
|
|
|
|
return '';
|
|
}
|
|
|
|
// public function getFileTypeAttribute($attribute)
|
|
// {
|
|
// try{
|
|
// $v = $this->getFileTypeOptions()[$attribute];
|
|
// }
|
|
// catch(\Throwable $th){
|
|
// $v = "Error";
|
|
// }
|
|
// return $v;
|
|
// }
|
|
// public function getFileTypeOptions()
|
|
// {
|
|
// return [
|
|
// 0 => 'D',
|
|
// 1 => 'F',
|
|
// ];
|
|
// }
|
|
}
|