Skip to content

Commit

Permalink
Added file upload functionality to SwiftRiver core
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimateprogramer committed May 26, 2011
1 parent 31c23c8 commit 830f50b
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 34 deletions.
121 changes: 121 additions & 0 deletions Modules/SiSPS/PushParsers/CSVPushParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
namespace Swiftriver\Core\Modules\SiSPS\PushParsers;
class CSVPushParser implements IPushParser
{
/**
* Implementation of IPushParser::PushAndParse
* @param $raw_content
* @param $post_content
* @param $get_content
* @return \Swiftriver\Core\ObjectModel\Content[] contentItems
*/
public function PushAndParse($raw_content = null, $post_content = null, $get_content = null, $file_content = null)
{
$logger = \Swiftriver\Core\Setup::GetLogger();

$logger->log("Core::Modules::SiSPS::PushParsers::CSVParser::PushAndParse [Method invoked]", \PEAR_LOG_INFO);
$logger->log("Core::Modules::SiSPS::PushParsers::CSVParser::PushAndParse [START: Extracting required parameters]", \PEAR_LOG_INFO);

$contentItems = array();

$settings = $this->GetSettings();

$file_name = "file_upload_".$settings["file_upload_id"];

if(!isset($file_content[$file_name])) {
$logger->log("Core::Modules::SiSPS::PushParsers::CSVParser::PushAndParse [No file uploaded with file_upload file name]", \PEAR_LOG_ERR);

return $contentItems;
}

$logger->log("Core::Modules::SiSPS::PushParsers::CSVParser::PushAndParse [END: Extracting required parameters]", \PEAR_LOG_INFO);

$file_handle = fopen($file_content[$file_name]["tmp_name"], "r");

if($file_handle) {
while (($data = fgetcsv($file_handle, 0, ",")) !== FALSE) {
$title = $data[0];
$text = $data[1];
$link = $data[2];
$date = $data[3];

if(strrpos($date, "-")) {
// Convert date to timestamp
$date = explode("-", $date);
$date = strptime($date[0]."-".$date[1]."-".$date[2], "%Y-%m-%d");
$date = mktime(0, 0, 0, $date['tm_month'], $date['tm_day'], $date['tm_year']);
}

$source_name = $this->ReturnType();
$source = \Swiftriver\Core\ObjectModel\ObjectFactories\SourceFactory::CreateSourceFromIdentifier($source_name, $settings["trusted"]);
$source->parent = $this->ReturnType();
$source->name = $source_name;
$source->link = $link;
$source->type = $this->ReturnType();
$source->subType = $this->ReturnType();

//Create a new Content item
$item = \Swiftriver\Core\ObjectModel\ObjectFactories\ContentFactory::CreateContent($source);

//Fill the Content Item
$item->text[] = new \Swiftriver\Core\ObjectModel\LanguageSpecificText(
null, //here we set null as we dont know the language yet
$title,
array($text));
$item->link = $link;
$item->date = $date;

//Add the item to the Content array
$contentItems[] = $item;
}
}
else {
$logger->log("Core::Modules::SiSPS::PushParsers::CSVParser::PushAndParse [Method finished]", \PEAR_LOG_INFO);

return $contentItems;
}

fclose($file_handle);


//return the content array
return $contentItems;
}

/**
* This method returns a string describing the implementation details
* of this parser
*
* @return string - implementation details
*/
public function GetDescription() {
return "This plugin allows you to upload CSV files and store them on SwiftRiver";
}

/**
* This method return an array of fields needed to implement the push mechanism that
* may be rendered by the UI framework (such as Sweeper / SwiftMeme and others).
*
* Returns a null if no field is required
*
* @return array[] of fields
*/
public function GetSettings() {
return array("trusted" => true,
"file_upload" => true,
"upload_path" => "api/plugins/contentpush/contentpush.php?origin=".$this->ReturnType(),
"file_upload_id" => $this->ReturnType());
}

/**
* This method returns a string describing the type of sources
* it can parse. For example, the FeedsParser returns "Feeds".
*
* @return string type of sources parsed
*/
public function ReturnType()
{
return "CSV";
}
}
?>
18 changes: 12 additions & 6 deletions Modules/SiSPS/PushParsers/IPushParser.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

/**
* @author am[at]swiftly[dot]org
*/

* @author am[at]swiftly[dot]org
*/
namespace Swiftriver\Core\Modules\SiSPS\PushParsers;
interface IPushParser{
/**
Expand All @@ -13,9 +11,10 @@ interface IPushParser{
* @param String $raw_content (if content gets sent raw)
* @param String $post_content (if content gets sent as HTTP POST)
* @param String $get_content (if content gets sent as HTTP GET)
* @param String $file_content (if content gets sent a file over HTTP upload)
* @return Swiftriver\Core\ObjectModel\Content[] contentItems
*/
public function PushAndParse($raw_content = null, $post_content = null, $get_content = null);
public function PushAndParse($raw_content = null, $post_content = null, $get_content = null, $file_content = null);

/**
* This method returns a string describing the implementation details
Expand All @@ -25,6 +24,13 @@ public function PushAndParse($raw_content = null, $post_content = null, $get_con
*/
public function GetDescription();

/**
* This function allows us to get the settings for each parser
*
* @return settings[]
*/
public function GetSettings();

/**
* This method returns a string describing the type of sources
* it can parse. For example, the RSSParser returns "Feeds".
Expand All @@ -33,4 +39,4 @@ public function GetDescription();
*/
public function ReturnType();
}
?>
?>
32 changes: 20 additions & 12 deletions Modules/SiSPS/PushParsers/QuiverPushParser.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<?php

/**
* @author am[at]swiftly[dot]org
*/

namespace Swiftriver\Core\Modules\SiSPS\PushParsers;
class QuiverPushParser implements IPushParser
{
Expand All @@ -14,18 +9,18 @@ class QuiverPushParser implements IPushParser
* @param $get_content
* @return \Swiftriver\Core\ObjectModel\Content[] contentItems
*/
public function PushAndParse($raw_content = null, $post_content = null, $get_content = null)
public function PushAndParse($raw_content = null, $post_content = null, $get_content = null, $file_content = null)
{
$logger = \Swiftriver\Core\Setup::GetLogger();
$logger->log("Core::Modules::SiSPS::PushParsers::QuiverParser::PushAndParse [Method invoked]", \PEAR_LOG_DEBUG);

$logger->log("Core::Modules::SiSPS::PushParsers::QuiverParser::PushAndParse [Method invoked]", \PEAR_LOG_DEBUG);
$logger->log("Core::Modules::SiSPS::PushParsers::QuiverParser::PushAndParse [START: Extracting required parameters]", \PEAR_LOG_DEBUG);

$settings = $this->get_settings();
$settings = $this->GetSettings();

$source_name = $this->ReturnType();
$source = \Swiftriver\Core\ObjectModel\ObjectFactories\SourceFactory::CreateSourceFromIdentifier($source_name, $settings["trusted"]);
$source->parent = "Quiver";
$source->parent = $this->ReturnType();
$source->name = $source_name;
$source->link = $get_content["u"];
$source->type = $this->ReturnType();
Expand All @@ -50,8 +45,9 @@ public function PushAndParse($raw_content = null, $post_content = null, $get_con
return $contentItems;
}

private function get_settings() {
return array("trusted" => true);
public function GetSettings() {
return array("trusted" => true,
"file_upload" => false);
}

private function get_quiver_link() {
Expand Down Expand Up @@ -92,6 +88,18 @@ public function GetDescription() {
return "Drag and drop this link to your bookmarks toolbar: ".$description;
}

/**
* This method return an array of fields needed to implement the push mechanism that
* may be rendered by the UI framework (such as Sweeper / SwiftMeme and others).
*
* Returns a null if no field is required
*
* @return array[] of fields
*/
public function GetFields() {
return null;
}

/**
* This method returns a string describing the type of sources
* it can parse. For example, the FeedsParser returns "Feeds".
Expand All @@ -103,4 +111,4 @@ public function ReturnType()
return "Quiver";
}
}
?>
?>
17 changes: 8 additions & 9 deletions Modules/SiSPS/SwiftriverPushParsingService.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

/**
* @author am[at]swiftly[dot]org
*/

* @author am[at]swiftly[dot]org
*/
namespace Swiftriver\Core\Modules\SiSPS;
class SwiftriverPushParsingService {
/**
/**
* This method will take the information prvided in the
* instance of a \Swiftriver\Core\ObjectModel\Source object
* and will make a call to the channel to fetch and content
Expand All @@ -19,13 +17,14 @@ class SwiftriverPushParsingService {
* @param $get_content
* @return Swiftriver\Core\ObjectModel\Content[] $contentItems
*/
public function FetchContentFromChannel($parser, $raw_content = null, $post_content = null, $get_content = null) {
public function FetchContentFromChannel($parser, $raw_content = null, $post_content = null, $get_content = null, $file_content = null) {
$logger = \Swiftriver\Core\Setup::GetLogger();
$logger->log("Core::Modules::SiSPS::SwiftriverPushParsingService::FetchContentFromChannel [Method invoked]", \PEAR_LOG_DEBUG);

if((!isset($raw_content) || $raw_content == null)
&& (!isset($post_content) || $post_content == null)
&& (!isset($get_content) || $get_content == null)) {
&& (!isset($get_content) || $get_content == null)
&& (!isset($file_content) || $file_content == null)){
$logger->log("Core::Modules::SiSPS::SwiftriverPushParsingService::FetchContentFromChannel [The channel object param is null]", \PEAR_LOG_DEBUG);
$logger->log("Core::Modules::SiSPS::SwiftriverPushParsingService::FetchContentFromChannel [Method finished]", \PEAR_LOG_DEBUG);
return;
Expand All @@ -46,7 +45,7 @@ public function FetchContentFromChannel($parser, $raw_content = null, $post_cont
try
{
//Get and parse all avaliable content items from the parser
$contentItems = $parser->PushAndParse($raw_content, $post_content, $get_content);
$contentItems = $parser->PushAndParse($raw_content, $post_content, $get_content, $file_content);
}
catch(\Exception $e)
{
Expand Down Expand Up @@ -79,4 +78,4 @@ public function ListAvailableParsers(){

return $parsers;
}
}
}
10 changes: 9 additions & 1 deletion Workflows/ChannelServices/ChannelServicesBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function ParseParsersToJSON($parsers)

public function ParsePushParsersToJSON($parsers)
{
$logger = \Swiftriver\Core\Setup::GetLogger();
$logger->log("Core::Workflows::ChannelServices::ChannelServicesBase::ParsePushParsersToJSON [Method Invoked]", \PEAR_LOG_INFO);

$logger->log("Core::Workflows::ChannelServices::ChannelServicesBase::ParsePushParsersToJSON [START: Encoding JSON]", \PEAR_LOG_INFO);

$return;

$return->channelTypes = array();
Expand All @@ -35,10 +40,13 @@ public function ParsePushParsersToJSON($parsers)
$channelType->parserType = "push";
$channelType->type = $parser->ReturnType();
$channelType->description = $parser->GetDescription();
$channelType->settings = $parser->GetSettings();
$return->channelTypes[] = $channelType;
unset($channelType);
}

$logger->log("Core::Workflows::ChannelServices::ChannelServicesBase::ParsePushParsersToJSON [END: Encoding JSON]", \PEAR_LOG_INFO);

return json_encode($return);
}

Expand Down Expand Up @@ -81,7 +89,7 @@ public function ParseChannelsToJSON($channels)

$json = rtrim($json, ",").']}';

$logger->log("Core:Workflows::ChannelServices::ChannelServicesBase::ParseChannelsToJSON [Method finsihed]", \PEAR_LOG_INFO);
$logger->log("Core::Workflows::ChannelServices::ChannelServicesBase::ParseChannelsToJSON [Method finsihed]", \PEAR_LOG_INFO);

return $json;
}
Expand Down
16 changes: 10 additions & 6 deletions Workflows/ChannelServices/PushToChannel.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

/**
* @author am[at]swiftly[dot]org
*/

* @author am[at]swiftly[dot]org
*/
namespace Swiftriver\Core\Workflows\ChannelServices;
class PushToChannel extends ChannelServicesBase {
/**
Expand Down Expand Up @@ -56,8 +54,14 @@ public function RunWorkflow($key)
$get_content = $_GET;
}

$file_content = null;

if($_FILES) {
$file_content = $_FILES;
}

$SiSPS = new \Swiftriver\Core\Modules\SiSPS\SwiftriverPushParsingService();
$rawContent = $SiSPS->FetchContentFromChannel($parser, $raw_content, $post_content, $get_content);
$rawContent = $SiSPS->FetchContentFromChannel($parser, $raw_content, $post_content, $get_content, $file_content);
}
catch (\Exception $e)
{
Expand Down Expand Up @@ -137,4 +141,4 @@ public function RunWorkflow($key)

return parent::FormatMessage("OK");
}
}
}

0 comments on commit 830f50b

Please sign in to comment.