Calling functions within your CSS files

I always thought CSS should have a little bit more power, like the use of inline programming functions and dynamic variables. That's exactly why I set out to write my Compression class. On top of compressing the CSS files, saving cached versions and assigning dynamic variables, the class can now call PHP functions that have been written inline within the CSS itself.

This allows for tons of new possibilities for allowing dynamic content, values and structure within boring old stylesheets. As an example, here is the PHP function, the CSS written with the function and the result.

// The PHP function
public function colWidth() {
	$args = func_get_args(); 
	$width = $args[0] * 100;
	return $width .'px';
}
// The CSS
.col1 { width: colWidth(5); }
.col2 { width: colWidth(3); }
.col3 { width: colWidth(1); }
// After being parsed
.col1 { width: 500px; }
.col2 { width: 300px; }
.col3 { width: 100px; }

This new feature has been released in the new Compression version 1.4, which is now available! You can view the updated documentation, or download the files to see the example usages, enjoy! Be sure to spread the word!

Download Compression v1.4

Cupcake and Uploader plugins updated

I want to thank everyone who has tested my forum plugin, and thanks again for all the people who reported bugs and features! The plugin is coming along nicely and I have a few features planned for the future. Cupcake has been updated again to provide more multi-byte character support and fix all the reported bugs. Additionally, I have added a quick-reply feature which can be enabled or disabled through the settings.

The Uploader plugin has also received an update, for there was a major bug, the validation never worked! This was my fault because it seems like I removed a reference variable to the parent model (&$Model) which would never update the primary model outside of the behavior, and in turn the validation never worked. I also updated the plugin with multi-byte support.

Come have a Cupcake, open beta released!

The time has arrived, I have pushed my Cupcake Forum plugin into public beta. You may now download and use the plugin as you wish, but be weary because its still in beta, so problems may arise. If you run into a bug, please post it on the GitHub issues. I also want to thank Matt Curry for testing the plugin beforehand, just like he tested the Uploader plugin, so give him your thanks as well. Heres a quick overview of Cupcake:

The Cupcake Forum is a slimmed down version of the popular bulletin board system, packaged into a plugin. The plugin can be dropped into any CakePHP application and will run smoothly with a bit of configuration. The plugin comes bundled with all the basic features of a stand-alone system, some of which include topics, posts, users, polls, moderators, staff and more. For a full list, view the chapter regarding Features.
Donations

I was originally going to only sell the plugin, but chose against that, simply because it doesn't deserve to be purchased, if anything CakePHP as a whole does. So instead I have added a donation button underneath all the download buttons. So if you really really love my scripts, donate, or else ill take all my scripts down! Yeah maybe.

Cupcake forum plugin, it's pretty delicious

If you have been following me on Twitter, you may of heard me mention a new CakePHP plugin I am developing, a forum plugin. Well I am officially announcing it as the Cupcake Forum Plugin. It is basically complete, but I would like to run a few more tests and perhaps get an "installer" type script. Alongside this announcement, I have uploaded the forum plugin onto this domain as a demo, and will be using it as a support forum for all my scripts. Click the screenshot of the forum to view the demo.

Cupcake isn't as robust as regular forum systems, that would be absurd. Instead it has all the necessary basic features for fully utilizing a forum. Some of those features include: users and profiles, topics, replying, sub-forums, read topics, flood protection, topic auto lock, hourly post and topic limitations, access levels and permissions, moderators, searching, locking mechanisms, sticky and important topic types, topic and post reporting and many more. Oh and that's not all, it also comes bundled with a built in administration panel where you can manage all aspects of the forum, instead of having to do it manually in the database.

I will be releasing the plugin sometime this week. If you have any suggestions for features, be sure to send me an email or reply to this entry. Looking forward to all your feedback on this, it took quite a while to develop!

New scripts galore

It has been quite a while since I posted a real entry, I just haven't worked in CakePHP for the past few weeks. Hopefully these two new scripts and the new script versions will make up for it. Be sure to updated your Databasic if you are using it!

New Scripts

Compression
Compression is a light weight class that will load a CSS stylesheet, bind and translate given variables, compress and remove white space and cache the output for future use. Upon each request will determine if the cached file should be loaded if the original has had no modifications.

Formation
Formation is a lightweight class that can build all necessary elements for a form, process the posted data (post, get, request), validate the data based on a schema and finally return a cleaned result. Has logic for error handling and applying a class to invalid fields. Additionally, all validator functions are static and can be used externally.

Updated Scripts

Databasic v2.3
Added more support for specific operators and rewrote how statement conditions are parsed. View the full changelog.

ZendCon 09

I just bought my ticket for ZendCon, 2 hours before the discount ended, pretty stoked about that. This will be my first PHP/Developers conference and I am thoroughly looking forward to it. I never really had an interest in these before but my passion for PHP has grown tremendously this past year, so its only fitting that I go. I will be going with some friends and colleagues, so it should be a blast and not just me by my lonesome.

I am looking forward to taking the Zend certification test and the tutorial panels at the convention should teach me all I need to know before hand. On top of that, I'm quite excited to see Nate Abele talk about CakePHP (pretty awesome seeing as how everything else is Zend). If any of the small group of people reading my blog are attending, be sure to say hello and we can have lunch.

See you all at ZendCon in October.

Blizzcon 09

So I have not posted an entry for the past week, but I have a good excuse! I was attending Blizzcon and was doing coverage on Diablo 3 and Starcraft 2. If you unaware of what Blizzcon is, its a giant convention held by Blizzard Entertainment that basically showcases their games. They have large art and gameplay discussion panels, live game booths and a ton more for the 25,000+ attendees to divulge into. I however don't get to go to all of it because I am labeled as "press" (because I run sc2armory.com and d3armory.com), so most of my time is spent in the press room writing and covering panels. However, I have written 3 small articles detailing what I did for the past 3 days, but beware I ain't that great of a writer!

If you are a fan of Blizzard, or Starcraft 2, or Diablo 3, give a big shout out. And I must say, yes, these games are pure awesome.

  • Pre-Blizzcon: The Fansite Summit
  • Blizzcon Day 1 Round-Up
  • Blizzcon Day 2 Round-Up

Oh, and I will be starting my new job on Wednesday. Looking forward to it.

Custom method for grabbing a row based on its ID

More times then none when working with a database, you need a general purpose method for grabbing fields from a row that matches an id. Cake has built in magic methods based on the table column that do just that, for example findById() or findBySlug(), but sometimes it grabs associated data that you do not want. Below is a basic method that you can place in your AppModel to grab a row based on its id, with optional parameters for restricting what fields to grab or what associations to contain.

/**
 * Grab a row and defined fields/containables
 *
 * @param int $id
 * @param array $fields
 * @param array $contain
 * @return array
 */
public function get($id, $fields = array(), $contain = false) {
	if (empty($fields)) {
		$fields = $this->alias .'.*';
	} else {
		foreach ($fields as $row => $field) {
			$fields[$row] = $this->alias .'.'. $field;
		}
	}
	return $this->find('first', array(
		'conditions' => array($this->alias .'.id' => $id),
		'fields' => $fields,
		'contain' => $contain
	));
}

With a little bit of editing, you can make it work for other fields other then id. You must also have containable listed in your behaviors for the 3rd argument to work. If you still aren't sure how to use this method, the following examples should help.

// Grab a basic row based on id
$user = $this->User->get($id);
// Grab a row and limit fields
$user = $this->User->get($id, array('id', 'username'));
// Grab a row, fields and associations
$user = $this->User->get($id, array('id', 'username'), array('Country', 'Profile'));

How to do plurals in l10n and i18n

So this was a tricky one for me to figure out as it is not stated in the cookbook (that I could find). It took me having to edit the core CakePHP files and posting a Trac issue until the Cake team explained to me how its done.

First off you may ask, why would we need plurals in our locale files when we are defining everything our self? Well its quite simple, it is used within the Time helper (relativeTime(), etc) and can be used in conjunction with __n(). As with some developers, including myself, we would assume that plurals would be written as followed within our default.po file.

msgid "minute"
msgstr "Minute"
msgid "minutes"
msgstr "Minutes"

I was always under the assumption that Cake did some inflection and logic magic on the backend to determine the plural form, but that's not the case. You need to define the plural form manually using msgid_plural. The correct way to write plurals would be like so.

msgid "minute"
msgid_plural "minutes"
msgstr[0] "Minute"
msgstr[1] "Minutes"

"Its a bit messier, cant understand why it wouldn't be msgstr_plural or something equivalent, but that is how you do it." (Has been answered below, its used for languages with multiple plural versions). For future reference, all CakePHP internal code that uses pluralization uses the strings in default.po, so you should place the locale strings there. If you place them in a separate locale file, you can then use __dn().

Databasic 2.1, now with more operator support!

I got pretty bored the other day and also noticed this contest over at NetTuts, and thought to myself, "Why not enter Databasic into the contest?". Well that is my plan, but I also wanted to fix some problems and restraints in the current version. The new version supports AND/OR operators in the conditions, as well the column operators (!=, <=, etc) have been rebuilt. With this change the new version is not backwards compatible! Sorry, but it shouldn't be too hard to fix your scripts to work correctly.

Here's a quick example of the new operator support in conditions.

$conditions = array(
    'OR' => array(
        array('name' => 'Miles'),
        array('name' => 'Johnson')
    ),
    'status' => 'active',
    'age >=' => 21
);

Download the new 2.1!
View the full change log and features