Monday, October 29, 2012

Using Symfony2 with Backbone.js

Symfony2 with Backbone.js
Inspired by some of the Symfony Live San Francisco talks , I decided to create this bundle and become a better OSS contributor.

This bundle allows you to easily start a backbone.js project with Symfony2, it creates a basic structure for your backbone files and also includes a generator to scaffold your backbone classes.

All the installation steps can be found in the README: https://github.com/gigo6000/DevtimeBackboneBundle

A simple Raffler app created with this bundle can be found here: https://github.com/gigo6000/DevtimeRafflerBundle


Wednesday, November 09, 2011

Symfony2 FOSTwitterBundle TwitterProvider

Symfony2 and Twittter


If you are trying to integrate Symfony2 with Twitter, you're probably trying to install FOSTwitterBundle, and you may have faced two problems, one is that you can't create a user session after you login with Twitter and the other one is that there is no TwitterProvider class available as example in the instructions.

To help you with the second one here is a TwitterProvider class I made based on FacabookProvider.php



To solve the authentication problem on Symfony what I found is that Twitter anywhere only works at client level, so I decided to create a custom login button and built the authorization URL.

Controller action:



Twig template:



Then after the user authenticates with Twitter, the custom TwitterProvider can get the authentication tokens from the session and authenticate the user in Symfony.

Tuesday, June 28, 2011

Capifony: Send an email after deployment


First, thanks to @cordoval at the #symfony channel for suggesting me to use a symfony2 command to do this.

To use this command you must have the SwiftmailerBundle enabled. More info here.

This is a simple Symfony2 command to send an email, you should add it to a "Command" directory inside your bundle and replace the namespace to fit your project bundle:





The email body is just "test" but you can probably modified it you need to.


Test the new command by running the following:

php app/console capifony:sendemail emailfrom@server.com emailto@server.com subject


Finally, this task will run the capifony:sendemail command after the deployment is complete, be sure to add it to your Capifony script:




Want to learn more about Symfony2 console/command-line commands?

http://symfony.com/doc/current/cookbook/console.html


http://www.craftitonline.com/2011/06/calling-commands-within-commands-in-symfony2/

Sunday, June 19, 2011

How to enable the twig truncate filter (Text extension) in Symfony2 beta5


Problem ?


In previous versions of Symfony2, to enable a extension you needed to add the Text and/or Debug extensions to an "extensions" array in your config.yml

//app/config/config.yml
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
extensions:
- twig.extension.debug
- twig.extension.text



This was because the extensions were added to the twig.xml file


//vendor/symfony/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
...
<service id="twig.extension.text" class="Twig_Extensions_Extension_Text" public="false">

<service id="twig.extension.debug" class="Twig_Extensions_Extension_Debug" public="false">
...




But, this is the way to do it now :

// app/config/config.yml
...
services:
twig.extension.text:
class: Twig_Extensions_Extension_Text
tags:
- { name: twig.extension }


I spent some time trying to figure out how to do it, so I hope it helps someone.

Monday, May 23, 2011

Symfony2 + Twig pagination class

I was looking for some pagination class to use with Symfony2, but most of the scripts that I found included html in the class, after some googling I found this post, and it was very helpful to get what I was looking for. I wanted something like the old digg pagination so I modified this class a little bit to get something like this:





The modifications I made:

- Show a range of pages around the current page as in the old digg.
- Parameters can be passed in a route, so you get a nice URL.



This is the class after the modifications:

<?php
/**
* Class to paginate a list of items in a old digg style
*
* @author Darko Gole%u0161
* @author Carlos Mafla <gigo6000@hotmail.com>
* @www.inchoo.net
*/
namespace Paginator;

class Paginator {

//current displayed page
protected $currentpage;
//limit items on one page
protected $limit;
//total number of pages that will be generated
protected $numpages;
//total items loaded from database
protected $itemscount;
//starting item number to be shown on page
protected $offset;
//pages to show at left and right of current page
protected $mid_range;
//range initial page
protected $start_range;
//range end page
protected $end_range;


function __construct($itemscount, $currentpage = 1,$limit = 20,$mid_range = 7) {
//set total items count from controller
$this->itemscount = $itemscount;

$this->currentpage = $currentpage;

$this->limit = $limit;

$this->mid_range= $mid_range;

//Set defaults
$this->setDefaults();

//Calculate number of pages total
$this->getInternalNumPages();
//Calculate first shown item on current page
$this->calculateOffset();


$this->calculateRange();

}


private function calculateRange() {

$this->start_range = $this->currentpage - floor($this->mid_range/2);
$this->end_range = $this->currentpage floor($this->mid_range/2);

if($this->start_range <= 0)
{
$this->end_range = abs($this->start_range) 1;
$this->start_range = 1;
}
if($this->end_range > $this->numpages)
{
$this->start_range -= $this->end_range-$this->numpages;
$this->end_range = $this->numpages;
}
$this->range = range($this->start_range,$this->end_range);


}

private function setDefaults() {
//If currentpage is set to null or is set to 0 or less
//set it to default (1)
if (($this->currentpage == null) || ($this->currentpage < 1)) {
$this->currentpage = 1;
}
//if limit is set to null set it to default (20)
if (($this->limit == null)) {
$this->limit = 20;
//if limit is any number less than 1 then set it to 0 for displaying
//items without limit
} else if ($this->limit < 1) {
$this->limit = 0;
}
}

public function getNumpages() {
return $this->numpages;
}

private function getInternalNumPages() {
//If limit is set to 0 or set to number bigger then total items count
//display all in one page
if (($this->limit < 1) || ($this->limit > $this->itemscount)) {
$this->numpages = 1;
} else {
//Calculate rest numbers from dividing operation so we can add one
//more page for this items
$restItemsNum = $this->itemscount % $this->limit;
//if rest items > 0 then add one more page else just divide items
//by limit
$restItemsNum > 0 ? $this->numpages = intval($this->itemscount / $this->limit) 1 : $this->numpages = intval($this->itemscount / $this->limit);
}
}



private function calculateOffset() {
//Calculet offset for items based on current page number
$this->offset = ($this->currentpage - 1) * $this->limit;
}

public function getCurrentpage() {
return $this->currentpage;
}

public function getCurrentUrl() {
return $this->currentUrl;
}

//For using from controller
public function getLimit() {
return $this->limit;
}
//For using from controller
public function getOffset() {
return $this->offset;
}

public function getRange()
{
return $this->range;
}

public function getMidRange()
{
return $this->mid_range;
}

}







And this is how you can implement it in the controller:

use Path\to\class\Paginator;

class ListController extends SiteController
{
/**
* @extra:Route("/{offset}", name="_items")
* @extra:Template()
*/
public function listAction( $offset = 1)
{
$repository = new ListRepository();

$limit = 20;
$midrange = 7;

$itemsCount = $repository->getListCount();


$paginator = new Paginator($itemsCount, $offset , $limit, $midrange);

$items = $repository->getList ($offset);

.
.
.
return array('items' => $items, 'paginator' => $paginator);
}
}



And this is an example of a twig template you can use:


<div class="paginator">
<ul>
{% if paginator.currentpage != 1 %}
<li> <a class="previous" href="{{ path('_items', { 'offset': paginator.currentpage-1 }) }}">Previous</a>

{% endif %}
{% for i in 1..paginator.numpages%}

{% if paginator.range.0 > 2 and i == paginator.range.0 %}
...
{% endif %}


{% if(i==1 or i==paginator.numpages or i in paginator.range) %}

{% if i==paginator.currentpage %}
<li><a class="active" href="{{ path('_items', { 'offset': i })}}">{{i}}</a></li>
{% else %}
<li><a href="{{ path('_items', { 'offset': i }) }}"> {{i}}</a></li>
{% endif %}
{% endif %}

{% if paginator.range[paginator.midrange -1] < paginator.numpages -1 and i == paginator.range[paginator.midrange-1] %}
...
{% endif %}

{% endfor %}


<li> <a class="next" href="{{ path('_items', { 'offset': paginator.currentpage 1 }) }}">Next</a>
</ul>
</div>






I'm sure there are many improvements to be made, but it may help someone to have an idea.

You can find the files on git:

https://github.com/gigo6000/Symfony2-Pagination-Class

Thursday, May 12, 2011

Wednesday, February 09, 2011

Symfony live 2011 interesting links - Symfony2 (day 2)

Fabien Potencier presentations:

http://www.slideshare.net/fabpot

Symfony2 from the trenches:

http://www.slideshare.net/jwage/symfony2-from-the-trenches



Being dangerous with Twig:


http://www.slideshare.net/weaverryan/being-dangerous-with-twig


Comment tag (do nothing): {# Comment #}

Print tag (show something): {{ 'print me' }}

Block tag (do something): {% set foo = 'hola' %}


Introducing Assetic: Asset Management for PHP 5.3:

Tuesday, February 08, 2011

Symfony live 2011 (Symfony2) interesting links

Twitter tag #sflive2011:

https://twitter.com/#!/search?q=%23sflive2011


Slides for the first presentation of the day, The Path to Symfony in the USA:

http://www.slideshare.net/dustin.whittle/the-path-to-symfony-in-the-usa


Symfony2 bundle repository:

http://www.symfony2bundles.org/

Symfony Questions / Anwers :

http://symfonyexperts.com/


Apostrophe presentation:

http://www.slideshare.net/tompunk/apostrophe-6853364

Symfony Live blogging:

http://window.punkave.com/2011/02/08/liveblogging-symfony-live/


Doctine in the real world (opensky) :

http://www.slideshare.net/jwage/doctrine-intherealworldsf-live2011sanfran

phpBB4 building end user applications with Symfony2:

http://prezi.com/gxrdwsplwplf/phpbb4-building-end-user-applications-with-symfony2/

Monday, August 30, 2010

PHP 5.3.3 short tags problem when upgrading

Recently after upgrading php to 5.3.3 I found a problem with short tags <?= and <? , it seems that you now must only use <?php as starting tag, and all my code with <?= was not processed by php.


Short tags looks nicer and cleaner but can be a problem for people using XML, find more info here:

http://wiki.php.net/rfc/shortags

Here is a useful command if you need to replace this tags in your old code:


grep -oiR '<?=' /path/to/dir | cut -d ":" -f 1 | xargs sed -i 's/<?=/<?php echo /g'

Thursday, September 03, 2009

How to resize or scale a list of images on linux

Here is a simple but powerful command when you got several images that you want to shrink:


mogrify -scale 50% *.jpg


This will reduce all the images by 50%


Or if you just want to reduce one image to 200x200 pixels for example:



convert -scale 200x200 image.jpg image_small.jpg

Friday, August 21, 2009

Installing virtualbox on Ubuntu 9.04


# virtualbox
WARNING: The character device /dev/vboxdrv does not exist.
Please install the virtualbox-ose-source package and the appropriate
headers, most likely linux-headers-generic.

You will not be able to start VMs until this problem is fixed.



root@ubuntu-laptop:/home/camafla# apt-get install linux-headers-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
linux-headers-2.6.28-11-server
The following NEW packages will be installed:
linux-headers-2.6.28-11-server linux-headers-server
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 672kB of archives.
After this operation, 8139kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://archive.ubuntu.com jaunty/main linux-headers-2.6.28-11-server 2.6.28-11.42 [669kB]
Get:2 http://archive.ubuntu.com jaunty/main linux-headers-server 2.6.28.11.15 [3388B]
Fetched 672kB in 6s (104kB/s)
Selecting previously deselected package linux-headers-2.6.28-11-server.
(Reading database ... 157949 files and directories currently installed.)
Unpacking linux-headers-2.6.28-11-server (from .../linux-headers-2.6.28-11-server_2.6.28-11.42_i386.deb) ...
Selecting previously deselected package linux-headers-server.
Unpacking linux-headers-server (from .../linux-headers-server_2.6.28.11.15_i386.deb) ...
Setting up linux-headers-2.6.28-11-server (2.6.28-11.42) ...
Examining /etc/kernel/header_postinst.d.
run-parts: executing /etc/kernel/header_postinst.d/dkms
* Running DKMS auto installation service for kernel 2.6.28-11-server
* vboxdrv (2.1.4)... vboxdrv (2.1.4): Installing module.
.............
......
[ OK ]
* vboxnetflt (2.1.4)... vboxnetflt (2.1.4): Installing module.
............
......
[ OK ]
run-parts: executing /etc/kernel/header_postinst.d/nvidia-common

Setting up linux-headers-server (2.6.28.11.15) ...

Monday, June 08, 2009

LPI 102 exam reference card (or cheat sheet if you like)


I recently cleared the LPIC-1 certification (LPI certification Level 1) , as an educational exercise to study for my second exam (exam 102) I created a reference card that helped me remember some commands and key concepts for the exam.

I want to share this to anyone interested in taking this exam or just anyone who wants a quick linux reference card to the objectives included in the exam.

Download

PNG version: lpi-102-reference-card-v1.png

PDF version: lpi-102-reference-card-v1.pdf

Wednesday, January 28, 2009

Installing Oracle OCI8 and PHP

I will describe here the steps I needed to install oracle connectivity for php. It's supposed you have php installed, in my case
I had php 5.1.6 throught RPM:

# rpm -qa | grep php
php-5.1.6-20.el5_2.1
php-adodb-4.81-1.el5.rf
php-pecl-mailparse-2.1.1-1.el5.rf
phpmyadmin-2.11.8.1-1.el5.rf
php-odbc-5.1.6-20.el5_2.1
php-pecl-fileinfo-1.0.4-1.el5.rf
php-cli-5.1.6-20.el5_2.1
php-pear-file-1.2.2-1.el5.rf
php-eaccelerator-5.1.6_0.9.5.2-4.el5.rf
php-pecl-memcache-2.1.2-1.el5.rf
php-pear-db-1.7.13-2.el5.rf
php-common-5.1.6-20.el5_2.1


Distro:

# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5 (Tikanga)

1. Download and install the Instant Client rpms from oracle which you can get here http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html.

This are the two files needed in my case:

# rpm -ivh oracle-instantclient11.1-basic-11.1.0.7.0-1.x86_64.rpm

# rpm -ivh oracle-instantclient11.1-devel-11.1.0.7.0-1.x86_64.rpm


2. Compile and install the oci8 extension:

Make sure you have the proper packages to compile a script like: php-devel, gcc,etc.


# pear install pecl/oci8

I used pear install pecl/oci8 instead of pear install oci8 because for some reason the second one returns an out of memory error and ignores the memory limit variable (memory_limit = XX ) from the
php.ini file.


When asked about this:

Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client : autodetect

If you have oracle installed in the same server just hit enter. If not (you just want to connect to another server) type:

instantclient and enter or the path to the oracle libraries e.g: /usr/lib/oracle/11.1/client64/lib/.


3. Finally, edit your php.ini file (/etc/php.ini) and add the extension=oci8.so in the Dynamic extensions section. Don't forget to restart apache so the changes take effect in apache.


Additional information can be found here: http://www.oracle.com/technology/pub/notes/technote_php_instant.html.

Wednesday, September 10, 2008

How to solve no sound in flash videos ( fedora )

After installing flash-plugin-9.0.124.0-release.i386 rpm I just got the flash plugin in my firefox but didn't get any sound. Googling about it I found a solution that worked, you need to install libflashsupport rpm, restart firefox and that's it:

just type this:

yum install libflashsupport


I guess this may work in other red hat based distros.

Monday, September 08, 2008

PHP Fatal error: Class 'DomDocument' not found in ...

After running symfony propel-build-all on a new symfony installation got this error. The solution is to install the php-xml rpm (deb).

[root@devel2 cms]# symfony propel-build-all
>> schema converting "/home/sfprojects/cm...lugin/config/schema.yml" to XML
>> schema putting /home/sfprojects/cms/pl...erated-sfGuardPlugin-schema.xml
>> file+ config/generated-sfGuardPlugin-schema.xml
>> file- /home/sfprojects/cms/plugins/sf...erated-sfGuardPlugin-schema.xml
Buildfile: /usr/share/pear/symfony/vendor/propel-generator/build.xml
[resolvepath] Resolved /home/sfprojects/cms/config to /home/sfprojects/cms/config

propel-project-builder > check-project-or-dir-set:

propel-project-builder > check-project-set:

propel-project-builder > set-project-dir:

propel-project-builder > check-buildprops-exists:

propel-project-builder > check-buildprops-for-propel-gen:

propel-project-builder > check-buildprops:

propel-project-builder > configure:
[echo] Loading project-specific props from /home/sfprojects/cms/config/propel.ini
[property] Loading /home/sfprojects/cms/config/propel.ini

propel-project-builder > om:
[phing] Calling Buildfile '/usr/share/pear/symfony/vendor/propel-generator/build-propel.xml' with target 'om'
[property] Loading /usr/share/pear/symfony/vendor/propel-generator/./default.properties

propel > check-run-only-on-schema-change:

propel > om-check:

propel > om:
[echo] +------------------------------------------+
[echo] | |
[echo] | Generating Peer-based Object Model for |
[echo] | YOUR Propel project! (NEW OM BUILDERS)! |
[echo] | |
[echo] +------------------------------------------+
[phingcall] Calling Buildfile '/usr/share/pear/symfony/vendor/propel-generator/build-propel.xml' with target 'om-template'
[property] Loading /usr/share/pear/symfony/vendor/propel-generator/./default.properties

propel > om-template:
[propel-om] Target database type: mysql
[propel-om] Target package: lib.model
[propel-om] Using template path: /usr/share/pear/symfony/vendor/propel-generator/templates
[propel-om] Output directory: /home/sfprojects/cms
[propel-om] Processing: schema.xml
PHP Fatal error: Class 'DomDocument' not found in /usr/share/pear/symfony/vendor/propel-generator/classes/propel/phing/AbstractPropelDataModelTask.php on line 406

Saturday, June 21, 2008

Mplayer crashes using beryl compiz-fusion

When you start to use compiz-fusion and all the fancy desktop effects you will notice a little problem, you can't watch videos!. There's a bug that crashes mplayer, totem and all your video players. Hopefully there's a workaround for mplayer (not sure how to solve the problem with totem).

1. Open mplayer.
2. Right click over the window.
3. Click on preferences.
4. Click on the Video tab .
5. Change the video driver from Xv to X11 and click Ok.

You may have to restart mplayer to see the changes.

Wednesday, June 18, 2008

Vista Blue Screen of Death: STOP: 0x0000008E



The famous blue screen of death attacked me again (previous bsod), this time on Vista, on my Dell inspiron 1520. It's very annoying that you have to deal with this kind of errors on a laptop recently bought. This error started to display a few seconds after logging in to the user account and with no clear reason of what could be the trigger of the error.

The solution this time actually had nothing to do (directly) with Windows Vista but with the laptop BIOS, and the solution came from the dell support site support.dell.com, the error itself suggested to update the BIOS , but I don't pay too much attention to this general error messages. If you're having this same problem you could go and follow this check list from dell, the BIOS upgrade was the second suggestion for a BSOF error and this was the one that worked for me, the file to do this is in the "Drivers and Downloads" section and you can select between all the different models.

Subversion: MKCOL of '...' 405 Method Not Allowed

I was messing with the .svn/entries file in one of my local working copy folders, also I rm -rf a folder and regenerated again (I'm using symfony admin generator) , long story, but the fact is that I think I screwed up something and started to get this error when trying to commit:

# svn commit -m "backend modifications"
Adding apps/backend/modules/comment
svn: Commit failed (details follow):
svn: MKCOL of '/projects/cms/!svn/wrk/5ca09002-f34f-0410-890e-9511925f86a5/trunk/apps/backend/modules/comment': 405 Method Not Allowed (http://svn.calipso.com.co)

After some googling, I found it could be some proxy problem, but I wasn't using a proxy. Later I found that this error could mean that
the directory already existed in the repository and couldn't be recreated again, so my next step was to delete the folder from the repository (svn del ...) and try to do a fresh commit after recreating the folder and files included in it, and this worked ;).

I hope this helps some lost soul out there.

Thursday, May 22, 2008

Unable to write config cache for "config/config_handlers.yml"

After upgrading to symfony version 1.0.16 from 1.0.8 I started to get this error:

Unable to write config cache for "config/config_handlers.yml"

I tried to chmod 777 the cache and it didn't work. The solution for me was to disable SELinux.

How to do that? edit your /etc/sysconfig/selinux file, and set SELINUX=disabled:


# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcinfg - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled

Thursday, February 21, 2008

Page redirection in php with header('Location...') : Blank page vs Working redirection

This kind of errors are what make debugging sometimes a very frustrating part of the developement, if you're suffering a blank page when trying to make a header('Location..'), give this a try:



This is a good redirection:

header('Location: http://www.example.com/');
exit();
?>

And this is a wrong redirection:


header('Location : http://www.example.com/');
exit();
?>


Notice a space between 'Location' and ':' this makes the redirection not to work!!!!

Simple errors likes this consume a lot of time :( , hope helps someone else...