template.class.inc.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Class DisplayTemplate
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once('../application/displayblock.class.inc.php');
  25. /**
  26. * This class manages the special template format used internally to build the iTop web pages
  27. */
  28. class DisplayTemplate
  29. {
  30. protected $m_sTemplate;
  31. protected $m_aTags;
  32. static protected $iBlockCount = 0;
  33. public function __construct($sTemplate)
  34. {
  35. $this->m_aTags = array('itopblock', 'itoptabs', 'itoptab', 'itoptoggle', 'itopstring');
  36. $this->m_sTemplate = $sTemplate;
  37. }
  38. public function Render(WebPage $oPage, $aParams = array())
  39. {
  40. $this->m_sTemplate = MetaModel::ApplyParams($this->m_sTemplate, $aParams);
  41. $iStart = 0;
  42. $iEnd = strlen($this->m_sTemplate);
  43. $iCount = 0;
  44. $iBeforeTagPos = $iStart;
  45. $iAfterTagPos = $iStart;
  46. while($sTag = $this->GetNextTag($iStart, $iEnd))
  47. {
  48. $sContent = $this->GetTagContent($sTag, $iStart, $iEnd);
  49. $iAfterTagPos = $iEnd + strlen('</'.$sTag.'>');
  50. $sOuterTag = substr($this->m_sTemplate, $iStart, $iAfterTagPos - $iStart);
  51. $oPage->add(substr($this->m_sTemplate, $iBeforeTagPos, $iStart - $iBeforeTagPos));
  52. if ($sTag == DisplayBlock::TAG_BLOCK)
  53. {
  54. try
  55. {
  56. $oBlock = DisplayBlock::FromTemplate($sOuterTag);
  57. if (is_object($oBlock))
  58. {
  59. $oBlock->Display($oPage, 'block_'.self::$iBlockCount, $aParams);
  60. }
  61. }
  62. catch(OQLException $e)
  63. {
  64. $oPage->p('Error in template (please contact your administrator) - Invalid query<!--'.$sOuterTag.'-->');
  65. }
  66. catch(Exception $e)
  67. {
  68. $oPage->p('Error in template (please contact your administrator)<!--'.$sOuterTag.'-->');
  69. }
  70. self::$iBlockCount++;
  71. }
  72. else
  73. {
  74. $aAttributes = $this->GetTagAttributes($sTag, $iStart, $iEnd);
  75. //$oPage->p("Tag: $sTag - ($iStart, $iEnd)");
  76. $this->RenderTag($oPage, $sTag, $aAttributes, $sContent);
  77. }
  78. $iAfterTagPos = $iEnd + strlen('</'.$sTag.'>');
  79. $iBeforeTagPos = $iAfterTagPos;
  80. $iStart = $iEnd;
  81. $iEnd = strlen($this->m_sTemplate);
  82. $iCount++;
  83. if ($iCount > 10) break; //@@@ Why ?? Debug ??
  84. }
  85. $oPage->add(substr($this->m_sTemplate, $iAfterTagPos));
  86. }
  87. public function GetNextTag(&$iStartPos, &$iEndPos)
  88. {
  89. $iChunkStartPos = $iStartPos;
  90. $sNextTag = null;
  91. $iStartPos = $iEndPos;
  92. foreach($this->m_aTags as $sTag)
  93. {
  94. // Search for the opening tag
  95. $iOpeningPos = stripos($this->m_sTemplate, '<'.$sTag.' ', $iChunkStartPos);
  96. if ($iOpeningPos === false)
  97. {
  98. $iOpeningPos = stripos($this->m_sTemplate, '<'.$sTag.'>', $iChunkStartPos);
  99. }
  100. if ($iOpeningPos !== false)
  101. {
  102. $iClosingPos = stripos($this->m_sTemplate, '</'.$sTag.'>', $iOpeningPos);
  103. }
  104. if ( ($iOpeningPos !== false) && ($iClosingPos !== false))
  105. {
  106. if ($iOpeningPos < $iStartPos)
  107. {
  108. // This is the next tag
  109. $iStartPos = $iOpeningPos;
  110. $iEndPos = $iClosingPos;
  111. $sNextTag = $sTag;
  112. }
  113. }
  114. }
  115. return $sNextTag;
  116. }
  117. public function GetTagContent($sTag, $iStartPos, $iEndPos)
  118. {
  119. $sContent = "";
  120. $iContentStart = strpos($this->m_sTemplate, '>', $iStartPos); // Content of tag start immediatly after the first closing bracket
  121. if ($iContentStart !== false)
  122. {
  123. $sContent = substr($this->m_sTemplate, 1+$iContentStart, $iEndPos - $iContentStart - 1);
  124. }
  125. return $sContent;
  126. }
  127. public function GetTagAttributes($sTag, $iStartPos, $iEndPos)
  128. {
  129. $aAttr = array();
  130. $iAttrStart = strpos($this->m_sTemplate, ' ', $iStartPos); // Attributes start just after the first space
  131. $iAttrEnd = strpos($this->m_sTemplate, '>', $iStartPos); // Attributes end just before the first closing bracket
  132. if ( ($iAttrStart !== false) && ($iAttrEnd !== false) && ($iAttrEnd > $iAttrStart))
  133. {
  134. $sAttributes = substr($this->m_sTemplate, 1+$iAttrStart, $iAttrEnd - $iAttrStart - 1);
  135. $aAttributes = explode(' ', $sAttributes);
  136. foreach($aAttributes as $sAttr)
  137. {
  138. if ( preg_match('/(.+) *= *"(.+)"$/', $sAttr, $aMatches) )
  139. {
  140. $aAttr[strtolower($aMatches[1])] = $aMatches[2];
  141. }
  142. }
  143. }
  144. return $aAttr;
  145. }
  146. protected function RenderTag($oPage, $sTag, $aAttributes, $sContent)
  147. {
  148. static $iTabContainerCount = 0;
  149. switch($sTag)
  150. {
  151. case 'itoptabs':
  152. $oPage->AddTabContainer('Tabs_'.$iTabContainerCount);
  153. $oPage->SetCurrentTabContainer('Tabs_'.$iTabContainerCount);
  154. $iTabContainerCount++;
  155. //$oPage->p('Content:<pre>'.htmlentities($sContent).'</pre>');
  156. $oTemplate = new DisplayTemplate($sContent);
  157. $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied
  158. $oPage->SetCurrentTabContainer('');
  159. break;
  160. case 'itoptab':
  161. $oPage->SetCurrentTab(str_replace('_', ' ', $aAttributes['name']));
  162. $oTemplate = new DisplayTemplate($sContent);
  163. $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied
  164. //$oPage->p('iTop Tab Content:<pre>'.htmlentities($sContent).'</pre>');
  165. $oPage->SetCurrentTab('');
  166. break;
  167. case 'itoptoggle':
  168. $sName = isset($aAttributes['name']) ? $aAttributes['name'] : 'Tagada';
  169. $bOpen = isset($aAttributes['open']) ? $aAttributes['open'] : true;
  170. $oPage->StartCollapsibleSection($sName, $bOpen);
  171. $oTemplate = new DisplayTemplate($sContent);
  172. $oTemplate->Render($oPage, array()); // no params to apply, they have already been applied
  173. //$oPage->p('iTop Tab Content:<pre>'.htmlentities($sContent).'</pre>');
  174. $oPage->EndCollapsibleSection();
  175. break;
  176. case 'itopstring':
  177. $oPage->add(Dict::S($sContent));
  178. break;
  179. case 'itopblock': // No longer used, handled by DisplayBlock::FromTemplate see above
  180. $oPage->add("<!-- Application Error: should be handled by DisplayBlock::FromTemplate -->");
  181. break;
  182. default:
  183. // Unknown tag, just ignore it or now -- output an HTML comment
  184. $oPage->add("<!-- unsupported tag: $sTag -->");
  185. }
  186. }
  187. /**
  188. * Unit test
  189. */
  190. static public function UnitTest()
  191. {
  192. require_once('../application/startup.inc.php');
  193. require_once("../application/itopwebpage.class.inc.php");
  194. $sTemplate = '<div class="page_header">
  195. <div class="actions_details"><a href="#"><span>Actions</span></a></div>
  196. <h1>$class$: <span class="hilite">$name$</span></h1>
  197. <itopblock blockclass="HistoryBlock" type="toggle" encoding="text/oql">SELECT CMDBChangeOp WHERE objkey = $id$ AND objclass = \'$class$\'</itopblock>
  198. </div>
  199. <img src="../../images/connect_to_network.png" style="margin-top:-10px; margin-right:10px; float:right">
  200. <itopblock blockclass="DisplayBlock" asynchronous="false" type="bare_details" encoding="text/oql">SELECT bizNetworkDevice AS d WHERE d.id = $id$</itopblock>
  201. <itoptabs>
  202. <itoptab name="Interfaces">
  203. <itopblock blockclass="DisplayBlock" type="list" encoding="text/oql">SELECT bizInterface AS i WHERE i.device_id = $id$</itopblock>
  204. </itoptab>
  205. <itoptab name="Contacts">
  206. <itopblock blockclass="DisplayBlock" type="list" encoding="text/oql">SELECT bizContact AS c JOIN ContactsLinks AS l ON l.contact_id = c.id WHERE l.object_id = $id$</itopblock>
  207. </itoptab>
  208. <itoptab name="Documents">
  209. <itopblock blockclass="DisplayBlock" type="list" encoding="text/oql">SELECT bizDocument AS d JOIN lnkDocumentRealObject as l ON l.document_id = d.id WHERE l.object_id = $id$)</itopblock>
  210. </itoptab>
  211. </itoptabs>';
  212. $oPage = new iTopWebPage('Unit Test', 3);
  213. //$oPage->add("Template content: <pre>".htmlentities($sTemplate)."</pre>\n");
  214. $oTemplate = new DisplayTemplate($sTemplate);
  215. $oTemplate->Render($oPage, array('class'=>'Network device','pkey'=> 271, 'name' => 'deliversw01.mecanorama.fr', 'org_id' => 3));
  216. $oPage->output();
  217. }
  218. }
  219. //DisplayTemplate::UnitTest();
  220. ?>