* @author Romain Quetiez * @author Denis Flaven * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL */ require_once('../application/displayblock.class.inc.php'); /** * This class manages the special template format used internally to build the iTop web pages */ class DisplayTemplate { protected $m_sTemplate; protected $m_aTags; static protected $iBlockCount = 0; public function __construct($sTemplate) { $this->m_aTags = array('itopblock', 'itopcheck', 'itoptabs', 'itoptab', 'itoptoggle', 'itopstring'); $this->m_sTemplate = $sTemplate; } public function Render(WebPage $oPage, $aParams = array()) { $this->m_sTemplate = MetaModel::ApplyParams($this->m_sTemplate, $aParams); $iStart = 0; $iEnd = strlen($this->m_sTemplate); $iCount = 0; $iBeforeTagPos = $iStart; $iAfterTagPos = $iStart; while($sTag = $this->GetNextTag($iStart, $iEnd)) { $sContent = $this->GetTagContent($sTag, $iStart, $iEnd); $iAfterTagPos = $iEnd + strlen(''); $sOuterTag = substr($this->m_sTemplate, $iStart, $iAfterTagPos - $iStart); $oPage->add(substr($this->m_sTemplate, $iBeforeTagPos, $iStart - $iBeforeTagPos)); if ($sTag == DisplayBlock::TAG_BLOCK) { try { $oBlock = DisplayBlock::FromTemplate($sOuterTag); if (is_object($oBlock)) { $oBlock->Display($oPage, 'block_'.self::$iBlockCount, $aParams); } } catch(OQLException $e) { $oPage->p('Error in template (please contact your administrator) - Invalid query'); } catch(Exception $e) { $oPage->p('Error in template (please contact your administrator)'); } self::$iBlockCount++; } else { $aAttributes = $this->GetTagAttributes($sTag, $iStart, $iEnd); //$oPage->p("Tag: $sTag - ($iStart, $iEnd)"); $this->RenderTag($oPage, $sTag, $aAttributes, $sContent); } $iAfterTagPos = $iEnd + strlen(''); $iBeforeTagPos = $iAfterTagPos; $iStart = $iEnd; $iEnd = strlen($this->m_sTemplate); $iCount++; if ($iCount > 10) break; //@@@ Why ?? Debug ?? } $oPage->add(substr($this->m_sTemplate, $iAfterTagPos)); } public function GetNextTag(&$iStartPos, &$iEndPos) { $iChunkStartPos = $iStartPos; $sNextTag = null; $iStartPos = $iEndPos; foreach($this->m_aTags as $sTag) { // Search for the opening tag $iOpeningPos = stripos($this->m_sTemplate, '<'.$sTag.' ', $iChunkStartPos); if ($iOpeningPos === false) { $iOpeningPos = stripos($this->m_sTemplate, '<'.$sTag.'>', $iChunkStartPos); } if ($iOpeningPos !== false) { $iClosingPos = stripos($this->m_sTemplate, '', $iOpeningPos); } if ( ($iOpeningPos !== false) && ($iClosingPos !== false)) { if ($iOpeningPos < $iStartPos) { // This is the next tag $iStartPos = $iOpeningPos; $iEndPos = $iClosingPos; $sNextTag = $sTag; } } } return $sNextTag; } public function GetTagContent($sTag, $iStartPos, $iEndPos) { $sContent = ""; $iContentStart = strpos($this->m_sTemplate, '>', $iStartPos); // Content of tag start immediatly after the first closing bracket if ($iContentStart !== false) { $sContent = substr($this->m_sTemplate, 1+$iContentStart, $iEndPos - $iContentStart - 1); } return $sContent; } public function GetTagAttributes($sTag, $iStartPos, $iEndPos) { $aAttr = array(); $iAttrStart = strpos($this->m_sTemplate, ' ', $iStartPos); // Attributes start just after the first space $iAttrEnd = strpos($this->m_sTemplate, '>', $iStartPos); // Attributes end just before the first closing bracket if ( ($iAttrStart !== false) && ($iAttrEnd !== false) && ($iAttrEnd > $iAttrStart)) { $sAttributes = substr($this->m_sTemplate, 1+$iAttrStart, $iAttrEnd - $iAttrStart - 1); $aAttributes = explode(' ', $sAttributes); foreach($aAttributes as $sAttr) { if ( preg_match('/(.+) *= *"(.+)"$/', $sAttr, $aMatches) ) { $aAttr[strtolower($aMatches[1])] = $aMatches[2]; } } } return $aAttr; } protected function RenderTag($oPage, $sTag, $aAttributes, $sContent) { static $iTabContainerCount = 0; switch($sTag) { case 'itoptabs': $oPage->AddTabContainer('Tabs_'.$iTabContainerCount); $oPage->SetCurrentTabContainer('Tabs_'.$iTabContainerCount); $iTabContainerCount++; //$oPage->p('Content:
'.htmlentities($sContent).'
'); $oTemplate = new DisplayTemplate($sContent); $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied $oPage->SetCurrentTabContainer(''); break; case 'itopcheck': $sClassName = $aAttributes['class']; if (MetaModel::IsValidClass($sClassName)) { $oTemplate = new DisplayTemplate($sContent); $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied } else { // Leave a trace for those who'd like to understand why nothing is displayed $oPage->add("\n"); } break; case 'itoptab': $oPage->SetCurrentTab(Dict::S(str_replace('_', ' ', $aAttributes['name']))); $oTemplate = new DisplayTemplate($sContent); $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied //$oPage->p('iTop Tab Content:
'.htmlentities($sContent).'
'); $oPage->SetCurrentTab(''); break; case 'itoptoggle': $sName = isset($aAttributes['name']) ? $aAttributes['name'] : 'Tagada'; $bOpen = isset($aAttributes['open']) ? $aAttributes['open'] : true; $oPage->StartCollapsibleSection(Dict::S($sName), $bOpen); $oTemplate = new DisplayTemplate($sContent); $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied //$oPage->p('iTop Tab Content:
'.htmlentities($sContent).'
'); $oPage->EndCollapsibleSection(); break; case 'itopstring': $oPage->add(Dict::S($sContent)); break; case 'itopblock': // No longer used, handled by DisplayBlock::FromTemplate see above $oPage->add(""); break; default: // Unknown tag, just ignore it or now -- output an HTML comment $oPage->add(""); } } /** * Unit test */ static public function UnitTest() { require_once('../application/startup.inc.php'); require_once("../application/itopwebpage.class.inc.php"); $sTemplate = ' SELECT bizNetworkDevice AS d WHERE d.id = $id$ SELECT bizInterface AS i WHERE i.device_id = $id$ SELECT bizContact AS c JOIN ContactsLinks AS l ON l.contact_id = c.id WHERE l.object_id = $id$ SELECT bizDocument AS d JOIN lnkDocumentRealObject as l ON l.document_id = d.id WHERE l.object_id = $id$) '; $oPage = new iTopWebPage('Unit Test', 3); //$oPage->add("Template content:
".htmlentities($sTemplate)."
\n"); $oTemplate = new DisplayTemplate($sTemplate); $oTemplate->Render($oPage, array('class'=>'Network device','pkey'=> 271, 'name' => 'deliversw01.mecanorama.fr', 'org_id' => 3)); $oPage->output(); } } //DisplayTemplate::UnitTest(); ?>