displayblock.class.inc.php 32 KB

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