263 lines
9.4 KiB
PHP
263 lines
9.4 KiB
PHP
<?php
|
|
|
|
require_once (dirname(__FILE__) . '/../../x13allegro.php');
|
|
|
|
use x13allegro\Api\DataProvider\CarrierOperatorsProvider;
|
|
use x13allegro\Api\DataProvider\DeliveryMethodsProvider;
|
|
|
|
final class AdminXAllegroCarriersController extends XAllegroController
|
|
{
|
|
protected $allegroAutoLogin = true;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->table = 'xallegro_carrier';
|
|
$this->multiple_fieldsets = true;
|
|
|
|
parent::__construct();
|
|
|
|
$this->tabAccess = Profile::getProfileAccess($this->context->employee->id_profile, Tab::getIdFromClassName('AdminXAllegroCarriers'));
|
|
|
|
$this->toolbar_title = $this->l('Przypisywanie przewoźników');
|
|
$this->tpl_folder = 'x_allegro_carriers/';
|
|
}
|
|
|
|
public function init()
|
|
{
|
|
parent::init();
|
|
|
|
$this->display = 'edit';
|
|
}
|
|
|
|
public function renderForm()
|
|
{
|
|
$carriers =
|
|
$deliveryMethods =
|
|
$carrierOperators =
|
|
$carriersAllegro = [];
|
|
|
|
foreach (Carrier::getCarriers($this->context->language->id, false, false, false, null, null) as $carrier) {
|
|
$carriers[] = [
|
|
'id' => $carrier['id_reference'],
|
|
'name' => $carrier['name']
|
|
];
|
|
}
|
|
|
|
try {
|
|
$dataProvider = new DeliveryMethodsProvider(
|
|
$this->allegroApi->sale()->deliveryMethods()->getAll()->deliveryMethods
|
|
);
|
|
|
|
$deliveryMethods = $dataProvider->getDeliveryMethods();
|
|
$deliveryMethods = $dataProvider->groupDeliveryMethods($deliveryMethods);
|
|
|
|
$carrierOperators = (new CarrierOperatorsProvider($this->allegroApi))->getCarriers();
|
|
}
|
|
catch (Exception $ex) {
|
|
$this->errors[] = (string)$ex;
|
|
}
|
|
|
|
foreach ($deliveryMethods as $type => $methods) {
|
|
foreach ($methods as $deliveryMethod) {
|
|
$carriersAllegro[$type][] = array(
|
|
'type' => 'select',
|
|
'label' => $deliveryMethod['name'],
|
|
'id_fields_shipment' => $deliveryMethod['id'],
|
|
'name' => 'id_fields_shipment[' . $deliveryMethod['id'] . '][id_carrier]',
|
|
'name_operator' => 'id_fields_shipment[' . $deliveryMethod['id'] . ']',
|
|
'class' => 'fixed-width-xxl',
|
|
'options' => array(
|
|
'query' => $carriers,
|
|
'id' => 'id',
|
|
'name' => 'name',
|
|
'default' => array(
|
|
'label' => $this->l('-- Wybierz --'),
|
|
'value' => 0
|
|
)
|
|
),
|
|
'options_operators' => array(
|
|
'query' => $carrierOperators,
|
|
'default' => array(
|
|
'label' => $this->l('-- Wybierz operatora przesyłki --'),
|
|
'value' => null
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
if (isset($carriersAllegro['free'])) {
|
|
$this->fields_form[0]['form'] = array(
|
|
'legend' => array(
|
|
'title' => $this->l('Darmowe opcje dostawy')
|
|
),
|
|
'input' => $carriersAllegro['free'],
|
|
'submit' => array(
|
|
'title' => $this->l('Zapisz')
|
|
)
|
|
);
|
|
}
|
|
|
|
if (isset($carriersAllegro['in_advance_courier'])) {
|
|
$this->fields_form[1]['form'] = array(
|
|
'legend' => array(
|
|
'title' => $this->l('Kurier - płatność z góry')
|
|
),
|
|
'input' => $carriersAllegro['in_advance_courier'],
|
|
'submit' => array(
|
|
'title' => $this->l('Zapisz')
|
|
)
|
|
);
|
|
}
|
|
|
|
if (isset($carriersAllegro['in_advance_package'])) {
|
|
$this->fields_form[2]['form'] = array(
|
|
'legend' => array(
|
|
'title' => $this->l('Paczka - płatność z góry')
|
|
),
|
|
'input' => $carriersAllegro['in_advance_package'],
|
|
'submit' => array(
|
|
'title' => $this->l('Zapisz')
|
|
)
|
|
);
|
|
}
|
|
|
|
if (isset($carriersAllegro['in_advance_letter'])) {
|
|
$this->fields_form[3]['form'] = array(
|
|
'legend' => array(
|
|
'title' => $this->l('List - płatność z góry')
|
|
),
|
|
'input' => $carriersAllegro['in_advance_letter'],
|
|
'submit' => array(
|
|
'title' => $this->l('Zapisz')
|
|
)
|
|
);
|
|
}
|
|
|
|
if (isset($carriersAllegro['in_advance_pos'])) {
|
|
$this->fields_form[4]['form'] = array(
|
|
'legend' => array(
|
|
'title' => $this->l('Odbiór w punkcie - płatność z góry')
|
|
),
|
|
'input' => $carriersAllegro['in_advance_pos'],
|
|
'submit' => array(
|
|
'title' => $this->l('Zapisz')
|
|
)
|
|
);
|
|
}
|
|
|
|
if (isset($carriersAllegro['cash_on_delivery_courier'])) {
|
|
$this->fields_form[5]['form'] = array(
|
|
'legend' => array(
|
|
'title' => $this->l('Kurier - płatność przy odbiorze')
|
|
),
|
|
'input' => $carriersAllegro['cash_on_delivery_courier'],
|
|
'submit' => array(
|
|
'title' => $this->l('Zapisz')
|
|
)
|
|
);
|
|
}
|
|
|
|
if (isset($carriersAllegro['cash_on_delivery_package'])) {
|
|
$this->fields_form[6]['form'] = array(
|
|
'legend' => array(
|
|
'title' => $this->l('Paczka - płatność przy odbiorze')
|
|
),
|
|
'input' => $carriersAllegro['cash_on_delivery_package'],
|
|
'submit' => array(
|
|
'title' => $this->l('Zapisz')
|
|
)
|
|
);
|
|
}
|
|
|
|
if (isset($carriersAllegro['cash_on_delivery_letter'])) {
|
|
$this->fields_form[7]['form'] = array(
|
|
'legend' => array(
|
|
'title' => $this->l('List - płatność przy odbiorze')
|
|
),
|
|
'input' => $carriersAllegro['cash_on_delivery_letter'],
|
|
'submit' => array(
|
|
'title' => $this->l('Zapisz')
|
|
)
|
|
);
|
|
}
|
|
|
|
if (isset($carriersAllegro['cash_on_delivery_pos'])) {
|
|
$this->fields_form[8]['form'] = array(
|
|
'legend' => array(
|
|
'title' => $this->l('Odbiór w punkcie - płatność przy odbiorze')
|
|
),
|
|
'input' => $carriersAllegro['cash_on_delivery_pos'],
|
|
'submit' => array(
|
|
'title' => $this->l('Zapisz')
|
|
)
|
|
);
|
|
}
|
|
|
|
if (isset($carriersAllegro['abroad'])) {
|
|
$this->fields_form[9]['form'] = array(
|
|
'legend' => array(
|
|
'title' => $this->l('Wysyłka za granicę - płatność z góry')
|
|
),
|
|
'input' => $carriersAllegro['abroad'],
|
|
'submit' => array(
|
|
'title' => $this->l('Zapisz')
|
|
)
|
|
);
|
|
}
|
|
|
|
return parent::renderForm();
|
|
}
|
|
|
|
public function postProcess()
|
|
{
|
|
if (Tools::isSubmit('submitAdd' . $this->table))
|
|
{
|
|
if ($this->tabAccess['edit'] !== '1') {
|
|
$this->errors[] = $this->l('Nie masz uprawnień do edycji w tym miejscu.');
|
|
}
|
|
else if (Tools::getValue('id_fields_shipment')) {
|
|
if (XAllegroCarrier::updateCarriers(Tools::getValue('id_fields_shipment'))) {
|
|
Tools::redirectAdmin($this->context->link->getAdminLink('AdminXAllegroCarriers') . '&conf=4');
|
|
}
|
|
}
|
|
else {
|
|
$this->errors[] = $this->l('Nie przesłano przewożników');
|
|
}
|
|
}
|
|
|
|
$this->display = 'edit';
|
|
|
|
return true;
|
|
}
|
|
|
|
public function getFieldsValue($obj)
|
|
{
|
|
$assignedCarriers = XAllegroCarrier::getAll();
|
|
|
|
foreach ($this->fields_form as $fieldset) {
|
|
if (isset($fieldset['form']['input'])) {
|
|
foreach ($fieldset['form']['input'] as $input)
|
|
{
|
|
if (!isset($this->fields_value[$input['name']])) {
|
|
$this->fields_value[$input['name']] = (isset($assignedCarriers[$input['id_fields_shipment']])
|
|
? $assignedCarriers[$input['id_fields_shipment']]['id_carrier'] : 0);
|
|
}
|
|
|
|
if (!isset($this->fields_value[$input['name_operator'] . '[id_operator]'])) {
|
|
$this->fields_value[$input['name_operator'] . '[id_operator]'] = (isset($assignedCarriers[$input['id_fields_shipment']])
|
|
? $assignedCarriers[$input['id_fields_shipment']]['id_operator'] : '');
|
|
}
|
|
|
|
if (!isset($this->fields_value[$input['name_operator'] . '[operator_name]'])) {
|
|
$this->fields_value[$input['name_operator'] . '[operator_name]'] = (isset($assignedCarriers[$input['id_fields_shipment']])
|
|
? $assignedCarriers[$input['id_fields_shipment']]['operator_name'] : '');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->fields_value;
|
|
}
|
|
}
|