View source
<?php
namespace Drupal\uc_cart\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\uc_cart\Plugin\CheckoutPaneManager;
use Drupal\uc_store\AjaxAttachTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class CheckoutForm extends FormBase {
use AjaxAttachTrait;
protected $checkoutPaneManager;
protected $session;
protected $dateTime;
public function __construct(CheckoutPaneManager $checkout_pane_manager, SessionInterface $session, TimeInterface $date_time) {
$this->checkoutPaneManager = $checkout_pane_manager;
$this->session = $session;
$this->dateTime = $date_time;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.uc_cart.checkout_pane'), $container
->get('session'), $container
->get('datetime.time'));
}
public function getFormId() {
return 'uc_cart_checkout_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $order = NULL) {
if ($processed = $form_state
->has('order')) {
$order = $form_state
->get('order');
}
else {
$form_state
->set('order', $order);
}
$form['#attributes']['class'][] = 'uc-cart-checkout-form';
$form['#attached']['library'][] = 'uc_cart/uc_cart.styles';
$form['panes'] = [
'#tree' => TRUE,
];
$filter = [
'enabled' => FALSE,
];
if (!$order
->isShippable() && $this
->config('uc_cart.settings')
->get('panes.delivery.settings.delivery_not_shippable')) {
$filter['shippable'] = TRUE;
}
$panes = $this->checkoutPaneManager
->getPanes($filter);
foreach ($panes as $id => $pane) {
if (!$form_state
->get([
'panes',
$id,
'prepared',
])) {
$pane
->prepare($order, $form, $form_state);
$form_state
->set([
'panes',
$id,
'prepared',
], TRUE);
$processed = FALSE;
}
}
if (!$processed) {
$order->line_items = $order
->getLineItems();
$order
->save();
}
foreach ($panes as $id => $pane) {
$form['panes'][$id] = $pane
->view($order, $form, $form_state);
$form['panes'][$id] += [
'#type' => 'details',
'#title' => $pane
->getTitle(),
'#id' => $id . '-pane',
'#open' => TRUE,
];
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('Cancel'),
'#validate' => [],
'#limit_validation_errors' => [],
'#submit' => [
[
$this,
'cancel',
],
],
];
$form['actions']['continue'] = [
'#type' => 'submit',
'#value' => $this
->t('Review order'),
'#button_type' => 'primary',
];
$form['#process'][] = [
$this,
'ajaxProcessForm',
];
$this->session
->remove('uc_checkout_review_' . $order
->id());
$this->session
->remove('uc_checkout_complete_' . $order
->id());
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$order = $form_state
->get('order');
$order
->setChangedTime($this->dateTime
->getRequestTime());
$form_state
->set('checkout_valid', TRUE);
foreach (Element::children($form_state
->getValue('panes')) as $id) {
$pane = $this->checkoutPaneManager
->createInstance($id);
if ($pane
->process($order, $form, $form_state) === FALSE) {
$form_state
->set('checkout_valid', FALSE);
}
}
$order->line_items = $order
->getLineItems();
$order
->save();
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->get('checkout_valid') === FALSE) {
$form_state
->setRedirect('uc_cart.checkout');
}
else {
$form_state
->setRedirect('uc_cart.checkout_review');
$this->session
->set('uc_checkout_review_' . $form_state
->get('order')
->id(), TRUE);
}
$form_state
->set('checkout_valid', NULL);
}
public function cancel(array &$form, FormStateInterface $form_state) {
$order = $form_state
->get('order');
if ($this->session
->get('cart_order') == $order
->id()) {
uc_order_comment_save($order
->id(), 0, $this
->t('Customer canceled this order from the checkout form.'));
$this->session
->remove('cart_order');
}
$this->session
->remove('uc_checkout_review_' . $order
->id());
$this->session
->remove('uc_checkout_complete_' . $order
->id());
$form_state
->setRedirect('uc_cart.cart');
}
}