File: D:/HostingSpaces/SBogers10/csb.komma.pro/app/Development/PreviewController.php
<?php
namespace App\Development;
use App\Notifications\CustomerRegistered;
use App\Notifications\SiteResetPasswordNotification;
use App\Orders\Mail\OrderManualActionRequiredStaff;
use App\Orders\Mail\OrderStatusUpdatedCustomer;
use App\Orders\Mail\OrderStatusUpdatedStaff;
use App\Orders\Models\Order;
use App\Orders\OrderStatus;
use App\Pages\PageService;
use App\Users\Models\SiteUser;
use Illuminate\Routing\Controller;
use Komma\KMS\Globalization\RegionInfo;
use Komma\KMS\Notifications\SetPassword;
use Komma\KMS\Users\Models\KmsUser;
use Komma\KMS\Notifications\KmsResetPasswordNotification;
use Illuminate\Mail\Markdown;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Illuminate\View\View;
class PreviewController extends Controller
{
/**
* @var \stdClass
*/
private $links;
public function __construct()
{
$pageService = new PageService();
$this->links = $pageService->getAllTranslatedPageRoutes();
}
/*
|--------------------------------------------------------------------------
| Mail previews
|--------------------------------------------------------------------------
|
| These functions must returns views that represent mails.
|
*/
/**
* @see KmsResetPasswordNotification
* @return HtmlString|View
*/
public function resetPasswordMail()
{
$customer = factory(SiteUser::class)->make();
$token = Str::random(32);
return $this->notificationToView(new SiteResetPasswordNotification($customer, $token));
}
/**
* @see KmsResetPasswordNotification
* @return HtmlString|View
*/
public function resetPasswordMailKms()
{
$customer = factory(KmsUser::class)->make();
$token = Str::random(32);
return $this->notificationToView(new KmsResetPasswordNotification($customer, $token));
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*@see SetPassword
*/
public function setPasswordMail()
{
$customer = factory(SiteUser::class)->make();
$token = Str::random(32);
return $this->notificationToView(new SetPassword($customer, $token));
}
/**
* @see CustomerRegistered
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function registeredMail()
{
$customer = factory(SiteUser::class)->make();
return $this->notificationToView(new CustomerRegistered($customer));
}
public function orderManualActionRequired(Order $order)
{
if(!$order || $order->exists == false) $order = Order::inRandomOrder()->wherehas('customer')->with('customer')->first();
$staff = factory(KmsUser::class)->make();
return new OrderManualActionRequiredStaff($order, $staff);
}
/**
* @param string $status
* @param string $staffOrCustomer
* @param Order|null $order
* @return OrderStatusUpdatedCustomer|OrderStatusUpdatedStaff|HtmlString|View
*/
public function orderStatusMail(string $status, string $staffOrCustomer, Order $order = null)
{
$status = strtolower($status);
if(!OrderStatus::isValidItem(strtolower($status))) throw new \RuntimeException('The status must be one of the values of the enum '.OrderStatus::class);
if(!in_array($staffOrCustomer, ['staff', 'customer', true])) throw new \RuntimeException('The staffOrCustomer parameter must be "staff" or "customer" verbatim');
if(!in_array(strtolower($status), array_keys(__('shop/orders.mail'))) || __('shop/orders.mail.'.$status.'.'.$staffOrCustomer) === 'shop/orders.mail.'.$status.'.'.$staffOrCustomer)
throw new \RuntimeException('The translation file "shop/orders.mail.'.$status.'" does not have an entry for: '.$staffOrCustomer.'. It probable is not there by intention.');
/** @var Order $order */
if(!$order) $order = Order::inRandomOrder()->first();
$order->status = $status;
if(strtolower($staffOrCustomer) == 'customer') {
return new OrderStatusUpdatedCustomer($order);
} else {
return new OrderStatusUpdatedStaff($order, factory(KmsUser::class)->make());
}
}
/*
|--------------------------------------------------------------------------
| Invoice / Shipping notes previews
|--------------------------------------------------------------------------
|
| These functions must returns views that represent mails.
|
*/
/**
* @see InvoiceService@makeInvoiceForOrder
* @param Order|null $order
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function invoice(Order $order = null)
{
if(!$order) $order = Order::inRandomOrder()->first();
return $this->invoiceService->getForOrderAsView($order);
}
/**
* @see InvoiceService@makeInvoiceForOrder
* @param Order|null $order
* @return
*/
public function invoicePdf(Order $order = null)
{
if(!$order) $order = Order::inRandomOrder()->first();
return $this->invoiceService->getForOrderAsPDF($order);
}
/**
* @see InvoiceService@makeInvoiceForOrder
* @param Order|null $order
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function invoicePdfDownload(Order $order = null)
{
if(!$order) $order = Order::inRandomOrder()->first();
return $this->invoiceService->getForOrderAsPDFDownload($order);
}
/*
|--------------------------------------------------------------------------
| Authentication view previews
|--------------------------------------------------------------------------
|
| These functions must return blade views that are related to authentication
|
*/
public function registerView()
{
$culturesForSelect = RegionInfo::getNeutralCultures()->map(function(RegionInfo $regionInfo) {
return [$regionInfo->getThreeLetterISORegionName() => $regionInfo->getNativeName() ? $regionInfo->getNativeName() : $regionInfo->getDisplayName()];
})->collapse()->sort()->toArray();
$neutralCulturesHotList = RegionInfo::getNeutralCultures()->filter(function(RegionInfo $regionInfo) {
return in_array($regionInfo->getThreeLetterISORegionName(), config('shop.country_hotlist', []), true);
})->map(function(RegionInfo $regionInfo) use(&$culturesForSelect) {
if(isset($culturesForSelect[$regionInfo->getThreeLetterISORegionName()])) unset($culturesForSelect[$regionInfo->getThreeLetterISORegionName()]); //Remove the item from the regular cultures list
return [$regionInfo->getThreeLetterISORegionName() => $regionInfo->getNativeName() !== '' ? $regionInfo->getNativeName() : $regionInfo->getDisplayName()];
})->collapse()->sort();
return view('templates.auth.register', ['links' => $this->links, 'cultures' => $culturesForSelect, 'culturesHotlist' => $neutralCulturesHotList]);
}
public function registeredView()
{
return view('templates.auth.registered', ['links' => $this->links]);
}
public function loginView()
{
return view('templates.auth.login', ['links' => $this->links]);
}
public function forgotPassword()
{
return view('templates.auth.password.email', ['links' => $this->links]);
}
public function set()
{
return view('templates.auth.password.set', ['token' => Str::random(32)]);
}
public function reset()
{
return view('templates.auth.password.reset', ['token' => Str::random(32)]);
}
public function loginViewKms()
{
return view('KMS::kms.auth.login', ['links' => $this->links]);
}
public function forgotPasswordKms()
{
return view('KMS::kms.auth.password.email', ['links' => $this->links]);
}
public function resetKms()
{
return view('KMS::kms.auth.password.reset', ['token' => Str::random(32)]);
}
/*
|--------------------------------------------------------------------------
| Helper methods
|--------------------------------------------------------------------------
|
| These functions provide useful help for the ones that return views.
*/
/**
* Converts a notification to view
*
* @param Notification $notification
* @return HtmlString|View
*/
private function notificationToView(Notification $notification)
{
/** @var MailMessage $mail */
$mail = $notification->toMail(\Auth::user());
if($mail->markdown) {
$markdown = new Markdown(view());
return $markdown->render($mail->markdown, $mail->data());
}
elseif($mail->view)
{
return view($mail->view, $mail->data());
} else {
throw new \RuntimeException('Preview controller: I don\'t know how to render a view for "'.get_class($notification).'"');
}
}
}