Digital Base – Blog » Programming

DigitalBase attending Symfony Live 2010

Posted by Gijs Nelissen in All, Programming

banner_squareYou must know by now, Digital Base is using Symfony as the framework of choice for all web development projects.

Therefore all DigitalBase developers will be attending Symfony Live 2010. Monday evening we will travel to Paris by train with a delegation of 4 symfony geeks/fans/enthousiasts.

From the symfony 2010 website :

Sensio Labs is proud to announce that the second symfony live conference will be held February 16-17th in Paris (in English). The symfony live conference is a unique opportunity to meet the symfony community, talk with the symfony core team, and share your experience with the framework. You can even sponsor the event.

The schedule :

symfony schedule

I look forward to the following items, in order of priority

What is important in a PHP editor/IDE ?

Posted by Gijs Nelissen in Programming

I’ve been building websites for a while now. So i could say i am quite experienced although my (programming) knowledge is limited mainly focussed on building php applications. As a developer time is expensive, that’s why i am always looking for ways to speed up the web development process. A good start for fast & secure development is to have the right tools and the right feeling/connection with your editor.

On day-to-day basis i could say 80% of the time i have an open editor somewhere. Based on the amount of time you are “working together” with this piece of software, you could compare it to a marriage (although you shouldn’t trade wives as soon as you meet a younger, smarter and more beautiful girl).

I have a few expectations / requirements for a modern, good working editor

Decent Code Completion / Code Assistance

We all know word completion : auto completion involves the program predicting a word or sentence without the user actually having to type it in completely. This feature is used alot in text messaging (sms), email clients, browsers, search engines etc…

google_image_autocomplete

Autocompletion speeds up the entire process of typing a message, entering an address or, in our case, writing PHP code.

A decent source code editor analyses your source and suggests the function based on the first characters of the variable, function name or object. Most editors these days have code completion although they come in many flavours.

eclipse autocomplete

Larger web applications tend to use alot of classes & files. That’s what it is really important your autocompletion allows you to use an unlimited depth of super- & sub classes (inheritance). For this your IDE should have a decent understanding of the relation between classes. If in some case the code completion does not work for a certain object, you should be able to tell the editor which class your object belongs to, so it will allow you to use the code completion on next occurences of the same object/variable.

Code Documentation / PHPDoc

Another important aspect of code completion is documentation. It is very useful for a programmer to get as much visible feedback as possible about the method/function he is calling. For a php core function this should be a short summary what the function does, for custom methods/functions this would be the description as entered by the one that created the class/method.

eclipse_link

Next to that information it could be useful to show the return type and perhaps some information about the needed arguments. All these things should be integrated in the IDE, readily available when i enter the name of a method/class without opening a seperate manual or the php.net website.

When adding custom classes/methods/functions it is important that you document this code using phpdoc. Eclipse will then pick up this documentation and show it whenever you are using it somewhere else throughout your project.

Syntax Highlighting / Coloring

Code should look good. If you’re spending most of your time looking at code, it’s important to take care of your eyes. A good syntax highlighter highlights the different code to make a clear distinction between actual code, variables & documentation. Marking different type of elements in seperate colors will increase readability and help you understand a function faster.

Syntax highlighting also helps in finding errors in your code. Some editors mark incorrect code (missing delimiter, etc), this comes in handy when you are paying less attention. The editor will immediately show you you are doing something wrong…

Personally i prefer dark editors, many other programmers agree that this is alot better for your eyes. Next to that i find dark color schemes to be alot clearer and more beautiful to look at. TO give you an idea, this is what my eclipse looks like :

screenshot1

Code Bookmarks

When we are writing code it is important to keep track of important locations throughout the project. Code Bookmarks is the easiest way to do it.

A good implementation of bookmarks should allow you to easily switch to next/previous bookmark and give you an overview/listing of the current “bookmarks”.

screenshot3

If you look on the left side, you will see a small flag that marks the current line as being bookmarked.

screenshot7

Once you have these “code bookmarks”, its hard to code without them.

Smart/Quick opening

Now where did i put this code/file again ? Which class defines the foo() method ? Most of you will agree with me that opening the correct resource/file/method takes alot of time. A good editor should allow you to find/open everything you are looking for with a few keystrokes and (more important) without touching your mouse.

My favourite editor (Eclipse) allows you to use ctrl+shift+R to “open resource” and quickly find the file you were looking for.

screenshot8

This means i do not need to touch my mouse or open the “file explorer” to look for the file. Easy & Fast…

I know some people who use “grep -r ‘ methodname()’ to find the location of a method. I personally think this stinks. A good editor should contains some ways to easily open a file/method.

Class Outline

It’s easy to lose track of the variables/methods that are defined in a class. This is where the “outline view” comes in handy. This “outline” view will give you a the overview of every method/variable defined.

screenshot9

Double clicking a method will take you to this method. No search, no scrolling, no touching the mouse. In eclipse there is an even better way. Once inside a class you can use CTRL+O to get a quick outline with autocomplete functionality, finding the method you were looking for never was this easy…

eclipse quick outline

Thanks Eclipse :)

Platform Independance

If you are a regular reader of this blog, you will know we are using several linux flavours in our office. So for us it is very important t he same editor can be used on Windows & Linux platforms. Most “multi-platform” ide’s are based on java (zend, eclipse,…) but there are some other ide’s that have clients for both platforms.

How to unsecure admin generated modules in Symfony

You gotta love the admin generator… There’s only one problem: it’s secured by default (according to the symfony site).
But I want to use these modules in a non-secured environment.
Adding credentials: [] in the admin generator does not work… Why?
Look at your automoduleactions. You’ll find a preExecute function:

  public function preExecute()
  {
    $this->configuration = new eventGeneratorConfiguration();
 
    if (!$this->getUser()->hasCredential($this->configuration->getCredentials($this->getActionName())))
    {
      $this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action'));
    }
 
    $this->dispatcher->notify(new sfEvent($this, 'admin.pre_execute', array('configuration' => $this->configuration)));
 
    $this->helper = new eventGeneratorHelper();
  }

This code is always executed before any action.

if (!$this->getUser()->hasCredential($this->configuration->getCredentials($this->getActionName())))

the getCredentials function:

  public function getCredentials($action)
  {
    if (0 === strpos($action, '_'))
    {
      $action = substr($action, 1);
    }
 
    return isset($this->configuration['credentials'][$action]) ? $this->configuration['credentials'][$action] : array();
  }
}

The problem is that hasCredential(array()) returns false.
My solution:
Override the hasCredential function in myUser

  public function hasCredential($credential, $useAnd = true)
  {
    //for usage in generator => + as credential will return hasCredential=true (even if user has no credentials at all)
    if($credential === true) return true;
    //btw => you could have checked for an empty array of credentials which is what the generator is returning
    return parent::hasCredential($credential, $useAnd);
  }

Now to make it work, put in generator.yml

  config:
     actions:
        _delete:        { credentials: admin }
        _list:            { credentials: + }
        index:           { credentials: + }
#        _new:         { credentials: + }
#        _edit:         { credentials: + }

Ps: You could also use false & -

Simplifying Queries: Using sum, count, etc in Propel 1.3

My old post handled Propel 1.2.

This is the way to do it in Propel 1.3

1
2
3
4
5
6
7
    $c = new Criteria();
    $c->add(FavoriteDatePeer::EVENTDATAPROPOSAL_ID, $this->getId());
    $c->addSelectColumn("SUM(".FavoriteDatePeer::SCORE.") as rank");
 
    $stmt = FavoriteDatePeer::doSelectStmt($c);
    $row = $stmt->fetch();
    return $row['rank'];

using dbFormExtraPlugin for datepicker or datetimepicker

Posted by Gijs Nelissen in All, Programming

For an internal web development project i was looking for a good date picker. I bounced onto sfFormExtraPlugin, but it took me a while to find the correct jquery & jquery UI css & javascript files and set the paths. I decided to create my owner datepicker based on the one in sfFormExtraPlugin (which was based on the jQuery UI Datepicker).

As we already had a plugin containing some of the widgets/validators we are using for some of our webdesign projects, i decided to include those widgets into the existing one named dbFormExtraPlugin, original name, i know…

Installing dbFormExtraPlugin

It’s really this simple, install, publish assets, en clear your cache.

symfony plugin:install dbFormExtraPlugin --release=0.0.5
symfony plugin:publish-assets
symfony cache:clear

sfWidgetFormDateJQuery

This is based on the normal jQuery date picker

class ProjectForm extends BaseProjectForm
{
  public function configure()
  {
 
      $this->widgetSchema['published_at'] = new sfWidgetFormDateJQuery();
  }
}

Should give you something like this

sfwidgetformdatejquery

sfWidgetFormDateTimeDyn

This is based on the dyndatetime project, something i found on google code. From their website :

jQuery has lots of date pickers, but no date- time pickers. This supports date and time, and renders the value to a single field in a configurable format.

Use it like this

class ProjectForm extends BaseProjectForm
{
  public function configure()
  {
      $this->widgetSchema['featured_at'] = new sfWidgetFormDateTimeDyn();
  }
}

Results in a popup, similar to the dynarch calendar picker, including time sliding capabilities !

sfwidgetformdatetimedyn

I still have some work todo on configuration/customisation but it does work (out of the box).
Other installation instructions here. Comments welcome. Leave a message on this post, post a bugticket on the symfony trac or contact us by email.

Developer Faster with lightning fast Eclipse

Posted by Gijs Nelissen in Programming

When maintaining & developming large web applications you might notice that eclipse slows up a bit. Building workspace, showing the autocomplete or syntax highlighting might take a while to load. The solution for this is to optimise your eclipse.ini and tell your favourite PHP development IDE to use more memory.

After installing eclipse you open the eclipse.ini in the root. If you followed my previous post, the default settings should be something like this :

-startup
plugins/org.eclipse.equinox.launcher_1.0.101.R34x_v20080819.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_1.0.101.R34x_v20080805
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
-vmargs
-Xms40m
-Xmx256m

This means eclipse will use in between 40 and 256mb. These default settings will allow you to run Eclipse on a pre historic PII with less then 1GB ram, although i wouldn’t recommend that.

Most of you, especially developers working in a development agency, will have powerful workstations. So lets tell eclipse to use a bit more resources

-startup
plugins/org.eclipse.equinox.launcher_1.0.101.R34x_v20080819.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_1.0.101.R34x_v20080805
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
-vmargs
-Xms256m
-Xmx1024m
-XX:MaxPermSize=256m

I changed the xms, xmx and xx:maxpermsize settings.

What does it mean ?

  • Xms sets the initial Java heap size
  • Xmx sets the maximum Java heap size
  • XX:MaxPermSize allows for the JVM to be able to grow the PermSize to the amount specified. Initially when the IDE is loaded, the MaxPermSize will still be the default value but will not actually take up that amount until it is needed.

Wordpress : Seperate RSS feed for Category / Author / Tag

Posted by Gijs Nelissen in Programming

As you may have read we migrated this blog to a wordpress one. Yeahyeah, we are not officially wordpressified.

My impression of the core

The default wordpress core holds alot of use’eable functions, but it took me a while to figure out how the thing worked. You need to know that wordpress is not really written very object oriented, so basically its a collection of functions although some “under the hood” core functionality is object oriented.

If i was not convinced wordpress is a serious project with a great future, i wouldn’t have chosen it, so let’s not start the “code quality” discussion.

The problem

Something i was missing after the default drupal installation was an easy way to retrieve the related rss feed based on the page the visitor was accessing. If a visitor was on the “all posts by author X” page, the linked rss feed should only hold those posts. Same for category pages and tag pages.

Getting the information of a tag, author of category is very easy. There are some built in functions like get_category, and get_category_feed_link that will give you this information. The downside is, this information is basically only available inside “the loop“.

The solution

To get the information about the selected category, author or tag i had to jump a few hoops. It’s a combination of core functions and some minor custom code where i had to retrieve the current tag from the queried variables.

Then I created a custom method that returns the most related feed based on the page request. The method will check what type of page the visitor is accessing (category, tag, author or main page) and return the appropriate rss feed.

/**
 * This method will give you the most related rss feed based on the current page request
 *
 * @param $type
 * @return string (feed url)
 */
function get_feed_url($type = "rss2_url") {
 
  if (is_category()) {
    $current = get_the_category();
    $current = $current[0]->cat_ID;
    return $url_rss = get_category_feed_link($current,$type);
  }
 
  if (is_author()) {
    $current = get_query_var('author_name');
    $current = get_userdatabylogin($current);
    $current = $current->ID;
 
    return get_author_feed_link($current,$type);
  }
 
  if (is_tag()) {
    $current = get_query_var('tag');
 
    $formaturl = "/feed/";
    switch ($type) {
      case "rss_url":
        $formaturl = "/rss/";
      break;
      case "atom_url":
        $formaturl = "/atom/";
      break;
 
    }
    return get_bloginfo("url") . "/tag/" . $current . $formaturl;
  }
 
  return get_bloginfo($type);
}

Examples

get_feed_url("rss2_url"); // on page /blog/
 
//returns : http://www.digitalbase.eu/blog/feed/
 
get_feed_url("atom_url"); // on page /blog/category/digitalbase/
 
//returns : http://www.digitalbase.eu/blog/category/digitalbase/feed/atom_url/
 
get_feed_url("rss_url"); // on page /blog/tag/symfony
 
//returns : http://www.digitalbase.eu/blog/tag/symfony/rss/

Enjoy

Installing Eclipse + Latest Php Development Tools (PDT 2.0)

Posted by Gijs Nelissen in Programming

Following up on my previous post on “installing 3.4 + PDT 2.x” i wanted to let you know there is a much easier way. As we are coming close to a stable version of pdt 2.x (launch on 29th of December) :

  • 2.0 M1 – November 03
  • 2.0 M2 – November 24
  • 2.0 RC1 – December 08
  • 2.0 RC2 – December 14
  • 2.0 RC3 – December 23
  • 2.0 Release – December 29
  • The PDT core team decided to setup an “update site” so you can easily fetch the latest version from within eclipse.

    Installation

    Getting & Running Eclipse

    Get the latest version of Eclipse from the download site. I went for “Eclipse Classic 3.4.1 (151 MB)“. Extract it and double eclipse the eclipse executable.

    Adding the PDT 2.x

    When you selected a workspace and fired up eclipse add the different update sites (Help > Software Updates… > Available Software > Manage Sites)

    A list of the ones you need to add :

    Also make sure to enable the default Ganymede update site.

    make sure these checkboxes are ... checked :)

    make sure these checkboxes are ... checked :)

    Expand the DLTK site and select the Dynamic Languages Toolkit – Core Frameworks or Dynamic Languages Toolkit – Core Frameworks SDK Feature.
    Then check the PDT SDK feature (PDT SDK 2.0.0 ….). Click install & restart eclipse.

    Custom .debs for Symfony (1.0/1.1 & 1.2) for Debian/Ubuntu

    Installing multiple versions of symfony

    As i wanted to test some of my projects compability against symfony 1.1, i wanted to install multiple symfony versions on my system. The main reason for this i want to keep the default symfony installation on my desktop as well as on our production environment.Â

    Now as you know, we are working with 5 workstations and multiple servers. So it’s a real pain in the *** doing this stuff all over again on each of those machines, so i decided to create some .debs.

    The Solution

    Thanks to a great guide posted on the Symfony Wiki i was up & running in under 5 minutes. I had to check out the different versions, create some folders to hold the different symfony installs and create some symlinks to make sure you can use those different versions by command line.

    The Packages

    I created 3 different packages for each version of symfony. You can install as many as you like, as those will all use a different path. Direct Link .

    I tested these packages on an Ubuntu Hardy & Feisty and it works like a charm. Let me know if it doesn’t for you.

    If you have a hard time figuring out which version of the symfony framework you want to use for your project, check this post

    Show me the magic

    You can see you now have 3 (or 4) different versions of Symfony available to you.Â

     Multiple Version of Symfony On the same system

    Configuration

    Now the hardest part (if you can even call it that). Howto tell your symfony application which symfony to use ? Just go to your applications config/config.php and update the paths to the corresponding version.

    [code]#52[/code]

    Don’t forget to also update the /js path in your virtual host configuration , change the version to the one you want to use for this project.

    Alias /sf /usr/share/php/symfony_1.0/data/web/sf

    Feedback/Suggestions welcome, just send an email to info@digitalbase.eu Â

    Update : if you have a problem opening PHP files (error occured during Selection Job Title) here is my follow-up post (the fix)

    dbFusionChartPlugin : Simple Example

    Posted by Gijs Nelissen in Programming

    This first example will show you howto integrate a FusionChart (free / non-free ) graph in your symfony application, using the dbFusionChartPlugin . I will explain 'example1' included in version 0.0.2 of our plugin.

    The Result

    Before getting to the code, here is what the final graph/chart will look like

    [partial]dbFusionChartExample/example1[/partial]

     

    The Partial

    The example1 is a partial that looks like this

    [code]dbfusionchart-example1-partial[/code]

    As you can see the code in this partial is pretty easy. First you tell the symfony application to load the FusionChart helper (using : use_helper). After that you load the graph/chart itself. dbfusionchart_js takes multiple arguments, the most important are the first 2.

    • the first argument is the type of chart (using class constants)
    • second argument is the url to the xml
    • extra arguments tell the dbFusionChart how big the chart should be (width & size).

    Very important in the url to the xml is that symfony knows it needs to return valid xml (an no html).

    The Action

    In the action.class of the dbFusionChartExample module we have the following action :

    [code]dbfusionchart-example1-action[/code]


    Line 5 – 6
    tell symfony to not include the debug bar and send the response as XML (and not html)
    On line 8 you will see the declaration of the chart. The classes are pretty smart, but the above example is a very basic example (copy pasted from FusionChart documentation).

    When you are ready setting the properties of your chart (we will explain that in further posts), you tell symfony to return the xml (see line : 40).