template.class.inc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. require_once('../application/displayblock.class.inc.php');
  3. /**
  4. * This class manages the special template format used internally to build the iTop web pages
  5. */
  6. class DisplayTemplate
  7. {
  8. protected $m_sTemplate;
  9. protected $m_aTags;
  10. static protected $iBlockCount = 0;
  11. public function __construct($sTemplate)
  12. {
  13. $this->m_aTags = array('itopblock', 'itoptabs', 'itoptab', 'itoptoggle');
  14. $this->m_sTemplate = $sTemplate;
  15. }
  16. public function Render(WebPage $oPage, $aParams = array())
  17. {
  18. $this->m_sTemplate = MetaModel::ApplyParams($this->m_sTemplate, $aParams);
  19. $iStart = 0;
  20. $iEnd = strlen($this->m_sTemplate);
  21. $iCount = 0;
  22. $iBeforeTagPos = $iStart;
  23. $iAfterTagPos = $iStart;
  24. while($sTag = $this->GetNextTag($iStart, $iEnd))
  25. {
  26. $sContent = $this->GetTagContent($sTag, $iStart, $iEnd);
  27. $iAfterTagPos = $iEnd + strlen('</'.$sTag.'>');
  28. $sOuterTag = substr($this->m_sTemplate, $iStart, $iAfterTagPos - $iStart);
  29. $oPage->add(substr($this->m_sTemplate, $iBeforeTagPos, $iStart - $iBeforeTagPos));
  30. if ($sTag == DisplayBlock::TAG_BLOCK)
  31. {
  32. $oBlock = DisplayBlock::FromTemplate($sOuterTag);
  33. if (is_object($oBlock))
  34. {
  35. $oBlock->Display($oPage, 'block_'.self::$iBlockCount, $aParams);
  36. }
  37. self::$iBlockCount++;
  38. }
  39. else
  40. {
  41. $aAttributes = $this->GetTagAttributes($sTag, $iStart, $iEnd);
  42. //$oPage->p("Tag: $sTag - ($iStart, $iEnd)");
  43. $this->RenderTag($oPage, $sTag, $aAttributes, $sContent);
  44. }
  45. $iAfterTagPos = $iEnd + strlen('</'.$sTag.'>');
  46. $iBeforeTagPos = $iAfterTagPos;
  47. $iStart = $iEnd;
  48. $iEnd = strlen($this->m_sTemplate);
  49. $iCount++;
  50. if ($iCount > 10) break; //@@@ Why ?? Debug ??
  51. }
  52. $oPage->add(substr($this->m_sTemplate, $iAfterTagPos));
  53. }
  54. public function GetNextTag(&$iStartPos, &$iEndPos)
  55. {
  56. $iChunkStartPos = $iStartPos;
  57. $sNextTag = null;
  58. $iStartPos = $iEndPos;
  59. foreach($this->m_aTags as $sTag)
  60. {
  61. // Search for the opening tag
  62. $iOpeningPos = stripos($this->m_sTemplate, '<'.$sTag.' ', $iChunkStartPos);
  63. if ($iOpeningPos === false)
  64. {
  65. $iOpeningPos = stripos($this->m_sTemplate, '<'.$sTag.'>', $iChunkStartPos);
  66. }
  67. if ($iOpeningPos !== false)
  68. {
  69. $iClosingPos = stripos($this->m_sTemplate, '</'.$sTag.'>', $iOpeningPos);
  70. }
  71. if ( ($iOpeningPos !== false) && ($iClosingPos !== false))
  72. {
  73. if ($iOpeningPos < $iStartPos)
  74. {
  75. // This is the next tag
  76. $iStartPos = $iOpeningPos;
  77. $iEndPos = $iClosingPos;
  78. $sNextTag = $sTag;
  79. }
  80. }
  81. }
  82. return $sNextTag;
  83. }
  84. public function GetTagContent($sTag, $iStartPos, $iEndPos)
  85. {
  86. $sContent = "";
  87. $iContentStart = strpos($this->m_sTemplate, '>', $iStartPos); // Content of tag start immediatly after the first closing bracket
  88. if ($iContentStart !== false)
  89. {
  90. $sContent = substr($this->m_sTemplate, 1+$iContentStart, $iEndPos - $iContentStart - 1);
  91. }
  92. return $sContent;
  93. }
  94. public function GetTagAttributes($sTag, $iStartPos, $iEndPos)
  95. {
  96. $aAttr = array();
  97. $iAttrStart = strpos($this->m_sTemplate, ' ', $iStartPos); // Attributes start just after the first space
  98. $iAttrEnd = strpos($this->m_sTemplate, '>', $iStartPos); // Attributes end just before the first closing bracket
  99. if ( ($iAttrStart !== false) && ($iAttrEnd !== false) && ($iAttrEnd > $iAttrStart))
  100. {
  101. $sAttributes = substr($this->m_sTemplate, 1+$iAttrStart, $iAttrEnd - $iAttrStart - 1);
  102. $aAttributes = explode(' ', $sAttributes);
  103. foreach($aAttributes as $sAttr)
  104. {
  105. if ( preg_match('/(.+) *= *"(.+)"$/', $sAttr, $aMatches) )
  106. {
  107. $aAttr[strtolower($aMatches[1])] = $aMatches[2];
  108. }
  109. }
  110. }
  111. return $aAttr;
  112. }
  113. protected function RenderTag($oPage, $sTag, $aAttributes, $sContent)
  114. {
  115. static $iTabContainerCount = 0;
  116. switch($sTag)
  117. {
  118. case 'itoptabs':
  119. $oPage->AddTabContainer('Tabs_'.$iTabContainerCount);
  120. $oPage->SetCurrentTabContainer('Tabs_'.$iTabContainerCount);
  121. $iTabContainerCount++;
  122. //$oPage->p('Content:<pre>'.htmlentities($sContent).'</pre>');
  123. $oTemplate = new DisplayTemplate($sContent);
  124. $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied
  125. $oPage->SetCurrentTabContainer('');
  126. break;
  127. case 'itoptab':
  128. $oPage->SetCurrentTab(str_replace('_', ' ', $aAttributes['name']));
  129. $oTemplate = new DisplayTemplate($sContent);
  130. $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied
  131. //$oPage->p('iTop Tab Content:<pre>'.htmlentities($sContent).'</pre>');
  132. $oPage->SetCurrentTab('');
  133. break;
  134. case 'itoptoggle':
  135. $sName = isset($aAttributes['name']) ? $aAttributes['name'] : 'Tagada';
  136. $bOpen = isset($aAttributes['open']) ? $aAttributes['open'] : true;
  137. $oPage->StartCollapsibleSection($sName, $bOpen);
  138. $oTemplate = new DisplayTemplate($sContent);
  139. $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied
  140. //$oPage->p('iTop Tab Content:<pre>'.htmlentities($sContent).'</pre>');
  141. $oPage->EndCollapsibleSection();
  142. break;
  143. case 'itopblock': // No longer used, handled by DisplayBlock::FromTemplate see above
  144. $oPage->add("<!-- Application Error: should be handled by DisplayBlock::FromTemplate -->");
  145. break;
  146. default:
  147. // Unknown tag, just ignore it or now -- output an HTML comment
  148. $oPage->add("<!-- unsupported tag: $sTag -->");
  149. }
  150. }
  151. /**
  152. * Unit test
  153. */
  154. static public function UnitTest()
  155. {
  156. require_once('../application/startup.inc.php');
  157. require_once("../application/itopwebpage.class.inc.php");
  158. $sTemplate = '<div class="page_header">
  159. <div class="actions_details"><a href="#"><span>Actions</span></a></div>
  160. <h1>$class$: <span class="hilite">$name$</span></h1>
  161. <itopblock blockclass="HistoryBlock" type="toggle" encoding="text/oql">SELECT CMDBChangeOp WHERE objkey = $pkey$ AND objclass = \'$class$\'</itopblock>
  162. </div>
  163. <img src="../../images/connect_to_network.png" style="margin-top:-10px; margin-right:10px; float:right">
  164. <itopblock blockclass="DisplayBlock" asynchronous="false" type="bare_details" encoding="text/sibusql">bizNetworkDevice: pkey = $pkey$</itopblock>
  165. <itoptabs>
  166. <itoptab name="Interfaces">
  167. <itopblock blockclass="DisplayBlock" type="list" encoding="text/sibusql">bizInterface: device_id = $pkey$</itopblock>
  168. </itoptab>
  169. <itoptab name="Contacts">
  170. <itopblock blockclass="DisplayBlock" type="list" encoding="text/sibusql">bizContact: PKEY IS contact_id IN (ContactsLinks: object_id = $pkey$)</itopblock>
  171. </itoptab>
  172. <itoptab name="Documents">
  173. <itopblock blockclass="DisplayBlock" type="list" encoding="text/sibusql">bizDocument: PKEY IS doc_id IN (lnkDocumentRealObject: object_id = $pkey$)</itopblock>
  174. </itoptab>
  175. </itoptabs>';
  176. $oPage = new iTopWebPage('Unit Test', 3);
  177. //$oPage->add("Template content: <pre>".htmlentities($sTemplate)."</pre>\n");
  178. $oTemplate = new DisplayTemplate($sTemplate);
  179. $oTemplate->Render($oPage, array('class'=>'Network device','pkey'=> 271, 'name' => 'deliversw01.mecanorama.fr', 'org_id' => 3));
  180. $oPage->output();
  181. }
  182. }
  183. //DisplayTemplate::UnitTest();
  184. ?>