displayblock.class.inc.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. <?php
  2. require_once('../application/webpage.class.inc.php');
  3. require_once('../application/utils.inc.php');
  4. require_once('../core/userrights.class.inc.php');
  5. /**
  6. * Helper class to manage 'blocks' of HTML pieces that are parts of a page and contain some list of cmdb objects
  7. *
  8. * Each block is actually rendered as a <div></div> tag that can be rendered synchronously
  9. * or as a piece of Javascript/JQuery/Ajax that will get its content from another page (ajax.render.php).
  10. * The list of cmdbObjects to be displayed into the block is defined by a filter
  11. * Right now the type of display is either: list, count, bare_details, details, csv, modify or search
  12. * - list produces a table listing the objects
  13. * - count produces a paragraphs with a sentence saying 'cont' objects found
  14. * - bare_details displays just the details of the attributes of the object (best if only one)
  15. * - details display the full details of each object found using its template (best if only one)
  16. * - csv displays a textarea with the CSV export of the list of objects
  17. * - modify displays the form to modify an object (best if only one)
  18. * - search displays a search form with the criteria of the filter set
  19. */
  20. class DisplayBlock
  21. {
  22. const TAG_BLOCK = 'itopblock';
  23. protected $m_oFilter;
  24. protected $m_sStyle;
  25. protected $m_bAsynchronous;
  26. protected $m_aParams;
  27. protected $m_oSet;
  28. public function __construct(DBObjectSearch $oFilter, $sStyle = 'list', $bAsynchronous = false, $aParams = array(), $oSet = null)
  29. {
  30. $this->m_oFilter = $oFilter;
  31. $this->m_sStyle = $sStyle;
  32. $this->m_bAsynchronous = $bAsynchronous;
  33. $this->m_aParams = $aParams;
  34. $this->m_oSet = $oSet;
  35. }
  36. /**
  37. * Constructs a DisplayBlock object from a DBObjectSet already in memory
  38. * @param $oSet DBObjectSet
  39. * @return DisplayBlock The DisplayBlock object, or null if the creation failed
  40. */
  41. public static function FromObjectSet(DBObjectSet $oSet, $sStyle, $aParams = array())
  42. {
  43. $oDummyFilter = new DBObjectSearch($oSet->GetClass());
  44. $oBlock = new DisplayBlock($oDummyFilter, $sStyle, false, $aParams, $oSet); // DisplayBlocks built this way are synchronous
  45. return $oBlock;
  46. }
  47. /**
  48. * Constructs a DisplayBlock object from an XML template
  49. * @param $sTemplate string The XML template
  50. * @return DisplayBlock The DisplayBlock object, or null if the template is invalid
  51. */
  52. public static function FromTemplate($sTemplate)
  53. {
  54. $iStartPos = stripos($sTemplate, '<'.self::TAG_BLOCK.' ',0);
  55. $iEndPos = stripos($sTemplate, '</'.self::TAG_BLOCK.'>', $iStartPos);
  56. if (($iStartPos === false) || ($iEndPos === false)) return null; // invalid template
  57. $sITopBlock = substr($sTemplate,$iStartPos, $iEndPos-$iStartPos);
  58. $sITopData = substr($sITopBlock, 1+stripos($sITopBlock, ">"));
  59. $sITopTag = substr($sITopBlock, 0, stripos($sITopBlock, ">"));
  60. $aMatches = array();
  61. $sBlockClass = "DisplayBlock";
  62. $bAsynchronous = false;
  63. $sBlockType = 'list';
  64. $sEncoding = 'text/serialize';
  65. if (preg_match('/ type="(.*)"/U',$sITopTag, $aMatches))
  66. {
  67. $sBlockType = strtolower($aMatches[1]);
  68. }
  69. if (preg_match('/ asynchronous="(.*)"/U',$sITopTag, $aMatches))
  70. {
  71. $bAsynchronous = (strtolower($aMatches[1]) == 'true');
  72. }
  73. if (preg_match('/ blockclass="(.*)"/U',$sITopTag, $aMatches))
  74. {
  75. $sBlockClass = $aMatches[1];
  76. }
  77. if (preg_match('/ objectclass="(.*)"/U',$sITopTag, $aMatches))
  78. {
  79. $sObjectClass = $aMatches[1];
  80. }
  81. if (preg_match('/ encoding="(.*)"/U',$sITopTag, $aMatches))
  82. {
  83. $sEncoding = strtolower($aMatches[1]);
  84. }
  85. if (preg_match('/ link_attr="(.*)"/U',$sITopTag, $aMatches))
  86. {
  87. // The list to display is a list of links to the specified object
  88. $aParams['link_attr'] = $aMatches[1]; // Name of the Ext. Key that make this linkage
  89. }
  90. if (preg_match('/ object_id="(.*)"/U',$sITopTag, $aMatches))
  91. {
  92. // The list to display is a list of links to the specified object
  93. $aParams['object_id'] = $aMatches[1]; // Id of the object to be linked to
  94. }
  95. // Parameters contains a list of extra parameters for the block
  96. // the syntax is param_name1:value1;param_name2:value2;...
  97. $aParams = array();
  98. if (preg_match('/ parameters="(.*)"/U',$sITopTag, $aMatches))
  99. {
  100. $sParameters = $aMatches[1];
  101. $aPairs = explode(';', $sParameters);
  102. foreach($aPairs as $sPair)
  103. {
  104. if (preg_match('/(.*)\:(.*)/',$sPair, $aMatches))
  105. {
  106. $aParams[trim($aMatches[1])] = trim($aMatches[2]);
  107. }
  108. }
  109. }
  110. if (!empty($aParams['link_attr']))
  111. {
  112. // Check that all mandatory parameters are present:
  113. if(empty($aParams['object_id']))
  114. {
  115. // if 'links' mode is requested the d of the object to link to must be specified
  116. throw new ApplicationException("Parameter object_id is mandatory when link_attr is specified. Check the definition of the display template.");
  117. }
  118. if(empty($aParams['target_attr']))
  119. {
  120. // if 'links' mode is requested the d of the object to link to must be specified
  121. throw new ApplicationException("Parameter target_attr is mandatory when link_attr is specified. Check the definition of the display template.");
  122. }
  123. }
  124. switch($sEncoding)
  125. {
  126. case 'text/serialize':
  127. $oFilter = CMDBSearchFilter::unserialize($sITopData);
  128. break;
  129. case 'text/sibusql':
  130. $oFilter = CMDBSearchFilter::FromSibusQL($sITopData);
  131. break;
  132. case 'text/oql':
  133. $oFilter = CMDBSearchFilter::FromOQL($sITopData);
  134. break;
  135. }
  136. return new $sBlockClass($oFilter, $sBlockType, $bAsynchronous, $aParams);
  137. }
  138. public function Display(web_page $oPage, $sId, $aExtraParams = array())
  139. {
  140. $aExtraParams = array_merge($aExtraParams, $this->m_aParams);
  141. if (!$this->m_bAsynchronous)
  142. {
  143. // render now
  144. $oPage->add("<div id=\"$sId\" class=\"display_block\">\n");
  145. $this->RenderContent($oPage, $aExtraParams);
  146. $oPage->add("</div>\n");
  147. }
  148. else
  149. {
  150. // render it as an Ajax (asynchronous) call
  151. $sFilter = $this->m_oFilter->serialize();
  152. $oPage->add("<div id=\"$sId\" class=\"display_block loading\">\n");
  153. $oPage->p("<img src=\"../images/indicator_arrows.gif\"> Loading...");
  154. $oPage->add("</div>\n");
  155. $oPage->add('
  156. <script language="javascript">
  157. $.get("ajax.render.php?filter='.$sFilter.'&style='.$this->m_sStyle.'",
  158. { operation: "ajax" },
  159. function(data){
  160. $("#'.$sId.'").empty();
  161. $("#'.$sId.'").append(data);
  162. $("#'.$sId.'").removeClass("loading");
  163. }
  164. );
  165. </script>'); // TO DO: add support for $aExtraParams in asynchronous/Ajax mode
  166. }
  167. }
  168. public function GetDisplay(web_page $oPage, $sId, $aExtraParams = array())
  169. {
  170. $sHtml = '';
  171. $aExtraParams = array_merge($aExtraParams, $this->m_aParams);
  172. if (!$this->m_bAsynchronous)
  173. {
  174. // render now
  175. $sHtml .= "<div id=\"$sId\" class=\"display_block\">\n";
  176. $sHtml .= $this->GetRenderContent($oPage, $aExtraParams);
  177. $sHtml .= "</div>\n";
  178. }
  179. else
  180. {
  181. // render it as an Ajax (asynchronous) call
  182. $sFilter = $this->m_oFilter->serialize();
  183. $sHtml .= "<div id=\"$sId\" class=\"display_block loading\">\n";
  184. $sHtml .= $oPage->GetP("<img src=\"../images/indicator_arrows.gif\"> Loading...");
  185. $sHtml .= "</div>\n";
  186. $sHtml .= '
  187. <script language="javascript">
  188. $.get("ajax.render.php?filter='.$sFilter.'&style='.$this->m_sStyle.'",
  189. { operation: "ajax" },
  190. function(data){
  191. $("#'.$sId.'").empty();
  192. $("#'.$sId.'").append(data);
  193. $("#'.$sId.'").removeClass("loading");
  194. }
  195. );
  196. </script>'; // TO DO: add support for $aExtraParams in asynchronous/Ajax mode
  197. }
  198. return $sHtml;
  199. }
  200. public function RenderContent(web_page $oPage, $aExtraParams = array())
  201. {
  202. $oPage->add($this->GetRenderContent($oPage, $aExtraParams));
  203. }
  204. public function GetRenderContent(web_page $oPage, $aExtraParams = array())
  205. {
  206. $sHtml = '';
  207. // Add the extra params into the filter if they make sense for such a filter
  208. $bDoSearch = utils::ReadParam('dosearch', false);
  209. if ($this->m_oSet == null)
  210. {
  211. if ($this->m_sStyle != 'links')
  212. {
  213. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  214. foreach($aFilterCodes as $sFilterCode)
  215. {
  216. $sExternalFilterValue = utils::ReadParam($sFilterCode, '');
  217. if (isset($aExtraParams[$sFilterCode]))
  218. {
  219. $this->m_oFilter->AddCondition($sFilterCode, $aExtraParams[$sFilterCode]); // Use the default 'loose' operator
  220. }
  221. else if ($bDoSearch && $sExternalFilterValue != "")
  222. {
  223. $this->m_oFilter->AddCondition($sFilterCode, $sExternalFilterValue); // Use the default 'loose' operator
  224. }
  225. }
  226. }
  227. $this->m_oSet = new CMDBObjectSet($this->m_oFilter);
  228. }
  229. switch($this->m_sStyle)
  230. {
  231. case 'count':
  232. if (isset($aExtraParams['group_by']))
  233. {
  234. $sGroupByField = $aExtraParams['group_by'];
  235. $aGroupBy = array();
  236. while($oObj = $this->m_oSet->Fetch())
  237. {
  238. $sValue = $oObj->Get($sGroupByField);
  239. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  240. }
  241. $sFilter = urlencode($this->m_oFilter->serialize());
  242. $aData = array();
  243. foreach($aGroupBy as $sValue => $iCount)
  244. {
  245. $aData[] = array ( 'group' => $sValue,
  246. 'value' => "<a href=\"./UI.php?operation=search&dosearch=1&filter=$sFilter&$sGroupByField=".urlencode($sValue)."\">$iCount</a>"); // TO DO: add the context information
  247. }
  248. $sHtml .= $oPage->GetTable(array('group' => array('label' => MetaModel::GetLabel($this->m_oFilter->GetClass(), $sGroupByField), 'description' => ''), 'value' => array('label'=>'Count', 'description' => 'Number of elements')), $aData);
  249. }
  250. else
  251. {
  252. // Simply count the number of elements in the set
  253. $iCount = $oSet->Count();
  254. $sHtml .= $oPage->GetP("$iCount objects matching the criteria.");
  255. }
  256. break;
  257. case 'list':
  258. if ( ($this->m_oSet->Count()> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) )
  259. {
  260. $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $aExtraParams);
  261. }
  262. else
  263. {
  264. $sHtml .= $oPage->GetP("No object to display.");
  265. $sClass = $this->m_oFilter->GetClass();
  266. $bDisplayMenu = isset($this->m_aParams['menu']) ? $this->m_aParams['menu'] == true : true;
  267. if ($bDisplayMenu)
  268. {
  269. if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES)
  270. {
  271. $sHtml .= $oPage->GetP("<a href=\"./UI.php?operation=new&class=$sClass\">Click here to create a new ".Metamodel::GetName($sClass)."</a>\n");
  272. }
  273. }
  274. }
  275. break;
  276. case 'links':
  277. //$bDashboardMode = isset($aExtraParams['dashboard']) ? ($aExtraParams['dashboard'] == 'true') : false;
  278. //$bSelectMode = isset($aExtraParams['select']) ? ($aExtraParams['select'] == 'true') : false;
  279. if ( ($this->m_oSet->Count()> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) )
  280. {
  281. //$sLinkage = isset($aExtraParams['linkage']) ? $aExtraParams['linkage'] : '';
  282. $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $aExtraParams);
  283. }
  284. else
  285. {
  286. $sClass = $this->m_oFilter->GetClass();
  287. $oAttDef = MetaModel::GetAttributeDef($sClass, $this->m_aParams['target_attr']);
  288. $sTargetClass = $oAttDef->GetTargetClass();
  289. $sHtml .= $oPage->GetP("No ".MetaModel::GetName($sTargetClass)." to display.");
  290. $bDisplayMenu = isset($this->m_aParams['menu']) ? $this->m_aParams['menu'] == true : true;
  291. if ($bDisplayMenu)
  292. {
  293. if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES)
  294. {
  295. $sHtml .= $oPage->GetP("<a href=\"../pages/UI.php?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&id=".$aExtraParams['object_id']."&target_class=$sTargetClass&addObjects=true\">Click here to add new ".Metamodel::GetName($sTargetClass)."s</a>\n");
  296. }
  297. }
  298. }
  299. break;
  300. case 'details':
  301. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES)
  302. {
  303. while($oObj = $this->m_oSet->Fetch())
  304. {
  305. $sHtml .= $oObj->GetDetails($oPage); // Still used ???
  306. }
  307. }
  308. break;
  309. case 'bare_details':
  310. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES)
  311. {
  312. while($oObj = $this->m_oSet->Fetch())
  313. {
  314. $sHtml .= $oObj->GetBareDetails($oPage);
  315. }
  316. }
  317. break;
  318. case 'csv':
  319. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES)
  320. {
  321. $sHtml .= "<textarea style=\"width:95%;height:98%\">\n";
  322. $sHtml .= cmdbAbstractObject::GetSetAsCSV($this->m_oSet);
  323. $sHtml .= "</textarea>\n";
  324. }
  325. break;
  326. case 'modify':
  327. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES)
  328. {
  329. while($oObj = $this->m_oSet->Fetch())
  330. {
  331. $sHtml .= $oObj->GetModifyForm($oPage);
  332. }
  333. }
  334. break;
  335. case 'search':
  336. $iSearchSectionId = 1;
  337. $sStyle = (isset($aExtraParams['open']) && ($aExtraParams['open'] == 'true')) ? 'SearchDrawer' : 'SearchDrawer DrawerClosed';
  338. $sHtml .= "<div id=\"Search_$iSearchSectionId\" class=\"$sStyle\">\n";
  339. $oPage->add_ready_script("\$(\"#LnkSearch_$iSearchSectionId\").click(function() {\$(\"#Search_$iSearchSectionId\").slideToggle('normal'); $(\"#LnkSearch_$iSearchSectionId\").toggleClass('open');});");
  340. $sHtml .= cmdbAbstractObject::GetSearchForm($oPage, $this->m_oSet, $aExtraParams);
  341. $sHtml .= "</div>\n";
  342. $sHtml .= "<div class=\"HRDrawer\"></div>\n";
  343. $sHtml .= "<div id=\"LnkSearch_$iSearchSectionId\" class=\"DrawerHandle\">Search</div>\n";
  344. break;
  345. case 'pie_chart':
  346. $sGroupBy = isset($aExtraParams['group_by']) ? $aExtraParams['group_by'] : '';
  347. $sFilter = $this->m_oFilter->ToOQL();
  348. $sHtml .= "
  349. <OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
  350. codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\"
  351. WIDTH=\"400\"
  352. HEIGHT=\"250\"
  353. id=\"charts\"
  354. ALIGN=\"\">
  355. <PARAM NAME=movie VALUE=\"../images/charts.swf?library_path=../images/charts_library&xml_source=".urlencode("../pages/ajax.render.php?operation=pie_chart&group_by=$sGroupBy&encoding=oql&filter=".urlencode($sFilter))."\">
  356. <PARAM NAME=\"quality\" VALUE=\"high\">
  357. <PARAM NAME=\"bgcolor\" VALUE=\"#ffffff\">
  358. <EMBED src=\"../images/charts.swf?library_path=../images/charts_library&xml_source=".urlencode("../pages/ajax.render.php?operation=pie_chart&group_by=$sGroupBy&encoding=oql&filter=".urlencode($sFilter))."\"
  359. quality=\"high\"
  360. bgcolor=\"#ffffff\"
  361. WIDTH=\"400\"
  362. HEIGHT=\"250\"
  363. NAME=\"charts\"
  364. ALIGN=\"\"
  365. swLiveConnect=\"true\"
  366. TYPE=\"application/x-shockwave-flash\"
  367. PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">
  368. </EMBED>
  369. </OBJECT>
  370. ";
  371. break;
  372. case 'pie_chart_ajax':
  373. if (isset($aExtraParams['group_by']))
  374. {
  375. $sGroupByField = $aExtraParams['group_by'];
  376. $aGroupBy = array();
  377. while($oObj = $this->m_oSet->Fetch())
  378. {
  379. $sValue = $oObj->Get($sGroupByField);
  380. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  381. }
  382. $sFilter = urlencode($this->m_oFilter->serialize());
  383. $aData = array();
  384. $sHtml .= "<chart>\n";
  385. $sHtml .= "<chart_type>3d pie</chart_type>\n";
  386. $sHtml .= "<chart_data>\n";
  387. $sHtml .= "<row>\n";
  388. $sHtml .= "<null/>\n";
  389. foreach($aGroupBy as $sValue => $void)
  390. {
  391. $sHtml .= "<string>$sValue</string>\n";
  392. }
  393. $sHtml .= "</row>\n";
  394. $sHtml .= "<row>\n";
  395. $sHtml .= "<string></string>\n";
  396. foreach($aGroupBy as $void => $iCount)
  397. {
  398. $sHtml .= "<number>$iCount</number>\n";
  399. }
  400. $sHtml .= "</row>\n";
  401. $sHtml .= "</chart_data>\n";
  402. $sHtml .= "
  403. <chart_value color='ffffff' alpha='90' font='arial' bold='true' size='10' position='inside' prefix='' suffix='' decimals='0' separator='' as_percentage='true' />
  404. <draw>
  405. <text color='000000' alpha='10' font='arial' rotation='0' bold='true' size='30' x='0' y='140' width='400' height='150' h_align='center' v_align='bottom'>|||||||||||||||||||||||||||||||||||||||||||||||</text>
  406. </draw>
  407. <legend_label layout='horizontal' bullet='circle' font='arial' bold='true' size='13' color='000000' alpha='85' />
  408. <legend_rect fill_color='ffffff' fill_alpha='10' line_color='ffffff' line_alpha='50' line_thickness='0' />
  409. <series_color>
  410. <color>ddaa41</color>
  411. <color>88dd11</color>
  412. <color>4e62dd</color>
  413. <color>ff8811</color>
  414. <color>4d4d4d</color>
  415. <color>5a4b6e</color>
  416. <color>1188ff</color>
  417. </series_color>
  418. ";
  419. $sHtml .= "</chart>\n";
  420. }
  421. else
  422. {
  423. // Simply count the number of elements in the set
  424. $iCount = $oSet->Count();
  425. $sHtml .= "<chart>\n</chart>\n";
  426. }
  427. break;
  428. case 'open_flash_chart':
  429. static $iChartCounter = 0;
  430. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  431. $sTitle = isset($aExtraParams['chart_title']) ? $aExtraParams['chart_title'] : '';
  432. $sGroupBy = isset($aExtraParams['group_by']) ? $aExtraParams['group_by'] : '';
  433. $sFilter = $this->m_oFilter->ToOQL();
  434. $sHtml .= "<script>
  435. swfobject.embedSWF(\"../images/open-flash-chart.swf\", \"my_chart_{$iChartCounter}\", \"400\", \"400\",\"9.0.0\", \"expressInstall.swf\",
  436. {\"data-file\":\"".urlencode("../pages/ajax.render.php?operation=open_flash_chart&params[group_by]=$sGroupBy&params[chart_type]=$sChartType&params[chart_title]=$sTitle&encoding=oql&filter=".urlencode($sFilter))."\"});
  437. </script>\n";
  438. $sHtml .= "<div id=\"my_chart_{$iChartCounter}\">Here goes the chart</div>\n";
  439. $iChartCounter++;
  440. break;
  441. case 'open_flash_chart_ajax':
  442. include './php-ofc-library/open-flash-chart.php';
  443. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  444. $oChart = new open_flash_chart();
  445. switch($sChartType)
  446. {
  447. case 'bars':
  448. $oChartElement = new bar_glass();
  449. if (isset($aExtraParams['group_by']))
  450. {
  451. $sGroupByField = $aExtraParams['group_by'];
  452. $aGroupBy = array();
  453. while($oObj = $this->m_oSet->Fetch())
  454. {
  455. $sValue = $oObj->Get($sGroupByField);
  456. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  457. }
  458. $sFilter = urlencode($this->m_oFilter->serialize());
  459. $aData = array();
  460. $aLabels = array();
  461. foreach($aGroupBy as $sValue => $iValue)
  462. {
  463. $aData[] = $iValue;
  464. $aLabels[] = $sValue;
  465. }
  466. $maxValue = max($aData);
  467. $oYAxis = new y_axis();
  468. $aMagicValues = array(1,2,5,10);
  469. $iMultiplier = 1;
  470. $index = 0;
  471. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  472. while($maxValue > $iTop)
  473. {
  474. $index++;
  475. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  476. if (($index % count($aMagicValues)) == 0)
  477. {
  478. $iMultiplier = $iMultiplier * 10;
  479. }
  480. }
  481. //echo "oYAxis->set_range(0, $iTop, $iMultiplier);\n";
  482. $oYAxis->set_range(0, $iTop, $iMultiplier);
  483. $oChart->set_y_axis( $oYAxis );
  484. $oChartElement->set_values( $aData );
  485. $oXAxis = new x_axis();
  486. $oXLabels = new x_axis_labels();
  487. // set them vertical
  488. $oXLabels->set_vertical();
  489. // set the label text
  490. $oXLabels->set_labels($aLabels);
  491. // Add the X Axis Labels to the X Axis
  492. $oXAxis->set_labels( $oXLabels );
  493. $oChart->set_x_axis( $oXAxis );
  494. }
  495. break;
  496. case 'pie':
  497. default:
  498. $oChartElement = new pie();
  499. $oChartElement->set_start_angle( 35 );
  500. $oChartElement->set_animate( true );
  501. $oChartElement->set_tooltip( '#label# - #val# (#percent#)' );
  502. if (isset($aExtraParams['group_by']))
  503. {
  504. $sGroupByField = $aExtraParams['group_by'];
  505. $aGroupBy = array();
  506. while($oObj = $this->m_oSet->Fetch())
  507. {
  508. $sValue = $oObj->Get($sGroupByField);
  509. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  510. }
  511. $sFilter = urlencode($this->m_oFilter->serialize());
  512. $aData = array();
  513. foreach($aGroupBy as $sValue => $iValue)
  514. {
  515. $aData[] = new pie_value($iValue, $sValue);
  516. }
  517. $oChartElement->set_values( $aData );
  518. $oChart->x_axis = null;
  519. }
  520. }
  521. if (isset($aExtraParams['chart_title'])) //@@ BUG: not passed via ajax !!!
  522. {
  523. $oTitle = new title( $aExtraParams['chart_title'] );
  524. $oChart->set_title( $oTitle );
  525. }
  526. $oChart->set_bg_colour('#FFFFFF');
  527. $oChart->add_element( $oChartElement );
  528. $sHtml = $oChart->toPrettyString();
  529. break;
  530. default:
  531. // Unsupported style, do nothing.
  532. $sHtml .= "Error: unsupported style of block: ".$this->m_sStyle;
  533. }
  534. return $sHtml;
  535. }
  536. }
  537. /**
  538. * Helper class to manage 'blocks' of HTML pieces that are parts of a page and contain some list of cmdb objects
  539. *
  540. * Each block is actually rendered as a <div></div> tag that can be rendered synchronously
  541. * or as a piece of Javascript/JQuery/Ajax that will get its content from another page (ajax.render.php).
  542. * The list of cmdbObjects to be displayed into the block is defined by a filter
  543. * Right now the type of display is either: list, count or details
  544. * - list produces a table listing the objects
  545. * - count produces a paragraphs with a sentence saying 'cont' objects found
  546. * - details display (as table) the details of each object found (best if only one)
  547. */
  548. class HistoryBlock extends DisplayBlock
  549. {
  550. public function GetRenderContent(web_page $oPage, $aExtraParams = array())
  551. {
  552. $sHtml = '';
  553. // Add the extra params into the filter if they make sense for such a filter
  554. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  555. foreach($aFilterCodes as $sFilterCode)
  556. {
  557. if (isset($aExtraParams[$sFilterCode]))
  558. {
  559. $this->m_oFilter->AddCondition($sFilterCode, $aExtraParams[$sFilterCode]); // Use the default 'loose' operator
  560. }
  561. }
  562. $oSet = new CMDBObjectSet($this->m_oFilter, array('date'=>false));
  563. $sHtml .= "<!-- filter: ".($this->m_oFilter->ToOQL())."-->\n";
  564. switch($this->m_sStyle)
  565. {
  566. case 'toggle':
  567. // First the latest change that the user is allowed to see
  568. do
  569. {
  570. $oLatestChangeOp = $oSet->Fetch();
  571. }
  572. while(is_object($oLatestChangeOp) && ($oLatestChangeOp->GetDescription() == ''));
  573. if (is_object($oLatestChangeOp))
  574. {
  575. global $oContext; // User Context.. should be statis instead of global...
  576. // There is one change in the list... only when the object has been created !
  577. $sDate = $oLatestChangeOp->GetAsHTML('date');
  578. $oChange = $oContext->GetObject('CMDBChange', $oLatestChangeOp->Get('change'));
  579. $sUserInfo = $oChange->GetAsHTML('userinfo');
  580. $oSet->Rewind(); // Reset the pointer to the beginning of the set
  581. $sHtml .= $oPage->GetStartCollapsibleSection("Last modified on $sDate by $sUserInfo.");
  582. //$sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $oSet);
  583. $aChanges = array();
  584. while($oChangeOp = $oSet->Fetch())
  585. {
  586. $sChangeDescription = $oChangeOp->GetDescription();
  587. if ($sChangeDescription != '')
  588. {
  589. // The change is visible for the current user
  590. $changeId = $oChangeOp->Get('change');
  591. $aChanges[$changeId]['date'] = $oChangeOp->Get('date');
  592. $aChanges[$changeId]['userinfo'] = $oChangeOp->Get('userinfo');
  593. if (!isset($aChanges[$changeId]['log']))
  594. {
  595. $aChanges[$changeId]['log'] = array();
  596. }
  597. $aChanges[$changeId]['log'][] = $sChangeDescription;
  598. }
  599. }
  600. $aAttribs = array('date' => array('label' => 'Date', 'description' => 'Date of the change'),
  601. 'userinfo' => array('label' => 'User', 'description' => 'User who made the change'),
  602. 'log' => array('label' => 'Changes', 'description' => 'Changes made to the object'),
  603. );
  604. $aValues = array();
  605. foreach($aChanges as $aChange)
  606. {
  607. $aValues[] = array('date' => $aChange['date'], 'userinfo' => $aChange['userinfo'], 'log' => "<ul><li>".implode('</li><li>', $aChange['log'])."</li></ul>");
  608. }
  609. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  610. $sHtml .= $oPage->GetEndCollapsibleSection();
  611. }
  612. break;
  613. default:
  614. $sHtml .= parent::GetRenderContent($oPage, $aExtraParams);
  615. }
  616. return $sHtml;
  617. }
  618. }
  619. class MenuBlock extends DisplayBlock
  620. {
  621. public function GetRenderContent(web_page $oPage, $aExtraParams = array())
  622. {
  623. $sHtml = '';
  624. $oAppContext = new ApplicationContext();
  625. $sContext = $oAppContext->GetForLink();
  626. $sClass = $this->m_oFilter->GetClass();
  627. $oSet = new CMDBObjectSet($this->m_oFilter);
  628. $sFilter = $this->m_oFilter->serialize();
  629. $aActions = array();
  630. $sUIPage = cmdbAbstractObject::ComputeUIPage($sClass);
  631. switch($oSet->Count())
  632. {
  633. case 0:
  634. // No object in the set, the only possible action is "new"
  635. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  636. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New', 'url' => "../page/$sUIPage?operation=new&class=$sClass&$sContext"); }
  637. break;
  638. case 1:
  639. $oObj = $oSet->Fetch();
  640. $id = $oObj->GetKey();
  641. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  642. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  643. $bIsBulkModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  644. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  645. // Just one object in the set, possible actions are "new / clone / modify and delete"
  646. if (isset($aExtraParams['link_attr']))
  647. {
  648. $id = $aExtraParams['object_id'];
  649. $sTargetAttr = $aExtraParams['target_attr'];
  650. $oAttDef = MetaModel::GetAttributeDef($sClass, $sTargetAttr);
  651. $sTargetClass = $oAttDef->GetTargetClass();
  652. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'Add...', 'url' => "../pages/$sUIPage?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&target_class=$sTargetClass&id=$id&addObjects=true&$sContext"); }
  653. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'Manage...', 'url' => "../pages/$sUIPage?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&target_class=$sTargetClass&id=$id&sContext"); }
  654. if ($bIsDeleteAllowed) { $aActions[] = array ('label' => 'Remove All', 'url' => "#"); }
  655. }
  656. else
  657. {
  658. $sUrl = self::GetAbsoluteUrl();
  659. $aActions[] = array ('label' => 'eMail', 'url' => "mailto:?subject=".$oSet->GetFilter()->__DescribeHTML()."&body=".urlencode("$sUrl?operation=search&filter=$sFilter&$sContext"));
  660. $aActions[] = array ('label' => 'CSV Export', 'url' => "../pages/$sUIPage?operation=search&filter=$sFilter&format=csv&$sContext");
  661. $aActions[] = array ('label' => 'Bookmark...', 'url' => "../pages/ajax.render.php?operation=create&class=$sClass&filter=$sFilter", 'class' => 'jqmTrigger');
  662. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New...', 'url' => "../pages/$sUIPage?operation=new&class=$sClass&$sContext"); }
  663. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'Clone...', 'url' => "../pages/$sUIPage?operation=clone&class=$sClass&id=$id&$sContext"); }
  664. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'Modify...', 'url' => "../pages/$sUIPage?operation=modify&class=$sClass&id=$id&$sContext"); }
  665. if ($bIsDeleteAllowed) { $aActions[] = array ('label' => 'Delete', 'url' => "../pages/$sUIPage?operation=delete&class=$sClass&id=$id&$sContext"); }
  666. }
  667. $aTransitions = $oObj->EnumTransitions();
  668. $aStimuli = Metamodel::EnumStimuli($sClass);
  669. foreach($aTransitions as $sStimulusCode => $aTransitionDef)
  670. {
  671. $iActionAllowed = UserRights::IsStimulusAllowed($sClass, $sStimulusCode, $oSet);
  672. switch($iActionAllowed)
  673. {
  674. case UR_ALLOWED_YES:
  675. $aActions[] = array('label' => $aStimuli[$sStimulusCode]->Get('label'), 'url' => "../pages/UI.php?operation=stimulus&stimulus=$sStimulusCode&class=$sClass&id=$id&$sContext");
  676. break;
  677. case UR_ALLOWED_DEPENDS:
  678. $aActions[] = array('label' => $aStimuli[$sStimulusCode]->Get('label').' (*)', 'url' => "../pages/UI.php?operation=stimulus&stimulus=$sStimulusCode&class=$sClass&id=$id&$sContext");
  679. break;
  680. default:
  681. // Do nothing
  682. }
  683. }
  684. //print_r($aTransitions);
  685. break;
  686. default:
  687. // Check rights
  688. // New / Modify
  689. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  690. $bIsBulkModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  691. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  692. if (isset($aExtraParams['link_attr']))
  693. {
  694. $id = $aExtraParams['object_id'];
  695. $sTargetAttr = $aExtraParams['target_attr'];
  696. $oAttDef = MetaModel::GetAttributeDef($sClass, $sTargetAttr);
  697. $sTargetClass = $oAttDef->GetTargetClass();
  698. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  699. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'Add...', 'url' => "../pages/$sUIPage?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&target_class=$sTargetClass&id=$id&addObjects=true&$sContext"); }
  700. //if ($bIsBulkModifyAllowed) { $aActions[] = array ('label' => 'Add...', 'url' => "../pages/$sUIPage?operation=modify_links&class=$sClass&linkage=".$aExtraParams['linkage']."&id=$id&addObjects=true&$sContext"); }
  701. if ($bIsBulkModifyAllowed) { $aActions[] = array ('label' => 'Manage...', 'url' => "../pages/$sUIPage?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&target_class=$sTargetClass&id=$id&sContext"); }
  702. if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => 'Remove All...', 'url' => "#"); }
  703. }
  704. else
  705. {
  706. // many objects in the set, possible actions are: new / modify all / delete all
  707. $sUrl = self::GetAbsoluteUrl();
  708. $aActions[] = array ('label' => 'eMail', 'url' => "mailto:?subject=".$oSet->GetFilter()->__DescribeHTML()."&body=".urlencode("$sUrl?operation=search&filter=$sFilter&$sContext"));
  709. $aActions[] = array ('label' => 'CSV Export', 'url' => "../pages/$sUIPage?operation=search&filter=$sFilter&format=csv&$sContext");
  710. $aActions[] = array ('label' => 'Bookmark...', 'url' => "../pages/ajax.render.php?operation=create&class=$sClass&filter=$sFilter", 'class' => 'jqmTrigger');
  711. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New...', 'url' => "../pages/$sUIPage?operation=new&class=$sClass&$sContext"); }
  712. if ($bIsBulkModifyAllowed) { $aActions[] = array ('label' => 'Modify All...', 'url' => "../pages/$sUIPage?operation=modify_all&filter=$sFilter&$sContext"); }
  713. if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => 'Delete All', 'url' => "../pages/$sUIPage?operation=delete_all&filter=$sFilter&$sContext"); }
  714. }
  715. }
  716. $sHtml .= "<div class=\"jd_menu_itop\"><ul class=\"jd_menu jd_menu_itop\">\n<li>Actions\n<ul>\n";
  717. foreach ($aActions as $aAction)
  718. {
  719. $sClass = isset($aAction['class']) ? " class=\"{$aAction['class']}\"" : "";
  720. $sHtml .= "<li><a href=\"{$aAction['url']}\"$sClass>{$aAction['label']}</a></li>\n<li>\n";
  721. }
  722. $sHtml .= "</ul>\n</li>\n</ul></div>\n";
  723. $oPage->add_ready_script("$(\"ul.jd_menu\").jdMenu();\n");
  724. return $sHtml;
  725. }
  726. static public function GetAbsoluteUrl()
  727. {
  728. // Build an absolute URL to this page on this server/port
  729. $sServerName = $_SERVER['SERVER_NAME'];
  730. $sProtocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
  731. if ($sProtocol == 'http')
  732. {
  733. $sPort = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT'];
  734. }
  735. else
  736. {
  737. $sPort = ($_SERVER['SERVER_PORT'] == 443) ? '' : ':'.$_SERVER['SERVER_PORT'];
  738. }
  739. // $_SERVER['REQUEST_URI'] is empty when running on IIS
  740. // Let's use Ivan Tcholakov's fix (found on www.dokeos.com)
  741. if (!empty($_SERVER['REQUEST_URI']))
  742. {
  743. $sPath = $_SERVER['REQUEST_URI'];
  744. }
  745. else
  746. {
  747. $sPath = $_SERVER['SCRIPT_NAME'];
  748. if (!empty($_SERVER['QUERY_STRING']))
  749. {
  750. $sPath .= '?'.$_SERVER['QUERY_STRING'];
  751. }
  752. $_SERVER['REQUEST_URI'] = $sPath;
  753. }
  754. $sPath = $_SERVER['REQUEST_URI'];
  755. $sUrl = "$sProtocol://{$sServerName}{$sPort}{$sPath}";
  756. return $sUrl;
  757. }
  758. }
  759. ?>