File: D:/HostingSpaces/brameda/brameda.nl/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\Link;
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\View;
use App\Komma\Kms\Core\Sections\NoLanguageTabsDirector;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\ValidationSet;
use App\Komma\Routes\RouteService;
use App\Komma\Kms\Core\Attributes\Title;
use App\Komma\Kms\Core\Sections\SectionTabGroups;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Kms\Core\Sections\SectionTabsBuilder;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Users\Genders;
use App\Komma\Users\Models\KmsUser;
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)
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
{
/** @var Order $model */
$model = $this->getModel();
/** @var KmsUser $customer */
$customer = $model->customer;
//*****************************************************************************************\\
//*** Define attribute validation sets ***\\
//*****************************************************************************************\\
$emailValidationSet = (new ValidationSet())
->setRules([
'required',
'email',
])
->setMessages([
'required' => __('validation.required'),
'email' => __('kms/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();
$shipmentsData = [
'order' => $order,
'shipments' => $order->shipments()->with('shipmentGroup')->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, 'order_number');
$attributes[] = (new Select())
->setItems($this->getOrderStatusItems())
->setLabelText(__('shop/orders.status_name'))
->mapValueFrom(Attribute::ValueFromModel, 'status');
$attributes[] = (new Title(__('shop/orders.customer_info')));
// $attributes[] = (new Attribute())
$attributes[] = (new Link(__('shop/orders.customer')))->setLink(route('site_users.show', ['site_user' => $customer]))->setLinkText($customer->first_name.' '.$customer->last_name.' ('.$customer->email.')');
$attributes[] = (new Title(__('shop/orders.invoice_address')));
$attributes[] = (new TextField(__('kms/customers.postal')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromItself, 'invoice_postal_code');
$attributes[] = (new TextField(__('kms/customers.city')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromItself, 'invoice_city');
$attributes[] = (new TextField(__('kms/customers.street')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromItself, 'invoice_street');
$attributes[] = (new Title(__('shop/orders.shipping_address')));
$attributes[] = (new TextField(__('kms/customers.postal')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromItself, 'shipping_postal_code');
$attributes[] = (new TextField(__('kms/customers.city')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromItself, 'shipping_city');
$attributes[] = (new TextField(__('kms/customers.street')))
->setReadOnly(false)
->mapValueFrom(Attribute::ValueFromItself, 'shipping_street');
$attributes[] = (new Title(__('shop/transactions.transactions')));
$attributes[] = (new View('shop.partials.orders.transactions'))->setViewData($transactionViewData);
if($model->status ) {
$attributes[] = (new Title(__('shop/shipments.shipments')));
$attributes[] = (new View('shop.partials.orders.shipments'))->setViewData($shipmentsData);
}
$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('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('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')),
]);
}
}