HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Products/Kms/ProductController.php
<?php

namespace App\Komma\Products\Kms;

/**
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

use App\Komma\Kms\Core\SectionController;
use App\Komma\Products\Models\Product;
use App\Komma\Products\Models\ProductTranslation;

final class ProductController extends SectionController
{
    protected $slug = 'products';

    protected $classModelName = Product::class;

    protected $forTranslationModelName = ProductTranslation::class;

    protected $sortable = true;

    public function __construct()
    {
        $productSection = new ProductSection($this->slug);
        parent::__construct($productSection);
        $this->modelService = app(ProductService::class);
    }

    public function getSidebarItems()
    {
        $products = Product::orderBy('sort_order')->with('translation')->get();
        $productItems = [];

        foreach ($products as $product) {
            $status = 1;
            if ($product->hide_on_site) {
                $status = 0;
            }
            if (! $product->active) {
                $status = 2;
            }

            $productItems[] = [
                'id' => $product->id,
                'title' => $product->translation->name,
                'thumbnail' => '',
                'children' => [],
                'status' => $status,
                'routes' => [],
            ];
        }

        return response()->json([
            'id' => 0,
            'title' => '',
            'thumbnail' => '',
            'children' => $productItems,
            'status' => 0,
            'routes' => [],
        ]);
    }

    public function saveSortOrder()
    {
        $tree = json_decode(request()->get('tree'), true);

        foreach ($tree['children'] as $sortOrder => $product) {
            Product::where('id', $product['id'])
                ->update(['sort_order' => $sortOrder]);
        }

        return $this->getSidebarItems();
    }
}