File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Kms/Development/PreviewController.php
<?php
namespace App\Komma\Kms\Development;
use App\Komma\Base\Controller;
use App\Komma\Globalization\RegionInfo;
use App\Komma\Locations\Models\Location;
use App\Komma\Orders\Models\Order as OrderModel;
use App\Komma\Reservations\Models\Reservation;
use App\Komma\Shop\Invoicing\InvoiceService;
use App\Komma\Shop\Orders\Mail\OrderManualActionRequiredStaff;
use App\Komma\Shop\Orders\Mail\OrderStatusUpdatedCustomer;
use App\Komma\Shop\Orders\Mail\OrderStatusUpdatedStaff;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Orders\OrderStatus;
use App\Komma\Users\Models\KmsUser;
use App\Komma\Users\Models\SiteUser;
use App\Mail\ClientAlmostReadyMail;
use App\Mail\ClientFavourMail;
use App\Mail\ClientInformationMail;
use App\Mail\ClientReminderMail;
use App\Mail\ClientReminderSimpleMail;
use App\Mail\OrderReceivedMail;
use App\Notifications\CustomerRegistered;
use App\Notifications\CustomerRegisteredSetPassword;
use App\Notifications\KmsResetPasswordNotification;
use App\Notifications\SiteResetPasswordNotification;
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 InvoiceService */
private $invoiceService;
public function __construct()
{
$this->invoiceService = app(InvoiceService::class);
}
/*
|--------------------------------------------------------------------------
| Mail previews
|--------------------------------------------------------------------------
|
| These functions must returns views that represent mails.
|
*/
/**
* @see SiteResetPasswordNotification
* @return HtmlString
*/
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));
}
/**
* @see CustomerRegisteredSetPassword
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function setPasswordMail()
{
$customer = factory(SiteUser::class)->make();
$token = Str::random(32);
return $this->notificationToView(new CustomerRegisteredSetPassword($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::getSpecificCultures()->map(function (RegionInfo $regionInfo) {
return [$regionInfo->getName() => $regionInfo->getNativeName()];
})->collapse()->toArray();
return view('site.templates.auth.register', ['links' => $this->links, 'cultures' => $culturesForSelect]);
}
public function registeredView()
{
return view('site.templates.auth.registered', ['links' => $this->links]);
}
public function loginView()
{
return view('site.templates.auth.login', ['links' => $this->links]);
}
public function forgotPassword()
{
return view('site.templates.auth.password.email', ['links' => $this->links]);
}
public function set()
{
return view('site.templates.auth.password.set', ['token' => Str::random(32)]);
}
public function reset()
{
return view('site.templates.auth.password.reset', ['token' => Str::random(32)]);
}
public function loginViewKms()
{
return view('kms.auth.login', ['links' => $this->links]);
}
public function forgotPasswordKms()
{
return view('kms.auth.password.email', ['links' => $this->links]);
}
public function resetKms()
{
return view('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).'"');
}
}
public function showMail1(Reservation $reservation) {
if (! $reservation->id) $reservation = Reservation::inRandomOrder()->first();
$reservation->loadMissing('location');
$location = Location::where('id', '=', $reservation->location)->first();
if (! $location) $location = Location::inRandomOrder()->first();
$order = $reservation->order;
if (! $order) $order = OrderModel::inRandomOrder()->first();
return new OrderReceivedMail($order, $location, $reservation);
}
public function showMail2(Reservation $reservation): ClientInformationMail
{
if (! $reservation->id) $reservation = Reservation::inRandomOrder()->first();
return new ClientInformationMail($reservation);
}
public function showMail3(Reservation $reservation): ClientReminderMail
{
if (! $reservation->id) $reservation = Reservation::inRandomOrder()->first();
return new ClientReminderMail($reservation);
}
public function showMail4(Reservation $reservation): ClientReminderSimpleMail
{
if (! $reservation->id) $reservation = Reservation::inRandomOrder()->first();
return new ClientReminderSimpleMail($reservation);
}
public function showMail5(Reservation $reservation): ClientAlmostReadyMail
{
if (! $reservation->id) $reservation = Reservation::inRandomOrder()->first();
return new ClientAlmostReadyMail($reservation);
}
public function showMail6(Reservation $reservation): ClientFavourMail
{
if (! $reservation->id) $reservation = Reservation::inRandomOrder()->first();
return new ClientFavourMail($reservation);
}
}