Skip to content
Code-Schnipsel Gruppen Projekte
Bestätigt Commit bfef9ed8 erstellt von Henning Leutz's avatar Henning Leutz :martial_arts_uniform:
Dateien durchsuchen

refactor: event handling

Übergeordneter 7155a3a7
Keine zugehörigen Branchen gefunden
Keine zugehörigen Tags gefunden
2 Merge Requests!2Next,!1Dev
werden angezeigt mit 451 Ergänzungen und 40 Löschungen
<?php
/**
* This file contains package_quiqqer_order-simple-checkout_ajax_frontend_payments
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_order-simple-checkout_ajax_frontend_payments',
function ($orderHash) {
},
['orderHash']
);
<?php
/**
* This file contains package_quiqqer_order-simple-checkout_ajax_frontend_shipping
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_order-simple-checkout_ajax_frontend_shipping',
function ($orderHash) {
if (!QUI::getPackageManager()->isInstalled('quiqqer/shipping')) {
return '';
}
$Checkout = new QUI\ERP\Order\SimpleCheckout\Checkout([
'orderHash' => $orderHash
]);
return $Checkout->getShipping();
},
['orderHash']
);
<?php
/**
* This file contains package_quiqqer_order-simple-checkout_ajax_frontend_setAddress
*/
use QUI\ERP\Address;
use QUI\ERP\Order\SimpleCheckout\Checkout;
use QUI\ERP\Shipping\Shipping;
QUI::$Ajax->registerFunction(
'package_quiqqer_order-simple-checkout_ajax_frontend_update',
function ($orderHash, $orderData) {
$orderData = json_decode($orderData, true);
if (!is_array($orderData)) {
return;
}
$Checkout = new Checkout(['orderHash' => $orderHash]);
$Order = $Checkout->getOrder();
$Order->setInvoiceAddress(new Address($orderData));
if (!empty($orderData['shipping']) && QUI::getPackageManager()->isInstalled('quiqqer/shipping')) {
$Order->setShipping(
Shipping::getInstance()->getShippingEntry($orderData['shipping'])
);
} else {
$Order->removeShipping();
}
$Order->save();
},
['orderHash', 'orderData']
);
define('package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckout', [
'qui/QUI',
'qui/controls/Control'
'qui/controls/Control',
'qui/utils/Form',
'Ajax'
], function(QUI, QUIControl) {
], function(QUI, QUIControl, QUIFormUtils, QUIAjax) {
'use strict';
return new Class({
......@@ -11,16 +13,90 @@ define('package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleChecko
Extends: QUIControl,
Type: 'package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckout',
Binds: [
'update'
],
initialize: function(options) {
this.parent(options);
this.$Delivery = null;
this.$Shipping = null;
this.$Payment = null;
this.addEvents({
onImport: this.$onImport
});
},
$onImport: function() {
Promise.all([
this.$getControl(this.getElm().getElement('.quiqqer-simple-checkout-delivery')),
this.$getControl(this.getElm().getElement('.quiqqer-simple-checkout-shipping')),
this.$getControl(this.getElm().getElement('.quiqqer-simple-checkout-payment'))
]).then((instances) => {
console.log(instances);
this.$Delivery = instances[0];
this.$Shipping = instances[1];
this.$Payment = instances[2];
this.$Delivery.addEvent('change', () => {
this.update().then(() => {
if (this.$Shipping) {
return this.$Shipping.refresh().then(() => {
this.$Payment.refresh();
});
}
this.$Payment.refresh();
});
});
if (this.$Shipping) {
this.$Shipping.addEvent('change', () => {
this.update().then(() => {
this.$Payment.refresh();
});
});
}
this.$Payment.addEvent('change', () => {
this.update().then(() => {
});
});
});
},
$getControl: function(Node) {
return new Promise((resolve) => {
if (!Node.get('data-qui')) {
return resolve(null);
}
if (Node.get('data-quiid')) {
resolve(QUI.Controls.getById(Node.get('data-quiid')));
return;
}
Node.addEvent('load', () => {
resolve(QUI.Controls.getById(Node.get('data-quiid')));
});
});
},
update: function() {
console.log('update');
return new Promise((resolve) => {
const orderData = QUIFormUtils.getFormData(this.getElm().getElement('form'));
QUIAjax.post('package_quiqqer_order-simple-checkout_ajax_frontend_update', resolve, {
'package': 'quiqqer/order-simple-checkout',
orderData: JSON.encode(orderData)
});
});
}
});
});
define('package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckoutDelivery', [
'qui/QUI',
'qui/controls/Control'
'qui/controls/Control',
'qui/utils/Form',
'Ajax'
], function(QUI, QUIControl) {
], function(QUI, QUIControl, QUIFormUtils, QUIAjax) {
'use strict';
return new Class({
......@@ -11,6 +13,10 @@ define('package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleChecko
Extends: QUIControl,
Type: 'package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckoutDelivery',
Binds: [
'$onChange'
],
initialize: function(options) {
this.parent(options);
......@@ -22,6 +28,7 @@ define('package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleChecko
},
$onImport: function() {
let loaded = false;
const BusinessType = this.getElm().getElement('[name="businessType"]');
const Company = this.getElm().getElement('.quiqqer-order-customerData-edit-company');
const VatId = this.getElm().getElement('.quiqqer-order-customerData-edit-vatId');
......@@ -48,10 +55,20 @@ define('package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleChecko
if (BusinessType.value === 'b2b') {
this.$showB2B();
}
if (loaded) {
this.$onChange();
}
});
}
BusinessType.fireEvent('change');
this.getElm().getElements('input').addEvent('change', this.$onChange);
loaded = true;
},
$onChange: function() {
this.fireEvent('change');
},
$hideB2B: function() {
......@@ -82,7 +99,7 @@ define('package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleChecko
Label.setStyle('overflow', 'hidden');
Label.setStyle('height', 0);
Label.setStyle('margin', null);
moofx(Label).animate({
opacity: 1,
height: Label.getScrollSize().y
......
define('package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckoutPayment', [
'qui/QUI',
'qui/controls/Control'
], function(QUI, QUIControl) {
'use strict';
return new Class({
Extends: QUIControl,
Type: 'package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckoutPayment',
initialize: function(options) {
this.parent(options);
this.addEvents({
onImport: this.$onImport
});
},
$onImport: function() {
},
refresh: function() {
}
});
});
define('package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckoutShipping', [
'qui/QUI',
'qui/controls/Control',
'Ajax'
], function(QUI, QUIControl, QUIAjax) {
'use strict';
return new Class({
Extends: QUIControl,
Type: 'package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckoutShipping',
Binds: [
'$onChange'
],
initialize: function(options) {
this.parent(options);
this.addEvents({
onImport: this.$onImport
});
},
$onImport: function() {
this.$registerEvents();
},
$registerEvents: function() {
this.getElm().getElements('[type="radio"]').addEvent('change', this.$onChange);
this.getElm().getElements('[type="radio"]').forEach((Node) => {
Node.getParent('.quiqqer-order-step-shipping-list-entry').addEvent('click', (e) => {
let Target = e.target;
if (!Target.hasClass('quiqqer-order-step-shipping-list-entry')) {
Target = Target.getParent('.quiqqer-order-step-shipping-list-entry');
}
Target.getElement('input').set('checked', true);
this.$onChange();
});
});
},
$onChange: function(e) {
if (typeOf(e) === 'domevent') {
e.stop();
}
this.fireEvent('change');
},
refresh: function() {
// @todo loader
return new Promise(() => {
QUIAjax.get('package_quiqqer_order-simple-checkout_ajax_frontend_shipping', (html) => {
const Ghost = new Element('div', {
html: html
});
this.getElm().set('html', Ghost.getFirst('div').get('html'));
Ghost.getElements('style').inject(this.getElm());
this.$registerEvents();
}, {
'package': 'quiqqer/order-simple-checkout'
});
});
}
});
});
......@@ -59,6 +59,10 @@
width: 100%;
}
.quiqqer-order-step-shipping header {
display: none;
}
/** right side
============================================= */
......
......@@ -4,16 +4,25 @@
use QUI;
use QUI\ERP\Order\OrderInProcess;
use QUI\ERP\Order\SimpleCheckout\Steps\CheckoutDelivery;
use QUI\ERP\Order\SimpleCheckout\Steps\CheckoutPayment;
use QUI\ERP\Order\SimpleCheckout\Steps\CheckoutShipping;
use QUI\Exception;
use function dirname;
use function file_exists;
/**
* Class Checkout
*
* Represents the simple checkout control
*/
class Checkout extends QUI\Control
{
public function __construct($attributes = [])
{
$this->setAttributes([
'orderHash' => false,
'template' => false,
'data-qui' => 'package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckout'
]);
......@@ -41,9 +50,9 @@ public function getBody(): string
$Engine->assign([
'Basket' => $Control,
'User' => $this->getUser(),
'Delivery' => new CheckoutDelivery(),
'Shipping' => new CheckoutShipping(),
'Payment' => new CheckoutPayment()
'Delivery' => new CheckoutDelivery($this),
'Shipping' => new CheckoutShipping($this),
'Payment' => new CheckoutPayment($this)
]);
return $Engine->fetch($template);
......@@ -67,6 +76,8 @@ public function send()
}
}
// region getter
/**
* @throws Exception
* @throws QUI\ERP\Order\Exception
......@@ -76,6 +87,13 @@ public function getOrder(): ?OrderInProcess
$Order = null;
$Orders = QUI\ERP\Order\Handler::getInstance();
if ($this->getAttribute('orderHash')) {
try {
return $Orders->getOrderInProcessByHash($this->getAttribute('orderHash'));
} catch (QUI\Exception $exception) {
}
}
try {
// select the last order in processing
$OrderInProcess = $Orders->getLastOrderInProcessFromUser($this->getUser());
......@@ -94,8 +112,30 @@ public function getOrder(): ?OrderInProcess
return $Order;
}
/**
* Returns the user associated with the current session.
*
* @return QUI\Interfaces\Users\User The user object.
*/
public function getUser(): QUI\Interfaces\Users\User
{
return QUI::getUserBySession();
}
/**
* Get the shipping html for the current order.
*
* @return string The shipping information
*/
public function getShipping(): string
{
$Shipping = new CheckoutShipping($this);
$Output = new QUI\Output();
$result = $Shipping->create();
$css = QUI\Control\Manager::getCSS();
return $Output->parse($css . $result);
}
//endregion
}
<?php
namespace QUI\ERP\Order\SimpleCheckout;
use QUI;
use function dirname;
class CheckoutShipping extends QUI\Control
{
public function __construct($attributes = [])
{
parent::__construct($attributes);
$this->addCSSFile(dirname(__FILE__) . '/CheckoutShipping.css');
$this->addCSSClass('quiqqer-simple-checkout-shipping');
}
public function getBody(): string
{
$Engine = QUI::getTemplateManager()->getEngine();
return $Engine->fetch(dirname(__FILE__) . '/CheckoutShipping.html');
}
}
<?php
namespace QUI\ERP\Order\SimpleCheckout;
interface CheckoutStepInterface
{
public function __construct(Checkout $Checkout, $attributes = []);
}
\ No newline at end of file
<?php
namespace QUI\ERP\Order\SimpleCheckout;
namespace QUI\ERP\Order\SimpleCheckout\Steps;
use QUI;
use QUI\ERP\Order\SimpleCheckout\Checkout;
use QUI\ERP\Order\SimpleCheckout\CheckoutStepInterface;
use QUI\Exception;
use QUI\Users\User;
......@@ -15,20 +17,25 @@
*
* @package Your\Package\Namespace
*/
class CheckoutDelivery extends QUI\Control
class CheckoutDelivery extends QUI\Control implements CheckoutStepInterface
{
protected Checkout $Checkout;
/**
* Constructor method for the SimpleCheckoutDelivery class.
*
* @param array $attributes Optional attributes to be passed to the parent constructor.
* @param Checkout $Checkout
* @param array $attributes
* @return void
*/
public function __construct($attributes = [])
public function __construct(Checkout $Checkout, $attributes = [])
{
$this->Checkout = $Checkout;
parent::__construct($attributes);
$this->addCSSFile(dirname(__FILE__) . '/CheckoutDelivery.css');
$this->addCSSClass('quiqqer-simple-checkout-delivery');
$this->addCSSClass('quiqqer-simple-checkout-delivery quiqqer-simple-checkout-step');
$this->setJavaScriptControl(
'package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckoutDelivery'
);
......@@ -123,6 +130,15 @@ public function getBody(): string
protected function getInvoiceAddress()
{
$User = QUI::getUserBySession();
$Order = $this->Checkout->getOrder();
$Address = $Order->getInvoiceAddress();
$attributes = $Address->getAttributes();
// is not empty
if (count($attributes) > 3) {
return $Address;
}
if ($User->getAttribute('quiqqer.erp.address')) {
try {
......@@ -141,4 +157,16 @@ protected function getInvoiceAddress()
return null;
}
/**
* Validates the invoice address of the current order.
*
* @throws QUI\ERP\Order\Exception|Exception
*/
public function validate()
{
QUI\ERP\Order\Controls\OrderProcess\CustomerData::validateAddress(
$this->Checkout->getOrder()->getInvoiceAddress()
);
}
}
<?php
namespace QUI\ERP\Order\SimpleCheckout;
namespace QUI\ERP\Order\SimpleCheckout\Steps;
use QUI;
use QUI\ERP\Order\SimpleCheckout\Checkout;
use QUI\ERP\Order\SimpleCheckout\CheckoutStepInterface;
use function dirname;
class CheckoutPayment extends QUI\Control
class CheckoutPayment extends QUI\Control implements CheckoutStepInterface
{
public function __construct($attributes = [])
protected Checkout $Checkout;
public function __construct(Checkout $Checkout, $attributes = [])
{
$this->Checkout = $Checkout;
parent::__construct($attributes);
$this->addCSSFile(dirname(__FILE__) . '/CheckoutPayment.css');
$this->addCSSClass('quiqqer-simple-checkout-payment');
$this->addCSSClass('quiqqer-simple-checkout-payment quiqqer-simple-checkout-step');
$this->setJavaScriptControl(
'package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckoutPayment'
);
}
public function getBody(): string
......
{if !isset($Shipping)}
<div class="order-simple-checkout-shipping--info">
Enter your delivery address to view available shipping methods.
</div>
\ No newline at end of file
</div>
{else}
{$Shipping->create()}
{/if}
\ No newline at end of file
<?php
namespace QUI\ERP\Order\SimpleCheckout\Steps;
use QUI;
use QUI\ERP\Order\SimpleCheckout\Checkout;
use QUI\ERP\Order\SimpleCheckout\CheckoutStepInterface;
use function dirname;
class CheckoutShipping extends QUI\Control implements CheckoutStepInterface
{
protected Checkout $Checkout;
public function __construct(Checkout $Checkout, $attributes = [])
{
$this->Checkout = $Checkout;
parent::__construct($attributes);
$this->addCSSFile(dirname(__FILE__) . '/CheckoutShipping.css');
$this->addCSSClass('quiqqer-simple-checkout-shipping quiqqer-simple-checkout-step');
$this->setJavaScriptControl(
'package/quiqqer/order-simple-checkout/bin/frontend/controls/SimpleCheckoutShipping'
);
}
public function getBody(): string
{
if (!QUI::getPackageManager()->isInstalled('quiqqer/shipping')) {
return '';
}
$Engine = QUI::getTemplateManager()->getEngine();
$Delivery = new CheckoutDelivery($this->Checkout);
try {
$Delivery->validate();
} catch (QUI\Exception $exception) {
return $Engine->fetch(dirname(__FILE__) . '/CheckoutShipping.html');
}
$Shipping = new QUI\ERP\Shipping\Order\Shipping([
'Order' => $this->Checkout->getOrder()
]);
$Engine->assign('Shipping', $Shipping);
return $Engine->fetch(dirname(__FILE__) . '/CheckoutShipping.html');
}
}
0% Lade oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren