displayblock.class.inc.php 29 KB

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