diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 1210c9949..07e0a1d07 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -124,6 +124,11 @@ + + + + + diff --git a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php index 738cb531d..5f311ad0e 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Session/Session.php +++ b/core/lib/Thelia/Core/HttpFoundation/Session/Session.php @@ -55,12 +55,15 @@ class Session extends BaseSession return $this; } + /** + * @return \Thelia\Model\Lang|null + */ public function getLang() { - return $this->get("lang", substr($this->getLocale(), 0, 2)); + return $this->get("lang"); } - public function setLang($lang) + public function setLang(Lang $lang) { $this->set("lang", $lang); diff --git a/core/lib/Thelia/Core/Template/Smarty/Exception/SmartyPluginException.php b/core/lib/Thelia/Core/Template/Smarty/Exception/SmartyPluginException.php new file mode 100644 index 000000000..fdaf93608 --- /dev/null +++ b/core/lib/Thelia/Core/Template/Smarty/Exception/SmartyPluginException.php @@ -0,0 +1,33 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Template\Smarty\Exception; + + +/** + * Class SmartyPluginException + * @package Thelia\Core\Template\Smarty\Exception + * @author Manuel Raynaud + */ +class SmartyPluginException extends \SmartyException +{} \ No newline at end of file diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php new file mode 100644 index 000000000..a095f214a --- /dev/null +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php @@ -0,0 +1,154 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Template\Smarty\Plugins; + +use Thelia\Core\HttpFoundation\Request; +use Thelia\Core\Template\Smarty\AbstractSmartyPlugin; +use Thelia\Core\Template\Smarty\Exception\SmartyPluginException; +use Thelia\Core\Template\Smarty\SmartyPluginDescriptor; + +/** + * + * format_date and format_date smarty function. + * + * Class Format + * @package Thelia\Core\Template\Smarty\Plugins + * @author Manuel Raynaud + */ +class Format extends AbstractSmartyPlugin +{ + protected $request; + + public function __construct(Request $request) + { + $this->request = $request; + } + + /** + * return date in expected format + * + * available parameters : + * date => DateTime object (mandatory) + * format => expected format + * output => list of default system format. Values available : + * date => date format + * time => time format + * datetime => datetime format (default) + * + * ex : + * {format_date date=$dateTimeObject format="Y-m-d H:i:s"} will output the format with specific format + * {format_date date=$dateTimeObject output="date"} will output the date using the default date system format + * {format_date date=$dateTimeObject} will output with the default datetime system format + * + * @param array $params + * @param null $template + * @throws \Thelia\Core\Template\Smarty\Exception\SmartyPluginException + * @return string + */ + public function formatDate($params, $template = null) + { + + if (array_key_exists("date", $params) === false) { + throw new SmartyPluginException("date is a mandatory parameter in format_date function"); + } + + $date = $params["date"]; + + if(!$date instanceof \DateTime) { + return ""; + } + + $format = null; + $output = array_key_exists("output", $params) ? $params["output"] : null; + + if (array_key_exists("format", $params)) { + $format = $params["format"]; + } else { + $session = $this->request->getSession(); + $lang = $session->getLang(); + + if($lang) { + switch ($output) { + case "date" : + $format = $lang->getDateFormat(); + break; + case "time" : + $format = $lang->getTimeFormat(); + break; + default: + case "datetime" : + $format = $lang->getDateTimeFormat(); + break; + } + } + } + + return $date->format($format); + + } + + /** + * + * display numbers in expected format + * + * available parameters : + * number => int or float number + * decimals => how many decimals format expected + * dec_point => separator for the decimal point + * thousands_sep => thousands separator + * + * ex : {format_number number="1246.12" decimals="1" dec_point="," thousands_sep=" "} will output "1 246,1" + * + * @param $params + * @param null $template + * @throws \Thelia\Core\Template\Smarty\Exception\SmartyPluginException + * @return string the expected number formatted + */ + public function formatNumber($params, $template = null) + { + if (array_key_exists("number", $params) === false) { + throw new SmartyPluginException("number is a mandatory parameter in format_number function"); + } + + $lang = $this->request->getSession()->getLang(); + + $number = $params["number"]; + $decimals = array_key_exists("decimals", $params) ? $params["decimals"] : $lang->getDecimals(); + $decPoint = array_key_exists("dec_point", $params) ? $params["dec_point"] : $lang->getDecimalSeparator(); + $thousandsSep = array_key_exists("thousands_sep", $params) ? $params["thousands_sep"] : $lang->getThousandsSeparator(); + + return number_format($number, $decimals, $decPoint, $thousandsSep); + } + + /** + * @return an array of SmartyPluginDescriptor + */ + public function getPluginDescriptors() + { + return array( + new SmartyPluginDescriptor("function", "format_date", $this, "formatDate"), + new SmartyPluginDescriptor("function", "format_number", $this, "formatNumber") + ); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/TheliaHttpKernel.php b/core/lib/Thelia/Core/TheliaHttpKernel.php index 94ab09f1b..7109f5ba2 100755 --- a/core/lib/Thelia/Core/TheliaHttpKernel.php +++ b/core/lib/Thelia/Core/TheliaHttpKernel.php @@ -126,7 +126,7 @@ class TheliaHttpKernel extends HttpKernel if ($lang) { $request->getSession() - ->setLang($lang->getCode()) + ->setLang($lang) ->setLocale($lang->getLocale()) ; } diff --git a/core/lib/Thelia/Model/Lang.php b/core/lib/Thelia/Model/Lang.php index 3339dec64..654c89719 100755 --- a/core/lib/Thelia/Model/Lang.php +++ b/core/lib/Thelia/Model/Lang.php @@ -24,15 +24,33 @@ class Lang extends BaseLang { return $default_lang; } - public function getDateFormat() { + public function getDateFormat() + { return "d/m/Y"; } - public function getTimeFormat() { + public function getTimeFormat() + { return "H:i:s"; } - public function getDateTimeFormat() { + public function getDateTimeFormat() + { return "d/m/Y H:i:s"; } + + public function getDecimalSeparator() + { + return "."; + } + + public function getThousandsSeparator() + { + return " "; + } + + public function getDecimals() + { + return 2; + } } diff --git a/core/lib/Thelia/Tests/Core/Template/Smarty/Plugins/FormatTest.php b/core/lib/Thelia/Tests/Core/Template/Smarty/Plugins/FormatTest.php new file mode 100644 index 000000000..38c00b55f --- /dev/null +++ b/core/lib/Thelia/Tests/Core/Template/Smarty/Plugins/FormatTest.php @@ -0,0 +1,274 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Tests\Core\Smarty\Plugins; + +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Thelia\Core\HttpFoundation\Request; +use Thelia\Core\HttpFoundation\Session\Session; +use Thelia\Core\Template\Smarty\Plugins\Format; + +/** + * @coversDefaultClass \Thelia\Core\Template\Smarty\Plugins\Format + */ +class FormatTest extends \PHPUnit_Framework_TestCase +{ + protected $request; + + public function setUp() + { + $this->request = new Request(); + + $this->request->setSession(new Session(new MockArraySessionStorage())); + } + + /** + * + * test formatDate method with expected format + * + * @covers ::formatDate + */ + public function testFormatDateWithSpecificFormat() + { + $dateTime = new \DateTime(); + $format = "Y-m-d H:i:s"; + + $formatClass = new Format($this->request); + + $render = $formatClass->formatDate(array( + "date" => $dateTime, + "format" => $format + )); + + $this->assertEquals($dateTime->format($format), $render); + } + + /** + * + * test formatDate method with date default format + * + * @covers ::formatDate + */ + public function testFormatDateWithDefaultSessionParam() + { + $dateTime = new \DateTime(); + + $langMock = $this->getLangMock(); + $this->request->getSession()->setLang($langMock); + + $formatClass = new Format($this->request); + + $render = $formatClass->formatDate(array("date" => $dateTime)); + + $this->assertEquals($dateTime->format("Y-m-d H:i:s"), $render); + } + + /** + * + * test formatDate method with time default format + * + * @covers ::formatDate + */ + public function testFormatDateWithDateSessionParam() + { + $dateTime = new \DateTime(); + + $langMock = $this->getLangMock(); + $this->request->getSession()->setLang($langMock); + + $formatClass = new Format($this->request); + + $render = $formatClass->formatDate(array( + "date" => $dateTime, + "output" => "date" + )); + + $this->assertEquals($dateTime->format("Y-m-d"), $render); + } + + /** + * + * test formatDate method with datetime default format + * + * @covers ::formatDate + */ + public function testFormatDateWithTimeSessionParam() + { + $dateTime = new \DateTime(); + + $langMock = $this->getLangMock(); + $this->request->getSession()->setLang($langMock); + + $formatClass = new Format($this->request); + + $render = $formatClass->formatDate(array( + "date" => $dateTime, + "output" => "time" + )); + + $this->assertEquals($dateTime->format("H:i:s"), $render); + } + + /** + * + * test formatDate method without output or expected format. datetime format must be return + * + * @covers ::formatDate + */ + public function testFormatDateWithDateTimeSessionParam() + { + $dateTime = new \DateTime(); + + $langMock = $this->getLangMock(); + $this->request->getSession()->setLang($langMock); + + $formatClass = new Format($this->request); + + $render = $formatClass->formatDate(array( + "date" => $dateTime, + "output" => "datetime" + )); + + $this->assertEquals($dateTime->format("Y-m-d H:i:s"), $render); + } + + /** + * test formatDate without mandatory parameters + * + * @covers ::formatDate + * @expectedException \Thelia\Core\Template\Smarty\Exception\SmartyPluginException + */ + public function testFormatDateWithoutDate() + { + $dateTime = new \DateTime(); + + $formatClass = new Format($this->request); + + $render = $formatClass->formatDate(array()); + + $this->assertEquals($dateTime->format("Y-m-d H:i:s"), $render); + } + + + /** + * test formatNumber without mandatory parameters + * + * @covers ::formatNumber + * @expectedException \Thelia\Core\Template\Smarty\Exception\SmartyPluginException + */ + public function testFormatNumberWithoutParams() + { + $formatClass = new Format($this->request); + + $render = $formatClass->formatNumber(array()); + } + + /** + * test formatDate specifying all parameters + * + * @covers ::formatNumber + */ + public function testFormatNumberWithAllParams() + { + $formatClass = new Format($this->request); + + $number = 1256.12; + $decimals = 1; + $decPoint = ","; + $thousandsSep = " "; + + $render = $formatClass->formatNumber(array( + "number" => $number, + "decimals" => $decimals, + "dec_point" => $decPoint, + "thousands_sep" => $thousandsSep + )); + + $this->assertEquals($render, "1 256,1"); + } + + /** + * @covers ::formatNumber + */ + public function testFormatNumberWithDefaultParameters() + { + $number = 1234.56; + $langMock = $this->getLangMock(); + $this->request->getSession()->setLang($langMock); + + $formatClass = new Format($this->request); + + $render = $formatClass->formatNumber(array( + "number" => $number + )); + + $this->assertEquals( $render, number_format($number, 2, ",", " ")); + } + + /** + * create a mock for Thelia\Model\Lang class + * @return \Thelia\Model\Lang instance + */ + public function getLangMock() + { + $mock = $this->getMock( + "Thelia\Model\Lang", + array( + "getDateFormat", + "getTimeFormat", + "getDateTimeFormat", + "getDecimalSeparator", + "getThousandsSeparator", + "getDecimals" + ) + ); + + $mock->expects($this->any()) + ->method("getDateFormat") + ->will($this->returnValue("Y-m-d")); + + $mock->expects($this->any()) + ->method("getTimeFormat") + ->will($this->returnValue("H:i:s")); + + $mock->expects($this->any()) + ->method("getDateTimeFormat") + ->will($this->returnValue("Y-m-d H:i:s")); + + $mock->expects($this->any()) + ->method("getDecimals") + ->will($this->returnValue(2)); + + $mock->expects($this->any()) + ->method("getDecimalSeparator") + ->will($this->returnValue(",")); + + $mock->expects($this->any()) + ->method("getThousandsSeparator") + ->will($this->returnValue(" ")); + + return $mock; + } + + + +} \ No newline at end of file diff --git a/reset_install.sh b/reset_install.sh index f3a635a9b..5c51ac60c 100755 --- a/reset_install.sh +++ b/reset_install.sh @@ -9,7 +9,7 @@ if [ ! -f local/config/database.yml ]; then echo "[FAILED] Please add your database informations in local/config/database.yml and start this script again." else echo -e "\n\e[01;34m[INFO] Downloading vendors\e[00m\n" - php composer install --prefer-dist --no-dev + composer install cd local/config/