<?php 
/**
 * cfrssget.php
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by      
 * the Free Software Foundation; either version 2 of the License, or         
 * (at your option) any later version.                                       
 *
 *
 * @author vuud
 * @package cpmfetch
 * @since 1.6.4
 * @version $Revision: 1.13 $
 *
 */

/**
* Include the needed libraries
*/
include "cpmfetch.php";
include "libs/feedcreator.class.php";


//error_reporting(E_ALL);
//ini_set('display_errors',0);  // 0 is off, 1 is on


$ENABLED = false;			//Set to false if you will be creating standalone feed modules

if ($ENABLED) {

		$default_command = "last";				//What to do if no cmd is specified
		// THESE SPECIFY THE INFORMATION USED IN YOUR FEED
	  // You only need to use this if you will be using this file as a feed
		
		$rssFeedTitle = "FistFullOfCode Photo Gallery";
		$rssFeedDescription = "The last 25 photos added to Fist Full Of Code.";
		$rssFeedSiteUrl = "http://www.xfistfullofcode.com";
		$rssFeedProviderUrl = "http://www.xfistfullofcode.com/cftest/rss_lastadded.php";
		$rssFeedImageTitle = "FFOC Logo";
		$rssFeedImageURL = 'http://www.xfistfullofcode.com/images/ffoc_logo.gif';
		$rssFeedImageLink = "http://www.xfistfullofcode.com";
		$rssFeedImageAlt = "logo";
		
		$rssItemTitle = "{{cName}} %a :: %t";
		$rssItemAlternateTitle = "%a :: %f";
		
		$rssItemDescription = "%f from album %a<br/>Filesize %S kb<br/>%c<br/>Rated %V / 5 stars (%v votes total)";
		$rssItemAlternateDescription = "%f from %a<br/>Filesize %S kb<br/>%a: %d<br/>Rated %V / 5 stars (%v votes total)";
		$totalToShow = 5;

		///// END OF USER SETTINGS /////////
	
		$cmd_parameter = "last";

		if (array_key_exists("cmd",$_GET)) {
			switch ($_GET['cmd']) {
				case ('mostviewed'):
				case ('toprated'):
				case ('last'):
				case ('random'):
				$cmd_parameter = $_GET['cmd'];
				break;
				
			default:
				$cmd_parameter = $default_command;
				break;
			}		
		} else {
			$cmd_parameter = $default_command;
		}
	
		$cfrss = new cfrss();
		$cfrss->startFeed($rssFeedTitle, $rssFeedDescription, $rssFeedSiteUrl, $rssFeedProviderUrl);
		$cfrss->setFeedImage($rssFeedImageTitle,$rssFeedImageURL,$rssFeedImageLink,$rssFeedImageAlt);
		$cfrss->setItemTitle($rssItemTitle,$rssItemAlternateTitle);
		$cfrss->setItemDescription($rssItemDescription,$rssItemAlternateDescription);

		switch ($cmd_parameter) {
				case ('mostviewed'):
						$cfrss->addMostViewedMedia($totalToShow);
						break;
				case ('toprated'):
						$cfrss->addTopRatedMedia($totalToShow); 
						break;		
				case ('last'):
						$cfrss->addLastAddedMedia($totalToShow);
						break;
				case ('random'):
						$cfrss->addRandomMedia($totalToShow);
						break;
				default:
						break;	
			}				

			$cfrss->endFeed();
}


////////// The cfrss object itself ///////////////////

class cfrss {

	var $cpm = "";
	var $rss = "";

//	var $urltocpm = "";       //from a web client point of view without domain name /photos
//	var $fullUrlToCpm = "";
//	var $filepathtocpm = "";  //server path to cpm directory  /home/vuud/cpm

	var $version = "2.0.0";	

	var $defaultimagesize = "thumb_";
	
	var $itemTitle = "";
	var $itemAltTitle = "";
	var $itemDescription = "";
	var $itemAltDescription = "";
	
	function cfrss ($config_file = "") {
			$this->cpm = new cpm($config_file);
			$this->cpm->cpm_setReturnType("resultset");
			$this->rss = new UniversalFeedCreator();
			$this->rss->encoding = $this->cpm->cpm_getConfigEntry('charset');
	}
	
	function startFeed($title,$description,$link,$syndicationUrl) {
		$this->rss->title = $title;
		$this->rss->description = $description;
		$this->rss->link = $link;
		$this->rss->syndicationURL = $syndicationUrl;

	}
	
	function setFeedImage($imgTitle,$imgUrl,$imglink,$imgDescription) {
		$image = new FeedImage();
		$image->title = $imgTitle;
		$image->url = $imgUrl;
		$image->link = $imglink;
		$image->description = $imgDescription;
		$this->rss->image = $image; 
	}
	
	
	function setItemTitle ($pattern,$altpattern="") {
		$this->itemTitle = $pattern;
		if($altpattern == "") {
			$this->itemAltTitle = $pattern; 
		} else {
			$this->itemAltTitle = $altpattern; 
		}
	}
	
	function setItemDescription ($pattern,$altpattern="") {
		$this->itemDescription = $pattern;
		
		if($altpattern == "") { 
			$this->itemAltDescription = $pattern; 
		} else {
			$this->itemAltDescription = $altpattern;
		}
	}
	
	function endFeed( ) {
		$this->rss->saveFeed("RSS1.0", "./tmp/feed.xml"); 
		$this->cpm->destroy();
	}
				
	function addLastAddedMedia($count,$source="") {
		$results = $this->cpm->cpm_viewLastAddedMedia($count,1,$source);
		$this->generateFeedFromResults($results);
  	}

	function addTopRatedMedia($count,$source="") {
		$results = $this->cpm->cpm_viewTopRatedMediaFrom($source,$count,1);
		$this->generateFeedFromResults($results);
	}

	function addMostViewedMedia ($count,$source="") {
		$results = $this->cpm->cpm_viewMostVotedMediaFrom($source,$count,1);
		$this->generateFeedFromResults($results);
	}

	function addRandomMedia ($count,$source="") {
		$results = $this->cpm->cpm_viewRandomMediaFrom($source,$count,1);
		$this->generateFeedFromResults($results);
	}

function generateFeedFromResults($results) {
   	foreach ($results as $data) {

		$item = new FeedItem(); 
    		
  			$item->link =  $this->cpm->createLink($data['pFilepath'], rawurlencode($data['pFilename']), $data['pAid'], $data['pPid']);
						
			$titleToUse = "";
			$descriptionToUse="";

					
			if (($titleToUse = $this->cpm->createDescription($this->itemTitle,$data,true)) === false) {
				$titleToUse = $this->cpm->createDescription($this->itemAltTitle,$data);
			}

			if (($descriptionToUse = $this->cpm->createDescription($this->itemDescription,$data,true)) === false) {
					$descriptionToUse = $this->cpm->createDescription($this->itemAltDescription,$data);	
			}

			$imagetag = '<a href="' . $item->link . '"><img src="'.
				 $this->cpm->urlEncodeImagePath($this->cpm->getImageToUse($data['pFilepath'],$data['pFilename'], $this->cpm->cfg['thumb_pfx'])) . '" align="right" /></a>';

			$descriptionToUse = $imagetag . $descriptionToUse;
		
			$item->title = $titleToUse;
			
			$item->description = $descriptionToUse;
    
			$item->date = date("r",$data['pCtime']);
			$item->source = $this->cpm->cfg['cpg_url'];
			$item->author = $data['pOwner_name'];
 			
			$this->rss->addItem($item); 
		}

	}	

} // end of class


?>
