displayblock.class.inc.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * DisplayBlock and derived class
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. require_once(APPROOT.'/application/webpage.class.inc.php');
  25. require_once(APPROOT.'/application/utils.inc.php');
  26. /**
  27. * Helper class to manage 'blocks' of HTML pieces that are parts of a page and contain some list of cmdb objects
  28. *
  29. * Each block is actually rendered as a <div></div> tag that can be rendered synchronously
  30. * or as a piece of Javascript/JQuery/Ajax that will get its content from another page (ajax.render.php).
  31. * The list of cmdbObjects to be displayed into the block is defined by a filter
  32. * Right now the type of display is either: list, count, bare_details, details, csv, modify or search
  33. * - list produces a table listing the objects
  34. * - count produces a paragraphs with a sentence saying 'cont' objects found
  35. * - bare_details displays just the details of the attributes of the object (best if only one)
  36. * - details display the full details of each object found using its template (best if only one)
  37. * - csv displays a textarea with the CSV export of the list of objects
  38. * - modify displays the form to modify an object (best if only one)
  39. * - search displays a search form with the criteria of the filter set
  40. */
  41. class DisplayBlock
  42. {
  43. const TAG_BLOCK = 'itopblock';
  44. protected $m_oFilter;
  45. protected $m_sStyle;
  46. protected $m_bAsynchronous;
  47. protected $m_aParams;
  48. protected $m_oSet;
  49. public function __construct(DBObjectSearch $oFilter, $sStyle = 'list', $bAsynchronous = false, $aParams = array(), $oSet = null)
  50. {
  51. $this->m_oFilter = $oFilter;
  52. $this->m_sStyle = $sStyle;
  53. $this->m_bAsynchronous = $bAsynchronous;
  54. $this->m_aParams = $aParams;
  55. $this->m_oSet = $oSet;
  56. }
  57. /**
  58. * Constructs a DisplayBlock object from a DBObjectSet already in memory
  59. * @param $oSet DBObjectSet
  60. * @return DisplayBlock The DisplayBlock object, or null if the creation failed
  61. */
  62. public static function FromObjectSet(DBObjectSet $oSet, $sStyle, $aParams = array())
  63. {
  64. $oDummyFilter = new DBObjectSearch($oSet->GetClass());
  65. $aKeys = array();
  66. while($oObject = $oSet->Fetch())
  67. {
  68. $aKeys[] = $oObject->GetKey();
  69. }
  70. $oSet->Rewind();
  71. $oDummyFilter->AddCondition('id', $aKeys, 'IN');
  72. $oBlock = new DisplayBlock($oDummyFilter, $sStyle, false, $aParams); // DisplayBlocks built this way are synchronous
  73. return $oBlock;
  74. }
  75. /**
  76. * Constructs a DisplayBlock object from an XML template
  77. * @param $sTemplate string The XML template
  78. * @return DisplayBlock The DisplayBlock object, or null if the template is invalid
  79. */
  80. public static function FromTemplate($sTemplate)
  81. {
  82. $iStartPos = stripos($sTemplate, '<'.self::TAG_BLOCK.' ',0);
  83. $iEndPos = stripos($sTemplate, '</'.self::TAG_BLOCK.'>', $iStartPos);
  84. $iEndTag = stripos($sTemplate, '>', $iStartPos);
  85. $aParams = array();
  86. if (($iStartPos === false) || ($iEndPos === false)) return null; // invalid template
  87. $sITopBlock = substr($sTemplate,$iStartPos, $iEndPos-$iStartPos+strlen('</'.self::TAG_BLOCK.'>'));
  88. $sITopData = substr($sTemplate, 1+$iEndTag, $iEndPos - $iEndTag - 1);
  89. $sITopTag = substr($sTemplate, $iStartPos + strlen('<'.self::TAG_BLOCK), $iEndTag - $iStartPos - strlen('<'.self::TAG_BLOCK));
  90. $aMatches = array();
  91. $sBlockClass = "DisplayBlock";
  92. $bAsynchronous = false;
  93. $sBlockType = 'list';
  94. $sEncoding = 'text/serialize';
  95. if (preg_match('/ type="(.*)"/U',$sITopTag, $aMatches))
  96. {
  97. $sBlockType = strtolower($aMatches[1]);
  98. }
  99. if (preg_match('/ asynchronous="(.*)"/U',$sITopTag, $aMatches))
  100. {
  101. $bAsynchronous = (strtolower($aMatches[1]) == 'true');
  102. }
  103. if (preg_match('/ blockclass="(.*)"/U',$sITopTag, $aMatches))
  104. {
  105. $sBlockClass = $aMatches[1];
  106. }
  107. if (preg_match('/ objectclass="(.*)"/U',$sITopTag, $aMatches))
  108. {
  109. $sObjectClass = $aMatches[1];
  110. }
  111. if (preg_match('/ encoding="(.*)"/U',$sITopTag, $aMatches))
  112. {
  113. $sEncoding = strtolower($aMatches[1]);
  114. }
  115. if (preg_match('/ link_attr="(.*)"/U',$sITopTag, $aMatches))
  116. {
  117. // The list to display is a list of links to the specified object
  118. $aParams['link_attr'] = $aMatches[1]; // Name of the Ext. Key that makes this linkage
  119. }
  120. if (preg_match('/ target_attr="(.*)"/U',$sITopTag, $aMatches))
  121. {
  122. // The list to display is a list of links to the specified object
  123. $aParams['target_attr'] = $aMatches[1]; // Name of the Ext. Key that make this linkage
  124. }
  125. if (preg_match('/ object_id="(.*)"/U',$sITopTag, $aMatches))
  126. {
  127. // The list to display is a list of links to the specified object
  128. $aParams['object_id'] = $aMatches[1]; // Id of the object to be linked to
  129. }
  130. // Parameters contains a list of extra parameters for the block
  131. // the syntax is param_name1:value1;param_name2:value2;...
  132. if (preg_match('/ parameters="(.*)"/U',$sITopTag, $aMatches))
  133. {
  134. $sParameters = $aMatches[1];
  135. $aPairs = explode(';', $sParameters);
  136. foreach($aPairs as $sPair)
  137. {
  138. if (preg_match('/(.*)\:(.*)/',$sPair, $aMatches))
  139. {
  140. $aParams[trim($aMatches[1])] = trim($aMatches[2]);
  141. }
  142. }
  143. }
  144. if (!empty($aParams['link_attr']))
  145. {
  146. // Check that all mandatory parameters are present:
  147. if(empty($aParams['object_id']))
  148. {
  149. // if 'links' mode is requested the d of the object to link to must be specified
  150. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_object_id'));
  151. }
  152. if(empty($aParams['target_attr']))
  153. {
  154. // if 'links' mode is requested the id of the object to link to must be specified
  155. throw new ApplicationException(Dict::S('UI:Error:MandatoryTemplateParameter_target_attr'));
  156. }
  157. }
  158. switch($sEncoding)
  159. {
  160. case 'text/serialize':
  161. $oFilter = CMDBSearchFilter::unserialize($sITopData);
  162. break;
  163. case 'text/oql':
  164. $oFilter = CMDBSearchFilter::FromOQL($sITopData);
  165. break;
  166. }
  167. return new $sBlockClass($oFilter, $sBlockType, $bAsynchronous, $aParams);
  168. }
  169. public function Display(WebPage $oPage, $sId, $aExtraParams = array())
  170. {
  171. $oPage->add($this->GetDisplay($oPage, $sId, $aExtraParams));
  172. }
  173. public function GetDisplay(WebPage $oPage, $sId, $aExtraParams = array())
  174. {
  175. $sHtml = '';
  176. $aExtraParams = array_merge($aExtraParams, $this->m_aParams);
  177. $aExtraParams['currentId'] = $sId;
  178. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  179. $bAutoReload = false;
  180. if (isset($aExtraParams['auto_reload']))
  181. {
  182. switch($aExtraParams['auto_reload'])
  183. {
  184. case 'fast':
  185. $bAutoReload = true;
  186. $iReloadInterval = MetaModel::GetConfig()->GetFastReloadInterval()*1000;
  187. break;
  188. case 'standard':
  189. case 'true':
  190. case true:
  191. $bAutoReload = true;
  192. $iReloadInterval = MetaModel::GetConfig()->GetStandardReloadInterval()*1000;
  193. break;
  194. default:
  195. if (is_numeric($aExtraParams['auto_reload']))
  196. {
  197. $bAutoReload = true;
  198. $iReloadInterval = $aExtraParams['auto_reload']*1000;
  199. }
  200. else
  201. {
  202. // incorrect config, ignore it
  203. $bAutoReload = false;
  204. }
  205. }
  206. }
  207. $sFilter = $this->m_oFilter->serialize(); // Used either for asynchronous or auto_reload
  208. if (!$this->m_bAsynchronous)
  209. {
  210. // render now
  211. $sHtml .= "<div id=\"$sId\" class=\"display_block\">\n";
  212. $sHtml .= $this->GetRenderContent($oPage, $aExtraParams, $sId);
  213. $sHtml .= "</div>\n";
  214. }
  215. else
  216. {
  217. // render it as an Ajax (asynchronous) call
  218. $sHtml .= "<div id=\"$sId\" class=\"display_block loading\">\n";
  219. $sHtml .= $oPage->GetP("<img src=\"../images/indicator_arrows.gif\"> ".Dict::S('UI:Loading'));
  220. $sHtml .= "</div>\n";
  221. $oPage->add_script('
  222. $.post("ajax.render.php?style='.$this->m_sStyle.'",
  223. { operation: "ajax", filter: "'.$sFilter.'", extra_params: "'.$sExtraParams.'" },
  224. function(data){
  225. $("#'.$sId.'").empty();
  226. $("#'.$sId.'").append(data);
  227. $("#'.$sId.'").removeClass("loading");
  228. }
  229. );
  230. ');
  231. }
  232. if ($bAutoReload)
  233. {
  234. $oPage->add_script('setInterval("ReloadBlock(\''.$sId.'\', \''.$this->m_sStyle.'\', \''.$sFilter.'\', \"'.$sExtraParams.'\")", '.$iReloadInterval.');');
  235. }
  236. return $sHtml;
  237. }
  238. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  239. {
  240. if (empty($aExtraParams['currentId']))
  241. {
  242. $sId = $oPage->GetUniqueId(); // Works only if the page is not an Ajax one !
  243. }
  244. else
  245. {
  246. $sId = $aExtraParams['currentId'];
  247. }
  248. $oPage->add($this->GetRenderContent($oPage, $aExtraParams, $sId));
  249. }
  250. public function GetRenderContent(WebPage $oPage, $aExtraParams = array(), $sId)
  251. {
  252. $sHtml = '';
  253. // Add the extra params into the filter if they make sense for such a filter
  254. $bDoSearch = utils::ReadParam('dosearch', false);
  255. if ($this->m_oSet == null)
  256. {
  257. $aQueryParams = array();
  258. if (isset($aExtraParams['query_params']))
  259. {
  260. $aQueryParams = $aExtraParams['query_params'];
  261. }
  262. if ($this->m_sStyle != 'links')
  263. {
  264. $oAppContext = new ApplicationContext();
  265. $sClass = $this->m_oFilter->GetClass();
  266. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($sClass));
  267. $aCallSpec = array($sClass, 'MapContextParam');
  268. if (is_callable($aCallSpec))
  269. {
  270. foreach($oAppContext->GetNames() as $sContextParam)
  271. {
  272. $sParamCode = call_user_func($aCallSpec, $sContextParam); //Map context parameter to the value/filter code depending on the class
  273. if (!is_null($sParamCode))
  274. {
  275. $sParamValue = $oAppContext->GetCurrentValue($sContextParam, null);
  276. if (!is_null($sParamValue))
  277. {
  278. $aExtraParams[$sParamCode] = $sParamValue;
  279. }
  280. }
  281. }
  282. }
  283. foreach($aFilterCodes as $sFilterCode)
  284. {
  285. $sExternalFilterValue = utils::ReadParam($sFilterCode, '', false, 'raw_data');
  286. $condition = null;
  287. if (isset($aExtraParams[$sFilterCode]))
  288. {
  289. $condition = $aExtraParams[$sFilterCode];
  290. }
  291. // else if ($bDoSearch && $sExternalFilterValue != "")
  292. if ($bDoSearch && $sExternalFilterValue != "")
  293. {
  294. // Search takes precedence over context params...
  295. unset($aExtraParams[$sFilterCode]);
  296. $condition = trim($sExternalFilterValue);
  297. }
  298. if (!is_null($condition))
  299. {
  300. $this->AddCondition($sFilterCode, $condition);
  301. }
  302. }
  303. if ($bDoSearch)
  304. {
  305. // Keep the table_id identifying this table if we're performing a search
  306. $sTableId = utils::ReadParam('_table_id_', null, false, 'raw_data');
  307. if ($sTableId != null)
  308. {
  309. $aExtraParams['table_id'] = $sTableId;
  310. }
  311. }
  312. }
  313. $aOrderBy = array();
  314. if (isset($aExtraParams['order_by']))
  315. {
  316. // Convert the string describing the order_by parameter into an array
  317. // The syntax is +attCode1,-attCode2
  318. // attCode1 => ascending, attCode2 => descending
  319. $aTemp = explode(',', $aExtraParams['order_by']);
  320. foreach($aTemp as $sTemp)
  321. {
  322. $aMatches = array();
  323. if (preg_match('/^([+-])?(.+)$/', $sTemp, $aMatches))
  324. {
  325. $bAscending = true;
  326. if ($aMatches[1] == '-')
  327. {
  328. $bAscending = false;
  329. }
  330. $aOrderBy[$aMatches[2]] = $bAscending;
  331. }
  332. }
  333. }
  334. $this->m_oSet = new CMDBObjectSet($this->m_oFilter, $aOrderBy, $aQueryParams);
  335. }
  336. switch($this->m_sStyle)
  337. {
  338. case 'count':
  339. if (isset($aExtraParams['group_by']))
  340. {
  341. if (isset($aExtraParams['group_by_label']))
  342. {
  343. $oGroupByExp = Expression::FromOQL($aExtraParams['group_by']);
  344. $sGroupByLabel = $aExtraParams['group_by_label'];
  345. }
  346. else
  347. {
  348. // Backward compatibility: group_by is simply a field id
  349. $sAlias = $this->m_oFilter->GetClassAlias();
  350. $oGroupByExp = new FieldExpression($aExtraParams['group_by'], $sAlias);
  351. $sGroupByLabel = MetaModel::GetLabel($this->m_oFilter->GetClass(), $aExtraParams['group_by']);
  352. }
  353. $aGroupBy = array();
  354. $aGroupBy['grouped_by_1'] = $oGroupByExp;
  355. $sSql = MetaModel::MakeGroupByQuery($this->m_oFilter, $aQueryParams, $aGroupBy);
  356. $aRes = CMDBSource::QueryToArray($sSql);
  357. $aGroupBy = array();
  358. $aLabels = array();
  359. $aValues = array();
  360. $iTotalCount = 0;
  361. foreach ($aRes as $iRow => $aRow)
  362. {
  363. $sValue = $aRow['grouped_by_1'];
  364. $aValues[$iRow] = $sValue;
  365. $sHtmlValue = $oGroupByExp->MakeValueLabel($this->m_oFilter, $sValue, $sValue);
  366. $aLabels[$iRow] = $sHtmlValue;
  367. $aGroupBy[$iRow] = (int) $aRow['_itop_count_'];
  368. $iTotalCount += $aRow['_itop_count_'];
  369. }
  370. $aData = array();
  371. $oAppContext = new ApplicationContext();
  372. $sParams = $oAppContext->GetForLink();
  373. foreach($aGroupBy as $iRow => $iCount)
  374. {
  375. // Build the search for this subset
  376. $oSubsetSearch = clone $this->m_oFilter;
  377. $oCondition = new BinaryExpression($oGroupByExp, '=', new ScalarExpression($aValues[$iRow]));
  378. $oSubsetSearch->AddConditionExpression($oCondition);
  379. $sFilter = urlencode($oSubsetSearch->serialize());
  380. $aData[] = array ( 'group' => $aLabels[$iRow],
  381. 'value' => "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&dosearch=1&$sParams&filter=$sFilter\">$iCount</a>"); // TO DO: add the context information
  382. }
  383. $aAttribs =array(
  384. 'group' => array('label' => $sGroupByLabel, 'description' => ''),
  385. 'value' => array('label'=> Dict::S('UI:GroupBy:Count'), 'description' => Dict::S('UI:GroupBy:Count+'))
  386. );
  387. $sFormat = isset($aExtraParams['format']) ? $aExtraParams['format'] : 'UI:Pagination:HeaderNoSelection';
  388. $sHtml .= $oPage->GetP(Dict::Format($sFormat, $iTotalCount));
  389. $sHtml .= $oPage->GetTable($aAttribs, $aData);
  390. }
  391. else
  392. {
  393. // Simply count the number of elements in the set
  394. $iCount = $this->m_oSet->Count();
  395. $sFormat = 'UI:CountOfObjects';
  396. if (isset($aExtraParams['format']))
  397. {
  398. $sFormat = $aExtraParams['format'];
  399. }
  400. $sHtml .= $oPage->GetP(Dict::Format($sFormat, $iCount));
  401. }
  402. break;
  403. case 'join':
  404. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']): array();
  405. if (!isset($aExtraParams['group_by']))
  406. {
  407. $sHtml .= $oPage->GetP(Dict::S('UI:Error:MandatoryTemplateParameter_group_by'));
  408. }
  409. else
  410. {
  411. $aGroupByFields = array();
  412. $aGroupBy = explode(',', $aExtraParams['group_by']);
  413. foreach($aGroupBy as $sGroupBy)
  414. {
  415. $aMatches = array();
  416. if (preg_match('/^(.+)\.(.+)$/', $sGroupBy, $aMatches) > 0)
  417. {
  418. $aGroupByFields[] = array('alias' => $aMatches[1], 'att_code' => $aMatches[2]);
  419. }
  420. }
  421. if (count($aGroupByFields) == 0)
  422. {
  423. $sHtml .= $oPage->GetP(Dict::Format('UI:Error:InvalidGroupByFields', $aExtraParams['group_by']));
  424. }
  425. else
  426. {
  427. $aResults = array();
  428. $aCriteria = array();
  429. while($aObjects = $this->m_oSet->FetchAssoc())
  430. {
  431. $aKeys = array();
  432. foreach($aGroupByFields as $aField)
  433. {
  434. $sAlias = $aField['alias'];
  435. if (is_null($aObjects[$sAlias]))
  436. {
  437. $aKeys[$sAlias.'.'.$aField['att_code']] = '';
  438. }
  439. else
  440. {
  441. $aKeys[$sAlias.'.'.$aField['att_code']] = $aObjects[$sAlias]->Get($aField['att_code']);
  442. }
  443. }
  444. $sCategory = implode($aKeys, ' ');
  445. $aResults[$sCategory][] = $aObjects;
  446. $aCriteria[$sCategory] = $aKeys;
  447. }
  448. $sHtml .= "<table>\n";
  449. // Construct a new (parametric) query that will return the content of this block
  450. $oBlockFilter = clone $this->m_oFilter;
  451. $aExpressions = array();
  452. $index = 0;
  453. foreach($aGroupByFields as $aField)
  454. {
  455. $aExpressions[] = '`'.$aField['alias'].'`.`'.$aField['att_code'].'` = :param'.$index++;
  456. }
  457. $sExpression = implode(' AND ', $aExpressions);
  458. $oExpression = Expression::FromOQL($sExpression);
  459. $oBlockFilter->AddConditionExpression($oExpression);
  460. $aExtraParams['menu'] = false;
  461. foreach($aResults as $sCategory => $aObjects)
  462. {
  463. $sHtml .= "<tr><td><h1>$sCategory</h1></td></tr>\n";
  464. if (count($aDisplayAliases) == 1)
  465. {
  466. $aSimpleArray = array();
  467. foreach($aObjects as $aRow)
  468. {
  469. $oObj = $aRow[$aDisplayAliases[0]];
  470. if (!is_null($oObj))
  471. {
  472. $aSimpleArray[] = $oObj;
  473. }
  474. }
  475. $oSet = CMDBObjectSet::FromArray($this->m_oFilter->GetClass(), $aSimpleArray);
  476. $sHtml .= "<tr><td>".cmdbAbstractObject::GetDisplaySet($oPage, $oSet, $aExtraParams)."</td></tr>\n";
  477. }
  478. else
  479. {
  480. $index = 0;
  481. $aArgs = array();
  482. foreach($aGroupByFields as $aField)
  483. {
  484. $aArgs['param'.$index] = $aCriteria[$sCategory][$aField['alias'].'.'.$aField['att_code']];
  485. $index++;
  486. }
  487. $oSet = new CMDBObjectSet($oBlockFilter, array(), $aArgs);
  488. $sHtml .= "<tr><td>".cmdbAbstractObject::GetDisplayExtendedSet($oPage, $oSet, $aExtraParams)."</td></tr>\n";
  489. }
  490. }
  491. $sHtml .= "</table>\n";
  492. }
  493. }
  494. break;
  495. case 'list':
  496. $aClasses = $this->m_oSet->GetSelectedClasses();
  497. $aAuthorizedClasses = array();
  498. if (count($aClasses) > 1)
  499. {
  500. // Check the classes that can be read (i.e authorized) by this user...
  501. foreach($aClasses as $sAlias => $sClassName)
  502. {
  503. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $this->m_oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  504. {
  505. $aAuthorizedClasses[$sAlias] = $sClassName;
  506. }
  507. }
  508. if (count($aAuthorizedClasses) > 0)
  509. {
  510. if($this->m_oSet->Count() > 0)
  511. {
  512. $sHtml .= cmdbAbstractObject::GetDisplayExtendedSet($oPage, $this->m_oSet, $aExtraParams);
  513. }
  514. else
  515. {
  516. // Empty set
  517. $sHtml .= $oPage->GetP(Dict::S('UI:NoObjectToDisplay'));
  518. }
  519. }
  520. else
  521. {
  522. // Not authorized
  523. $sHtml .= $oPage->GetP(Dict::S('UI:NoObjectToDisplay'));
  524. }
  525. }
  526. else
  527. {
  528. // The list is made of only 1 class of objects, actions on the list are possible
  529. if ( ($this->m_oSet->Count()> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) )
  530. {
  531. $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $aExtraParams);
  532. }
  533. else
  534. {
  535. $sHtml .= $oPage->GetP(Dict::S('UI:NoObjectToDisplay'));
  536. $sClass = $this->m_oFilter->GetClass();
  537. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  538. if ($bDisplayMenu)
  539. {
  540. if ((UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  541. {
  542. $oAppContext = new ApplicationContext();
  543. $sParams = $oAppContext->GetForLink();
  544. // 1:n links, populate the target object as a default value when creating a new linked object
  545. if (isset($aExtraParams['target_attr']))
  546. {
  547. $aExtraParams['default'][$aExtraParams['target_attr']] = $aExtraParams['object_id'];
  548. }
  549. $sDefault = '';
  550. if (!empty($aExtraParams['default']))
  551. {
  552. foreach($aExtraParams['default'] as $sKey => $sValue)
  553. {
  554. $sDefault.= "&default[$sKey]=$sValue";
  555. }
  556. }
  557. $sHtml .= $oPage->GetP("<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=new&class=$sClass&$sParams{$sDefault}\">".Dict::Format('UI:ClickToCreateNew', Metamodel::GetName($sClass))."</a>\n");
  558. }
  559. }
  560. }
  561. }
  562. break;
  563. case 'links':
  564. //$bDashboardMode = isset($aExtraParams['dashboard']) ? ($aExtraParams['dashboard'] == 'true') : false;
  565. //$bSelectMode = isset($aExtraParams['select']) ? ($aExtraParams['select'] == 'true') : false;
  566. if ( ($this->m_oSet->Count()> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) )
  567. {
  568. //$sLinkage = isset($aExtraParams['linkage']) ? $aExtraParams['linkage'] : '';
  569. $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $aExtraParams);
  570. }
  571. else
  572. {
  573. $sClass = $this->m_oFilter->GetClass();
  574. $oAttDef = MetaModel::GetAttributeDef($sClass, $this->m_aParams['target_attr']);
  575. $sTargetClass = $oAttDef->GetTargetClass();
  576. $sHtml .= $oPage->GetP(Dict::Format('UI:NoObject_Class_ToDisplay', MetaModel::GetName($sTargetClass)));
  577. $bDisplayMenu = isset($this->m_aParams['menu']) ? $this->m_aParams['menu'] == true : true;
  578. if ($bDisplayMenu)
  579. {
  580. if ((UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  581. {
  582. $oAppContext = new ApplicationContext();
  583. $sParams = $oAppContext->GetForLink();
  584. $sDefaults = '';
  585. if (isset($this->m_aParams['default']))
  586. {
  587. foreach($this->m_aParams['default'] as $sName => $sValue)
  588. {
  589. $sDefaults .= '&'.urlencode($sName).'='.urlencode($sValue);
  590. }
  591. }
  592. $sHtml .= $oPage->GetP("<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=modify_links&class=$sClass&sParams&link_attr=".$aExtraParams['link_attr']."&id=".$aExtraParams['object_id']."&target_class=$sTargetClass&addObjects=true$sDefaults\">".Dict::Format('UI:ClickToCreateNew', Metamodel::GetName($sClass))."</a>\n");
  593. }
  594. }
  595. }
  596. break;
  597. case 'details':
  598. while($oObj = $this->m_oSet->Fetch())
  599. {
  600. $sHtml .= $oObj->GetDetails($oPage); // Still used ???
  601. }
  602. break;
  603. case 'actions':
  604. $sClass = $this->m_oFilter->GetClass();
  605. $oAppContext = new ApplicationContext();
  606. $bContextFilter = isset($aExtraParams['context_filter']) ? isset($aExtraParams['context_filter']) != 0 : false;
  607. if ($bContextFilter)
  608. {
  609. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  610. foreach($oAppContext->GetNames() as $sFilterCode)
  611. {
  612. $sContextParamValue = $oAppContext->GetCurrentValue($sFilterCode, null);
  613. if (!is_null($sContextParamValue) && ! empty($sContextParamValue) && MetaModel::IsValidFilterCode($sClass, $sFilterCode))
  614. {
  615. $this->AddCondition($sFilterCode, $sContextParamValue);
  616. }
  617. }
  618. $aQueryParams = array();
  619. if (isset($aExtraParams['query_params']))
  620. {
  621. $aQueryParams = $aExtraParams['query_params'];
  622. }
  623. $this->m_oSet = new CMDBObjectSet($this->m_oFilter, array(), $aQueryParams);
  624. }
  625. $iCount = $this->m_oSet->Count();
  626. $sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.urlencode($this->m_oFilter->serialize());
  627. $sHtml .= '<p><a class="actions" href="'.$sHyperlink.'">';
  628. // Note: border set to 0 due to various browser interpretations (IE9 adding a 2px border)
  629. $sHtml .= MetaModel::GetClassIcon($sClass, true, 'float;left;margin-right:10px;border:0;');
  630. $sHtml .= MetaModel::GetName($sClass).': '.$iCount.'</a></p>';
  631. $sParams = $oAppContext->GetForLink();
  632. $sHtml .= '<p>';
  633. if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY))
  634. {
  635. $sHtml .= "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=new&class={$sClass}&$sParams\">".Dict::Format('UI:ClickToCreateNew', MetaModel::GetName($sClass))."</a><br/>\n";
  636. }
  637. $sHtml .= "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search_form&class={$sClass}&$sParams\">".Dict::Format('UI:SearchFor_Class', MetaModel::GetName($sClass))."</a>\n";
  638. $sHtml .= '</p>';
  639. break;
  640. case 'summary':
  641. $sClass = $this->m_oFilter->GetClass();
  642. $oAppContext = new ApplicationContext();
  643. $sTitle = isset($aExtraParams['title[block]']) ? $aExtraParams['title[block]'] : '';
  644. $sLabel = isset($aExtraParams['label[block]']) ? $aExtraParams['label[block]'] : '';
  645. $sStateAttrCode = isset($aExtraParams['status[block]']) ? $aExtraParams['status[block]'] : 'status';
  646. $sStatesList = isset($aExtraParams['status_codes[block]']) ? $aExtraParams['status_codes[block]'] : '';
  647. $bContextFilter = isset($aExtraParams['context_filter']) ? isset($aExtraParams['context_filter']) != 0 : false;
  648. if ($bContextFilter)
  649. {
  650. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  651. foreach($oAppContext->GetNames() as $sFilterCode)
  652. {
  653. $sContextParamValue = $oAppContext->GetCurrentValue($sFilterCode, null);
  654. if (!is_null($sContextParamValue) && ! empty($sContextParamValue) && MetaModel::IsValidFilterCode($sClass, $sFilterCode))
  655. {
  656. $this->AddCondition($sFilterCode, $sContextParamValue);
  657. }
  658. }
  659. $aQueryParams = array();
  660. if (isset($aExtraParams['query_params']))
  661. {
  662. $aQueryParams = $aExtraParams['query_params'];
  663. }
  664. $this->m_oSet = new CMDBObjectSet($this->m_oFilter, array(), $aQueryParams);
  665. }
  666. // Summary details
  667. $aCounts = array();
  668. $aStateLabels = array();
  669. if (!empty($sStateAttrCode) && !empty($sStatesList))
  670. {
  671. $aStates = explode(',', $sStatesList);
  672. $oAttDef = MetaModel::GetAttributeDef($sClass, $sStateAttrCode);
  673. foreach($aStates as $sStateValue)
  674. {
  675. $oFilter = clone($this->m_oFilter);
  676. $oFilter->AddCondition($sStateAttrCode, $sStateValue, '=');
  677. $oSet = new DBObjectSet($oFilter);
  678. $aCounts[$sStateValue] = $oSet->Count();
  679. $aStateLabels[$sStateValue] = $oAttDef->GetValueLabel($sStateValue);
  680. if ($aCounts[$sStateValue] == 0)
  681. {
  682. $aCounts[$sStateValue] = '-';
  683. }
  684. else
  685. {
  686. $sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.urlencode($oFilter->serialize());
  687. $aCounts[$sStateValue] = "<a href=\"$sHyperlink\">{$aCounts[$sStateValue]}</a>";
  688. }
  689. }
  690. }
  691. $sHtml .= '<div class="summary-details"><table><tr><th>'.implode('</th><th>', $aStateLabels).'</th></tr>';
  692. $sHtml .= '<tr><td>'.implode('</td><td>', $aCounts).'</td></tr></table></div>';
  693. // Title & summary
  694. $iCount = $this->m_oSet->Count();
  695. $sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.urlencode($this->m_oFilter->serialize());
  696. $sHtml .= '<h1>'.Dict::S(str_replace('_', ':', $sTitle)).'</h1>';
  697. $sHtml .= '<a class="summary" href="'.$sHyperlink.'">'.Dict::Format(str_replace('_', ':', $sLabel), $iCount).'</a>';
  698. break;
  699. case 'csv':
  700. $sHtml .= "<textarea style=\"width:95%;height:98%\">\n";
  701. $sHtml .= cmdbAbstractObject::GetSetAsCSV($this->m_oSet);
  702. $sHtml .= "</textarea>\n";
  703. break;
  704. case 'modify':
  705. if ((UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES))
  706. {
  707. while($oObj = $this->m_oSet->Fetch())
  708. {
  709. $sHtml .= $oObj->GetModifyForm($oPage);
  710. }
  711. }
  712. break;
  713. case 'search':
  714. $sStyle = (isset($aExtraParams['open']) && ($aExtraParams['open'] == 'true')) ? 'SearchDrawer' : 'SearchDrawer DrawerClosed';
  715. $sHtml .= "<div id=\"ds_$sId\" class=\"$sStyle\">\n";
  716. $oPage->add_ready_script(
  717. <<<EOF
  718. $("#dh_$sId").click( function() {
  719. $("#ds_$sId").slideToggle('normal', function() { $("#ds_$sId").parent().resize(); } );
  720. $("#dh_$sId").toggleClass('open');
  721. });
  722. EOF
  723. );
  724. $aExtraParams['currentId'] = $sId;
  725. $sHtml .= cmdbAbstractObject::GetSearchForm($oPage, $this->m_oSet, $aExtraParams);
  726. $sHtml .= "</div>\n";
  727. $sHtml .= "<div class=\"HRDrawer\"></div>\n";
  728. $sHtml .= "<div id=\"dh_$sId\" class=\"DrawerHandle\">".Dict::S('UI:SearchToggle')."</div>\n";
  729. break;
  730. case 'open_flash_chart':
  731. static $iChartCounter = 0;
  732. $oAppContext = new ApplicationContext();
  733. $sContext = $oAppContext->GetForLink();
  734. if (!empty($sContext))
  735. {
  736. $sContext = '&'.$sContext;
  737. }
  738. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  739. $sTitle = isset($aExtraParams['chart_title']) ? $aExtraParams['chart_title'] : '';
  740. $sGroupBy = isset($aExtraParams['group_by']) ? $aExtraParams['group_by'] : '';
  741. $sGroupByExpr = isset($aExtraParams['group_by_expr']) ? '&params[group_by_expr]='.$aExtraParams['group_by_expr'] : '';
  742. $sFilter = $this->m_oFilter->serialize();
  743. $sHtml .= "<div id=\"my_chart_$sId{$iChartCounter}\">If the chart does not display, <a href=\"http://get.adobe.com/flash/\" target=\"_blank\">install Flash</a></div>\n";
  744. $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  745. if (isset($aExtraParams['group_by_label']))
  746. {
  747. $sUrl = urlencode(utils::GetAbsoluteUrlAppRoot()."pages/ajax.render.php?operation=open_flash_chart&params[group_by]=$sGroupBy{$sGroupByExpr}&params[group_by_label]={$aExtraParams['group_by_label']}&params[chart_type]=$sChartType&params[chart_title]=$sTitle&params[currentId]=$sId&id=$sId&filter=".urlencode($sFilter));
  748. }
  749. else
  750. {
  751. $sUrl = urlencode(utils::GetAbsoluteUrlAppRoot()."pages/ajax.render.php?operation=open_flash_chart&params[group_by]=$sGroupBy{$sGroupByExpr}&params[chart_type]=$sChartType&params[chart_title]=$sTitle&params[currentId]=$sId&id=$sId&filter=".urlencode($sFilter));
  752. }
  753. $oPage->add_ready_script("swfobject.embedSWF(\"../images/open-flash-chart.swf\", \"my_chart_$sId{$iChartCounter}\", \"100%\", \"300\",\"9.0.0\", \"expressInstall.swf\",
  754. {\"data-file\":\"".$sUrl."\"}, {wmode: 'transparent'} );\n");
  755. $iChartCounter++;
  756. if (isset($aExtraParams['group_by']))
  757. {
  758. if (isset($aExtraParams['group_by_label']))
  759. {
  760. $oGroupByExp = Expression::FromOQL($aExtraParams['group_by']);
  761. $sGroupByLabel = $aExtraParams['group_by_label'];
  762. }
  763. else
  764. {
  765. // Backward compatibility: group_by is simply a field id
  766. $sAlias = $this->m_oFilter->GetClassAlias();
  767. $oGroupByExp = new FieldExpression($aExtraParams['group_by'], $sAlias);
  768. $sGroupByLabel = MetaModel::GetLabel($this->m_oFilter->GetClass(), $aExtraParams['group_by']);
  769. }
  770. $aGroupBy = array();
  771. $aGroupBy['grouped_by_1'] = $oGroupByExp;
  772. $sSql = MetaModel::MakeGroupByQuery($this->m_oFilter, $aQueryParams, $aGroupBy);
  773. $aRes = CMDBSource::QueryToArray($sSql);
  774. $aGroupBy = array();
  775. $aLabels = array();
  776. $aValues = array();
  777. $iTotalCount = 0;
  778. foreach ($aRes as $iRow => $aRow)
  779. {
  780. $sValue = $aRow['grouped_by_1'];
  781. $aValues[$iRow] = $sValue;
  782. $sHtmlValue = $oGroupByExp->MakeValueLabel($this->m_oFilter, $sValue, $sValue);
  783. $aLabels[$iRow] = $sHtmlValue;
  784. $aGroupBy[$iRow] = (int) $aRow['_itop_count_'];
  785. $iTotalCount += $aRow['_itop_count_'];
  786. }
  787. $aData = array();
  788. $idx = 0;
  789. $aURLs = array();
  790. foreach($aGroupBy as $iRow => $iCount)
  791. {
  792. // Build the search for this subset
  793. $oSubsetSearch = clone $this->m_oFilter;
  794. $oCondition = new BinaryExpression($oGroupByExp, '=', new ScalarExpression($aValues[$iRow]));
  795. $oSubsetSearch->AddConditionExpression($oCondition);
  796. $aURLs[$idx] = $oSubsetSearch->serialize();
  797. $idx++;
  798. }
  799. $sURLList = '';
  800. foreach($aURLs as $index => $sURL)
  801. {
  802. $sURLList .= "\taURLs[$index] = '".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&format=html{$sContext}&filter=".urlencode($sURL)."';\n";
  803. }
  804. $oPage->add_script(
  805. <<<EOF
  806. function ofc_drill_down_{$sId}(index)
  807. {
  808. var aURLs = new Array();
  809. {$sURLList}
  810. window.location.href=aURLs[index];
  811. }
  812. EOF
  813. );
  814. }
  815. break;
  816. case 'open_flash_chart_ajax':
  817. require_once(APPROOT.'/pages/php-ofc-library/open-flash-chart.php');
  818. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  819. $sId = utils::ReadParam('id', '');
  820. $oChart = new open_flash_chart();
  821. switch($sChartType)
  822. {
  823. case 'bars':
  824. $oChartElement = new bar_glass();
  825. if (isset($aExtraParams['group_by']))
  826. {
  827. if (isset($aExtraParams['group_by_label']))
  828. {
  829. $oGroupByExp = Expression::FromOQL($aExtraParams['group_by']);
  830. $sGroupByLabel = $aExtraParams['group_by_label'];
  831. }
  832. else
  833. {
  834. // Backward compatibility: group_by is simply a field id
  835. $sAlias = $this->m_oFilter->GetClassAlias();
  836. $oGroupByExp = new FieldExpression($aExtraParams['group_by'], $sAlias);
  837. $sGroupByLabel = MetaModel::GetLabel($this->m_oFilter->GetClass(), $aExtraParams['group_by']);
  838. }
  839. $aGroupBy = array();
  840. $aGroupBy['grouped_by_1'] = $oGroupByExp;
  841. $sSql = MetaModel::MakeGroupByQuery($this->m_oFilter, $aQueryParams, $aGroupBy);
  842. $aRes = CMDBSource::QueryToArray($sSql);
  843. $aGroupBy = array();
  844. $aLabels = array();
  845. $iTotalCount = 0;
  846. foreach ($aRes as $iRow => $aRow)
  847. {
  848. $sValue = $aRow['grouped_by_1'];
  849. $sHtmlValue = $oGroupByExp->MakeValueLabel($this->m_oFilter, $sValue, $sValue);
  850. $aLabels[$iRow] = strip_tags($sHtmlValue);
  851. $aGroupBy[$iRow] = (int) $aRow['_itop_count_'];
  852. $iTotalCount += $aRow['_itop_count_'];
  853. }
  854. $aData = array();
  855. $aChartLabels = array();
  856. $maxValue = 0;
  857. foreach($aGroupBy as $iRow => $iCount)
  858. {
  859. $oBarValue = new bar_value($iCount);
  860. $oBarValue->on_click("ofc_drill_down_$sId");
  861. $aData[] = $oBarValue;
  862. if ($iCount > $maxValue) $maxValue = $iCount;
  863. $aChartLabels[] = $aLabels[$iRow];
  864. }
  865. $oYAxis = new y_axis();
  866. $aMagicValues = array(1,2,5,10);
  867. $iMultiplier = 1;
  868. $index = 0;
  869. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  870. while($maxValue > $iTop)
  871. {
  872. $index++;
  873. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  874. if (($index % count($aMagicValues)) == 0)
  875. {
  876. $iMultiplier = $iMultiplier * 10;
  877. }
  878. }
  879. //echo "oYAxis->set_range(0, $iTop, $iMultiplier);\n";
  880. $oYAxis->set_range(0, $iTop, $iMultiplier);
  881. $oChart->set_y_axis( $oYAxis );
  882. $oChartElement->set_values( $aData );
  883. $oXAxis = new x_axis();
  884. $oXLabels = new x_axis_labels();
  885. // set them vertical
  886. $oXLabels->set_vertical();
  887. // set the label text
  888. $oXLabels->set_labels($aChartLabels);
  889. // Add the X Axis Labels to the X Axis
  890. $oXAxis->set_labels( $oXLabels );
  891. $oChart->set_x_axis( $oXAxis );
  892. }
  893. break;
  894. case 'pie':
  895. default:
  896. $oChartElement = new pie();
  897. $oChartElement->set_start_angle( 35 );
  898. $oChartElement->set_animate( true );
  899. $oChartElement->set_tooltip( '#label# - #val# (#percent#)' );
  900. $oChartElement->set_colours( array('#FF8A00', '#909980', '#2C2B33', '#CCC08D', '#596664') );
  901. if (isset($aExtraParams['group_by']))
  902. {
  903. if (isset($aExtraParams['group_by_label']))
  904. {
  905. $oGroupByExp = Expression::FromOQL($aExtraParams['group_by']);
  906. $sGroupByLabel = $aExtraParams['group_by_label'];
  907. }
  908. else
  909. {
  910. // Backward compatibility: group_by is simply a field id
  911. $sAlias = $this->m_oFilter->GetClassAlias();
  912. $oGroupByExp = new FieldExpression($aExtraParams['group_by'], $sAlias);
  913. $sGroupByLabel = MetaModel::GetLabel($this->m_oFilter->GetClass(), $aExtraParams['group_by']);
  914. }
  915. $aGroupBy = array();
  916. $aGroupBy['grouped_by_1'] = $oGroupByExp;
  917. $sSql = MetaModel::MakeGroupByQuery($this->m_oFilter, $aQueryParams, $aGroupBy);
  918. $aRes = CMDBSource::QueryToArray($sSql);
  919. $aGroupBy = array();
  920. $aLabels = array();
  921. $iTotalCount = 0;
  922. foreach ($aRes as $iRow => $aRow)
  923. {
  924. $sValue = $aRow['grouped_by_1'];
  925. $sHtmlValue = $oGroupByExp->MakeValueLabel($this->m_oFilter, $sValue, $sValue);
  926. $aLabels[$iRow] = strip_tags($sHtmlValue);
  927. $aGroupBy[$iRow] = (int) $aRow['_itop_count_'];
  928. $iTotalCount += $aRow['_itop_count_'];
  929. }
  930. $aData = array();
  931. foreach($aGroupBy as $iRow => $iCount)
  932. {
  933. $PieValue = new pie_value($iCount, $aLabels[$iRow]); //@@ BUG: not passed via ajax !!!
  934. $PieValue->on_click("ofc_drill_down_$sId");
  935. $aData[] = $PieValue;
  936. }
  937. $oChartElement->set_values( $aData );
  938. $oChart->x_axis = null;
  939. }
  940. }
  941. if (isset($aExtraParams['chart_title']))
  942. {
  943. $oTitle = new title( Dict::S($aExtraParams['chart_title']) );
  944. $oChart->set_title( $oTitle );
  945. }
  946. $oChart->set_bg_colour('#FFFFFF');
  947. $oChart->add_element( $oChartElement );
  948. $sHtml = $oChart->toPrettyString();
  949. break;
  950. default:
  951. // Unsupported style, do nothing.
  952. $sHtml .= Dict::format('UI:Error:UnsupportedStyleOfBlock', $this->m_sStyle);
  953. }
  954. return $sHtml;
  955. }
  956. /**
  957. * Add a condition (restriction) to the current DBObjectSearch on which the display block is based
  958. * taking into account the hierarchical keys for which the condition is based on the 'below' operator
  959. */
  960. protected function AddCondition($sFilterCode, $condition)
  961. {
  962. $sClass = $this->m_oFilter->GetClass();
  963. $bConditionAdded = false;
  964. // If the condition is an external key with a class having a hierarchy, use a "below" criteria
  965. if (MetaModel::IsValidAttCode($sClass, $sFilterCode))
  966. {
  967. $oAttDef = MetaModel::GetAttributeDef($sClass, $sFilterCode);
  968. if ($oAttDef->IsExternalKey())
  969. {
  970. $sHierarchicalKeyCode = MetaModel::IsHierarchicalClass($oAttDef->GetTargetClass());
  971. if ($sHierarchicalKeyCode !== false)
  972. {
  973. $oFilter = new DBObjectSearch($oAttDef->GetTargetClass());
  974. $oFilter->AddCondition('id', $condition);
  975. $oHKFilter = new DBObjectSearch($oAttDef->GetTargetClass());
  976. $oHKFilter->AddCondition_PointingTo($oFilter, $sHierarchicalKeyCode, TREE_OPERATOR_BELOW); // Use the 'below' operator by default
  977. $this->m_oFilter->AddCondition_PointingTo($oHKFilter, $sFilterCode);
  978. $bConditionAdded = true;
  979. }
  980. }
  981. }
  982. // In all other cases, just add the condition directly
  983. if (!$bConditionAdded)
  984. {
  985. $this->m_oFilter->AddCondition($sFilterCode, $condition); // Use the default 'loose' operator
  986. }
  987. }
  988. }
  989. /**
  990. * Helper class to manage 'blocks' of HTML pieces that are parts of a page and contain some list of cmdb objects
  991. *
  992. * Each block is actually rendered as a <div></div> tag that can be rendered synchronously
  993. * or as a piece of Javascript/JQuery/Ajax that will get its content from another page (ajax.render.php).
  994. * The list of cmdbObjects to be displayed into the block is defined by a filter
  995. * Right now the type of display is either: list, count or details
  996. * - list produces a table listing the objects
  997. * - count produces a paragraphs with a sentence saying 'cont' objects found
  998. * - details display (as table) the details of each object found (best if only one)
  999. */
  1000. class HistoryBlock extends DisplayBlock
  1001. {
  1002. public function GetRenderContent(WebPage $oPage, $aExtraParams = array(), $sId)
  1003. {
  1004. $sHtml = '';
  1005. $oSet = new CMDBObjectSet($this->m_oFilter, array('date'=>false));
  1006. $sHtml .= "<!-- filter: ".($this->m_oFilter->ToOQL())."-->\n";
  1007. switch($this->m_sStyle)
  1008. {
  1009. case 'toggle':
  1010. // First the latest change that the user is allowed to see
  1011. do
  1012. {
  1013. $oLatestChangeOp = $oSet->Fetch();
  1014. }
  1015. while(is_object($oLatestChangeOp) && ($oLatestChangeOp->GetDescription() == ''));
  1016. if (is_object($oLatestChangeOp))
  1017. {
  1018. // There is one change in the list... only when the object has been created !
  1019. $sDate = $oLatestChangeOp->GetAsHTML('date');
  1020. $oChange = MetaModel::GetObject('CMDBChange', $oLatestChangeOp->Get('change'));
  1021. $sUserInfo = $oChange->GetAsHTML('userinfo');
  1022. $sHtml .= $oPage->GetStartCollapsibleSection(Dict::Format('UI:History:LastModified_On_By', $sDate, $sUserInfo));
  1023. $sHtml .= $this->GetHistoryTable($oPage, $oSet);
  1024. $sHtml .= $oPage->GetEndCollapsibleSection();
  1025. }
  1026. break;
  1027. case 'table':
  1028. default:
  1029. $sHtml .= $this->GetHistoryTable($oPage, $oSet);
  1030. }
  1031. return $sHtml;
  1032. }
  1033. protected function GetHistoryTable(WebPage $oPage, DBObjectSet $oSet)
  1034. {
  1035. $sHtml = '';
  1036. // First the latest change that the user is allowed to see
  1037. $oSet->Rewind(); // Reset the pointer to the beginning of the set
  1038. $aChanges = array();
  1039. while($oChangeOp = $oSet->Fetch())
  1040. {
  1041. $sChangeDescription = $oChangeOp->GetDescription();
  1042. if ($sChangeDescription != '')
  1043. {
  1044. // The change is visible for the current user
  1045. $changeId = $oChangeOp->Get('change');
  1046. $aChanges[$changeId]['date'] = $oChangeOp->Get('date');
  1047. $aChanges[$changeId]['userinfo'] = $oChangeOp->Get('userinfo');
  1048. if (!isset($aChanges[$changeId]['log']))
  1049. {
  1050. $aChanges[$changeId]['log'] = array();
  1051. }
  1052. $aChanges[$changeId]['log'][] = $sChangeDescription;
  1053. }
  1054. }
  1055. $aAttribs = array('date' => array('label' => Dict::S('UI:History:Date'), 'description' => Dict::S('UI:History:Date+')),
  1056. 'userinfo' => array('label' => Dict::S('UI:History:User'), 'description' => Dict::S('UI:History:User+')),
  1057. 'log' => array('label' => Dict::S('UI:History:Changes'), 'description' => Dict::S('UI:History:Changes+')),
  1058. );
  1059. $aValues = array();
  1060. foreach($aChanges as $aChange)
  1061. {
  1062. $aValues[] = array('date' => $aChange['date'], 'userinfo' => htmlentities($aChange['userinfo'], ENT_QUOTES, 'UTF-8'), 'log' => "<ul><li>".implode('</li><li>', $aChange['log'])."</li></ul>");
  1063. }
  1064. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  1065. return $sHtml;
  1066. }
  1067. }
  1068. /**
  1069. * Displays the 'Actions' menu for a given (list of) object(s)
  1070. * The 'style' of the list (see constructor of DisplayBlock) can be either 'list' or 'details'
  1071. * For backward compatibility 'popup' is equivalent to 'list'...
  1072. */
  1073. class MenuBlock extends DisplayBlock
  1074. {
  1075. /**
  1076. * Renders the "Actions" popup menu for the given set of objects
  1077. *
  1078. * Note that the menu links containing (or ending) with a hash (#) will have their fragment
  1079. * part (whatever is after the hash) dynamically replaced (by javascript) when the menu is
  1080. * displayed, to correspond to the current hash/fragment in the page. This allows modifying
  1081. * an object in with the same tab active by default as the tab that was active when selecting
  1082. * the "Modify..." action.
  1083. */
  1084. public function GetRenderContent(WebPage $oPage, $aExtraParams = array(), $sId)
  1085. {
  1086. if ($this->m_sStyle == 'popup') // popup is a synonym of 'list' for backward compatibility
  1087. {
  1088. $this->m_sStyle = 'list';
  1089. }
  1090. $sHtml = '';
  1091. $oAppContext = new ApplicationContext();
  1092. $sContext = $oAppContext->GetForLink();
  1093. if (!empty($sContext))
  1094. {
  1095. $sContext = '&'.$sContext;
  1096. }
  1097. $sClass = $this->m_oFilter->GetClass();
  1098. $oSet = new CMDBObjectSet($this->m_oFilter);
  1099. $sFilter = $this->m_oFilter->serialize();
  1100. $sFilterDesc = $this->m_oFilter->ToOql(true);
  1101. $aActions = array();
  1102. $sUIPage = cmdbAbstractObject::ComputeStandardUIPage($sClass);
  1103. $sRootUrl = utils::GetAbsoluteUrlAppRoot();
  1104. // 1:n links, populate the target object as a default value when creating a new linked object
  1105. if (isset($aExtraParams['target_attr']))
  1106. {
  1107. $aExtraParams['default'][$aExtraParams['target_attr']] = $aExtraParams['object_id'];
  1108. }
  1109. $sDefault = '';
  1110. if (!empty($aExtraParams['default']))
  1111. {
  1112. foreach($aExtraParams['default'] as $sKey => $sValue)
  1113. {
  1114. $sDefault.= "&default[$sKey]=$sValue";
  1115. }
  1116. }
  1117. $bIsCreationAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_CREATE) == UR_ALLOWED_YES);
  1118. switch($oSet->Count())
  1119. {
  1120. case 0:
  1121. // No object in the set, the only possible action is "new"
  1122. if ($bIsCreationAllowed) { $aActions['UI:Menu:New'] = array ('label' => Dict::S('UI:Menu:New'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=new&class=$sClass{$sContext}{$sDefault}"); }
  1123. break;
  1124. case 1:
  1125. $oObj = $oSet->Fetch();
  1126. $id = $oObj->GetKey();
  1127. $bIsModifyAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES);
  1128. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  1129. $bIsBulkModifyAllowed = (!MetaModel::IsAbstract($sClass)) && UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  1130. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  1131. // Just one object in the set, possible actions are "new / clone / modify and delete"
  1132. if (!isset($aExtraParams['link_attr']))
  1133. {
  1134. if ($bIsModifyAllowed) { $aActions['UI:Menu:Modify'] = array ('label' => Dict::S('UI:Menu:Modify'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=modify&class=$sClass&id=$id{$sContext}#"); }
  1135. if ($bIsCreationAllowed) { $aActions['UI:Menu:New'] = array ('label' => Dict::S('UI:Menu:New'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=new&class=$sClass{$sContext}{$sDefault}"); }
  1136. if ($bIsDeleteAllowed) { $aActions['UI:Menu:Delete'] = array ('label' => Dict::S('UI:Menu:Delete'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=delete&class=$sClass&id=$id{$sContext}"); }
  1137. // Transitions / Stimuli
  1138. $aTransitions = $oObj->EnumTransitions();
  1139. if (count($aTransitions))
  1140. {
  1141. $this->AddMenuSeparator($aActions);
  1142. $aStimuli = Metamodel::EnumStimuli(get_class($oObj));
  1143. foreach($aTransitions as $sStimulusCode => $aTransitionDef)
  1144. {
  1145. $iActionAllowed = (get_class($aStimuli[$sStimulusCode]) == 'StimulusUserAction') ? UserRights::IsStimulusAllowed($sClass, $sStimulusCode, $oSet) : UR_ALLOWED_NO;
  1146. switch($iActionAllowed)
  1147. {
  1148. case UR_ALLOWED_YES:
  1149. $aActions[$sStimulusCode] = array('label' => $aStimuli[$sStimulusCode]->GetLabel(), 'url' => "{$sRootUrl}pages/UI.php?operation=stimulus&stimulus=$sStimulusCode&class=$sClass&id=$id{$sContext}");
  1150. break;
  1151. default:
  1152. // Do nothing
  1153. }
  1154. }
  1155. }
  1156. // Relations...
  1157. $aRelations = MetaModel::EnumRelations($sClass);
  1158. if (count($aRelations))
  1159. {
  1160. $this->AddMenuSeparator($aActions);
  1161. foreach($aRelations as $sRelationCode)
  1162. {
  1163. $aActions[$sRelationCode] = array ('label' => MetaModel::GetRelationVerbUp($sRelationCode), 'url' => "{$sRootUrl}pages/$sUIPage?operation=swf_navigator&relation=$sRelationCode&class=$sClass&id=$id{$sContext}");
  1164. }
  1165. }
  1166. $this->AddMenuSeparator($aActions);
  1167. // Static menus: Email this page & CSV Export
  1168. $sUrl = ApplicationContext::MakeObjectUrl($sClass, $id);
  1169. $aActions['UI:Menu:EMail'] = array ('label' => Dict::S('UI:Menu:EMail'), 'url' => "mailto:?subject=".urlencode($oObj->GetRawName())."&body=".urlencode($sUrl));
  1170. $aActions['UI:Menu:CSVExport'] = array ('label' => Dict::S('UI:Menu:CSVExport'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=search&filter=".urlencode($sFilter)."&format=csv{$sContext}");
  1171. // The style tells us whether the menu is displayed on a list of one object, or on the details of the given object
  1172. if ($this->m_sStyle == 'list')
  1173. {
  1174. // Actions specific to the list
  1175. $sOQL = addslashes($sFilterDesc);
  1176. $aActions['UI:Menu:AddToDashboard'] = array ('label' => Dict::S('UI:Menu:AddToDashboard'), 'url' => "#", 'onclick' => "return DashletCreationDlg('$sOQL')");
  1177. }
  1178. }
  1179. $this->AddMenuSeparator($aActions);
  1180. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  1181. {
  1182. $oSet->Rewind();
  1183. foreach($oExtensionInstance->EnumAllowedActions($oSet) as $sLabel => $sUrl)
  1184. {
  1185. $aActions[$sLabel] = array ('label' => $sLabel, 'url' => $sUrl);
  1186. }
  1187. }
  1188. break;
  1189. default:
  1190. // Check rights
  1191. // New / Modify
  1192. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  1193. $bIsBulkModifyAllowed = (!MetaModel::IsAbstract($sClass)) && UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  1194. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  1195. if (isset($aExtraParams['link_attr']))
  1196. {
  1197. $id = $aExtraParams['object_id'];
  1198. $sTargetAttr = $aExtraParams['target_attr'];
  1199. $oAttDef = MetaModel::GetAttributeDef($sClass, $sTargetAttr);
  1200. $sTargetClass = $oAttDef->GetTargetClass();
  1201. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  1202. if ($bIsModifyAllowed) { $aActions['UI:Menu:Add'] = array ('label' => Dict::S('UI:Menu:Add'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&target_class=$sTargetClass&id=$id&addObjects=true{$sContext}"); }
  1203. if ($bIsBulkModifyAllowed) { $aActions['UI:Menu:Manage'] = array ('label' => Dict::S('UI:Menu:Manage'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&target_class=$sTargetClass&id=$id{$sContext}"); }
  1204. //if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => 'Remove All...', 'url' => "#"); }
  1205. }
  1206. else
  1207. {
  1208. // many objects in the set, possible actions are: new / modify all / delete all
  1209. if ($bIsCreationAllowed) { $aActions['UI:Menu:New'] = array ('label' => Dict::S('UI:Menu:New'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=new&class=$sClass{$sContext}{$sDefault}"); }
  1210. if ($bIsBulkModifyAllowed) { $aActions['UI:Menu:ModifyAll'] = array ('label' => Dict::S('UI:Menu:ModifyAll'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=select_for_modify_all&class=$sClass&filter=".urlencode($sFilter)."{$sContext}"); }
  1211. if ($bIsBulkDeleteAllowed) { $aActions['UI:Menu:BulkDelete'] = array ('label' => Dict::S('UI:Menu:BulkDelete'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=select_for_deletion&filter=".urlencode($sFilter)."{$sContext}"); }
  1212. // Stimuli
  1213. $aStates = MetaModel::EnumStates($sClass);
  1214. if (count($aStates) > 0)
  1215. {
  1216. // Life cycle actions may be available... if all objects are in the same state
  1217. $oSet->Rewind();
  1218. $aStates = array();
  1219. while($oObj = $oSet->Fetch())
  1220. {
  1221. $aStates[$oObj->GetState()] = $oObj->GetState();
  1222. }
  1223. $oSet->Rewind();
  1224. if (count($aStates) == 1)
  1225. {
  1226. // All objects are in the same state...
  1227. $sState = array_pop($aStates);
  1228. $aTransitions = Metamodel::EnumTransitions($sClass, $sState);
  1229. if (count($aTransitions))
  1230. {
  1231. $this->AddMenuSeparator($aActions);
  1232. $aStimuli = Metamodel::EnumStimuli($sClass);
  1233. foreach($aTransitions as $sStimulusCode => $aTransitionDef)
  1234. {
  1235. $oChecker = new StimulusChecker($this->m_oFilter, $sState, $sStimulusCode);
  1236. $iActionAllowed = (get_class($aStimuli[$sStimulusCode]) == 'StimulusUserAction') ? $oChecker->IsAllowed() : UR_ALLOWED_NO;
  1237. switch($iActionAllowed)
  1238. {
  1239. case UR_ALLOWED_YES:
  1240. case UR_ALLOWED_DEPENDS:
  1241. $aActions[$sStimulusCode] = array('label' => $aStimuli[$sStimulusCode]->GetLabel(), 'url' => "{$sRootUrl}pages/UI.php?operation=select_bulk_stimulus&stimulus=$sStimulusCode&state=$sState&class=$sClass&filter=".urlencode($sFilter)."{$sContext}");
  1242. break;
  1243. default:
  1244. // Do nothing
  1245. }
  1246. }
  1247. }
  1248. }
  1249. }
  1250. $this->AddMenuSeparator($aActions);
  1251. $sUrl = utils::GetAbsoluteUrlAppRoot();
  1252. $aActions['UI:Menu:EMail'] = array ('label' => Dict::S('UI:Menu:EMail'), 'url' => "mailto:?subject=$sFilterDesc&body=".urlencode("{$sUrl}pages/$sUIPage?operation=search&filter=".urlencode($sFilter)."{$sContext}"));
  1253. $aActions['UI:Menu:CSVExport'] = array ('label' => Dict::S('UI:Menu:CSVExport'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=search&filter=".urlencode($sFilter)."&format=csv{$sContext}");
  1254. $sOQL = addslashes($sFilterDesc);
  1255. $aActions['UI:Menu:AddToDashboard'] = array ('label' => Dict::S('UI:Menu:AddToDashboard'), 'url' => "#", 'onclick' => "return DashletCreationDlg('$sOQL')");
  1256. }
  1257. $this->AddMenuSeparator($aActions);
  1258. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  1259. {
  1260. $oSet->Rewind();
  1261. foreach($oExtensionInstance->EnumAllowedActions($oSet) as $sLabel => $data)
  1262. {
  1263. if (is_array($data))
  1264. {
  1265. // New plugins can provide javascript handlers via the 'onclick' property
  1266. //TODO: enable extension of different menus by checking the 'target' property ??
  1267. $aActions[$sLabel] = array ('label' => $sLabel, 'url' => isset($data['url']) ? $data['url'] : '#', 'url' => isset($data['onclick']) ? $data['onclick'] : '');
  1268. }
  1269. else
  1270. {
  1271. // Backward compatibility with old plugins
  1272. $aActions[$sLabel] = array ('label' => $sLabel, 'url' => $data);
  1273. }
  1274. }
  1275. }
  1276. }
  1277. $aFavoriteActions = array();
  1278. $aCallSpec = array($sClass, 'GetShortcutActions');
  1279. if (is_callable($aCallSpec))
  1280. {
  1281. $aShortcutActions = call_user_func($aCallSpec, $sClass);
  1282. foreach ($aActions as $key => $aAction)
  1283. {
  1284. if (in_array($key, $aShortcutActions))
  1285. {
  1286. $aFavoriteActions[] = $aAction;
  1287. unset($aActions[$key]);
  1288. }
  1289. }
  1290. }
  1291. else
  1292. {
  1293. $aShortcutActions = array();
  1294. }
  1295. if (count($aFavoriteActions) > 0)
  1296. {
  1297. $sHtml .= "<div class=\"itop_popup actions_menu\"><ul>\n<li>".Dict::S('UI:Menu:OtherActions')."\n<ul>\n";
  1298. }
  1299. else
  1300. {
  1301. $sHtml .= "<div class=\"itop_popup actions_menu\"><ul>\n<li>".Dict::S('UI:Menu:Actions')."\n<ul>\n";
  1302. }
  1303. $sPrevUrl = '';
  1304. foreach ($aActions as $key => $aAction)
  1305. {
  1306. if (in_array($key, $aShortcutActions))
  1307. {
  1308. $aFavoriteActions[] = $aAction;
  1309. }
  1310. else
  1311. {
  1312. $sClass = isset($aAction['class']) ? " class=\"{$aAction['class']}\"" : "";
  1313. $sOnClick = isset($aAction['onclick']) ? " onclick=\"{$aAction['onclick']}\"" : "";
  1314. if (empty($aAction['url']))
  1315. {
  1316. if ($sPrevUrl != '') // Don't output consecutively two separators...
  1317. {
  1318. $sHtml .= "<li>{$aAction['label']}</li>\n";
  1319. }
  1320. $sPrevUrl = '';
  1321. }
  1322. else
  1323. {
  1324. $sHtml .= "<li><a href=\"{$aAction['url']}\"$sClass $sOnClick>{$aAction['label']}</a></li>\n";
  1325. $sPrevUrl = $aAction['url'];
  1326. }
  1327. }
  1328. }
  1329. $sHtml .= "</ul>\n</li>\n</ul></div>";
  1330. foreach(array_reverse($aFavoriteActions) as $aAction)
  1331. {
  1332. $sHtml .= "<div class=\"actions_button\"><a href='{$aAction['url']}'>{$aAction['label']}</a></div>";
  1333. }
  1334. static $bPopupScript = false;
  1335. if (!$bPopupScript)
  1336. {
  1337. // Output this once per page...
  1338. $oPage->add_ready_script("$(\"div.itop_popup>ul\").popupmenu();\n");
  1339. $bPopupScript = true;
  1340. }
  1341. return $sHtml;
  1342. }
  1343. /**
  1344. * Appends a menu separator to the current list of actions
  1345. * @param Hash $aActions The current actions list
  1346. * @return void
  1347. */
  1348. protected function AddMenuSeparator(&$aActions)
  1349. {
  1350. $sSeparator = '<hr class="menu-separator"/>';
  1351. if (count($aActions) > 0) // Make sure that the separator is not the first item in the menu
  1352. {
  1353. $aKeys = array_keys($aActions);
  1354. $sLastKey = array_pop($aKeys);
  1355. if ($aActions[$sLastKey]['label'] != $sSeparator) // Make sure there are no 2 consecutive separators
  1356. {
  1357. $aActions['sep_'.(count($aActions)-1)] = array('label' => $sSeparator, 'url' => '');
  1358. }
  1359. }
  1360. }
  1361. }
  1362. ?>