<?php
/*

	This class uses the SocialActions.org API to pull down related events/things going on that might be of interest

*/

require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php"); 

define("BASE_URL","http://search.socialactions.com/actions.json");
define("EXCLUDE_QUERY_TERMS"," -animals -pets -pet -animal -cat -dog -cats -dogs -puppy -kitten -rats -rat -vet -adoption -vegan");
define("LIMIT",10); //Results to return

define("SITE_WHITELIST","830666864,285985636,844796856,357620952,13110294,906305602");


Class SocialActions{
	/*
		Where:
			830666864 = Amazee 
			285985636 = Democracy In Action
			844796856 = Delicious
			357620952 = VolunteerMatch
			13110294 = Twitter ("#SocialAction")
			906305602 = The Point
	*/
	//Also of interest is the action_types paramater that is used to only show relevant types of social actions

	private $url,$paramaters,$query;
	public $actions;

	public function __construct(){
		$this->url = BASE_URL."?q=";
		$this->paramaters = urlencode(EXCLUDE_QUERY_TERMS)."&sites=".SITE_WHITELIST."&order=created_at&action_types=2,4,6,8&limit=".LIMIT;
		$actions = array();
	}
	
	public function search($q){
		$data = json_decode(curl_get_file_contents($this->url.urlencode("health").$this->paramaters));
		foreach($data as $row){
		
			$action['title'] = $row->title;
			$action['url'] = $row->url;
			$action['type'] = $row->action_type->name;
			//Strip out all but "white space" tags + bold
			$action['description'] = strip_tags($row->description,"<br><b><p>");
			
			//Create spaces from p tags + bold
			$action['description'] = str_replace(array("<b>","<p>")," ",$action['description']);
			$action['description'] = strip_tags($action['description'],"<br>"); //Remove all but breaks
			
			//Strip out non-description
			$strip_phrases = array(
				" Language: English",
				"[Read More]I want to help!&nbsp;&nbsp;|&nbsp;&nbsp;Donate to VolunteerMatch",
				"[Read More] I want to help!&nbsp;&nbsp;|&nbsp;&nbsp;Donate to VolunteerMatch"
				);
			$action['description'] = str_replace($strip_phrases,"",$action['description']);
			
			
			$action['created_at'] = strtotime($row->created_at);
			
			$id = substr($action['url'],-(strlen($action['url']) - strrpos($action['url'],'/') - 1)); //Create unique ID using search result URL
			$this->actions[$id] = $action;
			
		}
	}
	
	
}
?>