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/SBogers95/rentman.io/app/Komma/Shop/Orders/Kms/OrderSection.php
<?php
/**
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

namespace App\Komma\Shop\Orders\Kms;

//The new object oriented attributes
use App\Helpers\KommaHelpers;
use App\Komma\Globalization\CultureInfo;
use App\Komma\Globalization\CultureTypes;
use App\Komma\Globalization\RegionInfo;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\Select;
use App\Komma\Kms\Core\Attributes\TextField;
use App\Komma\Kms\Core\Attributes\Title;
use App\Komma\Kms\Core\Attributes\View;
use App\Komma\Kms\Core\Sections\NoLanguageTabsDirector;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\Sections\SectionTabGroups;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Kms\Core\Sections\SectionTabsBuilder;
use App\Komma\Kms\Core\ValidationSet;
use App\Komma\Routes\RouteService;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Users\Genders;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\MessageBag;

class OrderSection extends Section
{
    protected $title = 'Orders';

    protected $subtitle = 'All product orders';

    protected $slug = 'orders';

    protected $hasRoutes = false;

    public $showSave = 'all';

    public $showDelete = [1];

    public $showCreate = '';

    /**
     * OrderSection constructor.
     * @param OrderServiceInterface $sectionService
     * @param RouteService $routeService
     * @param SiteServiceInterface $siteService
     */
//    function __construct(Kms $kms, PageRepository $repository)
    public function __construct(OrderServiceInterface $sectionService, RouteService $routeService, SiteServiceInterface $siteService)
    {
        $this->title = ucfirst(__('shop/orders.orders'));
        $this->subtitle = __('shop/orders.all_orders');

        $sectionTabDirector = new NoLanguageTabsDirector(new SectionTabsBuilder()); //Can make tabs for us. also see KmsSection::__construct
        parent::__construct($sectionService, $routeService, $siteService, $sectionTabDirector);
    }

    /**
     * Generates the attributes for this section. They all must extend the App\Komma\Kms\Core\Attributes\Attribute class
     * This is the place where you need to setup your sections appearance. Just make sure you build an array of attributes
     * and put each attribute in a AbstractSectionTabItem with a SectionTabGroups constant to link them to a tab.
     *
     * @see PageRepository::saveModel()
     * @return Collection A collection of SectionTabItems
     */
    protected function generateAttributes(): Collection
    {
//        \Log::info("PageSection::Generating attributes");

        //*****************************************************************************************\\
        //*** Define attribute validation sets                                                  ***\\
        //*****************************************************************************************\\
        $emailValidationSet = (new ValidationSet())
            ->setRules([
                'required',
                'email',
            ])
            ->setMessages([
                'required' => __('validation.required'),
                'email' => __('kms/users.enterValidEmailAddress'),
            ]);

        //*****************************************************************************************\\
        //*** Determine and define gender dropdown data                                           ***\\
        //*****************************************************************************************\\
        $genderOptions = [];
        collect(Genders::getAsArray())->each(function ($gender) use (&$genderOptions) {
            $genderOptions[] = (new SelectOption())->setContent(__('auth.genders.'.$gender))->setHtmlContent(__('auth.genders.'.$gender))->setValue($gender);
        });

        //*****************************************************************************************\\
        //*** Determine and define culture dropdown data                                           ***\\
        //*****************************************************************************************\\
        $cultureOptions = [];
        collect(CultureInfo::getCultures(CultureTypes::SPECIFIC_CULTURES))->each(function (CultureInfo $cultureInfo) use (&$cultureOptions) {
            $cultureOptions[] = (new SelectOption())->setContent($cultureInfo->getNativeName())->setHtmlContent($cultureInfo->getNativeName())->setValue($cultureInfo->getName());
        });

        //*****************************************************************************************\\
        //*** Generate the attributes                                                           ***\\
        //*****************************************************************************************\\
        $attributes = [];

        /** @var Order $order */
        $order = $this->getModel();
        $orderedProducts = $order->orderedProducts()->get();
        $orderedProductGroups = $order->orderedProductGroups()->with('orderedProducts')->get();
        $orderedProductComposites = $order->orderedProductComposites()->with('orderedGroups.orderedProducts')->get();

        $orderOverviewData = [
            'products' => $orderedProducts,
            'product_groups' => $orderedProductGroups,
            'product_composites' => $orderedProductComposites,
        ];

        $transactionViewData = [
            'order'             => $order,
            'shopRegionInfo'    => new RegionInfo('NL'), //Shop was build with euros in mind and the Dutch language as the primary one
        ];

        //Build the general attributes and put them in the attributes array
        $attributes[] = (new Title(__('shop/orders.status_name')));

        $attributes[] = (new TextField(__('shop/orders.order_number')))
            ->setReadOnly(true)
            ->mapValueFrom(Attribute::ValueFromModel, 'id');

        $attributes[] = (new Select())
            ->setItems($this->getOrderStatusItems())
            ->setLabelText(__('shop/orders.status_name'))
            ->mapValueFrom(Attribute::ValueFromModel, 'status');

        $attributes[] = (new Title(__('shop/orders.invoice_data')));

        $attributes[] = (new TextField(__('kms/users.email')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_email')
            ->setValidationSet($emailValidationSet);

        $attributes[] = (new TextField(__('kms/users.first_name')))
            ->setPlaceholderText(__('kms/users.enter_first_name'))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_first_name');

        $attributes[] = (new TextField(__('kms/users.last_name')))
            ->setPlaceholderText(__('kms/users.enter_last_name'))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_last_name');

        $attributes[] = (new Select())
            ->setLabelText(__('auth.gender'))
            ->setItems($genderOptions)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_gender');

        $attributes[] = (new TextField(__('kms/customers.company_name')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_company');

        $attributes[] = (new TextField(__('kms/customers.postal')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_postal_code');

        $attributes[] = (new TextField(__('kms/customers.city')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_city');

        $attributes[] = (new TextField(__('kms/customers.street')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_street');

        $attributes[] = (new Select())
            ->setLabelText(__('kms/users.culture'))
            ->setItems($cultureOptions)
            ->mapValueFrom(Attribute::ValueFromModel, 'invoice_culture');

        $attributes[] = (new Title(__('shop/orders.shipping_data')));

        $attributes[] = (new TextField(__('kms/users.email')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_email')
            ->setValidationSet($emailValidationSet);

        $attributes[] = (new TextField(__('kms/users.first_name')))
            ->setPlaceholderText(__('kms/users.enter_first_name'))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_first_name');

        $attributes[] = (new TextField(__('kms/users.last_name')))
            ->setPlaceholderText(__('kms/users.enter_last_name'))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_last_name');

        $attributes[] = (new Select())
            ->setLabelText(__('auth.gender'))
            ->setItems($genderOptions)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_gender');

        $attributes[] = (new TextField(__('kms/customers.company_name')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_company');

        $attributes[] = (new TextField(__('kms/customers.postal')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_postal_code');

        $attributes[] = (new TextField(__('kms/customers.city')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_city');

        $attributes[] = (new TextField(__('kms/customers.street')))
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_street');

        $attributes[] = (new Select())
            ->setLabelText(__('kms/users.culture'))
            ->setItems($cultureOptions)
            ->mapValueFrom(Attribute::ValueFromModel, 'shipping_culture');

        $attributes[] = (new Title(__('shop/transactions.transactions')));
        $attributes[] = (new View('shop.partials.orders.transactions'))->setViewData($transactionViewData);

        $attributes[] = (new Title(__('shop/orders.order')));
        $attributes[] = (new View('shop.partials.orders.overview'))->setViewData($orderOverviewData);

        //Build an array with attributes for each current site language
        $languageIndexedAttributes = $this->createAttributesFromExistingAttributeForAllUsedLanguagesBySites([

        ]);

        //****************************************************************************************************************************************\\
        //*** Put the all attributes in a SectionTabItem so we can track for which tab they are. And then put SectionTabItems in a Collection  ***\\
        //****************************************************************************************************************************************\\
        $tabItems = new Collection();
        foreach ($attributes as $attribute) {
            $tabItems->push(new SectionTabItem($attribute, SectionTabGroups::General));
        }

        return $tabItems;
    }

    public function getOrderStatusItems()
    {
        $items = [];
        foreach (__('shop/orders.status') as $value => $translation) {
            $items[] = (new SelectOption())
                ->setValue($value)
                ->setContent($translation)
                ->setHtmlContent($translation);
        }

        return $items;
    }

    /**
     * For the index controller action
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function renderOrderIndex()
    {
        $saveRoute = $this->routeService->getSaveRoute($this->getSlug(), $this->getModelId());

        $successes = (\Session::has('successes')) ? \Session::get('successes') : new MessageBag();

        return \View::make('kms/shop/orders/index', [
            'siteSlug'                      => ! $this->siteService->getCurrentSite()->exists ? null : $this->siteService->getCurrentSite()->slug,
            'section'                      => $this,
            'saveRoute'                    => $saveRoute,
            'successes'                    => $successes,
            'maxUploadSize'                => KommaHelpers::fileUploadMaxSize(),
            'maxPostSize'                  => KommaHelpers::maxPostSize(),
            'preventNavigationTranslation' => json_encode(__('kms/prevent-navigation')),
        ]);
    }

    /**
     * Makes a full width view. The sidebar is not needed
     *
     * @return \Illuminate\Contracts\View\View
     */
    protected function makeView(): \Illuminate\Contracts\View\View
    {
        $saveRoute = $this->routeService->getSaveRoute($this->getSlug(), $this->getModelId());

        $successes = (\Session::has('successes')) ? \Session::get('successes') : new MessageBag();

        $shopRegionInfo = new RegionInfo('NL'); //Shop was build with euros in mind and the Dutch language as the primary one

        return \View::make('kms/section.fullwidth', [
            'siteSlug'                     => ! $this->siteService->getCurrentSite()->exists ? null : $this->siteService->getCurrentSite()->slug,
            'section'                      => $this,
            'saveRoute'                    => $saveRoute,
            'successes'                    => $successes,
            'maxUploadSize'                => KommaHelpers::fileUploadMaxSize(),
            'maxPostSize'                  => KommaHelpers::maxPostSize(),
            'preventNavigationTranslation' => json_encode(__('kms/prevent-navigation')),
        ]);
    }
}