Init du module DHL (sur la base du module Colissimo)

This commit is contained in:
2020-12-03 17:12:39 +01:00
parent 979767253d
commit 7f00efb081
48 changed files with 4198 additions and 541 deletions

View File

@@ -0,0 +1,67 @@
<?php
namespace DHL\Controller;
use DHL\DHL;
use DHL\Model\Config\DHLConfigValue;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Tools\URL;
/**
* Class Configuration
* @package DHL\Controller
* @author Thomas Arnaud <tarnaud@openstudio.fr>
*/
class Configuration extends BaseAdminController
{
public function editConfiguration()
{
if (null !== $response = $this->checkAuth(
AdminResources::MODULE,
[DHL::DOMAIN_NAME],
AccessManager::UPDATE
)) {
return $response;
}
$form = $this->createForm('dhl.configuration');
$error_message = null;
try {
$validateForm = $this->validateForm($form);
$data = $validateForm->getData();
DHL::setConfigValue(
DHLConfigValue::TRACKING_URL,
$data["tracking_url"]
);
return $this->redirectToConfigurationPage();
} catch (FormValidationException $e) {
$error_message = $this->createStandardFormValidationErrorMessage($e);
}
if (null !== $error_message) {
$this->setupFormErrorContext(
'configuration',
$error_message,
$form
);
$response = $this->render("module-configure", ['module_code' => 'DHL']);
}
return $response;
}
/**
* Redirect to the configuration page
*/
protected function redirectToConfigurationPage()
{
return RedirectResponse::create(URL::getInstance()->absoluteUrl('/admin/module/DHL'));
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace DHL\Controller;
use DHL\DHL;
use DHL\Model\Config\DHLConfigValue;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Model\AreaQuery;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Tools\URL;
/**
* Class EditPrices
* @package DHL\Controller
* @author Thelia <info@thelia.net>
*/
class EditPrices extends BaseAdminController
{
public function editprices()
{
// Get data & treat
$post = $this->getRequest();
$operation = $post->get('operation');
$area = $post->get('area');
$weight = $post->get('weight');
$price = $post->get('price');
if (preg_match("#^add|delete$#", $operation) &&
preg_match("#^\d+$#", $area) &&
preg_match("#^\d+\.?\d*$#", $weight)
) {
// check if area exists in db
$exists = AreaQuery::create()
->findPK($area);
if ($exists !== null) {
if (null !== $data = DHL::getConfigValue(DHLConfigValue::PRICES, null)) {
$json_data = json_decode(
$data,
true
);
}
if ((float) $weight > 0 && $operation == "add"
&& preg_match("#\d+\.?\d*#", $price)) {
$json_data[$area]['slices'][$weight] = $price;
} elseif ($operation == "delete") {
if (isset($json_data[$area]['slices'][$weight])) {
unset($json_data[$area]['slices'][$weight]);
}
} else {
throw new \Exception("Weight must be superior to 0");
}
ksort($json_data[$area]['slices']);
DHL::setConfigValue(DHLConfigValue::PRICES, json_encode($json_data));
} else {
throw new \Exception("Area not found");
}
} else {
throw new \ErrorException("Arguments are missing or invalid");
}
return $this->redirectToConfigurationPage();
}
/**
* Redirect to the configuration page
*/
protected function redirectToConfigurationPage()
{
return RedirectResponse::create(URL::getInstance()->absoluteUrl('/admin/module/DHL'));
}
}