Full CakePHP 1.2 App Part 12
OK so finally the full CakePHP application is coming to a close and this article is just going to wrap up a few loose ends. I've noticed a few errors and layout issues that I'm going to fix and hopefully by the end of the article the app will be fully complete.
Like all applications you could probably keep going forever adding new features, tweaking code and cleaning things up but there must come a time when you have to release it and the close the door. After this I probably wont be adding more features for some time but I do have a few ideas about great features you could try and implement.
The application can be found online at cakecatalog.jamesfairhurst.co.uk with a few more DVDs added.
Genre Filter Problem
When filtering DVDs by Genre there is a display error with only a few DVD covers being displayed per shelf. On further inspection the problem occurs because when the Genre filter is active I have to manually remove the DVDs that don't match. To display 8 DVDs per shelf I use the index of the array to determine the current position and because I've manually removed DVDs from the array the index has been messed up.
To fix this I've added a simple counter in the /app/views/dvds/index.ctp so instead of using the array key index I'm going to use the counter instead. The changed lines are below to you just need to do a quick copy and paste.
// file: app/views/dvds/index.ctp <?php // check $dvds variable exists and is not empty if(isset($dvds) && !empty($dvds)) : // init dvd count $count = 1; ?> <div class="shelf"> <?php foreach($dvds as $key=>$dvd): ?> <?php // calculate if this dvd is the last on the shelf // if dvd number can be divided by 8 with no remainders $last_dvd = ( (($count) % 8 == 0)? 'dvd-last' : '' ); ?>
Image Upload Problem
When uploading images that already exist there are spaces in the new name that throws up some validation errors when viewing. This is a simple fix by removing the spaces from the new filename like so:
// file: app/app_controller.php // from $url = $rel_url.'/'.$now.' - '.$filename; // to $url = $rel_url.'/'.$now.'-'.$filename;
Integrating the new Image Resize Class
Recently I created an Image Resize class that will resize images on the fly and save a cached version on disk so save processing time next time the image is requested.
I copied the images_controller.php file from that post and pasted it into the controllers directory. I also created a new cache folder located at /app/webroot/img/cache to hold all the processed images.
Below is a quick change to the index.ctp so utilise the Image Resize Class. I wont go through all the steps here to get up and running with is so check out my previous article if your interested.
// file: app/views/dvds/index.ctp <!-- <img src="/<?php echo $dvd['Dvd']['image']; ?>" alt="DVD Image: <?php echo $dvd['Dvd']['name'] ?>" width="100" height="150" /> --> <img src="/images/view/100/150/true/<?php echo $dvd['Dvd']['image']; ?>" alt="DVD Image: <?php echo $dvd['Dvd']['name'] ?>" width="100" height="150" />
Single DVD View
The view single DVD page is quite simple and will display the full details of the DVD along with the cover image. Not much has changed in the actual view.ctp file except that a new layout has been created and the page has been styled with CSS.
Single Genre View
The view single Genre page displays the DVD covers on the shelf like the main index page and includes a title to display what Genre is being displayed. The new layout has been used so that the header can be displayed without the form.
Source Code
The source code for this article can be downloaded using this link. If these articles are helping you out why not consider donating I can always use a beer! :)
Wrapping Up
Wow I've finally managed to wrap up most of the loose ends and the application is looking fairly complete, looking back I didn't think so time and effort would have gone into building an application like this but documenting every step is quite time consuming but hopefully its all been worth it.
Here a are few proposed improvements and features that I've not got round to implementing but they may get done in the future:
- Template system to change the look of the front end from the admin panel
- Amazon Lookup: When inputting a DVD automatically retreive data from Amazon
- A sprinkle of Ajax to make everything run a little more smoothly
Comments
user (05/09/2008 - 02:27)
hi, is there a full source code available for download?
James (06/09/2008 - 01:49)
The full source code has just been added.
Gerald (06/09/2008 - 21:26)
Hi!
I found this tutorial very interesting and wonderful specially for cakePHP beginners. One thing I wish you could have time considering too... that is, allowing the site visitor to search DVDs via Genre.
When I tried visiting the demo page of this tutorial series and tried the search form entering an existing genre as keyword:
"There are currently no DVDs in the database."
That's all I have in mind. Again, thanks for making this great tutorial!
user (07/09/2008 - 05:00)
thank You! :)
James (07/09/2008 - 10:11)
@Gerald: Thanks, I've not included a search via Genre because you can filter by Genre and then search through the DVDs that were found. Although adding a Genre search wouldn't be too much of a problem.
Neil Crookes (07/09/2008 - 12:07)
Hi James,
I just saw a mention of your site and your work with cake on Daniel Hofstetter's blog and thought I'd come along and check out a fellow UK baker's site. It looks good mate, so good I want to add your RSS feed to my reader - but I can't find your feed anywhere! Why don't you check out cake's rss helper and do a tutorial on it too? Keep up the good work.
Neil.
James (08/09/2008 - 02:47)
@Neil: Thanks for getting in touch, the RSS feed for the site should be available from your browser but it would be a good idea to do an article on it so that might be in the pipeline.
Kiran Aghor (18/09/2008 - 21:21)
I learnt a lot from this series.
-- creating admin section of the site
-- authentication
-- layouts, elements
-- adding a list of genres and storing them to the db(this technique was kool :)
-- uploading files with validations (great one!)
Thanx a million for this series. Please keep them coming when u get new ideas, techniques in cakephp.
James (19/09/2008 - 03:16)
@Kiran: Hey thanks for getting in touch and I'm glad the series helped you out.
Chirayu (26/09/2008 - 05:37)
Hey James. certainly excellent work. I copied source folder and restore database. working good, only thing i am not getting images in front and admin as well. but if i use absolute url , its fetching . any guess for that ?
James (27/09/2008 - 08:27)
@Chirayu: Thanks for commenting, hmmm not sure why the images aren't working. Have you tried adding a few Dvd's to the system?
Willem (09/10/2008 - 16:24)
Thanks for this great Tutorial!
Is there are pdf version planned? If not, is it okay to make one myself and give it to my friends?
Sorry for my bad english, I'm from germany and it's 1:23 ^^
James (10/10/2008 - 05:19)
@Willem: Hey thanks for commenting, a PDF is a great idea and your more than welcome to make one and send to friends. I might start compiling my own version for release on the site if I've got the time.
ProLoser (02/11/2008 - 17:15)
Hey I was looking at your blog and you seemed to have accomplished something I'm having one horrible hell of a time getting past: filtering by HABTM relationships for my search results. Especially for more than 1 filter. I want to know how you did it and I can't seem to locate the source code you said was available. Any help appreciated, thanks.
James (03/11/2008 - 13:46)
@ProLoser: Source code is at the bottom of every post in a zip file for the CakeCatalog, filtering by HABTM is quite tricky as you cant really write a custom sql statement to filter results, what I had to do is grab all the results and filter them manually in the Controller. If you check out the main dvds_controller.php you should be able to see whats going on.