I spent a little bit of time testing out two CakePHP e-commerce solutions that I found today: BakeSale and CakeCart. I installed the latest versions of each and attempted to play around as much as I could. BakeSale seems to be the more mature of the two, but I couldn’t get BakeSale to install. I posted some on each of their forums with the issues I had while installing. So far I dont think either are a good solution for professional installations.
We had a client recently request that we embed an RSS feed from a different site inside their WordPress install. This would’ve been very easy considering there are 100’s of RSS feed plugins that will accomplish this task. However, they wanted to only display RSS items that had certain keywords in the title. My initial search found no WP plugins that currently were capable of this. I also tried one of the more popular ones, FeedList, but it used the get_rss() function in the WordPress API. For some reason, this function was not properly parsing the feed they wanted to use, most likely due to it not being a valid feed.
So to make a long story short: I decided to make a quick hack up solution and write my own WP plugin. My only hurdle-like requirement is that it had to work for PHP4 as well because the client is on one of our older servers. I ended up finding a really good example by Ryan Stemkoski. He wrote a perfect function for me to tweak a little and expand upon. I am currently waiting to get approved on WordPress extend so I can host my plugin there. For now, I have created a page here where you can download the plugin.
Note: I haven’t tested this in a more recent version of WP yet but I will be soon. Let me know if you find any bugs!
I also figured I would post the source code in case someone needed a quick and dirty solution with some examples.
rss_lib.php
<?php
/*
* RSS Parsing Function
*
* Original Author: Ryan Stemkoski
* http://www.stemkoski.com/how-to-easily-parse-a-rss-feed-with-php-4-or-php-5/
*
* Slight modifications by Jesse R. Adams (DualTech Services, Inc.)
*/
function parseRSS($url) {
//PARSE RSS FEED
$feedeed = implode('', file($url));
$parser = xml_parser_create();
xml_parse_into_struct($parser, $feedeed, $valueals, $index);
xml_parser_free($parser);
//CONSTRUCT ARRAY
foreach($valueals as $keyey => $valueal){
if($valueal['type'] != 'cdata') {
$item[$keyey] = $valueal;
}
}
$i = 0;
foreach($item as $key => $value){
if($value['type'] == 'open') {
$i++;
$itemame[$i] = $value['tag'];
} elseif($value['type'] == 'close') {
$feed = $values[$i];
$item = $itemame[$i];
$i--;
if(count($values[$i])>1){
$values[$i][strtolower($item)][] = $feed;
} else {
$values[$i][strtolower($item)] = $feed;
}
} else {
$values[$i][strtolower($value['tag'])] = $value['value'];
}
}
//RETURN ARRAY VALUES
return $values[0];
}
/*
* Convert Raw RSS Parse into concise list of items
*
* Optionally pass in limit (on number of items)
* and/or array list of words to grab items by.
*
*/
function extractRSSItems($xmlArray, $limit = null, $filteredWords = array()) {
$array = array();
$regex = null;
if (count($filteredWords) > 0) {
$regex = implode('|', $filteredWords);
}
foreach($xmlArray['rss']['channel']['item'] as $item) {
if (!$regex || ($regex && preg_match("/$regex/i", $item['title']))) {
$array[] = $item;
}
}
if($limit) {
array_splice($array, ($limit - 1), (count($array) - $limit));
}
return $array;
}
?>
selective_rss.php
<?php
/*
Plugin Name: Selective RSS
Plugin URI: http://techno-geeks.org/selective-rss
Description: Simple Plugin that allows you to embed RSS feed items into Pages or Posts. Optionally allows you to choose how many items to display and allows you to limit items to ones that contain certain words in the titles.
Version: 0.1.0b
Author: Jesse R. Adams (DualTech Services, Inc.)
Author URI: http://www.dual-tech.com/about-dualtech-services/
*/
require_once('rss_lib.php');
function filter_feeds($content) {
$limit = null;
$filter = array();
$url = null;
$inputTag = preg_match('/\[srss (.+)\]/', $content, $matches);
$rawArgs = explode(',', $matches[1]);
foreach($rawArgs as $input) {
preg_match_all('/^(url|filter|limit)=(.+)$/i', $input, $matches);
$key = $matches[1][0];
$value = $matches[2][0]
if ($key == 'filter') {
$filter = explode(';', $value);
} else {
$$key = $value;
}
}
if ($url) {
$xml = parseRSS($url);
$items = extractRSSItems($xml, $limit, $filter);
$htmlToAdd = '';
foreach ($items as $item) {
$htmlToAdd .= '<a href="' . $item['link'] . '" target="new">' . $item['title'] . '</a>' . "<br/>\n";
$htmlToAdd .= $item['description'] . "<br/><br/>\n";
}
$content = preg_replace('/\[srss .+\]/', $htmlToAdd, $content);
}
return $content;
}
add_filter('the_content', 'filter_feeds');
?>
Update 06/19: Fixed the way arguments are parsed to be less limiting on possibilities.
It has been a long time (2-3 years) since I have had access to my turntables and vinyl with undamaged needles. I finally got around to mixing a few trance vinyl I have from way back in the day. I rushed the mixing so it doesn’t sound very good. I am mainly posting this because I like the songs and I want to use it as a place holder to see my progress. Hope you enjoy!
There are currently three methods for including and using models inside your controller. I am referring to the case when your controller does not belong to a model or or the model is not related to the model inside the controller you are working in. Although this case should be extremely rare, it seems to happen to me a lot. This post is basically a re-summary of Gwoo’s response in the CakePHP Google Groups discussion.
1. Controller::loadModel();
This method actually calls ClassRegistry::init() and saves the instance of the model as a parameter in the controller.
3. App:import() (not recommended)
This method only includes the file. This requires you to create an instance each time.
Example:
App::import('Car');
$car = new Car();
$cars = $car->find('all');
As you can see this is really simple and basic stuff. However, I felt the need to re-post this information. If for nothing else, I will be using this as a reference point for myself
Update 5/27/09: Miles Johnson has a nice write up on this on his blog
I was having some issues today with character encoding and I thought I’d post about it. There are three elements that you need to make sure you have if you want to properly use UTF-8 character encoding in your Cake app. My issue was that I did not have #2, although adding #1 didn’t hurt either.
1. Database Config
Add ‘encoding’ => ‘utf8′ to app/config/database.php