Snippet: Recursively Remove .svn Directories in Linux

January 29th, 2010 — 12:24am

Just a couple of snippets found in the net to make my life easier.
In the first one beware of the grave accent quotes.

rm -rf `find . -type d -name .svn`

or someone else proposed

find ./ -name .svn | xargs rm -fr

Comment » | Linux, snippet

Snippet: PHP Calling static class methods

December 1st, 2009 — 11:57am

Given the example class foo:

class foo {
  public static function myFunc() {
    return "foo - myFunc";
  }
}

The class name is known in the calling context:

  echo foo::myFunc();

The class name is stored in a php var:

  $class_name = "foo";
 
  /** PHP 5.2.x */
  echo call_user_func(array($class_name, "myFunc"));  
 
  /** PHP 5.3.x */
  echo $class_name::myFunc();

More info on call_user_func: http://es.php.net/manual/en/function.call-user-func.php
More info on static keyword: http://es.php.net/manual/en/language.oop5.static.php

Comment » | php

Snippet: sfWidgetFormJQueryDate for Spanish culture

November 16th, 2009 — 11:47am
  1. Be sure that sfFormExtraPlugin is installed:

    $ php symfony plugin:install sfFormExtraPlugin
  2. In the template:

    <?php use_javascript('ui/i18n/jquery-ui-i18n.js') ?>
  3. In the filter or form:

    $this->widgetSchema['my_date_field'] = new sfWidgetFormJQueryDate(array(
          'culture' => 'es',
          'format'  => '%day%/%month%/%year%', 
          'config'  => '{"showMonthAfterYear": false, "firstDay": 1 }'
        ));

More info on ‘config’ option in http://jqueryui.com/demos/datepicker.

Comment » | symfony

Symfony Partials: Accessing Raw Data defined in an Action

October 15th, 2009 — 10:54am

Sometimes we need to pass some message from the action to a partial via template. If this message only contains plain text there is no problem, we simply define the property in the action, and in the template we pass it to the partial, like this:

In the action:

// modules/mymodule/actions/actions.class.php
class mymoduleActions extends sfActions
{
  public function executeShow(sfWebRequest $request)
  {
    $this->msg = "My plain info message";
  }
}

In the template:

<!-- modules/mymodule/templates/showSuccess.php -->
<hr />
<h1>My Module</h1>
<?php include_partial('mypartial', array('msg' => $msg)); ?>
<hr />

And in the partial:

<!-- modules/mymodule/templates/_mypartial.php -->
<p><? echo $msg ?></p>

The problem comes when we include a little of html in the message, for example, lets change the action:

class mymoduleActions extends sfActions
{
  public function executeShow(sfWebRequest $request) 
  {
    $this->msg = "My <b>html</b> info message";
  }
}

Now, if we leave the template and the partial unchanged, you get this in the navigator:


My Module

My &lt;b&gt;html&lt;/b&gt; info message


Ok, it seems that we forgot to get raw data, so lets change the template:

<!-- modules/mymodule/templates/showSuccess.php -->
<hr />
<h1>My Module</h1>
<?php include_partial('mypartial', array('msg' => $sf_data->getRaw($msg))) ?>
<hr />

And now the result is:


My Module

My <b>html</b> info message


That is a little better, but it is not still the desired result. To get it, we have to also change the partial:

<!-- modules/mymodule/templates/_mypartial.php -->
<p><? echo $sf_data->getRaw($msg) ?></p>

And finally we get what we wanted:


My Module

My html info message


The conclusion is that if we want to send raw data from the action to the partial, we will have to raw it in both, the template and the partial.

3 comments » | symfony

Easy configuring timezone in web server

September 18th, 2009 — 12:36pm

Simply Include the next line in the .htaccess file of your web server:

php_value date.timezone Europe/Madrid

Here you can see the complete List of Supported Timezones.

Comment » | snippet, tips

Setting a value to a hidden form symfony field

July 16th, 2009 — 10:23am

The case

Sometimes, when creating a new object, we need to give a default value to a specific field but we don’t want this field to be visible in the form.
A tipical example would it be a blog -> post relationship. Where we want the blog_id field to be set automatically but we want it to be hidden.

Blog->Post

Blog->Post

The mistake

When creating a new post for a blog, we do not want the blog_id field to be visible, so we typically go to PostForm.class.php and unset the field blog_id:

//myproject/lib/form/PostForm.class.form
class PostForm extends BasePostForm
{
  public function configure()
  {
    unset( $this['blog_id'] );
  }
}

Then, we edit the action class and set a default value for the blog_id field:

// mypoject/modules/post/actions/actions.class.php
class postActions extends sfActions
{
  public function executeNew(sfWebRequest $request)
  {  
    // create the form
    $form = new PostForm();
 
    // get blog_id parameter from the request
    // and set the default value
    $form->setDefault("blog_id", $request->getParameter('blog_id') );
 
    $this->form = $form;
  }
  //... rest of actions here ...
}

Finally, when we try to create a new post, we realize that the blog_id has not been set correctly.

The solution

Instead of unsetting the blog_id field, we have to change its widget to a sfWidgetFormInputHidden:

//PostForm.class.form
class PostForm extends BasePostForm
{
  public function configure()
  {
    // the wrong way in this case
    // unset( $this['blog_id'] );
 
    // the right way in this case
    $this->widgetSchema['blog_id'] = new sfWidgetFormInputHidden();
  }
}

This way the field blog_id remains hidden but do exists in the form, so the setDefault method works correctly.

7 comments » | symfony

Symfony admin generator reference card 1.0

July 9th, 2009 — 9:43pm

Quick guide to all available options for the generator.yml file:
Symfony admin generator reference card 1.0

Comment » | symfony

Snippet: symfony simple frecuently used commands

June 29th, 2009 — 2:33pm

Syntaxis for some simple but frecuently used symfony commands

Database

Configure symfony to use especific mysql database

$ symfony configure:database "mysql:host=localhost;dbname=my_database" user_name password

Rebuild all. This command rebuild models, form, and tables, so destroy all data in database.

$ symfony propel:build-all

Rebuild all and load test data. This command rebuild models, form, and tables, so destroy all data in database and reload it from fixture files.

$ symfony propel:build-all-load

Frontend and Backend

Frontend Aplication. Generate frontend application

$ symfony generate:app --escaping-strategy=on --csrf-secret=FrontendSecret frontend

Frontend Module. Generate a module called module_name in frontend application. The module will have a show action in addition to the new, edit and delete ones.

$ symfony propel:generate-module --with-show --non-verbose-templates frontend module_name ModelObject

BackEnd Aplication. Generate backend application

$ symfony generate:app --escaping-strategy=on --csrf-secret=BackendSecret backend

BackEnd Module. Generate a fully functional module called ‘module_name’ in the backend, based on the model object named ‘ModuleObject’.

$ symfony propel:generate-admin backend ModuleObject --module=module_name

Comment » | symfony

Install .kext files for Mac Beginners

June 22nd, 2009 — 12:00am

One of the first thing I have had to learn, once installed Mac Os X 10.5.5 leopard on my pc ;-) is how to install the drivers for my specific hardware.

In Mac World, most drivers come in the form of .kext files and it is relatively easy to find them in internet, but how to install them?

There are several utilities you can use, one of the simplest one is kext helper, but just in case, I have always prefer to know what happens behind the scenes, so here is the long way:

  1. Supposing xxxxx.kext is the file you want to install, with the finder, copy it to /System/Library/Extensions
  2. Open Terminal from Applications/Utilities
  3. In the Terminal windows type the following:
    $ chmod -r 755 /System/Library/Extensions/xxxxx.kext
    $ chown -r root:wheel /System/Library/Extensions/xxxxx.kext
  4. Remove the cache files:
    $ rm /system/Library/Extensions.kext
  5. If you have kextcache (few people have):
    $ rm /System/Library/Extensions.kextcache
    $ rm Extensions.mcache
    $ sudo kextcache -k /System/Library/Extension
    
  6. And finally, reboot

Instead of removing cache files (step 4 and 5) you can specify the -f -v options at boot time. The -v option is the verbose option, which allow you to see what is happening while loading the os, and the -f option tells the os to reload all .kext files.

Comment » | mac os x

TODO: Change some windows habits

June 17th, 2009 — 12:00am

I have been studing symfony for the last two months and I have discovered a new world in web developing.

Armed with all my new skills, I am now migrating the web site www.ajedrezenmadrid.com from msaccess/asp to mysql/php.

As a senior windows developer I feel comfortable with my windows system, so I installed wampserver package as an easy way to get apache, mysql and php running in my windows xp system.

After the normal newbie counter-times, that I will explain in fore coming articles, and several weeks of hard work, I had a beta version of the backend running in my computer. It was time to deploy my work.

I used the straight-forward method of uploading all my project by hand via ftp, and every seemed to go well… except for one page. The web server was reporting an “unable to find the file …” exception, so I checked that the file existed, but, what to do next?

The problem was not so obvious for a long time windows developer. I had changed the case of a letter of one filename in an include php instruction. That was all, but it took me half an hour to find the problem, so today’s article is just a reminder for those of us that have always developed under Microsoft Windows systems:

Beware of the case-sensitive filenames!

Nowadays, most web servers are linux systems, which are case-sensitive, so remember to be very careful when writing filenames or you will encounter yourself following a “ghost-bug” for a while.

Comment » | tips

Back to top