create new smarty function for displaying date in expected format
This commit is contained in:
@@ -121,6 +121,11 @@
|
||||
<tag name="thelia.parser.register_plugin"/>
|
||||
</service>
|
||||
|
||||
<service id="smarty.plugin.format" class="Thelia\Core\Template\Smarty\Plugins\Format" scope="request">
|
||||
<argument type="service" id="request"/>
|
||||
<tag name="thelia.parser.register_plugin"/>
|
||||
</service>
|
||||
|
||||
<service id="smarty.plugin.thelialoop" class="Thelia\Core\Template\Smarty\Plugins\TheliaLoop" scope="request">
|
||||
<tag name="thelia.parser.register_plugin"/>
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
94
core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php
Normal file
94
core/lib/Thelia/Core/Template/Smarty/Plugins/Format.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?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 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 <mraynaud@openstudio.fr>
|
||||
*/
|
||||
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")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -126,7 +126,7 @@ class TheliaHttpKernel extends HttpKernel
|
||||
|
||||
if ($lang) {
|
||||
$request->getSession()
|
||||
->setLang($lang->getCode())
|
||||
->setLang($lang)
|
||||
->setLocale($lang->getLocale())
|
||||
;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
<?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 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);
|
||||
}
|
||||
|
||||
}
|
||||
2
install/sqldb.map
Normal file
2
install/sqldb.map
Normal file
@@ -0,0 +1,2 @@
|
||||
# Sqlfile -> Database map
|
||||
thelia.sql=thelia
|
||||
@@ -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/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user