Introducing CakeBattles
CakeBattles is an online application that allows Contenders to be pitted against each and allows users to vote on who/what would win. Each Contender can be assigned to multiple Tags which are used to create battles with similar Contenders. Each Contender can also have multiple images and these are displayed at random to keep the battles fresh.
The application was written in CakePHP and was inspired from CommandShift3 which allows users to vote on website designs. I wanted to create something similar and have been developing it in my spare time between work and freelance projects with the intent to release it online.
An example of the application in action can be seen here. Whilst developing the application I wanted to look at something pretty so I've used a bunch of hot women but I've tried to make the system as generic as possible and anything could be used e.g. films, TV shows or music albums.
CakePHP
I've been using CakePHP for some time now and its a great framework, if you've been programming in PHP for a while and have yet to try a framework I would recommend this one. CakeBattles is a great application to get your head around and covers quite a few concepts that are key in any program that you'll write.
Admin Section
CakeBattles features a password protected control panel that enables you to login and administer the application online. Some custom routes are setup so that the login and logout pages are accessible outside the usual url structure.
The admin section is quite basic, when logging in the user's details are checked against a user in database, if one is found then the user's details are stored in the Session. Before any Controller action the application checks that the user's details are stored in the Session. Pretty basic but does the job perfectly.
Data Validation
CakePHP has a great way of dealing with data validation, all the rules are setup beforehand in the Model and these are then checked when the data is being saved. CakeBattles is a fairly simple application so there is no need for complex validation rules but it will still give you an idea of how to validate data.
Associations
CakeBattles has a number of Associations in place, each Contender hasMany Contender Items (or images) and hasAndBelongsToMany Tags. These associations can be used across many types of applications including Blogs and Forums.
Components
CakePHP components are re-usable classes that can be used across your applications, CakeBattles uses an image upload class to test that uploaded files are indeed images and make it easy to upload a single or multiple images.
Reusable Elements
A great article on the bakery is the ability to create elements that request data from a Controller. This is ideal for views that are used throughout a website and you only want to code once. CakeBattles makes use of this in the footer section and grabs the top 5 Contenders as well as the most popular tags.
Dynamic Image Resizer
Working with images can be a bit of a pain so I've created an image Controller based on a few found on the Bakery that will take an image and resize it on the fly. This way you will only need to upload the image once and all the thumbnails etc will be made from that image. Caching is also done by the Controller so that the system is not resizing the image on every single request which will help with speed.
Commented Code
All the Controllers and Models are fully commented so you'll now exactly whats going on and the rationale behind my coding decisions. The View files dont contain much code and have therefore not been commented. If your a programmer and you've inherited another project you know how valuable comments can be so I've tried to be as thorough as possible.
Integrating jQuery
jQuery has been my Javascript library of choice for some time now and its fantastic. Sure there are other libraries out there that do similar jobs but I've found jQuery to be one of the best.
Ajax
Ajax is used in the system primarily on the front page and will allow users to vote on a Battle via Ajax and then will reload another without a page refresh. This is all done in under 20 lines of code and works like a charm. I've also used the library to fade in/out the Contender details saving space and creating a pretty slick user interaction.
Table Striping and Row Hovering
With jQuery its extremely easy to stripe tables and add a hover class to rows. This is in place throughout the admin section and will save you writing the same PHP code over and over and will also allow most browsers to highlight actual rows which is a (yet another) downfall of Internet Explorer.
Thickbox
For a lightbox I've used the fantastic Thickbox which will display the full image of any thumbnail. Integration is a breeze and will add a great effect to your images. This is used throughout the system to display larger images of thumbnails and works great with the Image Resizer class.
Tooltips
Tooltips are fairly common in online applications and will allow the user to see more information without taking up screen space. For these I've used the excellent Tooltip plugin to fade in/out tooltips and styled them with a simple black background with a slight transparency.
SWFUpload
SWFUpload is a flash file uploader that will display a progress bar on the upload which is great for user feedback especially for large files. This can be seen in action on the Submit a Contender page and is extremely useful for websites, the integration is a little tricky at first but works rather well once its all setup.
Source Code
You can obtain the source code for CakeBattles from CodeCanyon, I've been experimenting with selling a few items online and things seem to be going quite well. If you like the look of the application and would like to help me out then consider buying it.
Wrapping Up
So there you have it, CakeBattles is another CakePHP application that I've developed and I hope you like it. If your new to CakePHP the application will cover quite a lot of ground and will help you get to grips with the best parts of the framework quite easily. If you've used CakePHP before then there may be a few concepts in the application you haven't yet used so check out the app.
Let me know what you think by either sending me an email or by leaving a comment. If you have any questions regarding the project don't hesitate to ask and if you want to waste some time by looking at pretty ladies then get voting!
Comments
Erik Gyepes (21/11/2008 - 03:55)
Very interesting app James. I love it. I'm really thinking about donation as I would like to go through your code :p
But: I tried to upload image, but it seems to not work for me. When I click on the "Upload files" button, no file select popup appears. Maybe it is a bug in the latest Adobe Flash 10 (as the Flash uploader is not working for me in the Wordpress too) So don't know.
Anyway keep up the good work.
Lucas (21/11/2008 - 04:58)
excelent! now you have my attention! jeje
salutis!
James (21/11/2008 - 08:21)
@Erik: Hmm I'll have a look the flash issue asap and see if its a problem with flash 10, if so I would presume that SWFUpload would be updated, if it does I'll fix the issue and let you know.
Edit: The flash issue has been fixed and should now work with Flash 10, apparently SWFUpload stopped working when Adobe updated Flash and so they had to change the way SWFUpload worked.
@Lucas: Thanks for leaving a comment.
Jeff (25/11/2008 - 17:39)
Very nicely done... I created something like this (http://sidechooser.com) a while ago before I knew cake but no traffic... so I just left it hanging there.
Rafael Bandeira (19/12/2008 - 10:50)
Good work, a nice piece of work!
As a sidenote, I don't think that all those comments could do any good to the code and/or the project: instead of clarifying something, they make the code unreadable, large and with a big "you are too stupid" face looking at the person who's reading. i.e:
//redirect
$this->redirect([...]);
provokes a quick and not so kind answer:
"oh, serious!? are you sure?"
James (20/12/2008 - 05:32)
@Rafael: I try to comment as much as possible and its just habit now I guess but I understand that unwanted comments are abit of a bane.
Moon (23/01/2009 - 02:22)
I am very surprised by Rafael's comment ! I can't imagine that he took 2min of his life to write such message !
I think that commenting his code in one of the best practices for a developer ! Maybe the example he gave you is not too false, but I encourage you to continue wirting your code as you do !
It is very clear !
Good job mate.
James (23/01/2009 - 02:58)
@Moon: Every programmer is different really and I kinda agree that redundant comments are useless but that's my personal style, each to their own as they say. Thanks for commenting though.
John (16/02/2009 - 12:20)
Hi. Stumbled across this the other day. I really like CakeBattles - its very funny.
On the comments side, I know what Raphael is saying above, but I would always err on the side of over commenting. I think we all know that things that are obvious now can seem unintelligible in six months time. I've ended up having to fix things in an emergency at midnight on a Saturday night - and its times like that when you appreciate having even the simplest things spelled out.
I think one thing that is really beneficial is using phpDoc comments. They often seem like a chore, but I find them really useful. Sometimes just stopping for a second to summarise the code you've just written can be invaluable (if something isn't right you might as well refactor immediately).
Cheers
James (17/02/2009 - 00:53)
@John: Hey thanks for commenting, yeah I totally agree commenting is definitely a good thing and I've actually been looking at phpDoc to get a standardised system in place that will be really useful for creating an automated guide to my code. Again I think it's personal preference and I know coders can be set in their ways.
Robert (18/09/2009 - 19:42)
Is this still available? I'm in the process of learning Cake, and am interested into diving into as much code as possible. I've found your other tutorials and help very useful. I want to make sure it's still available before I donate my $16.27 (US conversion). Feel free to contact me by email. Thanks.
James (19/09/2009 - 02:08)
@Robert: Hi, yes it's still available. If you want to donate just just the paypal link. Thanks for your interest.
Robert (20/09/2009 - 01:45)
Cool. One quick question before I donate. Shouldn't the "won" + "lost" always be equal to the number of total "battles"? I've noticed sometimes they don't add up.
See http://cakebattles.jamesfairhurst.co.uk/tags/view/tv for example.
James (20/09/2009 - 03:51)
@Robert: hi, yes they should. I think I reset the database at one point though and must not have reset the total number of battles. That's the only thing I can think of that would make the numbers out of sync.
Robert (24/09/2009 - 07:14)
Ahh that makes sense. I just sent you my donation. Thanks for the help.
James (24/09/2009 - 11:27)
@Robert: I'm having trouble sending you the source code via email using both your paypal email and the email on your comments. Have you got another you use? I keep getting a "Delivery to the following recipient failed permanently:" message from gmail.
spidey (19/10/2009 - 08:21)
amazing blog
do you have time to make a tutorial about jquery and cakephp ?
thanks for everything
James (19/10/2009 - 09:43)
@spidey: What type of tutorial would you be after? I'm running very low on things todo so any suggestions would be good.
spidey (22/10/2009 - 03:18)
@James
i'm thinking about the very beginning. how to implement jquery whith cakephp. thing like that. the basics. how to use the Thickbox with cake.
i'm a very beginner with cake ;)
ps : your blog and the way you teach us are amazing
Dean (16/01/2010 - 11:16)
Is the code still available to purchase?
Good blog by the way :)
James (26/01/2010 - 04:56)
@Dean: Yeah it's still available if you want it. Let me know if so.