Commit du répertoire FedEx
This commit is contained in:
67
local/modules/FedEx/Controller/Configuration.php
Executable file
67
local/modules/FedEx/Controller/Configuration.php
Executable file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace FedEx\Controller;
|
||||
|
||||
use FedEx\FedEx;
|
||||
use FedEx\Model\Config\FedExConfigValue;
|
||||
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 FedEx\Controller
|
||||
* @author Laurent LE CORRE <laurent@thecoredev.fr>
|
||||
*/
|
||||
class Configuration extends BaseAdminController
|
||||
{
|
||||
public function editConfiguration()
|
||||
{
|
||||
if (null !== $response = $this->checkAuth(
|
||||
AdminResources::MODULE,
|
||||
[FedEx::DOMAIN_NAME],
|
||||
AccessManager::UPDATE
|
||||
)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$form = $this->createForm('fedex.configuration');
|
||||
$error_message = null;
|
||||
|
||||
try {
|
||||
$validateForm = $this->validateForm($form);
|
||||
$data = $validateForm->getData();
|
||||
|
||||
FedEx::setConfigValue(
|
||||
FedExConfigValue::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' => 'FedEx']);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect to the configuration page
|
||||
*/
|
||||
protected function redirectToConfigurationPage()
|
||||
{
|
||||
return RedirectResponse::create(URL::getInstance()->absoluteUrl('/admin/module/fedex'));
|
||||
}
|
||||
}
|
||||
74
local/modules/FedEx/Controller/EditPrices.php
Executable file
74
local/modules/FedEx/Controller/EditPrices.php
Executable file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace FedEx\Controller;
|
||||
|
||||
use FedEx\FedEx;
|
||||
use FedEx\Model\Config\FedExConfigValue;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Thelia\Model\AreaQuery;
|
||||
use Thelia\Controller\Admin\BaseAdminController;
|
||||
use Thelia\Tools\URL;
|
||||
|
||||
/**
|
||||
* Class EditPrices
|
||||
* @package FedEx\Controller
|
||||
* @author Laurent LE CORRE <laurent@thecoredev.fr>
|
||||
*/
|
||||
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 = FedEx::getConfigValue(FedExConfigValue::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']);
|
||||
|
||||
FedEx::setConfigValue(FedExConfigValue::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/fedex'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user