getElementById($element);
}
while ($element->firstChild) {
$element->removeChild($element->firstChild);
}
if ($text) {
$element->appendChild($document->createTextNode($text));
}
}
function populateMenuGo($db) {
global $document;
$query = queryAllWatches($db);
$menuGo = $document->getElementById('menu.go');
$menuGoItem = $menuGo->getElementsByTagName('li')->item(0);
$i = 0;
while ($row = $query->fetch(SQLITE_ASSOC)) {
if ($i++ > 0) {
$newMenuGoItem = $menuGoItem->cloneNode(TRUE);
$menuGo->appendChild($newMenuGoItem);
} else {
$newMenuGoItem = $menuGoItem;
}
$a = $newMenuGoItem->getElementsByTagName('a')->item(0);
$a->setAttribute("href", "?name=" . $row['symbol']);
setValue($a, "${row['title']} (${row['symbol']})");
}
}
function populateForms($watchRow) {
global $document;
if (!$watchRow) {
return;
}
setValue($document->getElementById('textTitle'), $watchRow['title']);
setValue($document->getElementById('textDescription'), $watchRow['description']);
$document->getElementById('symbol')->setAttribute('value', $watchRow['symbol']);
$document->getElementById('tag')->setAttribute('value', $watchRow['tag']);
}
function populateQuotes($watchRow) {
global $document;
if (!$watchRow) {
return;
}
$quote = simplexml_load_string(
get_cached_data(
getUrlQuotes($watchRow['symbol']),
get_quotes_as_xml,
YAHOOFINANCE_QUOTES_LIFETIME
)
);
setValue('yahoofinance.quotes.title', "Quotes ({$quote->name})");
setValue('yahoofinance.quotes.last_price', $quote->lastTrade->price);
setValue('yahoofinance.quotes.last_time',
"{$quote->lastTrade->time} EST ({$quote->lastTrade->date})" );
setValue('yahoofinance.quotes.change', $quote->change);
setValue('yahoofinance.quotes.open', $quote->open);
setValue('yahoofinance.quotes.high', $quote->high);
setValue('yahoofinance.quotes.low', $quote->low);
setValue('yahoofinance.quotes.volume', $quote->volume);
}
function populateChart($watchRow) {
global $document;
if (!$watchRow) {
return;
}
$img = $document->getElementById('yahoofinance.chart.img');
$img->setAttribute('src', "yahoo_chart.php?tag={$watchRow['symbol']}");
}
function populateRss($xmlChannel, $name) {
global $document;
$rss = simplexml_load_string($xmlChannel);
$channel = $rss->channel;
if ($rss->channel->item) {
$items = $rss->channel->item;
} else if ($rss->item) {
$items = $rss->item;
} else {
die ("Can't reckon this RSS format!");
}
$channelTitle = $document->getElementById($name . '.channel.title');
setValue($channelTitle, $channel->title);
$channelTitle->setAttribute("href", $channel->link);
setValue($name . '.channel.description', $channel->description);
setValue($name . '.channel.copyright', $channel->copyright);
$itemElement = $document->getElementById($name . '.item');
$itemsElement = $itemElement->parentNode;
while ($itemElement->nextSibling) {
$itemsElement->removeChild($itemElement->nextSibling);
}
if ($items) {
for( $i=0; $i< sizeof($items); $i++ ) {
$item = $items[$i];
//$itemElement = $document->getElementById($name . '.item');
if ($i > 0) {
$savedItem = $itemElement->cloneNode(true);
}
$itemDiv = $itemElement->getElementsByTagName("div")->item(0);
$itemTitle = $itemDiv->getElementsByTagName("h3")->item(0)->getElementsByTagName("a")->item(0);
setValue($itemTitle, $item->title);
//echo " {$item->link}";
$attribute = $document->createAttribute('href');
$attribute->value=htmlspecialchars($item->link);
$itemTitle->setAttributeNode($attribute);
//$itemTitle->setAttribute('href', htmlspecialchars($item->link));
$itemDescription = $itemDiv->getElementsByTagName("div")->item(0);
//$itemDescription = $document->getElementById($name . '.item.description');
setValue($itemDescription, $item->description);
if ($i > 0) {
$itemElement->removeAttribute("id");
$itemTitle->removeAttribute("id");
$itemDescription->removeAttribute("id");
$itemsElement->insertBefore($savedItem, $itemsElement->firstChild);
$itemsElement->appendChild($itemElement);
$itemElement = $savedItem;
}
}
}
}
function populateFinancialNews($watchRow) {
if (!$watchRow) {
return;
}
populateRss (
get_cached_data(
getUrlFinancialNews($watchRow['symbol']),
defaultCacheGet,
YAHOOFINANCE_NEWS_LIFETIME
),
'yahoofinance.news'
);
}
function populateDelicious($watchRow) {
if (!$watchRow) {
return;
}
populateRss (
get_cached_data(
getUrlDelicious($watchRow['tag']),
defaultCacheGet,
DELICIOUS_LIFETIME
),
'delicious'
);
}
// Open the database and fetch the current
// watch if needed.
$db=openDb();
$query = queryOneWatch($db);
$watchRow = $query->fetch(SQLITE_ASSOC);
// Create a DOM and load the document
$document = new DOMDocument();
$document->validateOnParse = TRUE;
$document->load("template.html");
// Populate the menu bar and the panels
populateMenuGo($db);
populateForms($watchRow);
populateQuotes($watchRow);
populateChart($watchRow);
populateFinancialNews($watchRow);
populateDelicious($watchRow);
// Send the result
header("Cache-Control: max-age=60");
header("Content-type: text/html");
print $document->saveXML();
?>