I created a snippet that puts data from a feed into the database,
Its a Raw snippet and it does wat I want it to do, still needs some checks like adding the right domain in front of relative path links and images inside description and make special characters apear ok in modx now lots of charackters are encoded wrong somehow
here it is use it at your own risk:
<?php
$parent = 39; //parent to put the new pages under
$rssfeed = 'http://www.kvk.nl/rss/nieuws?pub_id=14'; //put your rss feed here
$doc = new DOMDocument();
$doc->load($rrsfeed);
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'pagetitle' => addslashes($node->getElementsByTagName('title')->item(0)->nodeValue),
'longtitle' => addslashes($node->getElementsByTagName('title')->item(0)->nodeValue),
//lots of adding slashes and stripping some info to create a intro text and a seperate content from one description
'content' => str_replace(addslashes(getTextBetweenTags($node->getElementsByTagName('description')->item(0)->nodeValue,'strong')),'',addslashes($node->getElementsByTagName('description')->item(0)->nodeValue))."<br /> <em class=\"bron\">Bron: <a href=\"".addslashes($node->getElementsByTagName('link')->item(0)->nodeValue)."\" title=\"".addslashes($node->getElementsByTagName('title')->item(0)->nodeValue)." \">Kamer van Koophandel</a></em>",
'introtext' => addslashes(getTextBetweenTags($node->getElementsByTagName('description')->item(0)->nodeValue,'strong')),
'description' => addslashes($node->getElementsByTagName('title')->item(0)->nodeValue),
'createdon' => strtotime($node->getElementsByTagName('pubDate')->item(0)->nodeValue),
'parent' => $parent,
'published' => 1,
'hidemenu' => 1,
'template' => 3 // template that you like to use
);
// check if item is already in database
if (dimCheckDate($itemRSS['createdon']) == 0){
// insert item into database
insert_Item($itemRSS);
}
}
// an atempt to clear the cache I think that it does not realy works I need to clear cache by hand to see the pages online
$modx->clearCache();
return;
//function to put the data into the database
function insert_Item($itemArray) {
global $modx;
$table_name = $modx->getFullTableName ( 'site_content' );
$modx->db->insert ( $itemArray, $table_name );
}
//check date, so that no double entry's are made in the database
function dimCheckDate($pubDate) {
global $modx;
$res = $modx->db->select ( "createdon", $modx->getFullTableName ( 'site_content' ), "createdon='" . $pubDate . "'" );
if(count($modx->db->makeArray ( $res )) > 0){return 1;}
else{
return 0;
}
}
// function to get text inside tags to create a introtext or somthing from the first part (in my case it wass in <strong> tags
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname>(.*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
?>
Greets Dimmy