Skip to content

Instantly share code, notes, and snippets.

@nils-werner
Created October 16, 2010 11:45
Show Gist options
  • Save nils-werner/629706 to your computer and use it in GitHub Desktop.
Save nils-werner/629706 to your computer and use it in GitHub Desktop.
public function grab(&$param_pool=NULL){
// I am not able to access $param_pool, so I'm taking a detour via Frontend::Page()
$page = Frontend::Page();
// CREATE XML DOCUMENT
$doc = new DOMDocument;
$root = $doc->createElement($this->dsParamROOTELEMENT);
$doc->appendChild($root);
// APPEND ARTICLES
$articles = SymRead('articles')
->get('title')
->get('body')
->get('date')
->orderby('date','desc')
->perPage($this->dsParamLIMIT)
->readDomDocument('articles');
$xpath = new DOMXpath($articles);
foreach($xpath->query('//entry') as $node) {
$node->setAttribute('section','articles');
$root->appendChild(
$doc->importNode($node, true)
);
}
// APPEND FACEBOOK
$facebook = SymRead('facebook')
->get('title')
->get('body')
->get('date')
->orderby('date','desc')
->perPage($this->dsParamLIMIT)
->readDomDocument('facebook');
$xpath = new DOMXpath($facebook);
foreach($xpath->query('//entry') as $node) {
$node->setAttribute('section','facebook');
$root->appendChild(
$doc->importNode($node, true)
);
}
// APPEND TWITTER
$twitter = SymRead('twitter')
->get('body')
->get('link')
->get('date')
->orderby('date','desc')
->perPage($this->dsParamLIMIT)
->readDomDocument('twitter');
$xpath = new DOMXpath($twitter);
foreach($xpath->query('//entry') as $node) {
$node->setAttribute('section','twitter');
$root->appendChild(
$doc->importNode($node, true)
);
}
// SORT XML BY USING ARRAYS
$xpath = new DOMXPath($doc);
$entries = array();
foreach($xpath->query('//entry') as $node) {
$entries[] = array(
'node' => $node,
'date' => $xpath->evaluate('translate(concat(date, " ", date/@time),"- :","")', $node)
);
$node->parentNode->removeChild($node);
}
usort($entries, array($this, 'cmp'));
// ENTRIES SPLIT INTO OBJECTS, DELETE AND RECREATE XML
unset($doc);
unset($root);
unset($xpath);
$doc = new DOMDocument;
$doc->formatOutput = true;
$root = $doc->createElement($this->dsParamROOTELEMENT);
$doc->appendChild($root);
foreach($entries as $index => $entry) {
if($index > $this->dsParamLIMIT) break;
$root->appendChild(
$doc->importNode($entry['node'], true)
);
}
// PASS ON THE XML
return $doc->saveXML($doc->documentElement);
}
function cmp($a, $b) {
$a = $a['date'];
$b = $b['date'];
if($this->dsParamORDER == 'desc')
$coeff = -1;
else
$coeff = 1;
if($a == $b)
return 0;
elseif($a > $b)
return 1*$coeff;
else
return -1*$coeff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment