mivita/dev/app-bak/Repositories/DcRepository.php
2025-10-20 17:42:08 +02:00

151 lines
No EOL
4.2 KiB
PHP

<?php
namespace App\Repositories;
use Request;
use App\Models\DcTag;
use App\Models\DcFile;
use App\Models\DcCategory;
class DcRepository extends BaseRepository {
public function __construct()
{
}
public function makeThumb($id){
$file = DcFile::findOrFail($id);
$file->makeThumb();
}
public function deleteFile($id){
$file = DcFile::findOrFail($id);
$file->delete();
}
public function storeItem($obj, $data)
{
if($obj === 'category' && isset($data['dc_category_name'])){
$category = new DcCategory;
$category->name = $data['dc_category_name'];
$category->pos = 0 ;
$category->save();
\Session()->flash('alert-success', 'Kategorie erstellt');
return redirect(route('admin_downloadcenter_tags'));
}
if($obj === 'tag' && isset($data['dc_tag_name'])){
$data = Request::all();
$tag = new DcTag;
$tag->name = $data['dc_tag_name'];
$tag->pos = 0;
$tag->save();
\Session()->flash('alert-success', 'Tag erstellt');
return redirect(route('admin_downloadcenter_tags'));
}
if($obj === 'structure' && isset($data['nestable'])){
$bool = $this->updateStructure($data);
if(Request::ajax()){
return response()->json([
'success' => $bool,
'redirect' => route('admin_downloadcenter_tags', ['flash' => true])
]);
}
}
if($obj === 'update_ajax' && isset($data['action'])){
$active = $this->updateAjax($data);
if(Request::ajax()){
return response()->json([
'success' => $data['action'],
'active' => $active,
]);
}
}
return true;
}
protected function updateAjax($data){
if($data['action'] == 'update-tag-active' && isset($data['id'])){
$tag = DcTag::findOrFail($data['id']);
$tag->active = $tag->active ? 0 : 1;
$tag->save();
return $tag->active;
}
if($data['action'] == 'update-category-active' && isset($data['id'])){
$category = DcCategory::findOrFail($data['id']);
$category->active = $category->active ? 0 : 1;
$category->save();
return $category->active;
}
return false;
}
protected function updateStructure($data)
{
if(empty($data['nestable']) || !is_array($data['nestable'])){
return false;
}
$tags = DcTag::all();
foreach ($tags as $value) {
$value->category_id = null;
$value->pos = NULL;
$value->save();
}
$this->saveStructureLevel($data['nestable']);
return true;
}
protected function saveStructureLevel($nestable, $deep = 0, $category_id = false){
foreach ($nestable as $key => $value) {
if($value['id'] == 0){
continue;
}
if($deep == 0){
$cat = DcCategory::findOrFail($value['id']);
$cat->pos = $key;
$cat->save();
}
if($deep == 1){
$tag = DcTag::findOrFail($value['id']);
$tag->category_id = $category_id;
$tag->pos = $key;
$tag->save();
}
if(!empty($value['children'])){
$this->saveStructureLevel($value['children'], $deep+1, $value['id']);
}
}
}
public function deleteItem($obj, $id){
if($obj == 'category'){
$this->deleteCategory($id);
}
if($obj == 'tag'){
$this->deleteTag($id);
}
}
public function deleteCategory($id){
$cat = DcCategory::findOrFail($id);
$tags = DcTag::where('category_id', $cat->id)->get();
foreach ($tags as $tag) {
$this->deleteTag($tag->id);
}
$cat->delete();
}
public function deleteTag($id){
$tag = DcTag::findOrFail($id);
$tag->delete();
}
}