template.class.inc.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. public function __construct($sTemplate)
  11. {
  12. $this->m_aTags = array('itopblock', 'itoptabs', 'itoptab', 'itoptoggle');
  13. $this->m_sTemplate = $sTemplate;
  14. }
  15. public function Render(web_page $oPage, $aParams = array())
  16. {
  17. $this->ApplyParams($aParams);
  18. $iStart = 0;
  19. $iEnd = strlen($this->m_sTemplate);
  20. $iCount = 0;
  21. $iBeforeTagPos = $iStart;
  22. $iAfterTagPos = $iStart;
  23. while($sTag = $this->GetNextTag($iStart, $iEnd))
  24. {
  25. $sContent = $this->GetTagContent($sTag, $iStart, $iEnd);
  26. $aAttributes = $this->GetTagAttributes($sTag, $iStart, $iEnd);
  27. //$oPage->p("Tag: $sTag - ($iStart, $iEnd)");
  28. $oPage->add(substr($this->m_sTemplate, $iBeforeTagPos, $iStart - $iBeforeTagPos));
  29. $this->RenderTag($oPage, $sTag, $aAttributes, $sContent);
  30. $iAfterTagPos = $iEnd + strlen('</'.$sTag.'>');
  31. $iBeforeTagPos = $iAfterTagPos;
  32. $iStart = $iEnd;
  33. $iEnd = strlen($this->m_sTemplate);
  34. $iCount++;
  35. if ($iCount > 10) break;
  36. }
  37. $oPage->add(substr($this->m_sTemplate, $iAfterTagPos));
  38. }
  39. /**
  40. * Replaces all the parameters by the values passed in the hash array
  41. */
  42. public function ApplyParams($aParams)
  43. {
  44. $aSearches = array();
  45. $aReplacements = array();
  46. foreach($aParams as $sSearch => $sReplace)
  47. {
  48. $aSearches[] = '$'.$sSearch.'$';
  49. $aReplacements[] = $sReplace;
  50. }
  51. $this->m_sTemplate = str_replace($aSearches, $aReplacements, $this->m_sTemplate);
  52. }
  53. public function GetNextTag(&$iStartPos, &$iEndPos)
  54. {
  55. $iChunkStartPos = $iStartPos;
  56. $sNextTag = null;
  57. $iStartPos = $iEndPos;
  58. foreach($this->m_aTags as $sTag)
  59. {
  60. // Search for the opening tag
  61. $iOpeningPos = stripos($this->m_sTemplate, '<'.$sTag.' ', $iChunkStartPos);
  62. if ($iOpeningPos === false)
  63. {
  64. $iOpeningPos = stripos($this->m_sTemplate, '<'.$sTag.'>', $iChunkStartPos);
  65. }
  66. if ($iOpeningPos !== false)
  67. {
  68. $iClosingPos = stripos($this->m_sTemplate, '</'.$sTag.'>', $iOpeningPos);
  69. }
  70. if ( ($iOpeningPos !== false) && ($iClosingPos !== false))
  71. {
  72. if ($iOpeningPos < $iStartPos)
  73. {
  74. // This is the next tag
  75. $iStartPos = $iOpeningPos;
  76. $iEndPos = $iClosingPos;
  77. $sNextTag = $sTag;
  78. }
  79. }
  80. }
  81. return $sNextTag;
  82. }
  83. public function GetTagContent($sTag, $iStartPos, $iEndPos)
  84. {
  85. $sContent = "";
  86. $iContentStart = strpos($this->m_sTemplate, '>', $iStartPos); // Content of tag start immediatly after the first closing bracket
  87. if ($iContentStart !== false)
  88. {
  89. $sContent = substr($this->m_sTemplate, 1+$iContentStart, $iEndPos - $iContentStart - 1);
  90. }
  91. return $sContent;
  92. }
  93. public function GetTagAttributes($sTag, $iStartPos, $iEndPos)
  94. {
  95. $aAttr = array();
  96. $iAttrStart = strpos($this->m_sTemplate, ' ', $iStartPos); // Attributes start just after the first space
  97. $iAttrEnd = strpos($this->m_sTemplate, '>', $iStartPos); // Attributes end just before the first closing bracket
  98. if ( ($iAttrStart !== false) && ($iAttrEnd !== false) && ($iAttrEnd > $iAttrStart))
  99. {
  100. $sAttributes = substr($this->m_sTemplate, 1+$iAttrStart, $iAttrEnd - $iAttrStart - 1);
  101. $aAttributes = explode(' ', $sAttributes);
  102. foreach($aAttributes as $sAttr)
  103. {
  104. if ( preg_match('/(.+) *= *"(.+)"$/', $sAttr, $aMatches) )
  105. {
  106. $aAttr[strtolower($aMatches[1])] = $aMatches[2];
  107. }
  108. }
  109. }
  110. return $aAttr;
  111. }
  112. protected function RenderTag($oPage, $sTag, $aAttributes, $sContent)
  113. {
  114. static $iTabContainerCount = 0;
  115. static $iBlockCount = 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($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. $oPage->StartCollapsibleSection($aAttributes['name']);
  136. $oTemplate = new DisplayTemplate($sContent);
  137. $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied
  138. //$oPage->p('iTop Tab Content:<pre>'.htmlentities($sContent).'</pre>');
  139. $oPage->EndCollapsibleSection();
  140. break;
  141. case 'itopblock': // TO DO: Use DisplayBlock::FromTemplate here
  142. $sBlockClass = $aAttributes['blockclass'];
  143. $sBlockType = $aAttributes['type'];
  144. $aExtraParams = array();
  145. if (isset($aAttributes['linkage']))
  146. {
  147. $aExtraParams['linkage'] = $aAttributes['linkage'];
  148. }
  149. switch($aAttributes['encoding'])
  150. {
  151. case 'text/sibusql':
  152. $oFilter = CMDBSearchFilter::FromSibusQL($sContent);
  153. break;
  154. case 'text/oql':
  155. $oFilter = CMDBSearchFilter::FromOQL($sContent);
  156. break;
  157. case 'text/serialize':
  158. default:
  159. $oFilter = CMDBSearchFilter::unserialize($sContent);
  160. break;
  161. }
  162. $oBlock = new $sBlockClass($oFilter, $sBlockType, false, $aExtraParams);
  163. $oBlock->Display($oPage, 'block_'.$iBlockCount);
  164. $iBlockCount++;
  165. break;
  166. default:
  167. // Unknown tag, just ignore it or now -- output an HTML comment
  168. $oPage->add("<!-- unsupported tag: $sTag -->");
  169. }
  170. }
  171. /**
  172. * Unit test
  173. */
  174. static public function UnitTest()
  175. {
  176. require_once('../application/startup.inc.php');
  177. require_once("../application/itopwebpage.class.inc.php");
  178. $sTemplate = '<div class="page_header">
  179. <div class="actions_details"><a href="#"><span>Actions</span></a></div>
  180. <h1>$class$: <span class="hilite">$name$</span></h1>
  181. <itopblock blockclass="HistoryBlock" type="toggle" encoding="text/sibusql">CMDBChangeOpSetAttribute: objkey = $pkey$</itopblock>
  182. </div>
  183. <img src="../../images/connect_to_network.png" style="margin-top:-10px; margin-right:10px; float:right">
  184. <itopblock blockclass="DisplayBlock" asynchronous="true" type="bare_details" encoding="text/sibusql">bizNetworkDevice: pkey = $pkey$</itopblock>
  185. <itoptabs>
  186. <itoptab name="Interfaces">
  187. <itopblock blockclass="DisplayBlock" type="list" encoding="text/sibusql">bizInterface: device_id = $pkey$</itopblock>
  188. </itoptab>
  189. <itoptab name="Contacts">
  190. <itopblock blockclass="DisplayBlock" type="list" encoding="text/sibusql">bizContact: PKEY IS contact_id IN (ContactsLinks: object_id = $pkey$)</itopblock>
  191. </itoptab>
  192. <itoptab name="Documents">
  193. <itopblock blockclass="DisplayBlock" type="list" encoding="text/sibusql">bizDocument: PKEY IS doc_id IN (lnkDocumentRealObject: object_id = $pkey$)</itopblock>
  194. </itoptab>
  195. </itoptabs>';
  196. $oPage = new iTopWebPage('Unit Test', 3);
  197. //$oPage->add("Template content: <pre>".htmlentities($sTemplate)."</pre>\n");
  198. $oTemplate = new DisplayTemplate($sTemplate);
  199. $oTemplate->Render($oPage, array('class'=>'Network device','pkey'=> 271, 'name' => 'deliversw01.mecanorama.fr', 'org_id' => 3));
  200. $oPage->output();
  201. }
  202. }
  203. //DisplayTemplate::UnitTest();
  204. ?>