Combining Modalbox & CakePHP

February 28th, 2008

demo included!I’m pretty sure this can be implemented more elegantly, but I’ll describe my attempt to unite Modalbox with CakePHP 1.2.

First example – a modal box view
This example shows the steps I’ve taken to flavour a freshly baked cake app with modalbox. The starting point is a customer model, a controller and some basic index, view and edit views.
Showing a cakephp view in a modalbox, is just a few snippets away. Just make sure you include prototype, scriptaculous and the modalbox javascript and the modalbox css file in the default layout…

/app/views/layouts/default.ctp:
echo $html->css('modalbox');
echo $javascript->link('prototype');
echo $javascript->link('scriptaculous.js?load=effects');
echo $javascript->link('modalbox');

Then create the appropiate link to the view you want to load inside the modalbox. For example, if you want to show customer details from the customers/index.ctp view:

# ...somewhere in the loop that lists customers:
echo $html->link('View', array('action' => 'view', $customer['Customer']['id']), array('title' => 'Customer details', 'onclick' => "Modalbox.show(this.href, {title: this.title, width: 400}); return false;"));

Make sure you use the RequestHandler component so that the customer view is displayed in the ajax.ctp instead of the default.ctp layout. You can do this by making sure the following line is in /app/app_controller.php:

var $components = array('RequestHandler');

And… there you go! When you click the view link, the modalbox slides in, while the rest of the page dims to gray. When you hit the escape button, click the little ‘X’ or click anywhere in the gray area, the modal box slides back where it came from.

Ok, now forms!
Read the rest of this entry »

CD sleeves on-the-fly!

February 27th, 2008

A while ago, I’ve written a simple web interface around cdlablegen. Cdlabelgen is a perl script that allows you to automatically generate cd and dvd sleeves.
It’s installed on a closed network and my colleagues use it to label their scientific data archive. This is public version is now available on special request of a remote co-worker (company logo removed!).
hoesje.nerdnotes.org

vmcp

December 10th, 2007

Vmcp (vm-copy) is a simple script that allows you to copy/clone a VMware virtual computer image. Just specify the source directory and the destination directory. It will first just copy the wmware directory recursively en then change the configuration option ‘displayName’ in the vmx file.

#!/bin/sh

source=$1
dest=$2

echo copying virtual machine
cp -rv $source $dest

vmxsourcefile=`ls $source/*.vmx`
vmxdestfile=`ls $dest/*.vmx`

grep -v displayName $vmxsourcefile > $vmxdestfile
echo displayName = \"$dest\" >> $vmxdestfile

exit 0

Howto start your CakePHP project in Subversion

December 6th, 2007

This howto will demonstrate how to start your cakephp project from scratch, taking advantage of subversions “externals definition”. The result will be a basic CakePHP directory structure with only the “/app” part under your svn control and the other directories linked to a specified revision of cakephp. Once setup, if you do a ’svn chekout’ of your project, you will get all the files at the right location, so no need to merge your ‘/app ‘ with the contents of a tarbal which you downloaded from the CakePHP website.

Let’s get started!

First, we create a temporary directory structure, just for the purpose of importing it:

$ mkdir temp
$ mkdir temp/mynewproject
$ mkdir temp/mynewproject
$ svn import -m "New import" temp file:///home/me/subversion/
# remove the temporary directory structure
$ rm -rf temp

Now, we checkout a working copy:

$ cd ~/src
$ svn checkout file:///home/me/subversion/mynewproject

Ok, this just bought us an empty directory managed by subversion!! Let’s fill it with the use of some magic: Externals. We will create an externals definition for the directory. It will point to the current cakephp1.2 release, which is 5875 at time of writing.

$ cd mynewproject
$ svn propedit svn:externals .

This will fire up your text editor with an empty text file. Type the following line, save and quit:

third-party/cake_1.2.0.5875 -r 5875 https://svn.cakephp.org/repo/trunk/cake/1.2.x.x/

Since this is an actual change to the current directory, we should commit it back to the repository:

$ svn commit -m "Added externals definition for CakePHP 1.2 rev. 5875"

By updating our working copy, the cakephp files will be downloaded to the in the property specified directory:

$ svn update

Wow, that’s nice, isn’t it! Oh… and btw, don’t forget to donate some change to the project!

Let’s go and make some symlinks and a copy of the ‘app’ directory:

$ ln -s third-party/cake_1.2.0.5875/.htaccess
$ ln -s third-party/cake_1.2.0.5875/cake
$ ln -s third-party/cake_1.2.0.5875/index.php
# now create your own copy of the app directory
$ cp -r third-party/cake_1.2.0.5875/app .

Since we don’t need the .svn directories from the cakephp svn checkout. We find and delete them:

$ find app -type d | grep \.svn | sort -r | xargs rm -rf

Finally, add the ‘/app’ directory and the simlinks to our own subversion repository

$ svn add .htaccess app cake index.php
$ svn commit

That should be it!

Remember, when you want to upgrade your CakePHP version. It should be a matter of (a) editing the externals definition on the myproject directory. And (b) re-linking the symbolic links.

Preparing plogger thumbnails

October 12th, 2007

I recently started to use Plogger to publish family pictures. The photo blogger package is nice, but still in beta stage. The latter doesn’t go unnoticed when setting it up.

One of the things that has been bothering me is the fact that thumbnails are not only generated during import, but also during the first visit of the new albums. Especially on my very slow web host, it can take up to minutes to view a single picture that hasn’t been processed before.

A solution is a shell script that finds out what pages to download after adding a new set of pictures. It will then download those pages, forcing Plogger to generate all the necessary cache files. The script should be executed form the web server itself. It requires 3 lines (upper part of the script) of configuration: the url and filesystem path to the plogger installation and a log file that keeps track of the url’s already downloaded. If you want to redo the downloading, just remove this log file.

Reminder: Local documentation!

September 21st, 2007

I’ve been searching for some ‘getopt’ examples on the internet. Many tutorials show the same basic tricks. I completely looked over the local documentation, which in fact has very nice examles for bash and tcsh:

#!/bin/bash

# A small example program for using the new getopt(1) program.
# This program will only work with bash(1)
# An similar program using the tcsh(1) script language can be found
# as parse.tcsh

# Example input and output (from the bash prompt):
# ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "
# Option a
# Option c, no argument
# Option c, argument `more'
# Option b, argument ` very long '
# Remaining arguments:
# --> `par1'
# --> `another arg'
# --> `wow!*\?'

# Note that we use `"$@"' to let each command-line parameter expand to a
# separate word. The quotes around `$@' are essential!
# We need TEMP as the `eval set --' would nuke the return value of getopt.
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
     -n 'example.bash' -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
        case "$1" in
                -a|--a-long) echo "Option a" ; shift ;;
                -b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;;
                -c|--c-long)
                        # c has an optional argument. As we are in quoted mode,
                        # an empty parameter will be generated if its optional
                        # argument is not found.
                        case "$2" in
                                "") echo "Option c, no argument"; shift 2 ;;
                                *)  echo "Option c, argument \`$2'" ; shift 2 ;;
                        esac ;;
                --) shift ; break ;;
                *) echo "Internal error!" ; exit 1 ;;
        esac
done
echo "Remaining arguments:"
for arg do echo '--> '"\`$arg'" ; done

phpMyAdmin timeout

August 10th, 2007

The phpMyAdmin timeout is quite short by default (about half an hour). Put the following line in config.inc.php to make it longer:

$cfg['LoginCookieValidity'] = 60 * 60 * 24;

This will set the timeout to one day.
Source: dotticontra.org

MySQL re-installation

August 4th, 2007

De-installation is sometimes not so easy with APT. For example when you screwed up your mysql installation or lost mysql passwords you may want to reinstall mysql and start from scratch again. You probably installed mysql server using:

$ sudo apt-get install mysql-server

Then you might think that you need this command for removal:

$ sudo apt-get remove mysql-server

However, this is not the case. The mysql-server package is a sort of meta-package that makes sure that the latest (and greatest) available mysql server version will be installed. Currently, this means that you actually installed mysql-server version 5.0. Above command will only remove the meta-package, while mysql server 5 happily continues running on your system. To de-install the mysql-server, remove the package, the meta package pulled in:

$ sudo apt-get remove mysql-server-5.0

To also remove the configuration and the binary data, use instead:

$ sudo apt-get remove --purge mysql-server-5.0
# and for ubuntu Gutsy:
$ sudo apt-get purge mysql-server-5.0

Now that mysql server is ‘really’ gone, you can start over and install the mysql-server package:

$ sudo apt-get install mysql-server

‘Compact’ bash prompt

July 30th, 2007

Especially when working with cakephp, the following line in your .bash_profile script might come in handy:

HOSTCOLOR="31m"
PS1="\u@\[\e[${HOSTCOLOR}\]\h\[\e[0m\]:\w\n\[\e[33m\]\!\[\e[0m\] \$ "

It will print out the current working directory on the first line and the actual bash prompt on the second line. Making it less likely that your command will wrap lines after the first few typed characters.
It changes the command prompt from:

command line long

….into:

command line 'short'

The login prompt now shows the following info:

  • username
  • hostname in red (in my bash script, it will print red for one group of servers and blue for other servers)
  • current working directory
  • command history number, ie: use !520 to execute the previous command

Extracting layers from a XCF (gimp) file

July 21st, 2007

This is a script that extracts the layers of a XCF file to individual PNG files. It uses xcftools (debian/ubuntu package):

#!/bin/bash

xcffile=$1

layers=`xcfinfo $xcffile  | awk '{ print $NF }'`

destdir=`dirname $xcffile`/layers
mkdir $destdir

for layer in $layers
do
        echo saving $layer to $destdir/$layer.png
        xcf2png -o $destdir/$layer.png $xcffile $layer
done

exit 0