Archive for March, 2011

CMS

March 15th, 2011 | Category: websites/programming

So I have decided to create a CMS. Content Management System. Ok, I have been trying to come up with a content management system for some time and have just ended up running in circles. I have made about 4 different versions of a mod_rewrite type template and page separate system for my various websites. I think it’s time I listed out some goals.

CMS Goals:

The System Must:

  • Be OOPhp
  • Support MVC type architecture
  • Have a Database Abstraction layer in the Model that supports serialization for database-less (or at least sqlite) setups.
  • Support Templates
  • Support Manual Page Creation
  • Support XML type page creation
  • Support Php in pages.
  • Support non mod_rewrite installations.
  • Provide sitemap and rss feed.

The CMS should have (but may not in beta):

  • a dashboard

The CMS would be nice if it had:

  • a js dropdown style dashboard.
  • calendar support

 

List of notes:

VIEW       -> Templates
CONTROLLER -> Pages
MODEL      -> Abstract -> DB or Serialized Data Objects

Need to have a repo for universal functions outside of an object.
Needs to have a page data object that holds general data about each page. Autobuilt for manually created pages.

CMS Object controls all:


Controller Object deals with client pages/code as well as model calls

View  Object controls templating


Model Object controls access to saved data


Helper Object gives other Objects the ability to add multi controller/view code


System Object deals with UI and other system level stuff

PAGE


CONTROLLER

VIEW

Use a common template grabber

HELPER

 

Comments are off for this post

RSS and PHP

March 12th, 2011 | Category: websites/programming

Guess, what? It’s time for another post on php. This time our subject is RSS. First off, what is rss? I will skip the wikipedia definition and say that rss is a type of simple xml that search engines, web browsers and programs use to poll your site for news updates. RSS feeds are formatted in the XML format, meaning the layout of the file is very similar to html. If you are using firefox to view this webpage, the little orange square in the address bar is an example of an RSS feed. Let’s take a look at some example rss:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
 <channel>
  <title>Teslamad News Feed</title>
  <description>All things Teslamad</description>
  <link>http://www.teslmad.com</link>
  <pubDate>Mon, 02 Feb 2011 16:45:00 +0700 </pubDate>
  <item>
   <title>2.5W white laser</title>
   <description>New page added about our most recent adventure with the 2.5W white laser....</description>
   <link>http://www.teslamad.com/lasers/white25w</link>
   <pubDate>Tue, 22 Feb 2011 00:00:00 -0500</pubDate>
  </item>
  <item>
   <title>different event</title>
   <description>description</description>
   <link>http://www.example.com/news</link>
   <pubDate>Tue, 24 Feb 2011 11:18:23 -0500</pubDate>
  </item>
 </channel>
</rss>

So what is this? It is an RSS feed with two items. Each item is considered to be a different news post, it has its own publication date, title, description, and link to a page that has the whole story. Cool, huh?

What if we had a news bar on our webpage that we update weekly? If we wanted to add RSS we would need to either update each by hand, or write some php to handle both. I would like to evaluate both outcomes of this idea separately. First we will look at making each feed item directly in php and using some echo commands to output rss type formatting when a client wants the news in RSS. Then we will look at making an RSS feed and using the simpleXML extension, parse the file into php code for use in our html newsfeed output. I like the second method from a server time standpoint. The RSS feed is usually going to get more access than any given page in a medium or small site, so it makes sense to have it a flat file. That said, a large site with many users may prefer the php side due to the ease of using a database. For example, both WordPress and Drupal use a (much more detailed) version of the first method that we shall see. Their implementation uses a database to store each news item rather than storing them directly in a php file and format like I am about to show.

So first we shall look at the php to rss approach:

<?php
class Rss{
 public $title;
 public $description;
 public $link;
 public $pubDate;
 public $items = array();

 public function __construct($title,$description,$link,$pubDate){
  $this->title = $title;
  $this->description = $description;
  $this->link = $link;
  $this->pubDate = new DateTime($pubDate);
 }
 public function addItem($item){
  if(is_object($item))
   $this->items[] = $item;
 }
 public function asXML(){
  $output.= '<?xml version="1.0" encoding="UTF-8" ?>'."\r\n";
  $output.= '<rss version="2.0">'."\r\n";
  $output.= '<channel>'."\r\n";
  $output.= '<title>'.$this->title.'</title>'."\r\n";
  $output.= '<description>'.$this->description.'</description>'."\r\n";
  $output.= '<link>'.$this->link.'</link>'."\r\n";
  $output.= '<pubDate>'.$this->pubDate->format('D, d M Y H:i:s O').'</pubDate>'."\r\n";
  foreach($this->items as $feed)
   $output.= $feed->asXML();
  $output.=  '</channel>'."\r\n";
  $output.=  '</rss>'."\r\n";
  return $output;
 }
}
class feed{
 public $title;
 public $description;
 public $link;
 public $pubDate; 

 public function  __construct($pubDate,$description,$link,$title = null) {
  $this->description = $description;
  $this->link = $link;
  $this->pubDate = new DateTime($pubDate);
  if($title)
   $this->title = $title;
  else
   $this->title = substr($description,0,30).'...';
 }
 public function asXML(){
  $output.= '<item>'."\r\n";
  $output.= '<title>'.$this->title.'</title>'."\r\n";
  $output.= '<description>'.$this->description.'</description>'."\r\n";
  $output.= '<link>'.$this->link.'</link>'."\r\n";
  $output.= '<pubDate>'.$this->pubDate->format('D, d M Y H:i:s O').'</pubDate>'."\r\n";
  $output.= '</item>'."\r\n";
  return $output;
 }
}
function newsfeed(){
 $rss = new Rss('Teslamad News Feed','All things teslamad','http://www.teslamad.com','3/11/11');
 $rss->addItem(new feed('02/22/2011','New page added about our most recent adventure with the 2.5W white laser.','http://www.teslamad.com/lasers/white25w'));
 $rss->addItem(new feed('02/03/2011','Finally adjusted the size of the videos embeded throughout the site. Enjoy','http://www.teslamad.com/faraday/faraday'));
 $rss->addItem(new feed('01/05/2011','Just added a new video to the faraday page. The left sleeve is almost complete!','http://www.teslamad.com/faraday/faraday'));
 $rss->addItem(new feed('01/01/2011','HAPPY NEW YEAR',null));
 $rss->addItem(new feed('12/10/2010','The faraday suit page is now up and running and I finally started the video series of the contruction  progress.','http://www.teslamad.com/faraday/faraday'));
 echo $rss->asXML();
}
newsfeed();
?>

Ok, now what on earth did we just do? In short, we just took five lines of php input and used them to create an RSS feed (page). The feed objects are the actual news updates and they are all considered elements in our Rss object. Finally, we use the asXML function call to get the RSS output. The call is made to the Rss object which in turn calls the asXML function of each of the feed objects. The output is xml in the RSS format. If we wanted to add another post to our newsfeed, we would follow the same pattern in the php script in the newsfeed() function:

$rss->addItem(new feed('date','description','link','title'));

This allows us to make a php object into an RSS newsfeed. But what about our html side?  This code does not make our php news sidebar work. We need to add some functions to our code in respect to html so we can output a useful html newsfeed.

<?php
class Rss{
 public $title;
 public $description;
 public $link;
 public $pubDate;
 public $items = array();

 public function __construct($title,$description,$link,$pubDate){
  $this->title = $title;
  $this->description = $description;
  $this->link = $link;
  $this->pubDate = new DateTime($pubDate);
 }
 public function addItem($item){
  if(is_object($item))
   $this->items[] = $item;
 }
 public function asXML(){
  $output.= '<?xml version="1.0" encoding="UTF-8" ?>'."\r\n";
  $output.= '<rss version="2.0">'."\r\n";
  $output.= '<channel>'."\r\n";
  $output.= '<title>'.$this->title.'</title>'."\r\n";
  $output.= '<description>'.$this->description.'</description>'."\r\n";
  $output.= '<link>'.$this->link.'</link>'."\r\n";
  $output.= '<pubDate>'.$this->pubDate->format('D, d M Y H:i:s O').'</pubDate>'."\r\n";
  foreach($this->items as $feed)
   $output.= $feed->asXML();
  $output.=  '</channel>'."\r\n";
  $output.=  '</rss>'."\r\n";
  return $output;
 }
 public function asHTML(){
  $output.= '<h1>'.$this->title.'</h1>';
  foreach($this->items as $feed)
   $output.= $feed->asHTML();
  return $output;
 }
}
class feed{
 public $title;
 public $description;
 public $link;
 public $pubDate;

 public function  __construct($pubDate,$description,$link,$title = null) {
  $this->description = $description;
  $this->link = $link;
  $this->pubDate = new DateTime($pubDate);
  if($title)
   $this->title = $title;
  else
   $this->title = substr($description,0,30).'...';
 }
 public function asXML(){
  $output.= '<item>'."\r\n";
  $output.= '<title>'.$this->title.'</title>'."\r\n";
  $output.= '<description>'.$this->description.'</description>'."\r\n";
  $output.= '<link>'.$this->link.'</link>'."\r\n";
  $output.= '<pubDate>'.$this->pubDate->format('D, d M Y H:i:s O').'</pubDate>'."\r\n";
  $output.= '</item>'."\r\n";
  return $output;
 }
 public function asHTML(){
  $output.= '<span>'.$this->pubDate->format('M/j/Y').'</span>'."\r\n";
  $output.= '<p>'.$this->title.'<br />'."\r\n";
  $output.= '<a href="'.$this->link.'">'.$this->link.'</a></p>'."\r\n";
  $output.= '<hr><br />'."\r\n";
  return $output;
 }
}
function newsfeed($type){
 $rss = new Rss('Teslamad News Feed','All things teslamad','http://www.teslamad.com','3/11/11');
 $rss->addItem(new feed('02/22/2011','New page added about our most recent adventure with the 2.5W white laser.','http://www.teslamad.com/lasers/white25w.html'));
 $rss->addItem(new feed('02/03/2011','Finally adjusted the size of the videos embeded throughout the site. Enjoy','http://www.teslamad.com/faraday/faraday.html'));
 $rss->addItem(new feed('01/05/2011','Just added a new video to the faraday page. The left sleeve is almost complete!','http://www.teslamad.com/faraday/faraday.html'));
 $rss->addItem(new feed('01/01/2011','HAPPY NEW YEAR',null));
 $rss->addItem(new feed('12/10/2010','The faraday suit page is now up and running and I finally started the video series of the contruction progress.','http://www.teslamad.com/faraday/faraday.html'));
 if($type == 'XML')
  echo $rss->asXML();
 else
  echo $rss->asHTML();
}
newsfeed('HTML');
?>

This addition to our code makes it possible for us to add our news page to the site and output an rss feed with the same code. Yay! What I love is all we did is add two functions to the classes and we increased functionality. Some of you may have noticed the use of the Datetime object, if you would like to know more about it, check out the php.net page on it and rejoice. I wish it had been around 6 years ago.

Moving on, let’s look at the RSS to php newsfeed option, the code is intentionally similar, but we are going to use an existing rss file that is static. This file is loaded using the simplexml_load_file command which creates a new simpleXML object. Let’s take a look at our rss input:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
 <title>Teslamad News Feed</title>
 <description>All things Teslamad</description>
 <link>http://www.sfskies.com/files/CMS3/</link>
 <pubDate>Mon, 02 Feb 2011 16:45:00 +0700 </pubDate>
 <item>
  <title>New page added about our most recent adventure with the 2.5W white laser....</title>
  <description>New page added about our most recent adventure with the 2.5W white laser....</description>
  <link>http://www.sfskies.com/files/CMS3/index.php?q=lasers/white25w</link>
  <pubDate>Tue, 22 Feb 2011 00:00:00 -0500</pubDate>
 </item>
 <item>
  <title>Finally adjusted the size of the videos embeded throughout the site. Enjoy...</title>
  <description>Finally adjusted the size of the videos embeded throughout the site. Enjoy...</description>
  <link>http://www.sfskies.com/files/CMS3/index.php?q=faraday/faraday</link>
  <pubDate>Thu, 03 Feb 2011 00:00:00 -0500</pubDate>
 </item>
 <item>
  <title>Just added a new video to the faraday page. The left sleeve is almost complete!...</title>
  <description>Just added a new video to the faraday page. The left sleeve is almost complete!...</description>
  <link>http://www.sfskies.com/files/CMS3/index.php?q=faraday/faraday</link>
  <pubDate>Wed, 05 Jan 2011 00:00:00 -0500</pubDate>
 </item>
 <item>
  <title>HAPPY NEW YEAR...</title>
  <description>HAPPY NEW YEAR...</description>
  <link>http://www.sfskies.com/files/CMS3/index.php?q=</link>
  <pubDate>Sat, 01 Jan 2011 00:00:00 -0500</pubDate>
 </item>
 <item>
  <title>The faraday suit page is now up and running and I finally started the video series of the contructio...</title>
  <description>The faraday suit page is now up and running and I finally started the video series of the contructio...</description>
  <link>http://www.sfskies.com/files/CMS3/index.php?q=faraday/faraday</link>
  <pubDate>Fri, 10 Dec 2010 00:00:00 -0500</pubDate>
 </item>
</channel>
</rss>

Now that we have our input, let’s take a look at the php:

<?php
class rss{
 public $title;
 public $description;
 public $link;
 public $lastBuildDate;
 public $pubDate;
 public $items = array();

 public function __construct($file){
  $xml                 = simplexml_load_file($file);
  $this->title         = $xml->channel->title;
  $this->description   = $xml->channel->description;
  $this->link          = $xml->channel->link;
  $this->lastBuildDate = $xml->channel->lastBuildDate;
  $this->pubDate       = $xml->channel->pubDate;

  foreach($xml->channel->item as $feed)
   $this->items[] = new feed($feed);
 }
 public function asHTML(){
  $output.= '<h1>'.$this->title.'</h1>';
  foreach($this->items as $item)
   $output.= $item->asHTML();
  return $output;
 }
}
class feed{
 public $title;
 public $description;
 public $link;
 public $guid;
 public $pubDate;

 public function __construct($feed){
  $this->title       = $feed->title;
  $this->description = $feed->description;
  $this->link        = $feed->link;
  $this->guid        = $feed->guid;
  $this->pubDate     = new DateTime($feed->pubDate);
 }
 public function asHTML(){
  $output.= '<span>'.$this->pubDate->format('M/j/Y').'</span>'."\r\n";
  $output.= '<p>'.$this->title.'<br />'."\r\n";
  $output.= '<a href="'.$this->link.'">'.$this->link.'</a></p>'."\r\n";
  $output.= '<hr><br />'."\r\n";
  return $output;
 }
}
$rss = new rss('teslamad.rss');
echo $rss->asHTML();
?>

So our rss to newsfeed code uses very similar elements to the php to rss method, but they differ in the source of the data. In the first example, the data is written in php, in the second, the php gets the data from an rss file. It just depends on the designer’s use and comfort level with each method.

Comments are off for this post