displayblock.class.inc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. if (!$bDashboardMode)
  241. {
  242. $sHtml .= $oPage->GetP($this->m_oSet->Count()." object(s).");
  243. }
  244. $sLinkage = isset($aExtraParams['linkage']) ? $aExtraParams['linkage'] : '';
  245. $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $sLinkage, !$bDashboardMode /* bDisplayMenu */);
  246. }
  247. else
  248. {
  249. $sHtml .= $oPage->GetP("No object to display.");
  250. $sClass = $this->m_oFilter->GetClass();
  251. if (!$bDashboardMode)
  252. {
  253. if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES)
  254. {
  255. $sHtml .= $oPage->GetP("<a href=\"./UI.php?operation=new&class=$sClass\">Click here to create a new ".Metamodel::GetName($sClass)."</a>\n");
  256. }
  257. }
  258. }
  259. break;
  260. case 'details':
  261. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES)
  262. {
  263. while($oObj = $this->m_oSet->Fetch())
  264. {
  265. $sHtml .= $oObj->GetDetails($oPage);
  266. }
  267. }
  268. break;
  269. case 'bare_details':
  270. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES)
  271. {
  272. while($oObj = $this->m_oSet->Fetch())
  273. {
  274. $sHtml .= $oObj->GetBareDetails($oPage);
  275. }
  276. }
  277. break;
  278. case 'csv':
  279. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES)
  280. {
  281. $sHtml .= "<textarea style=\"width:100%;height:98%\">\n";
  282. $sHtml .= cmdbAbstractObject::GetSetAsCSV($this->m_oSet);
  283. $sHtml .= "</textarea>\n";
  284. }
  285. break;
  286. case 'modify':
  287. if (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES)
  288. {
  289. while($oObj = $this->m_oSet->Fetch())
  290. {
  291. $sHtml .= $oObj->GetModifyForm($oPage);
  292. }
  293. }
  294. break;
  295. case 'search':
  296. $iSearchSectionId = 1;
  297. $sStyle = (isset($aExtraParams['open']) && ($aExtraParams['open'] == 'true')) ? 'SearchDrawer' : 'SearchDrawer DrawerClosed';
  298. $sHtml .= "<div id=\"Search_$iSearchSectionId\" class=\"$sStyle\">\n";
  299. $sHtml .= "<h1>Search form for ".Metamodel::GetName($this->m_oSet->GetClass())."</h1>\n";
  300. $oPage->add_ready_script("\$(\"#LnkSearch_$iSearchSectionId\").click(function() {\$(\"#Search_$iSearchSectionId\").slideToggle('normal'); $(\"#LnkSearch_$iSearchSectionId\").toggleClass('open');});");
  301. $sHtml .= cmdbAbstractObject::GetSearchForm($oPage, $this->m_oSet, $aExtraParams);
  302. $sHtml .= "</div>\n";
  303. $sHtml .= "<div class=\"HRDrawer\"/></div>\n";
  304. $sHtml .= "<div id=\"LnkSearch_$iSearchSectionId\" class=\"DrawerHandle\">Search</div>\n";
  305. break;
  306. case 'pie_chart':
  307. $sGroupBy = isset($aExtraParams['group_by']) ? $aExtraParams['group_by'] : '';
  308. $sFilter = $this->m_oFilter->ToOQL();
  309. $sHtml .= "
  310. <OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
  311. codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\"
  312. WIDTH=\"400\"
  313. HEIGHT=\"250\"
  314. id=\"charts\"
  315. ALIGN=\"\">
  316. <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))."\">
  317. <PARAM NAME=\"quality\" VALUE=\"high\">
  318. <PARAM NAME=\"bgcolor\" VALUE=\"#ffffff\">
  319. <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))."\"
  320. quality=\"high\"
  321. bgcolor=\"#ffffff\"
  322. WIDTH=\"400\"
  323. HEIGHT=\"250\"
  324. NAME=\"charts\"
  325. ALIGN=\"\"
  326. swLiveConnect=\"true\"
  327. TYPE=\"application/x-shockwave-flash\"
  328. PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\">
  329. </EMBED>
  330. </OBJECT>
  331. ";
  332. break;
  333. case 'pie_chart_ajax':
  334. if (isset($aExtraParams['group_by']))
  335. {
  336. $sGroupByField = $aExtraParams['group_by'];
  337. $aGroupBy = array();
  338. while($oObj = $this->m_oSet->Fetch())
  339. {
  340. $sValue = $oObj->Get($sGroupByField);
  341. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  342. }
  343. $sFilter = urlencode($this->m_oFilter->serialize());
  344. $aData = array();
  345. $sHtml .= "<chart>\n";
  346. $sHtml .= "<chart_type>3d pie</chart_type>\n";
  347. $sHtml .= "<chart_data>\n";
  348. $sHtml .= "<row>\n";
  349. $sHtml .= "<null/>\n";
  350. foreach($aGroupBy as $sValue => $void)
  351. {
  352. $sHtml .= "<string>$sValue</string>\n";
  353. }
  354. $sHtml .= "</row>\n";
  355. $sHtml .= "<row>\n";
  356. $sHtml .= "<string></string>\n";
  357. foreach($aGroupBy as $void => $iCount)
  358. {
  359. $sHtml .= "<number>$iCount</number>\n";
  360. }
  361. $sHtml .= "</row>\n";
  362. $sHtml .= "</chart_data>\n";
  363. $sHtml .= "
  364. <chart_value color='ffffff' alpha='90' font='arial' bold='true' size='10' position='inside' prefix='' suffix='' decimals='0' separator='' as_percentage='true' />
  365. <draw>
  366. <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>
  367. </draw>
  368. <legend_label layout='horizontal' bullet='circle' font='arial' bold='true' size='13' color='000000' alpha='85' />
  369. <legend_rect fill_color='ffffff' fill_alpha='10' line_color='ffffff' line_alpha='50' line_thickness='0' />
  370. <series_color>
  371. <color>ddaa41</color>
  372. <color>88dd11</color>
  373. <color>4e62dd</color>
  374. <color>ff8811</color>
  375. <color>4d4d4d</color>
  376. <color>5a4b6e</color>
  377. <color>1188ff</color>
  378. </series_color>
  379. ";
  380. $sHtml .= "</chart>\n";
  381. }
  382. else
  383. {
  384. // Simply count the number of elements in the set
  385. $iCount = $oSet->Count();
  386. $sHtml .= "<chart>\n</chart>\n";
  387. }
  388. break;
  389. case 'open_flash_chart':
  390. static $iChartCounter = 0;
  391. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  392. $sTitle = isset($aExtraParams['chart_title']) ? $aExtraParams['chart_title'] : '';
  393. $sGroupBy = isset($aExtraParams['group_by']) ? $aExtraParams['group_by'] : '';
  394. $sFilter = $this->m_oFilter->ToOQL();
  395. $sHtml .= "<script>
  396. swfobject.embedSWF(\"../images/open-flash-chart.swf\", \"my_chart_{$iChartCounter}\", \"400\", \"400\",\"9.0.0\", \"expressInstall.swf\",
  397. {\"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))."\"});
  398. </script>\n";
  399. $sHtml .= "<div id=\"my_chart_{$iChartCounter}\">Here goes the chart</div>\n";
  400. $iChartCounter++;
  401. break;
  402. case 'open_flash_chart_ajax':
  403. include './php-ofc-library/open-flash-chart.php';
  404. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  405. $oChart = new open_flash_chart();
  406. switch($sChartType)
  407. {
  408. case 'bars':
  409. $oChartElement = new bar_glass();
  410. if (isset($aExtraParams['group_by']))
  411. {
  412. $sGroupByField = $aExtraParams['group_by'];
  413. $aGroupBy = array();
  414. while($oObj = $this->m_oSet->Fetch())
  415. {
  416. $sValue = $oObj->Get($sGroupByField);
  417. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  418. }
  419. $sFilter = urlencode($this->m_oFilter->serialize());
  420. $aData = array();
  421. $aLabels = array();
  422. foreach($aGroupBy as $sValue => $iValue)
  423. {
  424. $aData[] = $iValue;
  425. $aLabels[] = $sValue;
  426. }
  427. $maxValue = max($aData);
  428. $oYAxis = new y_axis();
  429. $aMagicValues = array(1,2,5,10);
  430. $iMultiplier = 1;
  431. $index = 0;
  432. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  433. while($maxValue > $iTop)
  434. {
  435. $index++;
  436. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  437. if (($index % count($aMagicValues)) == 0)
  438. {
  439. $iMultiplier = $iMultiplier * 10;
  440. }
  441. }
  442. //echo "oYAxis->set_range(0, $iTop, $iMultiplier);\n";
  443. $oYAxis->set_range(0, $iTop, $iMultiplier);
  444. $oChart->set_y_axis( $oYAxis );
  445. $oChartElement->set_values( $aData );
  446. $oXAxis = new x_axis();
  447. $oXLabels = new x_axis_labels();
  448. // set them vertical
  449. $oXLabels->set_vertical();
  450. // set the label text
  451. $oXLabels->set_labels($aLabels);
  452. // Add the X Axis Labels to the X Axis
  453. $oXAxis->set_labels( $oXLabels );
  454. $oChart->set_x_axis( $oXAxis );
  455. }
  456. break;
  457. case 'pie':
  458. default:
  459. $oChartElement = new pie();
  460. $oChartElement->set_start_angle( 35 );
  461. $oChartElement->set_animate( true );
  462. $oChartElement->set_tooltip( '#label# - #val# (#percent#)' );
  463. if (isset($aExtraParams['group_by']))
  464. {
  465. $sGroupByField = $aExtraParams['group_by'];
  466. $aGroupBy = array();
  467. while($oObj = $this->m_oSet->Fetch())
  468. {
  469. $sValue = $oObj->Get($sGroupByField);
  470. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  471. }
  472. $sFilter = urlencode($this->m_oFilter->serialize());
  473. $aData = array();
  474. foreach($aGroupBy as $sValue => $iValue)
  475. {
  476. $aData[] = new pie_value($iValue, $sValue);
  477. }
  478. $oChartElement->set_values( $aData );
  479. $oChart->x_axis = null;
  480. }
  481. }
  482. if (isset($aExtraParams['chart_title'])) //@@ BUG: not passed via ajax !!!
  483. {
  484. $oTitle = new title( $aExtraParams['chart_title'] );
  485. $oChart->set_title( $oTitle );
  486. }
  487. $oChart->set_bg_colour('#FFFFFF');
  488. $oChart->add_element( $oChartElement );
  489. $sHtml = $oChart->toPrettyString();
  490. break;
  491. default:
  492. // Unsupported style, do nothing.
  493. $sHtml .= "Error: unsupported style of block: ".$this->m_sStyle;
  494. }
  495. return $sHtml;
  496. }
  497. }
  498. /**
  499. * Helper class to manage 'blocks' of HTML pieces that are parts of a page and contain some list of cmdb objects
  500. *
  501. * Each block is actually rendered as a <div></div> tag that can be rendered synchronously
  502. * or as a piece of Javascript/JQuery/Ajax that will get its content from another page (ajax.render.php).
  503. * The list of cmdbObjects to be displayed into the block is defined by a filter
  504. * Right now the type of display is either: list, count or details
  505. * - list produces a table listing the objects
  506. * - count produces a paragraphs with a sentence saying 'cont' objects found
  507. * - details display (as table) the details of each object found (best if only one)
  508. */
  509. class HistoryBlock extends DisplayBlock
  510. {
  511. public function GetRenderContent(web_page $oPage, $aExtraParams = array())
  512. {
  513. $sHtml = '';
  514. // Add the extra params into the filter if they make sense for such a filter
  515. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  516. foreach($aFilterCodes as $sFilterCode)
  517. {
  518. if (isset($aExtraParams[$sFilterCode]))
  519. {
  520. $this->m_oFilter->AddCondition($sFilterCode, $aExtraParams[$sFilterCode]); // Use the default 'loose' operator
  521. }
  522. }
  523. $oSet = new CMDBObjectSet($this->m_oFilter, array('date'=>false));
  524. $sHtml .= "<!-- filter: ".($this->m_oFilter->ToOQL())."-->\n";
  525. switch($this->m_sStyle)
  526. {
  527. case 'toggle':
  528. $oLatestChangeOp = $oSet->Fetch();
  529. if (is_object($oLatestChangeOp))
  530. {
  531. global $oContext; // User Context.. should be statis instead of global...
  532. // There is one change in the list... only when the object has been created !
  533. $sDate = $oLatestChangeOp->GetAsHTML('date');
  534. $oChange = $oContext->GetObject('CMDBChange', $oLatestChangeOp->Get('change'));
  535. $sUserInfo = $oChange->GetAsHTML('userinfo');
  536. $oSet->Load(); // Reset the pointer to the beginning of the set: there should be a better way to do this...
  537. $sHtml .= $oPage->GetStartCollapsibleSection("Last modified on $sDate by $sUserInfo.");
  538. $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $oSet);
  539. $sHtml .= $oPage->GetEndCollapsibleSection();
  540. }
  541. break;
  542. default:
  543. $sHtml .= parent::GetRenderContent($oPage, $aExtraParams);
  544. }
  545. return $sHtml;
  546. }
  547. }
  548. class MenuBlock extends DisplayBlock
  549. {
  550. public function GetRenderContent(web_page $oPage, $aExtraParams = array())
  551. {
  552. $sHtml = '';
  553. $oAppContext = new ApplicationContext();
  554. $sContext = $oAppContext->GetForLink();
  555. $sClass = $this->m_oFilter->GetClass();
  556. $oSet = new CMDBObjectSet($this->m_oFilter);
  557. $sFilter = $this->m_oFilter->serialize();
  558. $aActions = array();
  559. $sUIPage = cmdbAbstractObject::ComputeUIPage($sClass);
  560. switch($oSet->Count())
  561. {
  562. case 0:
  563. // No object in the set, the only possible action is "new"
  564. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  565. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New', 'url' => "../page/$sUIPage?operation=new&class=$sClass&$sContext"); }
  566. break;
  567. case 1:
  568. $oObj = $oSet->Fetch();
  569. $id = $oObj->GetKey();
  570. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  571. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  572. $bIsBulkModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  573. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  574. // Just one object in the set, possible actions are "new / clone / modify and delete"
  575. if (isset($aExtraParams['linkage']))
  576. {
  577. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New...', 'url' => "#"); }
  578. if ($bIsBulkModifyAllowed) { $aActions[] = array ('label' => 'Modify All...', 'url' => "#"); }
  579. if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => 'Remove All', 'url' => "#"); }
  580. if ($bIsModifyAllowed | $bIsDeleteAllowed) { $aActions[] = array ('label' => 'Manage Links...', 'url' => "#"); }
  581. }
  582. else
  583. {
  584. $aActions[] = array ('label' => 'eMail', 'url' => "mailto:?subject=".$oSet->GetFilter()->__DescribeHTML()."&body=".urlencode("http://localhost:81/pages/UI.php?operation=search&filter=$sFilter&$sContext"));
  585. $aActions[] = array ('label' => 'CSV Export', 'url' => "../pages/$sUIPage?operation=search&filter=$sFilter&format=csv&$sContext");
  586. $aActions[] = array ('label' => 'Bookmark...', 'url' => "../pages/ajax.render.php?operation=create&class=$sClass&filter=$sFilter", 'class' => 'jqmTrigger');
  587. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New...', 'url' => "../pages/$sUIPage?operation=new&class=$sClass&$sContext"); }
  588. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'Clone...', 'url' => "../pages/$sUIPage?operation=clone&class=$sClass&id=$id&$sContext"); }
  589. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'Modify...', 'url' => "../pages/$sUIPage?operation=modify&class=$sClass&id=$id&$sContext"); }
  590. if ($bIsDeleteAllowed) { $aActions[] = array ('label' => 'Delete', 'url' => "../pages/$sUIPage?operation=delete&class=$sClass&id=$id&$sContext"); }
  591. }
  592. $aTransitions = $oObj->EnumTransitions();
  593. $aStimuli = Metamodel::EnumStimuli($sClass);
  594. foreach($aTransitions as $sStimulusCode => $aTransitionDef)
  595. {
  596. $iActionAllowed = UserRights::IsStimulusAllowed($sClass, $sStimulusCode, $oSet);
  597. switch($iActionAllowed)
  598. {
  599. case UR_ALLOWED_YES:
  600. $aActions[] = array('label' => $aStimuli[$sStimulusCode]->Get('label'), 'url' => "../pages/UI.php?operation=stimulus&stimulus=$sStimulusCode&class=$sClass&id=$id&$sContext");
  601. break;
  602. case UR_ALLOWED_DEPENDS:
  603. $aActions[] = array('label' => $aStimuli[$sStimulusCode]->Get('label').' (*)', 'url' => "../pages/UI.php?operation=stimulus&stimulus=$sStimulusCode&class=$sClass&id=$id&$sContext");
  604. break;
  605. default:
  606. // Do nothing
  607. }
  608. }
  609. //print_r($aTransitions);
  610. break;
  611. default:
  612. // Check rights
  613. // New / Modify
  614. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  615. $bIsBulkModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  616. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  617. if (isset($aExtraParams['linkage']))
  618. {
  619. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  620. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New...', 'url' => "#"); }
  621. if ($bIsBulkModifyAllowed) { $aActions[] = array ('label' => 'Modify All...', 'url' => "#"); }
  622. if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => 'Remove All', 'url' => "#"); }
  623. if ($bIsModifyAllowed | $bIsDeleteAllowed) { $aActions[] = array ('label' => 'Manage Links...', 'url' => "#"); }
  624. }
  625. else
  626. {
  627. // many objects in the set, possible actions are: new / modify all / delete all
  628. $aActions[] = array ('label' => 'eMail', 'url' => "mailto:?subject=".$oSet->GetFilter()->__DescribeHTML()."&body=".urlencode("http://localhost:81/pages/UI.php?operation=search&filter=$sFilter&$sContext"));
  629. $aActions[] = array ('label' => 'CSV Export', 'url' => "../pages/$sUIPage?operation=search&filter=$sFilter&format=csv&$sContext");
  630. $aActions[] = array ('label' => 'Bookmark...', 'url' => "../pages/ajax.render.php?operation=create&class=$sClass&filter=$sFilter", 'class' => 'jqmTrigger');
  631. if ($bIsModifyAllowed) { $aActions[] = array ('label' => 'New...', 'url' => "../pages/$sUIPage?operation=new&class=$sClass&$sContext"); }
  632. if ($bIsBulkModifyAllowed) { $aActions[] = array ('label' => 'Modify All...', 'url' => "../pages/$sUIPage?operation=modify_all&filter=$sFilter&$sContext"); }
  633. if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => 'Delete All', 'url' => "../pages/$sUIPage?operation=delete_all&filter=$sFilter&$sContext"); }
  634. }
  635. }
  636. $sHtml .= "<div class=\"jd_menu_itop\"><ul class=\"jd_menu jd_menu_itop\">\n<li>Actions\n<ul>\n";
  637. foreach ($aActions as $aAction)
  638. {
  639. $sClass = isset($aAction['class']) ? " class=\"{$aAction['class']}\"" : "";
  640. $sHtml .= "<li><a href=\"{$aAction['url']}\"$sClass>{$aAction['label']}</a></li>\n<li>\n";
  641. }
  642. $sHtml .= "</ul>\n</li>\n</ul></div>\n";
  643. $oPage->add_ready_script("$(\"ul.jd_menu\").jdMenu();\n");
  644. return $sHtml;
  645. }
  646. }
  647. ?>