From eeea885543c86419bbba2c241cc46f86d8f0696e Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 2 Sep 2013 23:36:34 +0200 Subject: [PATCH] create new smarty function for displaying date in expected format --- core/lib/Thelia/Config/Resources/config.xml | 5 + .../Core/HttpFoundation/Session/Session.php | 7 +- .../Core/Template/Smarty/Plugins/Format.php | 94 +++++++++++++++++++ core/lib/Thelia/Core/TheliaHttpKernel.php | 2 +- core/lib/Thelia/Model/Lang.php | 24 ++++- .../Template/Smarty/Plugins/FormatTest.php | 58 ++++++++++++ install/sqldb.map | 2 + reset_install.sh | 2 +- 8 files changed, 187 insertions(+), 7 deletions(-) create mode 100644 core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php create mode 100644 core/lib/Thelia/Tests/Core/Template/Smarty/Plugins/FormatTest.php create mode 100644 install/sqldb.map diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 733e4303a..4c1bf61f8 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -121,6 +121,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/Plugins/Format.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php new file mode 100644 index 000000000..c2fe1652d --- /dev/null +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php @@ -0,0 +1,94 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Template\Smarty\Plugins; + +use Thelia\Core\HttpFoundation\Request; +use Thelia\Core\Template\Smarty\AbstractSmartyPlugin; +use Thelia\Core\Template\Smarty\SmartyPluginDescriptor; + +/** + * 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; + } + + /** + * @param $params + * @param $smarty + */ + public function formatDate($params, &$smarty = null) + { + $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); + + } + + /** + * @return an array of SmartyPluginDescriptor + */ + public function getPluginDescriptors() + { + return array( + new SmartyPluginDescriptor("function", "format_date", $this, "formatDate") + ); + } +} \ 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..cb89b53e1 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 getThousandSeparator() + { + 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..439ab7ee0 --- /dev/null +++ b/core/lib/Thelia/Tests/Core/Template/Smarty/Plugins/FormatTest.php @@ -0,0 +1,58 @@ +. */ +/* */ +/*************************************************************************************/ +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; + +class FormatTest extends \PHPUnit_Framework_TestCase +{ + protected $request; + protected $session; + + public function setUp() + { + $this->session = new Session(new MockArraySessionStorage()); + $this->request = new Request(); + + $this->request->setSession($this->session); + } + + 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); + } + +} \ No newline at end of file diff --git a/install/sqldb.map b/install/sqldb.map new file mode 100644 index 000000000..63a93baa8 --- /dev/null +++ b/install/sqldb.map @@ -0,0 +1,2 @@ +# Sqlfile -> Database map +thelia.sql=thelia 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/