Files
domokits/local/modules/DHL/Controller/EditPrices.php

123 lines
3.7 KiB
PHP

<?php
namespace DHL\Controller;
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\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Log\Tlog;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Tools\URL;
/**
* Class EditPrices
* @package DHL\Controller
* @author Thelia <info@thelia.net>
*/
class EditPrices extends BaseAdminController
{
/**
* Redirect to the configuration page
*/
protected function redirectToConfigurationPage()
{
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 ]);
}
}