Create a gist now

Instantly share code, notes, and snippets.

<?php
namespace SOTB\InvoiceBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Invoice form type.
*
* @author Matt Drollette <matt@drollette.com>
*/
class InvoiceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('dateOfIssue', 'date', array(
'data_timezone' => 'UTC',
'user_timezone' => $options['user_timezone']
))
->add('terms', 'textarea', array(
'required' => false
))
->add('visibleNotes', 'textarea', array(
'required' => false
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults(array(
'data_class' => 'SOTB\CoreBundle\Document\Invoice',
'user_timezone' => 'UTC'
));
}
public function getName()
{
return 'sotb_invoice_type';
}
}
<?php
namespace SOTB\CoreBundle\Twig\Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @author Matt Drollette <matt@drollette.com>
*/
class LocaleExtension extends \Twig_Extension
{
protected $container;
protected $timezone;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->timezone = $this->container->get('session')->get('timezone', 'UTC');
}
public function getFunctions()
{
return array();
}
public function getFilters()
{
return array(
'datetime' => new \Twig_Filter_Method($this, 'formatDatetime', array('is_safe' => array('html'))),
);
}
public function formatDatetime($date, $timezone = null)
{
if (null === $timezone) {
$timezone = $this->timezone;
}
if (!$date instanceof \DateTime) {
if (ctype_digit((string) $date)) {
$date = new \DateTime('@'.$date);
} else {
$date = new \DateTime($date);
}
}
if (!$timezone instanceof \DateTimeZone) {
$timezone = new \DateTimeZone($timezone);
}
$date->setTimezone($timezone);
return $date;
}
public function getName()
{
return 'sotb_core_locale';
}
}
<?php
namespace SOTB\CoreBundle\EventListener;
/**
* @author Matt Drollette <matt@drollette.com>
*/
class LocaleListener
{
protected $container;
public function __construct($container)
{
$this->container = $container;
}
public function onKernelRequest($request)
{
$locales = array();
$request = $request->getRequest();
if ($request) {
$locales[] = $request->getLocale();
}
if ($request->server->has('HTTP_ACCEPT_LANGUAGE')) {
$locales[] = \Locale::acceptFromHttp($request->server->get('HTTP_ACCEPT_LANGUAGE'));
}
foreach ($locales as $locale) {
if (!empty($locale)) {
if ($request) {
$request->setLocale($locale);
}
\Locale::setDefault($locale);
break;
}
}
}
}
<?php
namespace SOTB\CoreBundle\EventListener;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Session\Session;
/**
* @author Matt Drollette <matt@drollette.com>
*/
class SecurityListener
{
protected $security;
protected $session;
/**
* Constructs a new instance of SecurityListener.
*
* @param SecurityContext $security The security context
* @param Session $session The session
*/
public function __construct(SecurityContext $security, Session $session)
{
$this->security = $security;
$this->session = $session;
}
/**
* Invoked after a successful login.
*
* @param InteractiveLoginEvent $event The event
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$timezone = $this->security->getToken()->getUser()->getTimezone();
if (empty($timezone)) {
$timezone = 'UTC';
}
$this->session->set('timezone', $timezone);
}
}
<service id="sotb_core.twig.extension" class="SOTB\CoreBundle\Twig\Extension\LocaleExtension">
<tag name="twig.extension"/>
<argument type="service" id="service_container"/>
</service>
<service id="sotb_core.listener.login" class="SOTB\CoreBundle\EventListener\SecurityListener">
<tag name="kernel.event_listener" event="security.interactive_login" method="onSecurityInteractiveLogin"/>
<argument type="service" id="security.context"/>
<argument type="service" id="session"/>
</service>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment