Repository URL to install this package:
|
Version:
3.2.0 ▾
|
<?php
namespace Evsmash\Files;
use Evsmash\Core\Database\Baum;
use Evsmash\Core\Database\SoftDeleting;
use Evsmash\Core\Dec\Dec;
use Evsmash\Core\Simpy\Params;
use Evsmash\Files\Schemas\FilesDirectories as Schema;
class FilesDirectory extends Baum {
use SoftDeleting;
// validate
static public function validate() {
return Schema::validate();
}
// has many
public function products() {
return $this->hasMany('Evsmash\Files\File', 'directory_id');
}
// params
public function scopeParams($query, $params = false) {
$scope = new Params();
$scope->scope($query, $params);
$scope->search(['name']);
$scope->order(['lft' => 'asc']);
return $scope->query;
}
// parents
static public function parents() {
// get
$root = FilesDirectory::where('depth', 0)->first();
$elements = $root->getDescendantsAndSelf();
// output
$output = [];
foreach($elements as $row) {
// prefix
$prefix = '';
for($i = 0; $i < $row->depth; $i++) {
$prefix .= '--';
}
// output
$output[$row->id] = $prefix.' '.$row->name;
}
return $output;
}
// tree sort
static public function treesort($node) {
if($node->isLeaf()) {
return '<li id="list_'.$node->id.'"><div>'.$node->name.'</div></li>';
} else {
$html = '<li id="list_'.$node->id.'"><div>'.$node->name.'</div>';
$html .= '<ol class="list-unstyled">';
foreach($node->children as $child) {
$html .= FilesDirectory::treesort($child);
}
$html .= '</ol>';
$html .= '</li>';
}
return $html;
}
// dropdown
static public function dropdown($id, $directories, $current) {
// directories
$dirs = [];
foreach($directories as $row) {
// key
$key = '/files/move/file:'.$id.'/directory:'.$row->id;
// name
$name = '';
for($i = 0; $i < $row->depth; $i++) {
$name .= '--';
}
if($current == $row->id) {
$name .= ' <strong>'.$row->name.'</strong>';
} else {
$name .= ' '.$row->name;
}
// assign
$dirs[$key] = $name;
}
// dropdown
return Dec::dropdown('Move', $dirs, 'btn-warning');
}
}