Files
sterivein/local/modules/Colissimo/Controller/EditPrices.php
Benjamin Perche d00bc84cd5 Add dynamical prices editing in Colissimo
modified:   local/modules/Colissimo/AdminIncludes/module_configuration.html
	modified:   local/modules/Colissimo/Colissimo.php
	modified:   local/modules/Colissimo/Config/config.xml
	modified:   local/modules/Colissimo/Config/module.xml
	modified:   local/modules/Colissimo/Config/prices.json
	new file:   local/modules/Colissimo/Config/routing.xml
	modified:   local/modules/Colissimo/Config/thelia.sql
	new file:   local/modules/Colissimo/Controller/EditPrices.php
	new file:   local/modules/Colissimo/I18n/en_US.php
	new file:   local/modules/Colissimo/I18n/fr_FR.php
	modified:   local/modules/Colissimo/Listener/SendMail.php
	new file:   local/modules/Colissimo/Loop/CheckRightsLoop.php
	modified:   local/modules/Colissimo/Loop/Price.php
	modified:   local/modules/Colissimo/documentation/TarifsAvril2013.pdf
	modified:   local/modules/Colissimo/documentation/readme.txt
2014-02-19 09:51:22 +01:00

91 lines
4.5 KiB
PHP
Executable File

<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Colissimo\Controller;
use Colissimo\Colissimo;
use Thelia\Model\AreaQuery;
use Thelia\Controller\Admin\BaseAdminController;
/**
* Class EditPrices
* @package Colissimo\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) {
$json_path= __DIR__."/../".Colissimo::JSON_PRICE_RESOURCE;
if (is_readable($json_path)) {
$json_data = json_decode(file_get_contents($json_path),true);
} elseif(!file_exists($json_path)) {
$json_data = array();
} else {
throw new \Exception("Can't read Colissimo".Colissimo::JSON_PRICE_RESOURCE.". Please change the rights on the file.");
}
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']);
if ((file_exists($json_path) ?is_writable($json_path):is_writable(__DIR__."/../"))) {
$file = fopen($json_path, 'w');
fwrite($file, json_encode($json_data));;
fclose($file);
} else {
throw new \Exception("Can't write Colissimo".Colissimo::JSON_PRICE_RESOURCE.". Please change the rights on the file.");
}
} else {
throw new \Exception("Area not found");
}
} else {
throw new \ErrorException("Arguments are missing or invalid");
}
return $this->redirectToRoute("admin.module.configure",array(),
array ( 'module_code'=>"Colissimo",
'_controller' => 'Thelia\\Controller\\Admin\\ModuleController::configureAction'));
}
}