Days Ago Twig Filter

Here is a twig filter that converts a datetime object into minutes ago/days ago. Props to headzoo for the hard part.

[php]
new \Twig_Filter_Function(
‘\Application\CommentBundle\Twig\CommentExtension::daysAgoFilter’
)
);
}

public function getName()
{
return ‘CommentExtension';
}

public static function daysAgoFilter($date)
{
$display = array(‘Year’, ‘Month’, ‘Day’, ‘Hour’, ‘Minute’, ‘Second’);
$ago = ‘Ago';

$date = getdate($date->getTimestamp());
$current = getdate();
$p = array(‘year’, ‘mon’, ‘mday’, ‘hours’, ‘minutes’, ‘seconds’);
$factor = array(0, 12, 30, 24, 60, 60);

for ($i = 0; $i < 6; $i++) { if ($i > 0) {
$current[$p[$i]] += $current[$p[$i – 1]] * $factor[$i];
$date[$p[$i]] += $date[$p[$i – 1]] * $factor[$i];
}
if ($current[$p[$i]] – $date[$p[$i]] > 1) {
$value = $current[$p[$i]] – $date[$p[$i]];
return $value . ‘ ‘ . $display[$i] . (($value != 1) ? ‘s’ : ”) . ‘ ‘ . $ago;
}
}

return ‘just now';
}
}
[/php]

And of course, load the extension in your config

[php]
services:
comment.twig.extension:
class: Application\CommentBundle\Twig\CommentExtension
tags:
– { name: twig.extension }
[/php]

So long netbeans, hello vim!

I’ve finally had enough of using Netbeans. As my projects grew, Netbeans got slower and slower to the point where I was waiting a painfully long time for it to do rather unremarkable things. I don’t even want to know how much time I’ve wasted waiting for it to boot, create a file, or provide code hinting.

Throughout my e-ducation, vim has always come up as being an incredibly powerful editor that a lot of the well known programmers use. Of course, it was advertised to have a rather high learning curve. So I put vim on the backburner indefinitely because I’ve had enough on my plate as is learning Symfony2, JQuery, KnockoutJS, git, the list goes on.

My frustration with Netbeans finally led me to give vim a real chance a few days ago and I can see now what all the fuss is about. Obviously I’m nowhere close to learning all of the commands yet, but after going through the vimtutor a few times and forcing myself to use it exclusively, I am starting to get the hang of it. It’s absolutely the most nerdy way to edit text and I love it!

What I like most about vim is that there is a shortcut for everything. If there isn’t, you just make one. I’ve started to customize my .vimrc file to suit my style and am giddy with the thought of how much time it is going to save me in the future. And it damn well better because right now it owes me. Cheers to the long term!

A few sites that helped me get this far:
.vimrc example
vim cheatsheet
learn vim progressivel

Using the scss filter in assetic

Since there isn’t a whole lot of information on getting the scss filter to work in assetic I’ll quickly show you how I got it to work.

If you didn’t already know, assetic makes managing your css and javascript really clean and easy. The scss filter and the –watch option makes styling a site a lot more efficient and admittedly, rather enjoyable!

The biggest problem I encountered was configuring assetic with sass. Originally I was using rvm to manage ruby (i use ubuntu 11.10 btw) but there are some issues with assetic and rvm. see discussion. Instead, I removed rvm and just installed ruby-1.9.1 from source.
[php]
sudo apt-get install ruby1.9.1
[/php]
Next, install compass
[php]
sudo gem install compass
[/php]
For me the sass executable is installed in /usr/local/bin. Since this is different than the default location, my configuration looks like this:
[php]
//config.yml
# Assetic Configuration
assetic:
debug: ‘%kernel.debug%’
use_controller: false
sass: /usr/local/bin/sass
filters:
scss: ~
cssrewrite: ~
yui_css: { jar: ‘%kernel.root_dir%/Resources/java/yuicompressor-2.4.6.jar’ }
[/php]

Now in the head of your main layout just include the stylesheets you want assetic to manage.

[php]
// app/Resources/layout.html.twig
{% stylesheets output=’css/compiled/style.css’ filter=’scss, ?yui_css’
‘scss/common/*.scss’
‘jquery/css/common/*.css’
‘@ApplicationSiteBundle/Resources/public/scss/*.scss’
%}

{% endstylesheets %}
{% javascripts output=’js/compiled/script.js’ filter=’?yui_js’
‘jquery/js/common/*.js’
‘@ApplicationSiteBundle/Resources/public/js/*.js’
%}

{% endjavascripts %}
[/php]

Sometimes when you add new file to assetic it fails to dump it. Just clear the cache, dump the assets, and refresh the browser.

[php]
sudo rm -rf app/cache/*
[/php]
[php]
sudo chmod 777 app/cache -R
[/php]
[php]
app/console assetic:dump
[/php]

You can make assetic refresh upon any changes in real time by using the –watch option
[php]
app/console assetic:dump –watch
[/php]

Hope this helps someone out there!
Feedback appreciated.

Ubuntu 11.10 sqlite error

I was getting this error in the console after upgrading to 11.10.

PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib/php5/20090626/sqlite.so’ – /usr/lib/php5/20090626/sqlite.so: cannot open shared object file: No such file or directory in Unknown on line 0

run

sudo rm /etc/php5/conf.d/sqlite.ini

to solve your problem

more info here

https://bugs.launchpad.net/ubuntu/+source/php5/+bug/875262

Ubuntu 10.04 microphone fix

Here’s the solution I used to get my acer aspire microphone working for skype. I use Ubuntu 10.04 Lucid, for other version of Ubuntu click here

[php]
sudo add-apt-repository ppa:team-iquik/alsa;
sudo add-apt-repository ppa:ubuntu-audio-dev/ppa;
sudo apt-get update; sudo apt-get dist-upgrade;
sudo apt-get install linux-alsa-driver-modules-$(uname -r) –force-yes;
sudo apt-get –reinstall install alsa-base alsa-utils; killall pulseaudio; rm -r ~/.pulse*
[/php]

reboot

Adding JQuery UI Datepicker and timepicker to Symfony2 forms

I am a big fan of being able to customise form layout without editing each field individually.

Here I will show how to add a class attribute to a date and time field so that it can utilise the JQuery UI datepicker and timepicker. I also make use of the JQuery modal dialog for the form itself.

controller/PostController.php
[php]
container->get(‘doctrine.orm.entity_manager’);
$post = $em->getRepository(‘Application\BlogBundle\Entity\Post’)->find($id);
if (!$post) {
throw new NotFoundHttpException(“Post not found”);
} else {
return $post;
}
}

/**
* @Route(“/new”, name=”blog_post_new”)
* @Template()
*/
public function newAction()
{
$request = $this->container->get(‘request’);
$em = $this->container->get(‘doctrine.orm.entity_manager’);

$post = new Post();

$form = $this->container->get(‘form.factory’)->create(new PostType());

$form->setData($post);

if ($request->getMethod() == ‘POST’) {
$form->bindRequest($request);

if ($form->isValid()) {
$em->persist($post);
$em->flush();

$this->get(‘session’)->setFlash(‘notice’, ‘Post created!’);

return new RedirectResponse( $this->container->get(‘router’)->generate(‘blog_post_list’), 301);

}
}

return array(
‘ajax’ => $this->ajax,
‘form’ => $form->createView(),
‘post’ => $post,
);
}
}
[/php]

entity/Post.php
[php]
createdAt = new \DateTime(‘now’);
}

/** @orm\PreUpdate */
public function PreUpdate()
{
$this->updatedAt = new \DateTime(‘now’);
}

/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}

/**
* Set date
*
* @param string $date
*/
public function setDate($date)
{
$this->date = $date;
}

/**
* Get date
*
* @return string $date
*/
public function getDate()
{
return $this->date;
}

/**
* Set time
*
* @param string $time
*/
public function setTime($time)
{
$this->time = $time;
}

/**
* Get time
*
* @return string $time
*/
public function getTime()
{
return $this->time;
}

/**
* Set post
*
* @param string $post
*/
public function setPost($post)
{
$this->post = $post;
}

/**
* Get post
*
* @return string $post
*/
public function getPost()
{
return $this->post;
}

/**
* Set owner
*
* @param Application\UserBundle\Entity\User $owner
*/
public function setOwner(\Application\UserBundle\Entity\User $owner)
{
$this->owner = $owner;
}

/**
* Get owner
*
* @return Application\UserBundle\Entity\User $owner
*/
public function getOwner()
{
return $this->owner;
}
}
[/php]

form/Type/PostType.php
[php]
add(‘date’, ‘date’, array(
‘attr’ => array(‘class’ => ‘date’),
‘widget’ => ‘single_text’,
‘input’ => ‘string’,
‘format’ => ‘dd/MM/yy’, //\IntlDateFormatter::FULL
))
->add(‘time’, ‘time’, array(
‘attr’ => array(‘class’ => ‘time’),
‘widget’ => ‘single_text’,
‘input’ => ‘string’
))
->add(‘post’, ‘textarea’);
}
}
[/php]

Resources/views/Post/form.html.twig
[php]

{{ form_widget(form) }}
{{ form_rest(form) }}
{% if not ajax %}

{% endif %}

[/php]

web/js/common.js
[php]
// see http://jqueryui.com/demos/datepicker/” for more info
$(“form input.date”).datepicker({
dateFormat: ‘yy-mm-dd’
});

//see http://trentrichardson.com/examples/timepicker for more info
$(“form input.time”).timepicker({
stepMinute: 15,
ampm: true,
hourMin: 6,
hourMax: 20
});

$(“.formDialog”).dialog({
autoOpen: true,
minHeight: ‘325’,
minWidth: $(“.formDialog”).width(),
position: ‘center’,
modal: true,
show: ‘slide’,
hide: ‘slide’,
close: function() { $(this).dialog(“destroy”).remove();},
buttons: {
‘Save': function() {
disableSaveButton( $(this) );
showSpinner();
$(“.formDialog form”).submit();
},
‘Cancel': function() {
$(this).dialog(“close”);
}
}
});
[/php]
Screenshots



Feedback appreciated!