File: D:/HostingSpaces/SBogers10/zelfverkopen.komma.pro/app/KommaApp/Orders/Kms/OrderController.php
<?php
namespace App\KommaApp\Orders\Kms;
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
use App\KommaApp\Kms\Core\SectionController;
use App\KommaApp\Orders\Models\Order;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class OrderController extends SectionController
{
protected $slug = "orders";
protected $forModelName = Order::class;
function __construct(OrderSection $section)
{
parent::__construct($section);
if($subTitleString = $this->getSubtitleStringOfOrderStatusFromUrl())
$section->setSubTitle($subTitleString);
}
private function getSubtitleStringOfOrderStatusFromUrl() {
$statusId = \Input::get('status', null);
if($statusId === null) return false;
switch ($statusId) {
case (Order::ORDER_STATUS_NEW):
return trans('kms/orders.statusList.'.Order::ORDER_STATUS_NEW);
case (Order::ORDER_STATUS_PROCESSING):
return trans('kms/orders.statusList.'.Order::ORDER_STATUS_PROCESSING);
case (Order::ORDER_STATUS_AWAITING_PAYMENT):
return trans('kms/orders.statusList.'.Order::ORDER_STATUS_AWAITING_PAYMENT);
case (Order::ORDER_STATUS_CLOSED):
return trans('kms/orders.statusList.'.Order::ORDER_STATUS_CLOSED);
case (Order::ORDER_STATUS_IN_SALE):
return trans('kms/orders.statusList.'.Order::ORDER_STATUS_IN_SALE);
case (Order::ORDER_STATUS_DELETED):
return trans('kms/orders.statusList.'.Order::ORDER_STATUS_DELETED);
case (Order::ORDER_STATUS_REFUNDED):
return trans('kms/orders.statusList.'.Order::ORDER_STATUS_REFUNDED);
case (Order::ORDER_STATUS_CREDIT):
return trans('kms/orders.statusList.'.Order::ORDER_STATUS_CREDIT);
default:
return false;
}
}
public function autosaveProductsToOrder(Request $request, Order $order) {
return $this->section->getSectionService()->autosaveProductsToOrder($request, $order);
}
/**
* Only for sending mails manually
*
* @param $orderId
* @return string
*/
public function sendPayedMailsForOrderId($orderId)
{
\Log::debug('sending mails for order with order_id: '.$orderId);
/** @var OrderService $service */
$service = $this->section->getSectionService();
if($service->sendPaymentReceivedMail($orderId)) {
return 'done';
}
return 'Is the order id correct?';
}
/**
* We pass a boolean to the render method so we can distinguish between a create or an edit.
*
* @return \Illuminate\Contracts\View\View|mixed
*/
public function create()
{
//Set errors
$this->section->setErrors(\Session::get('errors'));
// Build section tabs
$this->section->getSectionTabDirector()->buildTabs();
//Load an empty model for the form
$this->section->loadModel();
$this->section->loadModels();
//Render the form
$this->section->setForModelName($this->forModelName);
return $this->section->render(true);
}
/**
* We pass a boolean to the render method so we can distinguish between a create or an edit.
*
* This method will validate and save the model
* It is called on create form submit,
* and it is called on the form edit.
*
* @param null|Model $model
* @param bool $forCreate If we are updating an model that exists (true) or a new one (false)
* @return mixed
* @throws \Exception
*/
public function store($model = null, $forCreate = true)
{
$model = $this->section->setOrCreateModel($model);
$this->section->generateAttributesAndAddThemToTabs($forCreate);
//Validate the form data
$validator = $this->section->validateInputAndReturnValidator();
//Check if the validator fails
if ($validator->fails()) {
//It fails, redirect back with errors
return \Redirect::back()->withInput()->withErrors($validator->messages());
}
$id = $this->section->save($model);
$routeParameters = [];
if($this->kms->getSiteSlug())
$routeParameters['site'] = $this->kms->getSiteSlug();
$routeParameters[$this->slug] = $id;
return \Redirect::action('\\'.get_class($this) . '@show', $routeParameters)
->with('success', __('kms/global.saved'))
->with('current_tab' , \Input::get('tab-slug')); // Pass the opened tab
}
public function update($idOrModel)
{
if(!is_a($idOrModel, Model::class)) throw new \InvalidArgumentException("Please make sure that you've set route model binding for the section '".get_class($this)."'");
//Handle the rest in the store method
return $this->store($idOrModel, false);
}
}