View source
<?php
namespace Drupal\uc_order\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\uc_order\Entity\Order;
use Drupal\uc_order\OrderInterface;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
use Symfony\Component\HttpFoundation\Response;
class OrderController extends ControllerBase {
public function createForUser(UserInterface $user) {
$order = Order::create([
'uid' => $user
->id(),
'order_status' => uc_order_state_default('post_checkout'),
]);
$order
->save();
uc_order_comment_save($order
->id(), $this
->currentUser()
->id(), $this
->t('Order created by the administration.'), 'admin');
return $this
->redirect('entity.uc_order.edit_form', [
'uc_order' => $order
->id(),
]);
}
public function invoice(OrderInterface $uc_order, $print = FALSE) {
$invoice = [
'#theme' => 'uc_order_invoice',
'#order' => $uc_order,
'#op' => $print ? 'print' : 'view',
];
if ($print) {
$build = [
'#theme' => 'uc_order_invoice_page',
'#content' => $invoice,
];
$markup = \Drupal::service('renderer')
->renderPlain($build);
$response = new Response($markup);
$response->headers
->set('Content-Type', 'text/html; charset=utf-8');
return $response;
}
return $invoice;
}
public function log(OrderInterface $uc_order) {
$result = \Drupal::database()
->query('SELECT order_log_id, uid, changes, created FROM {uc_order_log} WHERE order_id = :id ORDER BY order_log_id DESC', [
':id' => $uc_order
->id(),
]);
$header = [
$this
->t('Time'),
$this
->t('User'),
$this
->t('Changes'),
];
$rows = [];
foreach ($result as $change) {
$rows[] = [
\Drupal::service('date.formatter')
->format($change->created, 'short'),
[
'data' => [
'#theme' => 'username',
'#account' => User::load($change->uid),
],
],
[
'data' => [
'#markup' => $change->changes,
],
],
];
}
$build['log'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this
->t('No changes have been logged for this order.'),
];
return $build;
}
public function pageTitle(OrderInterface $uc_order) {
return $uc_order
->label();
}
}