Ca avance pas mal sur DHL...

This commit is contained in:
2020-12-04 20:59:52 +01:00
parent 2453c207aa
commit 9572b71f14
12 changed files with 2518 additions and 558 deletions

View File

@@ -2,10 +2,15 @@
namespace DHL\Controller;
use DHL\DHL;
use DHL\Model\Config\DHLConfigValue;
use DHL\Model\Map\DHLDeliveryPriceTableMap;
use DHL\Model\DHLDeliveryPrice;
use DHL\Model\DHLDeliveryPriceQuery;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Propel;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Model\AreaQuery;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Log\Tlog;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Tools\URL;
@@ -16,54 +21,6 @@ use Thelia\Tools\URL;
*/
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
*/
@@ -71,4 +28,95 @@ class EditPrices extends BaseAdminController
{
return RedirectResponse::create(URL::getInstance()->absoluteUrl('/admin/module/DHL'));
}
public function editprices()
{
// Get data & treat
$post = $this->getRequest();
$operation = $post->get('operation');
$area = $post->get('area');
$module_id = $post->get('module_id');
$weight = $post->get('weight');
$price = $post->get('price');
$price_id = $post->get('price_id');
$con = Propel::getServiceContainer()->getReadConnection(DHLDeliveryPriceTableMap::DATABASE_NAME);
switch ($operation) {
case "add" :
$this->createAction($con, $module_id, $area, $weight, $price);
break;
case "update" :
$this->updateAction($con, $module_id, $price_id, $price);
break;
case "delete" :
$this->deleteAction($con, $module_id, $price_id);
break;
}
return $this->redirectToConfigurationPage();
}
public function updateAction(ConnectionInterface $con, $moduleId, $priceId, $price)
{
if (null !== $response = $this->checkAuth(AdminResources::MODULE, 'DHL', AccessManager::UPDATE)) {
return $response;
}
$errorMessage = false;
try {
DHLDeliveryPriceQuery::create()->filterById($priceId)
->update(['PriceWithTax' => $price],
$con);
} catch (\Exception $ex) {
$errorMessage = $ex->getMessage();
Tlog::getInstance()->error("Failed to update price : $errorMessage");
}
return $this->render('module_configuration', [
'module_id' => $moduleId,
'error_message' => $errorMessage
]);
}
public function createAction(ConnectionInterface $con, $moduleId, $areaId, $weight, $price)
{
if (null !== $response = $this->checkAuth(AdminResources::MODULE, 'DHL', AccessManager::UPDATE)) {
return $response;
}
$errorMessage = false;
try {
(new DHLDeliveryPrice())
->setAreaId($areaId)
->setPriceWithTax($price)
->setMaxWeight($weight)
->save();
} catch (\Exception $ex) {
$errorMessage = $ex->getMessage();
Tlog::getInstance()->error("Failed to add price : $errorMessage");
}
return $this->render('module_configuration', [
'module_id' => $moduleId,
'error_message' => $errorMessage
]);
}
public function deleteAction(ConnectionInterface $con, $moduleId, $priceId)
{
if (null !== $response = $this->checkAuth(AdminResources::MODULE, 'DHL', AccessManager::DELETE)) {
return $response;
}
DHLDeliveryPriceQuery::create()->filterById($priceId)->delete();
return $this->render('module_configuration', [ 'module_id' => $moduleId ]);
}
}