David Golding



Cursed Leap Years!

By David Golding | Print This Post Print This Post

I used to think that leap years only happened every four years. In fact, every centennial year that is not evenly divisible by 400 is not a leap year.

I don’t expect to live to 2100, so it shouldn’t matter. But, unfortunately, in programming world, remembering this caveat on the leap year could result in a bug, or worse, world cataclysm like we saw didn’t see with Y2K.

I recently landed this bug and had to write a way out of it. Here’s my solution in PHP, JavaScript, and Cappuccino.

1
2
3
4
5
6
7
8
9
10
11
function isLeapYear($aYear) {
    if (($aYear % 4) == 0) {
        if (($aYear % 100) == 0 && (($aYear % 400) != 0) {
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}
1
2
3
4
5
6
7
8
9
10
11
function isLeapYear(aYear) {
    if ((aYear % 4) == 0) {
        if ((aYear % 100) == 0 && ((aYear % 400) != 0) {
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}
1
2
3
4
5
6
7
8
9
10
11
+ (BOOL)isLeapYear:(int)aYear {
    if ((aYear % 4) == 0) {
        if ((aYear % 100) == 0 && ((aYear % 400) != 0) {
            return NO;
        } else {
            return YES;
        }
    } else {
        return NO;
    }
}


Upgrading PHP in MAMP

By David Golding | Print This Post Print This Post

I wanted to take advantage of PHP 5.3’s new

1
namespace

feature but in my localhost environment, I use MAMP. Upgrading PHP proved a little tricky, but actually very easy. The tricky part was figuring out how to make it work, but on the whole, it’s not too bad.

First, run the

1
phpinfo()

function in a PHP script on your localhost or go to PHPMyAdmin and hunt down the configuration page. You should see a large chunk of configuration markup at or near the top:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
'./configure' '--with-mysql=/Applications/MAMP/Library'
'--with-apxs2=/Applications/MAMP/Library/bin/apxs'
'--with-gd' '--with-jpeg-dir=/Applications/MAMP/Library'
'--with-png-dir=/Applications/MAMP/Library' '--with-zlib'
'--with-freetype-dir=/Applications/MAMP/Library'
'--prefix=/Applications/MAMP/bin/php5' '--exec-prefix=/Applications/MAMP/bin/php5'
'--sysconfdir=/Applications/MAMP/conf/php5' '--with-soap'
'--with-config-file-path=/Applications/MAMP/conf/php5'
'--enable-track-vars' '--enable-bcmath' '--enable-ftp' '--enable-gd-native-ttf'
'--with-bz2=/usr' '--with-ldap' '--with-mysqli=/Applications/MAMP/Library/bin/mysql_config'
'--with-sqlite' '--with-ttf' '--with-t1lib=/Applications/MAMP/Library'
'--enable-mbstring=all' '--with-curl=/Applications/MAMP/Library' '--enable-dbx'
'--enable-sockets' '--enable-bcmath' '--with-imap=shared,/Applications/MAMP/Library/lib/imap-2006i'
'--enable-soap' '--with-kerberos' '--enable-calendar'
'--with-pgsql=shared,/Applications/MAMP/Library/pg' '--enable-dbase'
'--enable-exif' '--with-libxml-dir=/Applications/MAMP/Library'
'--with-gettext=shared,/Applications/MAMP/Library' '--with-xsl=/Applications/MAMP/Library'
'--with-pdo-mysql=shared,/Applications/MAMP/Library' '--with-pdo-pgsql=shared,/Applications/MAMP/Library/pg'
'--with-mcrypt=shared,/Applications/MAMP/Library' '--with-openssl'

Copy and paste this whole chunk into your text editor and remove the single quotes (search and replace should do it). Look for the flag

1
--with-pdo-mysql=shared,/Applications/MAMP/Library

and replace it with:

1
--with-pdo-mysql=/Applications/MAMP/Library

If you don’t do this, you might end up with an

1
ld: symbol(s) not found

error.

Finally, add the following flag to the end:

1
--without-iconv

After you have downloaded the latest PHP release of your choosing from PHP Sources Snapshots,

1
cd

to the downloaded directory in Terminal. Paste your reformatted configuration string (all of it, including the beginning

1
./configure

command) and run it.

After the configuration phase is finished, run:

1
2
$ make
$ sudo make install

Relaunch MAMP, and you’re good to go.

For you CakePHP users out there, now you can play with Cake3 releases (which apparently will be optimized for PHP 5.3+).


Using Cappuccino with CakePHP

By David Golding | Print This Post Print This Post

If you haven’t given Cappuccino a look, you ought to. It’s a JavaScript framework designed after Apple’s Cocoa framework for building web applications. You wouldn’t use Cappuccino to build informational web sites, or really anything other than a web app, but for web apps it rocks. All of the HTML, DOM, and JavaScript is abstracted out and substituted instead is one language that handles all the heavy lifting for you: Objective-J.

Objective-J is a superset of JavaScript much like Objective-C is to C. If you’re already familiar with Objective-C, you should find Objective-J a cinch. And if you’re like I was and come out of a PHP, HTML background, it’s still simple to learn (with a little bit of patience).

Cappuccino mainly runs as a client-side framework, meaning that most of the functionality and coding happens without talking to the server. This doesn’t mean that it can’t communicate with the server; on the contrary, it works like you’d expect with other JavaScript frameworks like JQuery or Prototype, but it does so with its own Objective-J flavor. The handling is superb, allowing you to essentially Cocoa-ify the web browser without worrying about cross-browser compatibility and lots of hard coding. When going back and forth between server-side scripting and Cappuccino, all you have to do is send data in JSON format and Cappuccino can handle the rest of your needs.

On the Server: CakePHP. In the browser: Cappuccino.

I wanted to combine the powers of Cappuccino and CakePHP, but it required a little bit of configuring to get it working correctly. I’ll outline the steps I took to get Cappuccino working with Cake. Let’s start with a basic Cake application running out of the box, no controllers or anything. I’m using the standard directory structure for a Cake app that’s running correctly.

Step 1: Load the Cappuccino Libraries. Once you’ve downloaded the Cappuccino starter package, you’ll see a folder called “NewApplication.” Rename this to “cappuccino” and copy it to the Cake app’s

1
webroot/js

directory. All of your Cappuccino programming will happen in this folder. We just need to tell Cake to run the Cappuccino app.

Step 2: Create the Layout. Create

1
views/layouts/default.ctp

and copy and paste the markup from the starter index.html file here. Select all in the

1
webroot/js/cappuccino/index.html

file and paste it in this default layout file.

Step 3: Configure the Layout. You should notice a

1
<script>

area that contains a JavaScript environment variable named

1
OBJJ_MAIN_FILE

. This main file won’t run out of the box now that we’re calling this view from within CakePHP. All we have to do is add the

1
<base>

tag and let Cake provide the correct path, and it should line all of the libraries up correctly. Replace the code within this script area with the following:

1
2
3
1    var path = "<?=$html->url('/js/cappuccino/',true);?>";
2    document.write("<base href=\"" + path + "\"><!- -[if IE]><"+"/base><![endif]- ->");
3    OBJJ_MAIN_FILE = "main.j";

Notice that on line 1, I use the HTML helper’s URL function to provide the path as an absolute URI. Line 2 addresses an IE inconsistency (any surprise there?) regarding the base tag. Line 3 is the same OBJJ_MAIN_FILE parameter that is included in the starter index.html Cappuccino file.

Step 4: Launch the App. Now just call your Cake application in the web browser, and you should see “Hello World” execute appropriately.

Adjusting this Setup

You may want to use Cappuccino in only some CakePHP actions. If that’s the case, then create a different layout file, something like

1
cappuccino.ctp

, then call this layout only in the actions that you want to use Cappuccino. The following

1
$this->layout = 'cappuccino';

is used in the controller actions to then switch to this Cappuccino-based layout.

Remember that Cappuccino will come first in the code hierarchy now that we’ve got it installed. In other words, if you want Cake to provide content for these Cappuccino views, you’ll need to get Cappuccino first to run GET or POST requests using Cake routes. Then, you’ll need to change your output from HTML to JSON when talking back, unless you have segments of your Cappuccino app that are meant to display HTML output. But that’s a subject for another day. Take a look at Nice Panorama’s Tutorial Demos to get a sense of what I mean by this.


« Older Entries | Newer Entries »

Beginning CakePHP: From Novice to Professional by David Golding

David Golding

A blog about CakePHP, web design, and grad studies in religion. © 2008, D. Golding