Convert lat, lng and zoom values to pixel (x,y) on a map

March 6th, 2009 by Carlos Barros

Working on a project this week, that involves Google Maps, I needed to, given a latitude, a longitude and a zoom level, calculate the pixel position (x,y) within the map. If you need to do this using javascript, it’s pretty easy, as GMap API provides a handy method to do this:

var pixel = map.getCurrentMapType().getProjection().fromLatLngToPixel(new GLatLng(10,20),map.getZoom());

Unfortunately, my need was to convert lat, lng and zoom into pixel position from PHP. I “googled” about this subject for a while, and it’s incredible how frequent this need is, and how this information lacks. I saw a lot of people asking for similar things, and also some people that claim that got it working, but without providing any code for this. Googling a bit more I figured out that google uses one thing called Mercator Projection, but even this being a standard, I didn’t find anything that does what I needed. After a lot of time searching about this, I finally stumbled upon a perl script with a set of methods that accomplishes the task. Actually, this perl script is more focused on working with map “tiles”, but there are two methods that does exactly what I needed. It was just a matter of converting these perl methods into php and we’re done.

Read the rest of this entry »

sfPropelActAsSortableBehaviorPlugin group patch

February 20th, 2009 by Carlos Barros

I guess that most Symfony developers should know this Symfony plugin, but for those who not, here’s an small quote:

“The sfPropelActAsSortableBehaviorPlugin is a symfony plugin that provides a new Propel behavior.Model classes with this behavior enabled become sortable, which means that they have new methods to deal with a position attribute.”

This Symfony plugin is really useful as it can save you a lot of code if you need to sort your objects, but in a recent project I worked on, I noticed this plugin lacks one functionality: it can’t handle “sorting groups” within the same object. For instance, suppose you have an Product and Category tables, and that each product belongs to a single category. Then, suppose you want to be able to sort your products so you can control how they show up in your pages. Using sfPropelActAsSortableBehaviorPlugin you can only sort the entire Product table, and that’s not really what we want. We need to be able to sort products within each category.
I guess this problem is really common, and as I had to handle this in a recent project, I decided to write a patch for this plugin, and I’d like to share it. Before one ask me why I didn’t submit it as a official ticket, the reason is that there is already a patch submitted to handle this issue, from almost a year ago, that was not released yet, and I bet most people (including me) is not aware of, so I’m writing this post to tell about this for other users, and maybe make Kris Wallsmith (current leader) release his version, that in fact seem to be more powerful than mine, as it support creating sorting groups using more than one column.

So, how does it work?? sfPropelActAsSortableBehaviorPlugin is really simple to use, all you need is a Integer column in your model, and then activate the new behavior for you class using something like this:

sfPropelBehavior::add('Item', array('act_as_sortable' => array('column' => 'rank')));

After applying this patch, the only thing you need to do is to add one more element to the above array:

sfPropelBehavior::add('Item', array('act_as_sortable' => array('column' => 'rank','group' => 'category_id')));

And that’s it. New sfPropelActAsSortableBehaviorPlugin will use “category_id” as a “sorting group”.

You can now use all methods provided by the new behavior to sort your objects, just like you used to do without the patch. Also, if you don’t have a group category, just omit it from behavior initialization, and it will work as if u didn’t apply this patch.

Find below the full patch:

Read the rest of this entry »

This site may harm your computer???

January 31st, 2009 by Carlos Barros

Today (actually a few minutes ago), I was googling about some random subjects, when I clicked on a link and got that well known Google anti-phishing page alerting me that the website I was about to view could harm my computer. Well, nothing wrong here so far, BUT…. I went back to Google results I noticed that ALL results was marked as harmful. That was weird, so to be sure, I started a new search for the word Google, and guess what?? I got the same warnings, even for Google pages. This is clearly some guy doing something really wrong at Google.  A few minutes (less than 4 minutes) later, the problem was solved, but I had a chance to take some screenshots :) .. Well, this is an really off-topic and useless post, but this was, at least, funny to witness this!

Using embedFormForEach in Symfony, Part II

January 1st, 2009 by Carlos Barros

In my last post I talked about embedFormForEach method, that will embed a form inside another N times, and in the end of that article, I promised a new post on this subject to show how to use it to dynamically add/remove/sort cars, and here it is. As usual, I put live an example here (I integrated this new form on the old city picker form). In this example, when you create (or edit) an user, you will see a new control called Cars, and a green plus icon. If you click that icon, it will add a new “car form” into the user form. You can add as many cars as you want. Each new form has a set of controls that can be used to sort and remove cars from the form. Let’s take a look on how it all works.

Read the rest of this entry »

Using embedFormForEach in Symfony

December 1st, 2008 by Carlos Barros

The new Form sub-framework, introduced in Symfony 1.1, is really great, and the more I work with it, the more I like it. One of the nicest feature of form sub-framework is the ability to embed a form inside another one, using the embedForm method. Also, symfony provides another method, called embedFormForEach that will embed a form inside another one N times. At first glance this method doesn’t seem to be very useful, but I found it really useful in a project I worked a few weeks ago. Consider the following scenario: you want a form to ask users to input information about all cars they have (make and model). In this form you have a select box (this can be an input box too, but I’m using a select box in this example) where users specify how many cars they have and then it will show X fields where users can input cars model and make (X = number of cars user selected), using ajax to load fields. You can see an example here.

Read the rest of this entry »

SOAP WebService in Symfony

November 16th, 2008 by Carlos Barros

One interesting topic on web development is webservice development. There are several techniques to implement a webservice out there, and today I’ll talk about one technique that I worked in the recent past that I really like: SOAP. As per wikipedia:

SOAP, originally defined as Simple Object Access Protocol, is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks. It relies on Extensible Markup Language (XML) as its message format and usually relies on other Application Layer protocols, most notably Remote Procedure Call (RPC) and HTTP for message negotiation and transmission. SOAP forms the foundation layer of the web services protocol stack providing a basic messaging framework upon which abstract layers can be built.

The plan for this tutorial is to build a complete set of webservice methods to interact with the citypicker, built in a previous post. For this, I’ll use a great symfony plugin called ckWebService. This plugin enables the developer to expose your actions as a SOAP webservices. Another great functionality is the built-in WSDL generator, that parses module’s doc comment in order to identify which actions should be exposed and it’s input/output parameters.

Read the rest of this entry »

Conditional validator in Symfony, another approach

November 5th, 2008 by Carlos Barros

Today morning I was reading the Symfony blog, more specifically A week of symfony #96 and then I came across a tutorial talking about conditional validators.This is very usefull, I find myself using this technique very often, so I decided to write about another techinique I use sometimes to implement a conditional validation, but for a different scenario. Imagine the following: you have a contact form where the users specify how they want to be contacted, email or phone, throught a select box. Then you have two input boxes where the users can enter both email and phone. If the user opts to be contacted via email, email field becomes mandatory and phone number optional, and the opposite happens if the user opts to be contacted via phone. So, how to implement such a form in symfony?

Read the rest of this entry »

Joomla like theme for symfony admin generator

October 29th, 2008 by Carlos Barros

When I first started working with symfony admin generator, I searched for some nice themes that I could use in my project, and for my surprise, I didn’t find anything. Searching a bit more I came across a blog post on SymfonyLab (http://www.symfonylab.com/backend-generator-theme/) asking about themes. In the post the author pointed an joomla like theme (http://www.symfony-project.org/forum/index.php/m/16515), created by a user called Draven, that I really liked, but it’s outdated. I’m not really a joomla fan, but I must admin I like their backend design, so I decided to do a new implementation of this theme. It’s based on Draven’s implementation, in fact the css is almost the same, with some changes here there, and some small layout changes as well (specially in the edit page). Just one note that’s worth saying, it’s not really a THEME, but just a css, some layout templates and a collection of images, that changes the look of generated admin module. Here are some screenshots:

Also you can see it in action here (use anything as username/password).

Read the rest of this entry »

Country/State/City picker in Symfony 1.1

October 26th, 2008 by Carlos Barros

Have you ever seen those forms where you select a country, then it populates a list of states, then you pick a state and it populate a list of cities? I’ve seem many of these and I really like them. In one of my past projects I was asked to add a similar form to the system, using symfony, and I’ll show how I accomplished this, making use of the excellent new symfony 1.1 form system. For those who want to see what it looks like before keep on reading, I put very simple sample online in this link.

Read the rest of this entry »

Overlabel with Prototype

July 30th, 2008 by Carlos Barros

Some time ago I was searching for some overlabel script to use in a project and found a great script, created by Mike Brittain (here), and since then I’ve used this script in all my projects. But last week I was working in a project that uses Prototype library and I wondered if there was an overlabel plugin for prototype. After some time googling about the subject, I only came up with a JQuery plugin (here), that is a port of Mike Brittain script to JQuery library. So, as I didn’t find what I was looking for, I decided to make my own port and created a prototype plugin. This took me less than 15 minutes to get a working version of it.

This is the resulting code:

var overLabelPlugin = {
 overlabel: function (obj)
 {
  var  field;
 
  obj = $(obj);
 
  // check for attribute
  if(!obj.readAttribute('for') ||
   !(field = $(obj.readAttribute('for'))))
    return false;
 
  // apply overlabel style
  obj.className = 'overlabel-apply';
  if(field.value != '')
   $$('label[for='+$(field).readAttribute('id')+']').invoke('hide');
 
  // setup event handlers
  field.onfocus = function ()
  {
   $$('label[for='+$(this).readAttribute('id')+']').invoke('hide');
  }
 
  field.onblur = function ()
  {
   if($(this).value === '')
    $$('label[for='+$(this).readAttribute('id')+']').invoke('show');
  }
 
  obj.onclick = function ()
  {
   $($(this).readAttribute('for')).focus();
  }
 }
}
 
Event.observe(window,'load',function ()
{
 Element.addMethods(overLabelPlugin);
});

The only change I made to the original technique was the usage of hide() and show() instead of text-ident:-1000px; to hide/show overlabels.

To activate overlabel, you can do something like:

<html>
 <head>
  <title>Overlabel with Prototype</title>
  <style rel="stylesheet" type="text/css">
  label.overlabel-apply {
    position:absolute;
    top:3px;
    left:5px;
    z-index:1;
    color:#999;
    font-family:verdana serif sans-serif;
    font-size:10pt;
  }
  div.overlabel_container {
    position:relative;
  }
  </stype>
  <script src="prototype.js" type="text/javascript"></script>
  <script src="overlabel.js" type="text/javascript"></script>
  <script type="text/javascript">
  Event.observe(window,'load',function ()
  {
   $$('.overlabel').invoke('overlabel');
  });>
 
  </script>
 </head>
 <body>
  <div class="overlabel_container">
   <label class="overlabel" for="uname">Username</label>
   <input id="uname" name="uname" type="text" />
  </div>
 </body>
</html>

I’m by no means a Prototype expert, so if there’s some better way to do this, please let me know.