template.class.inc.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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->m_sTemplate = MetaModel::ApplyParams($this->m_sTemplate, $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. public function GetNextTag(&$iStartPos, &$iEndPos)
  40. {
  41. $iChunkStartPos = $iStartPos;
  42. $sNextTag = null;
  43. $iStartPos = $iEndPos;
  44. foreach($this->m_aTags as $sTag)
  45. {
  46. // Search for the opening tag
  47. $iOpeningPos = stripos($this->m_sTemplate, '<'.$sTag.' ', $iChunkStartPos);
  48. if ($iOpeningPos === false)
  49. {
  50. $iOpeningPos = stripos($this->m_sTemplate, '<'.$sTag.'>', $iChunkStartPos);
  51. }
  52. if ($iOpeningPos !== false)
  53. {
  54. $iClosingPos = stripos($this->m_sTemplate, '</'.$sTag.'>', $iOpeningPos);
  55. }
  56. if ( ($iOpeningPos !== false) && ($iClosingPos !== false))
  57. {
  58. if ($iOpeningPos < $iStartPos)
  59. {
  60. // This is the next tag
  61. $iStartPos = $iOpeningPos;
  62. $iEndPos = $iClosingPos;
  63. $sNextTag = $sTag;
  64. }
  65. }
  66. }
  67. return $sNextTag;
  68. }
  69. public function GetTagContent($sTag, $iStartPos, $iEndPos)
  70. {
  71. $sContent = "";
  72. $iContentStart = strpos($this->m_sTemplate, '>', $iStartPos); // Content of tag start immediatly after the first closing bracket
  73. if ($iContentStart !== false)
  74. {
  75. $sContent = substr($this->m_sTemplate, 1+$iContentStart, $iEndPos - $iContentStart - 1);
  76. }
  77. return $sContent;
  78. }
  79. public function GetTagAttributes($sTag, $iStartPos, $iEndPos)
  80. {
  81. $aAttr = array();
  82. $iAttrStart = strpos($this->m_sTemplate, ' ', $iStartPos); // Attributes start just after the first space
  83. $iAttrEnd = strpos($this->m_sTemplate, '>', $iStartPos); // Attributes end just before the first closing bracket
  84. if ( ($iAttrStart !== false) && ($iAttrEnd !== false) && ($iAttrEnd > $iAttrStart))
  85. {
  86. $sAttributes = substr($this->m_sTemplate, 1+$iAttrStart, $iAttrEnd - $iAttrStart - 1);
  87. $aAttributes = explode(' ', $sAttributes);
  88. foreach($aAttributes as $sAttr)
  89. {
  90. if ( preg_match('/(.+) *= *"(.+)"$/', $sAttr, $aMatches) )
  91. {
  92. $aAttr[strtolower($aMatches[1])] = $aMatches[2];
  93. }
  94. }
  95. }
  96. return $aAttr;
  97. }
  98. protected function RenderTag($oPage, $sTag, $aAttributes, $sContent)
  99. {
  100. static $iTabContainerCount = 0;
  101. static $iBlockCount = 0;
  102. switch($sTag)
  103. {
  104. case 'itoptabs':
  105. $oPage->AddTabContainer('Tabs_'.$iTabContainerCount);
  106. $oPage->SetCurrentTabContainer('Tabs_'.$iTabContainerCount);
  107. $iTabContainerCount++;
  108. //$oPage->p('Content:<pre>'.htmlentities($sContent).'</pre>');
  109. $oTemplate = new DisplayTemplate($sContent);
  110. $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied
  111. $oPage->SetCurrentTabContainer('');
  112. break;
  113. case 'itoptab':
  114. $oPage->SetCurrentTab(str_replace('_', ' ', $aAttributes['name']));
  115. $oTemplate = new DisplayTemplate($sContent);
  116. $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied
  117. //$oPage->p('iTop Tab Content:<pre>'.htmlentities($sContent).'</pre>');
  118. $oPage->SetCurrentTab('');
  119. break;
  120. case 'itoptoggle':
  121. $oPage->StartCollapsibleSection($aAttributes['name']);
  122. $oTemplate = new DisplayTemplate($sContent);
  123. $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied
  124. //$oPage->p('iTop Tab Content:<pre>'.htmlentities($sContent).'</pre>');
  125. $oPage->EndCollapsibleSection();
  126. break;
  127. case 'itopblock': // TO DO: Use DisplayBlock::FromTemplate here
  128. $sBlockClass = $aAttributes['blockclass'];
  129. $sBlockType = $aAttributes['type'];
  130. $aExtraParams = array();
  131. if (isset($aAttributes['link_attr']))
  132. {
  133. $aExtraParams['link_attr'] = $aAttributes['link_attr'];
  134. // Check that all mandatory parameters are present:
  135. if(empty($aAttributes['object_id']))
  136. {
  137. // if 'links' mode is requested the d of the object to link to must be specified
  138. throw new ApplicationException("Parameter object_id is mandatory when link_attr is specified. Check the definition of the display template.");
  139. }
  140. if(empty($aAttributes['target_attr']))
  141. {
  142. // if 'links' mode is requested the d of the object to link to must be specified
  143. throw new ApplicationException("Parameter target_attr is mandatory when link_attr is specified. Check the definition of the display template.");
  144. }
  145. $aExtraParams['object_id'] = $aAttributes['object_id'];
  146. $aExtraParams['target_attr'] = $aAttributes['target_attr'];
  147. }
  148. switch($aAttributes['encoding'])
  149. {
  150. case 'text/sibusql':
  151. $oFilter = CMDBSearchFilter::FromSibusQL($sContent);
  152. break;
  153. case 'text/oql':
  154. $oFilter = CMDBSearchFilter::FromOQL($sContent);
  155. break;
  156. case 'text/serialize':
  157. default:
  158. $oFilter = CMDBSearchFilter::unserialize($sContent);
  159. break;
  160. }
  161. $oBlock = new $sBlockClass($oFilter, $sBlockType, false, $aExtraParams);
  162. $oBlock->Display($oPage, 'block_'.$iBlockCount);
  163. $iBlockCount++;
  164. break;
  165. default:
  166. // Unknown tag, just ignore it or now -- output an HTML comment
  167. $oPage->add("<!-- unsupported tag: $sTag -->");
  168. }
  169. }
  170. /**
  171. * Unit test
  172. */
  173. static public function UnitTest()
  174. {
  175. require_once('../application/startup.inc.php');
  176. require_once("../application/itopwebpage.class.inc.php");
  177. $sTemplate = '<div class="page_header">
  178. <div class="actions_details"><a href="#"><span>Actions</span></a></div>
  179. <h1>$class$: <span class="hilite">$name$</span></h1>
  180. <itopblock blockclass="HistoryBlock" type="toggle" encoding="text/oql">SELECT CMDBChangeOp WHERE objkey = $pkey$ AND objclass = \'$class$\'</itopblock>
  181. </div>
  182. <img src="../../images/connect_to_network.png" style="margin-top:-10px; margin-right:10px; float:right">
  183. <itopblock blockclass="DisplayBlock" asynchronous="true" type="bare_details" encoding="text/sibusql">bizNetworkDevice: pkey = $pkey$</itopblock>
  184. <itoptabs>
  185. <itoptab name="Interfaces">
  186. <itopblock blockclass="DisplayBlock" type="list" encoding="text/sibusql">bizInterface: device_id = $pkey$</itopblock>
  187. </itoptab>
  188. <itoptab name="Contacts">
  189. <itopblock blockclass="DisplayBlock" type="list" encoding="text/sibusql">bizContact: PKEY IS contact_id IN (ContactsLinks: object_id = $pkey$)</itopblock>
  190. </itoptab>
  191. <itoptab name="Documents">
  192. <itopblock blockclass="DisplayBlock" type="list" encoding="text/sibusql">bizDocument: PKEY IS doc_id IN (lnkDocumentRealObject: object_id = $pkey$)</itopblock>
  193. </itoptab>
  194. </itoptabs>';
  195. $oPage = new iTopWebPage('Unit Test', 3);
  196. //$oPage->add("Template content: <pre>".htmlentities($sTemplate)."</pre>\n");
  197. $oTemplate = new DisplayTemplate($sTemplate);
  198. $oTemplate->Render($oPage, array('class'=>'Network device','pkey'=> 271, 'name' => 'deliversw01.mecanorama.fr', 'org_id' => 3));
  199. $oPage->output();
  200. }
  201. }
  202. //DisplayTemplate::UnitTest();
  203. ?>