* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
/**
* @property Carrier $object
*/
class AdminCarrierWizardControllerCore extends AdminController
{
protected $wizard_access;
public function __construct()
{
$this->bootstrap = true;
$this->display = 'view';
$this->table = 'carrier';
$this->identifier = 'id_carrier';
$this->className = 'Carrier';
$this->lang = false;
$this->deleted = true;
$this->step_number = 0;
$this->type_context = Shop::getContext();
$this->old_context = Context::getContext();
$this->multishop_context = Shop::CONTEXT_ALL;
$this->context = Context::getContext();
$this->fieldImageSettings = [
'name' => 'logo',
'dir' => 's',
];
parent::__construct();
$this->tabAccess = Profile::getProfileAccess($this->context->employee->id_profile, Tab::getIdFromClassName('AdminCarriers'));
}
public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);
$this->addJqueryPlugin('smartWizard');
$this->addJqueryPlugin('typewatch');
$this->addJs(_PS_JS_DIR_ . 'admin/carrier_wizard.js');
}
public function initWizard()
{
$this->wizard_steps = [
'name' => 'carrier_wizard',
'steps' => [
[
'title' => $this->trans('General settings', [], 'Admin.Shipping.Feature'),
],
[
'title' => $this->trans('Shipping locations and costs', [], 'Admin.Shipping.Feature'),
],
[
'title' => $this->trans('Size, weight, and group access', [], 'Admin.Shipping.Feature'),
],
[
'title' => $this->trans('Summary', [], 'Admin.Global'),
], ],
];
if (Shop::isFeatureActive()) {
$multistore_step = [
[
'title' => $this->trans('MultiStore', [], 'Admin.Global'),
],
];
array_splice($this->wizard_steps['steps'], 1, 0, $multistore_step);
}
}
public function renderView()
{
$this->initWizard();
if (Tools::getValue('id_carrier') && $this->access('edit')) {
$carrier = $this->loadObject();
} elseif ($this->access('add')) {
$carrier = new Carrier();
}
if ((!$this->access('edit') && Tools::getValue('id_carrier')) || (!$this->access('add') && !Tools::getValue('id_carrier'))) {
$this->errors[] = $this->trans('You do not have permission to use this wizard.', [], 'Admin.Shipping.Notification');
return;
}
$currency = $this->getActualCurrency();
$this->tpl_view_vars = [
'currency_sign' => $currency->sign,
'PS_WEIGHT_UNIT' => Configuration::get('PS_WEIGHT_UNIT'),
'enableAllSteps' => Validate::isLoadedObject($carrier),
'wizard_steps' => $this->wizard_steps,
'validate_url' => $this->context->link->getAdminLink('AdminCarrierWizard'),
'carrierlist_url' => $this->context->link->getAdminLink('AdminCarriers') . '&conf=' . ((int) Validate::isLoadedObject($carrier) ? 4 : 3),
'multistore_enable' => Shop::isFeatureActive(),
'wizard_contents' => [
'contents' => [
0 => $this->renderStepOne($carrier),
1 => $this->renderStepThree($carrier),
2 => $this->renderStepFour($carrier),
3 => $this->renderStepFive($carrier),
],
],
'labels' => [
'next' => $this->trans('Next', [], 'Admin.Global'),
'previous' => $this->trans('Previous', [], 'Admin.Global'),
'finish' => $this->trans('Finish', [], 'Admin.Actions'), ],
];
if (Shop::isFeatureActive()) {
array_splice($this->tpl_view_vars['wizard_contents']['contents'], 1, 0, [0 => $this->renderStepTwo($carrier)]);
}
$this->context->smarty->assign([
'carrier_logo' => (Validate::isLoadedObject($carrier) && file_exists(_PS_SHIP_IMG_DIR_ . $carrier->id . '.jpg') ? _THEME_SHIP_DIR_ . $carrier->id . '.jpg' : false),
]);
$this->context->smarty->assign([
'logo_content' => $this->createTemplate('logo.tpl')->fetch(),
]);
$this->addjQueryPlugin(['ajaxfileupload']);
return parent::renderView();
}
public function initBreadcrumbs($tab_id = null, $tabs = null)
{
if (Tools::getValue('id_carrier')) {
$this->display = 'edit';
} else {
$this->display = 'add';
}
parent::initBreadcrumbs((int) Tab::getIdFromClassName('AdminCarriers'));
$this->display = 'view';
}
public function initPageHeaderToolbar()
{
parent::initPageHeaderToolbar();
$this->page_header_toolbar_btn['cancel'] = [
'href' => $this->context->link->getAdminLink('AdminCarriers'),
'desc' => $this->trans('Cancel', [], 'Admin.Actions'),
];
}
public function renderStepOne($carrier)
{
$this->fields_form = [
'form' => [
'id_form' => 'step_carrier_general',
'input' => [
[
'type' => 'text',
'label' => $this->trans('Carrier name', [], 'Admin.Shipping.Feature'),
'name' => 'name',
'required' => true,
'hint' => [
$this->trans('Allowed characters: letters, spaces and "%special_chars%".', ['%special_chars%' => '().-'], 'Admin.Shipping.Help'),
$this->trans('The carrier\'s name will be displayed during checkout.', [], 'Admin.Shipping.Help'),
$this->trans('For in-store pickup, enter 0 to replace the carrier name with your shop name.', [], 'Admin.Shipping.Help'),
],
],
[
'type' => 'text',
'label' => $this->trans('Transit time', [], 'Admin.Shipping.Feature'),
'name' => 'delay',
'lang' => true,
'required' => true,
'maxlength' => 512,
'hint' => $this->trans('The delivery time will be displayed during checkout.', [], 'Admin.Shipping.Help'),
],
[
'type' => 'text',
'label' => $this->trans('Speed grade', [], 'Admin.Shipping.Feature'),
'name' => 'grade',
'required' => false,
'size' => 1,
'hint' => $this->trans('Enter "0" for a longest shipping delay, or "9" for the shortest shipping delay.', [], 'Admin.Shipping.Help'),
],
[
'type' => 'logo',
'label' => $this->trans('Logo', [], 'Admin.Global'),
'name' => 'logo',
],
[
'type' => 'text',
'label' => $this->trans('Tracking URL', [], 'Admin.Shipping.Feature'),
'name' => 'url',
'hint' => $this->trans('Delivery tracking URL: Type \'@\' where the tracking number should appear. It will be automatically replaced by the tracking number.', [], 'Admin.Shipping.Help'),
'desc' => $this->trans('For example: \'http://example.com/track.php?num=@\' with \'@\' where the tracking number should appear.', [], 'Admin.Shipping.Help'),
],
],
],
];
$tpl_vars = ['max_image_size' => (int) Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE') / 1024 / 1024];
$fields_value = $this->getStepOneFieldsValues($carrier);
return $this->renderGenericForm(['form' => $this->fields_form], $fields_value, $tpl_vars);
}
public function renderStepTwo($carrier)
{
$this->fields_form = [
'form' => [
'id_form' => 'step_carrier_shops',
'force' => true,
'input' => [
[
'type' => 'shop',
'label' => $this->trans('Shop association', [], 'Admin.Global'),
'name' => 'checkBoxShopAsso',
],
],
],
];
$fields_value = $this->getStepTwoFieldsValues($carrier);
return $this->renderGenericForm(['form' => $this->fields_form], $fields_value);
}
public function renderStepThree($carrier)
{
$this->fields_form = [
'form' => [
'id_form' => 'step_carrier_ranges',
'input' => [
'shipping_handling' => [
'type' => 'switch',
'label' => $this->trans('Add handling costs', [], 'Admin.Shipping.Feature'),
'name' => 'shipping_handling',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => [
[
'id' => 'shipping_handling_on',
'value' => 1,
'label' => $this->trans('Enabled', [], 'Admin.Global'),
],
[
'id' => 'shipping_handling_off',
'value' => 0,
'label' => $this->trans('Disabled', [], 'Admin.Global'),
],
],
'hint' => $this->trans('Include the handling costs (as set in Shipping > Preferences) in the final carrier price.', [], 'Admin.Shipping.Help'),
],
'is_free' => [
'type' => 'switch',
'label' => $this->trans('Free shipping', [], 'Admin.Shipping.Feature'),
'name' => 'is_free',
'required' => false,
'class' => 't',
'values' => [
[
'id' => 'is_free_on',
'value' => 1,
'label' => '
',
],
[
'id' => 'is_free_off',
'value' => 0,
'label' => '
',
],
],
],
'shipping_method' => [
'type' => 'radio',
'label' => $this->trans('Billing', [], 'Admin.Shipping.Feature'),
'name' => 'shipping_method',
'required' => false,
'class' => 't',
'br' => true,
'values' => [
[
'id' => 'billing_price',
'value' => Carrier::SHIPPING_METHOD_PRICE,
'label' => $this->trans('According to total price.', [], 'Admin.Shipping.Feature'),
],
[
'id' => 'billing_weight',
'value' => Carrier::SHIPPING_METHOD_WEIGHT,
'label' => $this->trans('According to total weight.', [], 'Admin.Shipping.Feature'),
],
],
],
'id_tax_rules_group' => [
'type' => 'select',
'label' => $this->trans('Tax', [], 'Admin.Global'),
'name' => 'id_tax_rules_group',
'options' => [
'query' => TaxRulesGroup::getTaxRulesGroups(true),
'id' => 'id_tax_rules_group',
'name' => 'name',
'default' => [
'label' => $this->trans('No tax', [], 'Admin.Global'),
'value' => 0,
],
],
],
'range_behavior' => [
'type' => 'select',
'label' => $this->trans('Out-of-range behavior', [], 'Admin.Shipping.Feature'),
'name' => 'range_behavior',
'options' => [
'query' => [
[
'id' => 0,
'name' => $this->trans('Apply the cost of the highest defined range', [], 'Admin.Shipping.Feature'),
],
[
'id' => 1,
'name' => $this->trans('Disable carrier', [], 'Admin.Shipping.Feature'),
],
],
'id' => 'id',
'name' => 'name',
],
'hint' => $this->trans('Out-of-range behavior occurs when no defined range matches the customer\'s cart (e.g. when the weight of the cart is greater than the highest weight limit defined by the weight ranges).', [], 'Admin.Shipping.Help'),
],
'zones' => [
'type' => 'zone',
'name' => 'zones',
],
],
],
];
if (Configuration::get('PS_ATCP_SHIPWRAP')) {
unset($this->fields_form['form']['input']['id_tax_rules_group']);
}
$tpl_vars = [];
$tpl_vars['PS_WEIGHT_UNIT'] = Configuration::get('PS_WEIGHT_UNIT');
$currency = $this->getActualCurrency();
$tpl_vars['currency_sign'] = $currency->sign;
$fields_value = $this->getStepThreeFieldsValues($carrier);
$this->getTplRangesVarsAndValues($carrier, $tpl_vars, $fields_value);
return $this->renderGenericForm(['form' => $this->fields_form], $fields_value, $tpl_vars);
}
/**
* @param Carrier $carrier
*
* @return string
*/
public function renderStepFour($carrier)
{
$this->fields_form = [
'form' => [
'id_form' => 'step_carrier_conf',
'input' => [
[
'type' => 'text',
'label' => $this->trans('Maximum package width (%s)', ['%s' => Configuration::get('PS_DIMENSION_UNIT')], 'Admin.Shipping.Feature'),
'name' => 'max_width',
'required' => false,
'hint' => $this->trans('Maximum width managed by this carrier. Set the value to "0", or leave this field blank to ignore.', [], 'Admin.Shipping.Help') . ' ' . $this->trans('The value must be an integer.', [], 'Admin.Shipping.Help'),
],
[
'type' => 'text',
'label' => $this->trans('Maximum package height (%s)', ['%s' => Configuration::get('PS_DIMENSION_UNIT')], 'Admin.Shipping.Feature'),
'name' => 'max_height',
'required' => false,
'hint' => $this->trans('Maximum height managed by this carrier. Set the value to "0", or leave this field blank to ignore.', [], 'Admin.Shipping.Help') . ' ' . $this->trans('The value must be an integer.', [], 'Admin.Shipping.Help'),
],
[
'type' => 'text',
'label' => $this->trans('Maximum package depth (%s)', ['%s' => Configuration::get('PS_DIMENSION_UNIT')], 'Admin.Shipping.Feature'),
'name' => 'max_depth',
'required' => false,
'hint' => $this->trans('Maximum depth managed by this carrier. Set the value to "0", or leave this field blank to ignore.', [], 'Admin.Shipping.Help') . ' ' . $this->trans('The value must be an integer.', [], 'Admin.Shipping.Help'),
],
[
'type' => 'text',
'label' => $this->trans('Maximum package weight (%s)', ['%s' => Configuration::get('PS_WEIGHT_UNIT')], 'Admin.Shipping.Feature'),
'name' => 'max_weight',
'required' => false,
'hint' => $this->trans('Maximum weight managed by this carrier. Set the value to "0", or leave this field blank to ignore.', [], 'Admin.Shipping.Help'),
],
[
'type' => 'group',
'label' => $this->trans('Group access', [], 'Admin.Shipping.Feature'),
'name' => 'groupBox',
'values' => Group::getGroups(Context::getContext()->language->id),
'hint' => $this->trans('Mark the groups that are allowed access to this carrier.', [], 'Admin.Shipping.Help'),
],
],
],
];
$fields_value = $this->getStepFourFieldsValues($carrier);
// Added values of object Group
$carrier_groups = $carrier->getGroups();
$carrier_groups_ids = [];
if (is_array($carrier_groups)) {
foreach ($carrier_groups as $carrier_group) {
$carrier_groups_ids[] = $carrier_group['id_group'];
}
}
$groups = Group::getGroups($this->context->language->id);
foreach ($groups as $group) {
$fields_value['groupBox_' . $group['id_group']] = Tools::getValue('groupBox_' . $group['id_group'], (in_array($group['id_group'], $carrier_groups_ids) || empty($carrier_groups_ids) && !$carrier->id));
}
return $this->renderGenericForm(['form' => $this->fields_form], $fields_value);
}
public function renderStepFive($carrier)
{
$this->fields_form = [
'form' => [
'id_form' => 'step_carrier_summary',
'input' => [
[
'type' => 'switch',
'label' => $this->trans('Enabled', [], 'Admin.Global'),
'name' => 'active',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => [
[
'id' => 'active_on',
'value' => 1,
],
[
'id' => 'active_off',
'value' => 0,
],
],
'hint' => $this->trans('Enable the carrier in the front office.', [], 'Admin.Shipping.Help'),
],
],
],
];
$template = $this->createTemplate('controllers/carrier_wizard/summary.tpl');
$fields_value = $this->getStepFiveFieldsValues($carrier);
$active_form = $this->renderGenericForm(['form' => $this->fields_form], $fields_value);
$active_form = str_replace(['