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/blijegasten/blijegasten.be/app/Komma/Shop/Products/Product/ProductModelService.php
<?php declare(strict_types=1);

namespace App\Komma\Shop\Products\Product;

use App\Helpers\KommaHelpers;
use App\Komma\Documents\Kms\DocumentableInterface;
use App\Komma\Kms\Core\AbstractTranslatableModel;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\Models\Traits\HasThumbnailInterface;
use App\Komma\Kms\Core\Entities\DisplayNameInterface;
use App\Komma\Kms\Core\ModelService;
use App\Komma\Kms\Core\Sections\SidebarListItem;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Orders\Product\OrderedProduct;
use App\Komma\Shop\Stock\StockService;
use App\Komma\Sites\HasSitesInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class ProductModelService extends ModelService
{
    /** @var StockService */
    private $stockService;

    public function __construct()
    {
        $this->setModelClassName(Product::class);
        parent::__construct();
    }

    public function save(Model $model, Collection $attributes = null): Model
    {
        return parent::save($model, $attributes); // TODO: Change the autogenerated stub
    }

    public static function getDepositTypes(): Collection
    {
        $depositTypes = collect();
        foreach (Product::DEPOSIT_TYPES as $depositTypeId => $depositType) {

            $selectOption = (new SelectOption())
                ->setValue($depositTypeId)
                ->setContent(__('shop/products.deposit_types.' . $depositType))
                ->setHtmlContent(__('shop/products.deposit_types.' . $depositType));

            $depositTypes->push($selectOption);
        }

        return $depositTypes;
    }

    /**
     * Makes a single orderedProduct based of a product
     *
     * @param Product $product
     * @param Order $order
     * @param int $quantity
     * @return OrderedProduct
     */
    public static function createOrderedProductFromProduct(Product $product, Order $order, int $quantity)
    {
        $stockService = new StockService();

        $attributes = $product->getAttributes();
        unset($attributes['id']);

        $orderedProduct = new OrderedProduct($attributes);
        $orderedProduct->product()->associate($product);

        $orderedProduct->quantity = $quantity;

        $order->orderedProducts()->save($orderedProduct);
        $afterProduct = $stockService->stockProductable($product, -$quantity);

        return $orderedProduct;
    }

    /**
     * Returns all models for the sidebar menu in the backend
     *
     * @return array
     */
    public function getModelsForSideBar():array
    {
        if(!is_a($this->modelClassName, DisplayNameInterface::class, true)) throw new \RuntimeException('Please let class "'.$this->modelClassName.'" implement '.DisplayNameInterface::class.'. it is needed to build a sidebar.');
        if(!is_a($this->modelClassName, HasThumbnailInterface::class, true)) throw new \RuntimeException('Please let class "'.$this->modelClassName.'" implement '.HasThumbnailInterface::class.'. It is needed to build a sidebar');

        if(isset($this->orderBy)){
            if($this->orderReverse) $models = $this->modelClassName::orderBy($this->orderBy, 'desc');
            else $models = $this->modelClassName::orderBy($this->orderBy);
        }
        else $models = $this->modelClassName::orderBy('id');

        //Only load the models from the current site. Or load all when we currently have the default site
        $site = $this->siteService->getCurrentSite();
        if(new $this->modelClassName instanceof HasSitesInterface && $site->exists) {
            $models->whereHas('Sites', function ($query) use ($site) {
                $query->where('site_id', '=', $site->id);
            });
        }

        $models->with('categories', 'categories.translation');

        // Get the models
        $models = $models->get();

        // Preload translation to prevent additional queries later on
        if(is_a(new $this->modelClassName, AbstractTranslatableModel::class)){
            $models = $models->load('translations', 'translation');
        }

        //Preload documents to prevent additional queries later on
        if(is_a(new $this->modelClassName, DocumentableInterface::class)){
            $models = $models->load('documents');
        }

        $sidebarList = [];
        foreach ($models as $model) {
            /** @var Model|HasThumbnailInterface|DisplayNameInterface $model */
            if($model->getAttribute('lft') == 1) continue; //Skip the root model if it is one

            $sidebarListItem = new SidebarListItem();
            $sidebarListItem->setThumbnail($model);
            $sidebarListItem->alsoSearchInAttributesOfModel($model);
            $model->generateThumbnail();

            //Set the values for the sidebar
            $sidebarListItem->setId($model->id);

            $sidebarListItem->setStatus($model->active);
            $title = KommaHelpers::str_limit_full_word($model->getSidebarName(), 75);
            $sidebarListItem->setName($title);
            $sidebarListItem->setThumbnail($model->getThumbnail());

            // Make sure there is always a unique key
            $key = $title;
            if(isset($sidebarList[$key])) $key = $title.$model->id;

            $sidebarList[$key] = $sidebarListItem;

        }

        ksort($sidebarList);

        return $sidebarList;
    }
}