/** * Generate report */ public function generate() { //run collect $this->collectReport(); //load tmpl if (file_exists($this->baseDir . '/ressources/template/default.html')) { //load html template $template = file_get_contents($this->baseDir . '/ressources/template/default.html'); $itemsHtml = ''; if (!empty($this->_reports)) { foreach (xml2array_parse($this->_reports) as $itemId => $itemValue) { $itemsHtml .= '<tr><td class="no-border">'; $itemsHtml .= htmlentities($itemId); $itemsHtml .= file_exists($this->baseDir . '/ressources/img/icons/' . strtolower($itemId) . '.png') ? ' <img src="img/icons/' . strtolower($itemId) . '.png" alt="' . strtolower($itemId) . '" height="35" />' : ''; $itemsHtml .= '</td><td>'; $itemsHtml .= htmlentities($itemValue); $itemsHtml .= '</td></tr>' . "\n"; } } else { $itemsHtml = '<tr><td colspan="2">Report is empty</td><tr>'; } //set and put record $this->_htmlReport = str_replace("{DEFAULT:REPORT:ITEMS}", $itemsHtml, $template); $this->putReport(); } }
function xml2array_parse($xml) { foreach ($xml->children() as $parent => $child) { $return["{$parent}"] = xml2array_parse($child) ? xml2array_parse($child) : "{$child}"; } return $return; }
function xml2array_parse(&$string, &$r, $compact = 1) { $n = 0; do { #-- split chunks $l = strpos($string, "<"); $p = strpos($string, ">", $l); $text = $attr = $close = $tag = false; if ($l === false) { $text = $string; $string = false; } else { $tag = strtok(substr($string, $l + 1, $p - $l - 1), " "); if (strncmp($tag, "![CDATA[", 8) == 0 && ($p = strpos($string, "]]>", $l))) { $text = substr($string, $l + 9, $p - $l - 9); } else { if ($l) { $text = xml_decode_entities(substr($string, 0, $l)); } $attr = strtok(""); $close = $attr && $attr[strlen($attr) - 1] == "/"; $string = substr($string, $p + 1); } } #-- insert text/body content into array if (trim($text)) { # if ($compact) { $r[",{$n}"] = $text; # } # else { # $r[] = $text; # } $n++; } #-- recurse for tags if ($tag && $tag[0] >= 'A') { #-- don't read <? <! </ pseudo-tags # if ($compact) { $r["{$tag},{$n}"] = array(); $new =& $r["{$tag},{$n}"]; # } else { # $r[] = array($tag => array()); # $new = &$r[count($r)-1][$tag]; # } if (!$close) { xml2array_parse($string, $new, $compact); } $n++; } #-- stop if no more tags or content if (empty($tag) && empty($text) || empty($string)) { $tag = "/"; } } while ($tag[0] != "/"); }