displayblock.class.inc.php 27 KB

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