displayblock.class.inc.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  212. foreach($aFilterCodes as $sFilterCode)
  213. {
  214. $sExternalFilterValue = utils::ReadParam($sFilterCode, '');
  215. if (isset($aExtraParams[$sFilterCode]))
  216. {
  217. $this->m_oFilter->AddCondition($sFilterCode, $aExtraParams[$sFilterCode]); // Use the default 'loose' operator
  218. }
  219. else if ($bDoSearch && $sExternalFilterValue != "")
  220. {
  221. $this->m_oFilter->AddCondition($sFilterCode, $sExternalFilterValue); // Use the default 'loose' operator
  222. }
  223. }
  224. $this->m_oSet = new CMDBObjectSet($this->m_oFilter);
  225. }
  226. switch($this->m_sStyle)
  227. {
  228. case 'count':
  229. if (isset($aExtraParams['group_by']))
  230. {
  231. $sGroupByField = $aExtraParams['group_by'];
  232. $aGroupBy = array();
  233. while($oObj = $this->m_oSet->Fetch())
  234. {
  235. $sValue = $oObj->Get($sGroupByField);
  236. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  237. }
  238. $sFilter = urlencode($this->m_oFilter->serialize());
  239. $aData = array();
  240. foreach($aGroupBy as $sValue => $iCount)
  241. {
  242. $aData[] = array ( 'group' => $sValue,
  243. 'value' => "<a href=\"./UI.php?operation=search&dosearch=1&filter=$sFilter&$sGroupByField=".urlencode($sValue)."\">$iCount</a>"); // TO DO: add the context information
  244. }
  245. $sHtml .= $oPage->GetTable(array('group' => array('label' => MetaModel::GetLabel($this->m_oFilter->GetClass(), $sGroupByField), 'description' => ''), 'value' => array('label'=>'Count', 'description' => 'Number of elements')), $aData);
  246. }
  247. else
  248. {
  249. // Simply count the number of elements in the set
  250. $iCount = $oSet->Count();
  251. $sHtml .= $oPage->GetP("$iCount objects matching the criteria.");
  252. }
  253. break;
  254. case 'list':
  255. if ( ($this->m_oSet->Count()> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) )
  256. {
  257. $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $aExtraParams);
  258. }
  259. else
  260. {
  261. $sHtml .= $oPage->GetP("No object to display.");
  262. $sClass = $this->m_oFilter->GetClass();
  263. $bDisplayMenu = isset($this->m_aParams['menu']) ? $this->m_aParams['menu'] == true : true;
  264. if ($bDisplayMenu)
  265. {
  266. if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES)
  267. {
  268. $sHtml .= $oPage->GetP("<a href=\"./UI.php?operation=new&class=$sClass\">Click here to create a new ".Metamodel::GetName($sClass)."</a>\n");
  269. }
  270. }
  271. }
  272. break;
  273. case 'links':
  274. //$bDashboardMode = isset($aExtraParams['dashboard']) ? ($aExtraParams['dashboard'] == 'true') : false;
  275. //$bSelectMode = isset($aExtraParams['select']) ? ($aExtraParams['select'] == 'true') : false;
  276. if ( ($this->m_oSet->Count()> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) )
  277. {
  278. //$sLinkage = isset($aExtraParams['linkage']) ? $aExtraParams['linkage'] : '';
  279. $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $aExtraParams);
  280. }
  281. else
  282. {
  283. $sHtml .= $oPage->GetP("No object to display.");
  284. $sClass = $this->m_oFilter->GetClass();
  285. $bDisplayMenu = isset($this->m_aParams['menu']) ? $this->m_aParams['menu'] == true : true;
  286. if ($bDisplayMenu)
  287. {
  288. if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES)
  289. {
  290. $oAttDef = MetaModel::GetAttributeDef($sClass, $this->m_aParams['target_attr']);
  291. $sTargetClass = $oAttDef->GetTargetClass();
  292. $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");
  293. }
  294. }
  295. }
  296. break;
  297. case 'details':
  298. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES)
  299. {
  300. while($oObj = $this->m_oSet->Fetch())
  301. {
  302. $sHtml .= $oObj->GetDetails($oPage); // Still used ???
  303. }
  304. }
  305. break;
  306. case 'bare_details':
  307. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES)
  308. {
  309. while($oObj = $this->m_oSet->Fetch())
  310. {
  311. $sHtml .= $oObj->GetBareDetails($oPage);
  312. }
  313. }
  314. break;
  315. case 'csv':
  316. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES)
  317. {
  318. $sHtml .= "<textarea style=\"width:100%;height:98%\">\n";
  319. $sHtml .= cmdbAbstractObject::GetSetAsCSV($this->m_oSet);
  320. $sHtml .= "</textarea>\n";
  321. }
  322. break;
  323. case 'modify':
  324. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES)
  325. {
  326. while($oObj = $this->m_oSet->Fetch())
  327. {
  328. $sHtml .= $oObj->GetModifyForm($oPage);
  329. }
  330. }
  331. break;
  332. case 'search':
  333. $iSearchSectionId = 1;
  334. $sStyle = (isset($aExtraParams['open']) && ($aExtraParams['open'] == 'true')) ? 'SearchDrawer' : 'SearchDrawer DrawerClosed';
  335. $sHtml .= "<div id=\"Search_$iSearchSectionId\" class=\"$sStyle\">\n";
  336. $oPage->add_ready_script("\$(\"#LnkSearch_$iSearchSectionId\").click(function() {\$(\"#Search_$iSearchSectionId\").slideToggle('normal'); $(\"#LnkSearch_$iSearchSectionId\").toggleClass('open');});");
  337. $sHtml .= cmdbAbstractObject::GetSearchForm($oPage, $this->m_oSet, $aExtraParams);
  338. $sHtml .= "</div>\n";
  339. $sHtml .= "<div class=\"HRDrawer\"></div>\n";
  340. $sHtml .= "<div id=\"LnkSearch_$iSearchSectionId\" class=\"DrawerHandle\">Search</div>\n";
  341. break;
  342. case 'pie_chart':
  343. $sGroupBy = isset($aExtraParams['group_by']) ? $aExtraParams['group_by'] : '';
  344. $sFilter = $this->m_oFilter->ToOQL();
  345. $sHtml .= "
  346. <OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
  347. codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\"
  348. WIDTH=\"400\"
  349. HEIGHT=\"250\"
  350. id=\"charts\"
  351. ALIGN=\"\">
  352. <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))."\">
  353. <PARAM NAME=\"quality\" VALUE=\"high\">
  354. <PARAM NAME=\"bgcolor\" VALUE=\"#ffffff\">
  355. <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))."\"
  356. quality=\"high\"
  357. bgcolor=\"#ffffff\"
  358. WIDTH=\"400\"
  359. HEIGHT=\"250\"
  360. NAME=\"charts\"
  361. ALIGN=\"\"
  362. swLiveConnect=\"true\"
  363. TYPE=\"application/x-shockwave-flash\"
  364. PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">
  365. </EMBED>
  366. </OBJECT>
  367. ";
  368. break;
  369. case 'pie_chart_ajax':
  370. if (isset($aExtraParams['group_by']))
  371. {
  372. $sGroupByField = $aExtraParams['group_by'];
  373. $aGroupBy = array();
  374. while($oObj = $this->m_oSet->Fetch())
  375. {
  376. $sValue = $oObj->Get($sGroupByField);
  377. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  378. }
  379. $sFilter = urlencode($this->m_oFilter->serialize());
  380. $aData = array();
  381. $sHtml .= "<chart>\n";
  382. $sHtml .= "<chart_type>3d pie</chart_type>\n";
  383. $sHtml .= "<chart_data>\n";
  384. $sHtml .= "<row>\n";
  385. $sHtml .= "<null/>\n";
  386. foreach($aGroupBy as $sValue => $void)
  387. {
  388. $sHtml .= "<string>$sValue</string>\n";
  389. }
  390. $sHtml .= "</row>\n";
  391. $sHtml .= "<row>\n";
  392. $sHtml .= "<string></string>\n";
  393. foreach($aGroupBy as $void => $iCount)
  394. {
  395. $sHtml .= "<number>$iCount</number>\n";
  396. }
  397. $sHtml .= "</row>\n";
  398. $sHtml .= "</chart_data>\n";
  399. $sHtml .= "
  400. <chart_value color='ffffff' alpha='90' font='arial' bold='true' size='10' position='inside' prefix='' suffix='' decimals='0' separator='' as_percentage='true' />
  401. <draw>
  402. <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>
  403. </draw>
  404. <legend_label layout='horizontal' bullet='circle' font='arial' bold='true' size='13' color='000000' alpha='85' />
  405. <legend_rect fill_color='ffffff' fill_alpha='10' line_color='ffffff' line_alpha='50' line_thickness='0' />
  406. <series_color>
  407. <color>ddaa41</color>
  408. <color>88dd11</color>
  409. <color>4e62dd</color>
  410. <color>ff8811</color>
  411. <color>4d4d4d</color>
  412. <color>5a4b6e</color>
  413. <color>1188ff</color>
  414. </series_color>
  415. ";
  416. $sHtml .= "</chart>\n";
  417. }
  418. else
  419. {
  420. // Simply count the number of elements in the set
  421. $iCount = $oSet->Count();
  422. $sHtml .= "<chart>\n</chart>\n";
  423. }
  424. break;
  425. case 'open_flash_chart':
  426. static $iChartCounter = 0;
  427. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  428. $sTitle = isset($aExtraParams['chart_title']) ? $aExtraParams['chart_title'] : '';
  429. $sGroupBy = isset($aExtraParams['group_by']) ? $aExtraParams['group_by'] : '';
  430. $sFilter = $this->m_oFilter->ToOQL();
  431. $sHtml .= "<script>
  432. swfobject.embedSWF(\"../images/open-flash-chart.swf\", \"my_chart_{$iChartCounter}\", \"400\", \"400\",\"9.0.0\", \"expressInstall.swf\",
  433. {\"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))."\"});
  434. </script>\n";
  435. $sHtml .= "<div id=\"my_chart_{$iChartCounter}\">Here goes the chart</div>\n";
  436. $iChartCounter++;
  437. break;
  438. case 'open_flash_chart_ajax':
  439. include './php-ofc-library/open-flash-chart.php';
  440. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  441. $oChart = new open_flash_chart();
  442. switch($sChartType)
  443. {
  444. case 'bars':
  445. $oChartElement = new bar_glass();
  446. if (isset($aExtraParams['group_by']))
  447. {
  448. $sGroupByField = $aExtraParams['group_by'];
  449. $aGroupBy = array();
  450. while($oObj = $this->m_oSet->Fetch())
  451. {
  452. $sValue = $oObj->Get($sGroupByField);
  453. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  454. }
  455. $sFilter = urlencode($this->m_oFilter->serialize());
  456. $aData = array();
  457. $aLabels = array();
  458. foreach($aGroupBy as $sValue => $iValue)
  459. {
  460. $aData[] = $iValue;
  461. $aLabels[] = $sValue;
  462. }
  463. $maxValue = max($aData);
  464. $oYAxis = new y_axis();
  465. $aMagicValues = array(1,2,5,10);
  466. $iMultiplier = 1;
  467. $index = 0;
  468. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  469. while($maxValue > $iTop)
  470. {
  471. $index++;
  472. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  473. if (($index % count($aMagicValues)) == 0)
  474. {
  475. $iMultiplier = $iMultiplier * 10;
  476. }
  477. }
  478. //echo "oYAxis->set_range(0, $iTop, $iMultiplier);\n";
  479. $oYAxis->set_range(0, $iTop, $iMultiplier);
  480. $oChart->set_y_axis( $oYAxis );
  481. $oChartElement->set_values( $aData );
  482. $oXAxis = new x_axis();
  483. $oXLabels = new x_axis_labels();
  484. // set them vertical
  485. $oXLabels->set_vertical();
  486. // set the label text
  487. $oXLabels->set_labels($aLabels);
  488. // Add the X Axis Labels to the X Axis
  489. $oXAxis->set_labels( $oXLabels );
  490. $oChart->set_x_axis( $oXAxis );
  491. }
  492. break;
  493. case 'pie':
  494. default:
  495. $oChartElement = new pie();
  496. $oChartElement->set_start_angle( 35 );
  497. $oChartElement->set_animate( true );
  498. $oChartElement->set_tooltip( '#label# - #val# (#percent#)' );
  499. if (isset($aExtraParams['group_by']))
  500. {
  501. $sGroupByField = $aExtraParams['group_by'];
  502. $aGroupBy = array();
  503. while($oObj = $this->m_oSet->Fetch())
  504. {
  505. $sValue = $oObj->Get($sGroupByField);
  506. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  507. }
  508. $sFilter = urlencode($this->m_oFilter->serialize());
  509. $aData = array();
  510. foreach($aGroupBy as $sValue => $iValue)
  511. {
  512. $aData[] = new pie_value($iValue, $sValue);
  513. }
  514. $oChartElement->set_values( $aData );
  515. $oChart->x_axis = null;
  516. }
  517. }
  518. if (isset($aExtraParams['chart_title'])) //@@ BUG: not passed via ajax !!!
  519. {
  520. $oTitle = new title( $aExtraParams['chart_title'] );
  521. $oChart->set_title( $oTitle );
  522. }
  523. $oChart->set_bg_colour('#FFFFFF');
  524. $oChart->add_element( $oChartElement );
  525. $sHtml = $oChart->toPrettyString();
  526. break;
  527. default:
  528. // Unsupported style, do nothing.
  529. $sHtml .= "Error: unsupported style of block: ".$this->m_sStyle;
  530. }
  531. return $sHtml;
  532. }
  533. }
  534. /**
  535. * Helper class to manage 'blocks' of HTML pieces that are parts of a page and contain some list of cmdb objects
  536. *
  537. * Each block is actually rendered as a <div></div> tag that can be rendered synchronously
  538. * or as a piece of Javascript/JQuery/Ajax that will get its content from another page (ajax.render.php).
  539. * The list of cmdbObjects to be displayed into the block is defined by a filter
  540. * Right now the type of display is either: list, count or details
  541. * - list produces a table listing the objects
  542. * - count produces a paragraphs with a sentence saying 'cont' objects found
  543. * - details display (as table) the details of each object found (best if only one)
  544. */
  545. class HistoryBlock extends DisplayBlock
  546. {
  547. public function GetRenderContent(web_page $oPage, $aExtraParams = array())
  548. {
  549. $sHtml = '';
  550. // Add the extra params into the filter if they make sense for such a filter
  551. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  552. foreach($aFilterCodes as $sFilterCode)
  553. {
  554. if (isset($aExtraParams[$sFilterCode]))
  555. {
  556. $this->m_oFilter->AddCondition($sFilterCode, $aExtraParams[$sFilterCode]); // Use the default 'loose' operator
  557. }
  558. }
  559. $oSet = new CMDBObjectSet($this->m_oFilter, array('date'=>false));
  560. $sHtml .= "<!-- filter: ".($this->m_oFilter->ToOQL())."-->\n";
  561. switch($this->m_sStyle)
  562. {
  563. case 'toggle':
  564. // First the latest change that the user is allowed to see
  565. do
  566. {
  567. $oLatestChangeOp = $oSet->Fetch();
  568. }
  569. while(is_object($oLatestChangeOp) && ($oLatestChangeOp->GetDescription() == ''));
  570. if (is_object($oLatestChangeOp))
  571. {
  572. global $oContext; // User Context.. should be statis instead of global...
  573. // There is one change in the list... only when the object has been created !
  574. $sDate = $oLatestChangeOp->GetAsHTML('date');
  575. $oChange = $oContext->GetObject('CMDBChange', $oLatestChangeOp->Get('change'));
  576. $sUserInfo = $oChange->GetAsHTML('userinfo');
  577. $oSet->Rewind(); // Reset the pointer to the beginning of the set
  578. $sHtml .= $oPage->GetStartCollapsibleSection("Last modified on $sDate by $sUserInfo.");
  579. //$sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $oSet);
  580. $aChanges = array();
  581. while($oChangeOp = $oSet->Fetch())
  582. {
  583. $sChangeDescription = $oChangeOp->GetDescription();
  584. if ($sChangeDescription != '')
  585. {
  586. // The change is visible for the current user
  587. $changeId = $oChangeOp->Get('change');
  588. $aChanges[$changeId]['date'] = $oChangeOp->Get('date');
  589. $aChanges[$changeId]['userinfo'] = $oChangeOp->Get('userinfo');
  590. if (!isset($aChanges[$changeId]['log']))
  591. {
  592. $aChanges[$changeId]['log'] = array();
  593. }
  594. $aChanges[$changeId]['log'][] = $sChangeDescription;
  595. }
  596. }
  597. $aAttribs = array('date' => array('label' => 'Date', 'description' => 'Date of the change'),
  598. 'userinfo' => array('label' => 'User', 'description' => 'User who made the change'),
  599. 'log' => array('label' => 'Changes', 'description' => 'Changes made to the object'),
  600. );
  601. $aValues = array();
  602. foreach($aChanges as $aChange)
  603. {
  604. $aValues[] = array('date' => $aChange['date'], 'userinfo' => $aChange['userinfo'], 'log' => "<ul><li>".implode('</li><li>', $aChange['log'])."</li></ul>");
  605. }
  606. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  607. $sHtml .= $oPage->GetEndCollapsibleSection();
  608. }
  609. break;
  610. default:
  611. $sHtml .= parent::GetRenderContent($oPage, $aExtraParams);
  612. }
  613. return $sHtml;
  614. }
  615. }
  616. class MenuBlock extends DisplayBlock
  617. {
  618. public function GetRenderContent(web_page $oPage, $aExtraParams = array())
  619. {
  620. $sHtml = '';
  621. $oAppContext = new ApplicationContext();
  622. $sContext = $oAppContext->GetForLink();
  623. $sClass = $this->m_oFilter->GetClass();
  624. $oSet = new CMDBObjectSet($this->m_oFilter);
  625. $sFilter = $this->m_oFilter->serialize();
  626. $aActions = array();
  627. $sUIPage = cmdbAbstractObject::ComputeUIPage($sClass);
  628. switch($oSet->Count())
  629. {
  630. case 0:
  631. // No object in the set, the only possible action is "new"
  632. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  633. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New', 'url' => "../page/$sUIPage?operation=new&class=$sClass&$sContext"); }
  634. break;
  635. case 1:
  636. $oObj = $oSet->Fetch();
  637. $id = $oObj->GetKey();
  638. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  639. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  640. $bIsBulkModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  641. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  642. // Just one object in the set, possible actions are "new / clone / modify and delete"
  643. if (isset($aExtraParams['link_attr']))
  644. {
  645. $id = $aExtraParams['object_id'];
  646. $sTargetAttr = $aExtraParams['target_attr'];
  647. $oAttDef = MetaModel::GetAttributeDef($sClass, $sTargetAttr);
  648. $sTargetClass = $oAttDef->GetTargetClass();
  649. 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"); }
  650. 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"); }
  651. if ($bIsDeleteAllowed) { $aActions[] = array ('label' => 'Remove All', 'url' => "#"); }
  652. }
  653. else
  654. {
  655. $sUrl = self::GetAbsoluteUrl();
  656. $aActions[] = array ('label' => 'eMail', 'url' => "mailto:?subject=".$oSet->GetFilter()->__DescribeHTML()."&body=".urlencode("$sUrl?operation=search&filter=$sFilter&$sContext"));
  657. $aActions[] = array ('label' => 'CSV Export', 'url' => "../pages/$sUIPage?operation=search&filter=$sFilter&format=csv&$sContext");
  658. $aActions[] = array ('label' => 'Bookmark...', 'url' => "../pages/ajax.render.php?operation=create&class=$sClass&filter=$sFilter", 'class' => 'jqmTrigger');
  659. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New...', 'url' => "../pages/$sUIPage?operation=new&class=$sClass&$sContext"); }
  660. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'Clone...', 'url' => "../pages/$sUIPage?operation=clone&class=$sClass&id=$id&$sContext"); }
  661. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'Modify...', 'url' => "../pages/$sUIPage?operation=modify&class=$sClass&id=$id&$sContext"); }
  662. if ($bIsDeleteAllowed) { $aActions[] = array ('label' => 'Delete', 'url' => "../pages/$sUIPage?operation=delete&class=$sClass&id=$id&$sContext"); }
  663. }
  664. $aTransitions = $oObj->EnumTransitions();
  665. $aStimuli = Metamodel::EnumStimuli($sClass);
  666. foreach($aTransitions as $sStimulusCode => $aTransitionDef)
  667. {
  668. $iActionAllowed = UserRights::IsStimulusAllowed($sClass, $sStimulusCode, $oSet);
  669. switch($iActionAllowed)
  670. {
  671. case UR_ALLOWED_YES:
  672. $aActions[] = array('label' => $aStimuli[$sStimulusCode]->Get('label'), 'url' => "../pages/UI.php?operation=stimulus&stimulus=$sStimulusCode&class=$sClass&id=$id&$sContext");
  673. break;
  674. case UR_ALLOWED_DEPENDS:
  675. $aActions[] = array('label' => $aStimuli[$sStimulusCode]->Get('label').' (*)', 'url' => "../pages/UI.php?operation=stimulus&stimulus=$sStimulusCode&class=$sClass&id=$id&$sContext");
  676. break;
  677. default:
  678. // Do nothing
  679. }
  680. }
  681. //print_r($aTransitions);
  682. break;
  683. default:
  684. // Check rights
  685. // New / Modify
  686. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  687. $bIsBulkModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  688. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  689. if (isset($aExtraParams['link_attr']))
  690. {
  691. $id = $aExtraParams['object_id'];
  692. $sTargetAttr = $aExtraParams['target_attr'];
  693. $oAttDef = MetaModel::GetAttributeDef($sClass, $sTargetAttr);
  694. $sTargetClass = $oAttDef->GetTargetClass();
  695. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  696. 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"); }
  697. //if ($bIsBulkModifyAllowed) { $aActions[] = array ('label' => 'Add...', 'url' => "../pages/$sUIPage?operation=modify_links&class=$sClass&linkage=".$aExtraParams['linkage']."&id=$id&addObjects=true&$sContext"); }
  698. 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"); }
  699. if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => 'Remove All...', 'url' => "#"); }
  700. }
  701. else
  702. {
  703. // many objects in the set, possible actions are: new / modify all / delete all
  704. $sUrl = self::GetAbsoluteUrl();
  705. $aActions[] = array ('label' => 'eMail', 'url' => "mailto:?subject=".$oSet->GetFilter()->__DescribeHTML()."&body=".urlencode("$sUrl?operation=search&filter=$sFilter&$sContext"));
  706. $aActions[] = array ('label' => 'CSV Export', 'url' => "../pages/$sUIPage?operation=search&filter=$sFilter&format=csv&$sContext");
  707. $aActions[] = array ('label' => 'Bookmark...', 'url' => "../pages/ajax.render.php?operation=create&class=$sClass&filter=$sFilter", 'class' => 'jqmTrigger');
  708. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New...', 'url' => "../pages/$sUIPage?operation=new&class=$sClass&$sContext"); }
  709. if ($bIsBulkModifyAllowed) { $aActions[] = array ('label' => 'Modify All...', 'url' => "../pages/$sUIPage?operation=modify_all&filter=$sFilter&$sContext"); }
  710. if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => 'Delete All', 'url' => "../pages/$sUIPage?operation=delete_all&filter=$sFilter&$sContext"); }
  711. }
  712. }
  713. $sHtml .= "<div class=\"jd_menu_itop\"><ul class=\"jd_menu jd_menu_itop\">\n<li>Actions\n<ul>\n";
  714. foreach ($aActions as $aAction)
  715. {
  716. $sClass = isset($aAction['class']) ? " class=\"{$aAction['class']}\"" : "";
  717. $sHtml .= "<li><a href=\"{$aAction['url']}\"$sClass>{$aAction['label']}</a></li>\n<li>\n";
  718. }
  719. $sHtml .= "</ul>\n</li>\n</ul></div>\n";
  720. $oPage->add_ready_script("$(\"ul.jd_menu\").jdMenu();\n");
  721. return $sHtml;
  722. }
  723. static public function GetAbsoluteUrl()
  724. {
  725. // Build an absolute URL to this page on this server/port
  726. $sServerName = $_SERVER['SERVER_NAME'];
  727. $sProtocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
  728. if ($sProtocol == 'http')
  729. {
  730. $sPort = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT'];
  731. }
  732. else
  733. {
  734. $sPort = ($_SERVER['SERVER_PORT'] == 443) ? '' : ':'.$_SERVER['SERVER_PORT'];
  735. }
  736. // $_SERVER['REQUEST_URI'] is empty when running on IIS
  737. // Let's use Ivan Tcholakov's fix (found on www.dokeos.com)
  738. if (!empty($_SERVER['REQUEST_URI']))
  739. {
  740. $sPath = $_SERVER['REQUEST_URI'];
  741. }
  742. else
  743. {
  744. $sPath = $_SERVER['SCRIPT_NAME'];
  745. if (!empty($_SERVER['QUERY_STRING']))
  746. {
  747. $sPath .= '?'.$_SERVER['QUERY_STRING'];
  748. }
  749. $_SERVER['REQUEST_URI'] = $sPath;
  750. }
  751. $sPath = $_SERVER['REQUEST_URI'];
  752. $sUrl = "$sProtocol://{$sServerName}{$sPort}{$sPath}";
  753. return $sUrl;
  754. }
  755. }
  756. ?>