Category: webdev

  • Cookies and hugs to Penny!

    Yesterday, I talked about summit and how we need help with its development. I’m happy today that Penelope Stowe has joined us in breaking fixing summit. Congrats for her first merge proposal.

    Here are your cookies 🙂

  • Blog moved and summit bitesize

    I’ve moved my blog from wordpress.com to nigelb.me. I originally planned to run wordpress here too and export my posts from the old blog here. Unfortunately, that ran into problems. My VPS wouldn’t run nginx + php-cgi + mysql at the same time. Every time I tried, I would run out of memory. My first instinct was to increase the RAM, which I did. But, I looked for a better solution. My friend suggested Jekyll. In Jekyll, basically, the posts are written in markup and then converted to HTML files. That got me interested, I could eleminate php and mysql out of the picture. That’s a lot of memory saved to do other things. It took me a fair few number of hours to set everything up. But I’m very happy with the security (no admin panel really) and I’ve used git hooks so I write a blog post on my computer and push to the repository, which updates the live site. Awesome and geeky. With a bit of effort, I’ve gotten RSS feeds and comments working too.

    Now, the other thing about summit. If you’ve ever participated in a UDS, you probably know summit. You’ve probably griped about it a couple of times, haven’t you? 😉 Well, you should also know that summit is open-source and built on Python/Django. Right now, we’re looking for fresh blood to come and join us. We’ve fixed a bunch of bugs post-UDS and we’re looking for more people to join us in the fun. I’ve tagged a few bugs as bitesize. They are quite easy and if you need help setting up the environment and actually going about fixing the bug, please feel free to ping me (nigelb), Michael Hall (mhall119), or Chris Johnston (cjohnston) in #ubuntu-website on irc.freenode.net. If you’re a web developer who wants to contribute to community projects other than summit, please take a look at the Community Web Projects. I believe we have enough for everyone 🙂

  • Working with Google Maps

    There used to be a time when there was this huge maps craze, it has since passed, but Google Maps remains the most recognized map applications seen on the Internet. Recently, I worked on Google Maps API for a client. This post is a retrospect look at how it went. I’ve not worked with other map systems, so I cannot compare my experience.

    My task at hand was to create a store locater that would take an address as input and plot all the points on a map that was within 100 miles of the given location. A fairly simple map application, except I decided to innovate. My first stop was the articles page on the Google Maps API Reference page. I found a very handy tutorial which was exactly about creating a store, wow that made my work much easier. What I found very helpful from that tutorial was the formula. this formula.

    SELECT id, ( 3959 * acos ( cos ( radians (37) ) * cos ( radians ( lat ) ) * cos ( radians ( lng ) - radians (–122) ) + sin ( radians (37) ) * sin ( radians ( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20; 

    That is the heart of the entire module. That formula returns coordinates that are within 25 miles of a point with coordinates (37, –122). The complexity (if at all) of the application is to pass data from a database using PHP or other server-side language and passing into a JavaScript function. The tutorial that I was looking at used xml to pass data to the JavaScript function. This of course is nice, but I was a bit lazy and a bit innovative.

    In my quest for something better, I discovered JSON. Now, this seemed simple enough since is 2010 and most languages have JSON support including PHP. So, I put all the results into a hidden textbox as JSON and wrote a JavaScript function that would execute on window load. Using that information, I could then loop through it and mark points on the map from that.

    jQuery being an awesome library provided a means for me to do exactly that. I could loop through each of them and plot points on the map quite painlessly.

    function markOnMap (x, y) {     geocoder = new google.maps.Geocoder ();     //center the map to the coordinates of the searched address     latlng = new google.maps.LatLng (x, y);     var myOptions = {         center: latlng,         zoom: 8,         mapTypeId: google.maps.MapTypeId.ROADMAP,         mapTypeControl: false     }     map = new google.maps.Map (document.getElementById ('map_canvas'), myOptions);     var markers = document.getElementById ('marker').value;     var mapPoints = $.parseJSON (markers);     var marker = new Array ();     i = 0;     $.each (mapPoints, function () {         var latlng = new google.maps.LatLng (this.lat, this.lng);         marker[i] = new google.maps.Marker ({             map: map,             draggable: true,             position: latlng,             content: '<b>Name : </b>' + this.name,         });         google.maps.event.addListener (marker[i], 'click', function () {             infowind = new google.maps.InfoWindow ({ content: this.content });             infowind.open (map, this);         });         i++;     }); } 

    When using infowindows, its very important that the content is stored inside the marker and then used to pop out the infowindow, that’s the only way that works. I spend about 5 hours trying to figure that one out.

  • Automatic database backup

    I use git extensively for version control. Its nice to use git since it backs up the code, but the database still remains unversioned. Well, thankfully, XAMPP has a script that does a database dump. So, I wrote a script to do the dump and then commit it to git.

    Step 1

    Create a directory in the home directory for the database dump

    $ mkdir projectname-database $ cd projectname-database 

    Step 2

    Initialize git in the directory

    $ git init 

    Step 3

    Do a database dump for the initial commit

    $ /opt/lampp/bin/mysqldump -u root database-name > database.sql 

    Step 4

    Commit the database file

    $ git add . $ git commit -am 'Initial database commit' 

    Step 5

    Now we’ll write a script to do the database dump and add commits. Open your favorite text editor and write the following script. Save the file as probably ‘database-project’ in your home folder

    #!/bin/bash /opt/lampp/bin/mysqldump -u root database-name > /home/username/projectname-database/database.sql cd /home/username/projectname-database/ git commit -am 'Database dump at `date`' 

    Save the script so that you can run it via a cron.

    Step 6

    Give the script executable permissions.

    chmod a+x /home/username/database-project 

    Step 7

    Open crontab with the following command

    $ crontab -e 

    Step 8

    Add the following line

    00,30 * * * * /home/username/database-project 

    This cron will run the script every 30 minutes.

    Basically what happens here is the database dump will be taken every 30 minutes and the change will be committed to a git repository allowing you to keep track of the overwriting. I’ve found it quite useful and I hope you will too.

  • Baking on Ubuntu while using xampp

    So, I’m a web developer working on Ubuntu. Since I don’t want to get into complications like pinning to have earlier versions of PHP (useful when developing on Drupal), I use XAMPP. Its a bit of a work to get php ready to bake while using XAMPP on Ubuntu. These instructions are probably generic to any bash shell, but tested and working on Ubuntu. Also note, I assume you install XAMPP in the expected directory at /opt/lampp.

    1. Add an alias for php in .bashrc. Open the ~/.bashrc file and add the following line:

       alias php='/opt/lampp/bin/php' 
    2. When running the bake script, run it as follows:

       php cake.php bake