displayblock.class.inc.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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. $sHtml .= MetaModel::GetClassIcon($sClass, true, 'float;left;margin-right:10px;');
  629. $sHtml .= MetaModel::GetName($sClass).': '.$iCount.'</a></p>';
  630. $sParams = $oAppContext->GetForLink();
  631. $sHtml .= '<p>';
  632. if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY))
  633. {
  634. $sHtml .= "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=new&class={$sClass}&$sParams\">".Dict::Format('UI:ClickToCreateNew', MetaModel::GetName($sClass))."</a><br/>\n";
  635. }
  636. $sHtml .= "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search_form&class={$sClass}&$sParams\">".Dict::Format('UI:SearchFor_Class', MetaModel::GetName($sClass))."</a>\n";
  637. $sHtml .= '</p>';
  638. break;
  639. case 'summary':
  640. $sClass = $this->m_oFilter->GetClass();
  641. $oAppContext = new ApplicationContext();
  642. $sTitle = isset($aExtraParams['title[block]']) ? $aExtraParams['title[block]'] : '';
  643. $sLabel = isset($aExtraParams['label[block]']) ? $aExtraParams['label[block]'] : '';
  644. $sStateAttrCode = isset($aExtraParams['status[block]']) ? $aExtraParams['status[block]'] : 'status';
  645. $sStatesList = isset($aExtraParams['status_codes[block]']) ? $aExtraParams['status_codes[block]'] : '';
  646. $bContextFilter = isset($aExtraParams['context_filter']) ? isset($aExtraParams['context_filter']) != 0 : false;
  647. if ($bContextFilter)
  648. {
  649. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  650. foreach($oAppContext->GetNames() as $sFilterCode)
  651. {
  652. $sContextParamValue = $oAppContext->GetCurrentValue($sFilterCode, null);
  653. if (!is_null($sContextParamValue) && ! empty($sContextParamValue) && MetaModel::IsValidFilterCode($sClass, $sFilterCode))
  654. {
  655. $this->AddCondition($sFilterCode, $sContextParamValue);
  656. }
  657. }
  658. $aQueryParams = array();
  659. if (isset($aExtraParams['query_params']))
  660. {
  661. $aQueryParams = $aExtraParams['query_params'];
  662. }
  663. $this->m_oSet = new CMDBObjectSet($this->m_oFilter, array(), $aQueryParams);
  664. }
  665. // Summary details
  666. $aCounts = array();
  667. $aStateLabels = array();
  668. if (!empty($sStateAttrCode) && !empty($sStatesList))
  669. {
  670. $aStates = explode(',', $sStatesList);
  671. $oAttDef = MetaModel::GetAttributeDef($sClass, $sStateAttrCode);
  672. foreach($aStates as $sStateValue)
  673. {
  674. $oFilter = clone($this->m_oFilter);
  675. $oFilter->AddCondition($sStateAttrCode, $sStateValue, '=');
  676. $oSet = new DBObjectSet($oFilter);
  677. $aCounts[$sStateValue] = $oSet->Count();
  678. $aStateLabels[$sStateValue] = $oAttDef->GetValueLabel($sStateValue);
  679. if ($aCounts[$sStateValue] == 0)
  680. {
  681. $aCounts[$sStateValue] = '-';
  682. }
  683. else
  684. {
  685. $sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.urlencode($oFilter->serialize());
  686. $aCounts[$sStateValue] = "<a href=\"$sHyperlink\">{$aCounts[$sStateValue]}</a>";
  687. }
  688. }
  689. }
  690. $sHtml .= '<div class="summary-details"><table><tr><th>'.implode('</th><th>', $aStateLabels).'</th></tr>';
  691. $sHtml .= '<tr><td>'.implode('</td><td>', $aCounts).'</td></tr></table></div>';
  692. // Title & summary
  693. $iCount = $this->m_oSet->Count();
  694. $sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.urlencode($this->m_oFilter->serialize());
  695. $sHtml .= '<h1>'.Dict::S(str_replace('_', ':', $sTitle)).'</h1>';
  696. $sHtml .= '<a class="summary" href="'.$sHyperlink.'">'.Dict::Format(str_replace('_', ':', $sLabel), $iCount).'</a>';
  697. break;
  698. case 'csv':
  699. $sHtml .= "<textarea style=\"width:95%;height:98%\">\n";
  700. $sHtml .= cmdbAbstractObject::GetSetAsCSV($this->m_oSet);
  701. $sHtml .= "</textarea>\n";
  702. break;
  703. case 'modify':
  704. if ((UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES))
  705. {
  706. while($oObj = $this->m_oSet->Fetch())
  707. {
  708. $sHtml .= $oObj->GetModifyForm($oPage);
  709. }
  710. }
  711. break;
  712. case 'search':
  713. $sStyle = (isset($aExtraParams['open']) && ($aExtraParams['open'] == 'true')) ? 'SearchDrawer' : 'SearchDrawer DrawerClosed';
  714. $sHtml .= "<div id=\"ds_$sId\" class=\"$sStyle\">\n";
  715. $oPage->add_ready_script(
  716. <<<EOF
  717. $("#dh_$sId").click( function() {
  718. $("#ds_$sId").slideToggle('normal', function() { $("#ds_$sId").parent().resize(); } );
  719. $("#dh_$sId").toggleClass('open');
  720. });
  721. EOF
  722. );
  723. $aExtraParams['currentId'] = $sId;
  724. $sHtml .= cmdbAbstractObject::GetSearchForm($oPage, $this->m_oSet, $aExtraParams);
  725. $sHtml .= "</div>\n";
  726. $sHtml .= "<div class=\"HRDrawer\"></div>\n";
  727. $sHtml .= "<div id=\"dh_$sId\" class=\"DrawerHandle\">".Dict::S('UI:SearchToggle')."</div>\n";
  728. break;
  729. case 'open_flash_chart':
  730. static $iChartCounter = 0;
  731. $oAppContext = new ApplicationContext();
  732. $sContext = $oAppContext->GetForLink();
  733. if (!empty($sContext))
  734. {
  735. $sContext = '&'.$sContext;
  736. }
  737. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  738. $sTitle = isset($aExtraParams['chart_title']) ? $aExtraParams['chart_title'] : '';
  739. $sGroupBy = isset($aExtraParams['group_by']) ? $aExtraParams['group_by'] : '';
  740. $sGroupByExpr = isset($aExtraParams['group_by_expr']) ? '&params[group_by_expr]='.$aExtraParams['group_by_expr'] : '';
  741. $sFilter = $this->m_oFilter->serialize();
  742. $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";
  743. $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  744. if (isset($aExtraParams['group_by_label']))
  745. {
  746. $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));
  747. }
  748. else
  749. {
  750. $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));
  751. }
  752. $oPage->add_ready_script("swfobject.embedSWF(\"../images/open-flash-chart.swf\", \"my_chart_$sId{$iChartCounter}\", \"100%\", \"300\",\"9.0.0\", \"expressInstall.swf\",
  753. {\"data-file\":\"".$sUrl."\"}, {wmode: 'transparent'} );\n");
  754. $iChartCounter++;
  755. if (isset($aExtraParams['group_by']))
  756. {
  757. if (isset($aExtraParams['group_by_label']))
  758. {
  759. $oGroupByExp = Expression::FromOQL($aExtraParams['group_by']);
  760. $sGroupByLabel = $aExtraParams['group_by_label'];
  761. }
  762. else
  763. {
  764. // Backward compatibility: group_by is simply a field id
  765. $sAlias = $this->m_oFilter->GetClassAlias();
  766. $oGroupByExp = new FieldExpression($aExtraParams['group_by'], $sAlias);
  767. $sGroupByLabel = MetaModel::GetLabel($this->m_oFilter->GetClass(), $aExtraParams['group_by']);
  768. }
  769. $aGroupBy = array();
  770. $aGroupBy['grouped_by_1'] = $oGroupByExp;
  771. $sSql = MetaModel::MakeGroupByQuery($this->m_oFilter, $aQueryParams, $aGroupBy);
  772. $aRes = CMDBSource::QueryToArray($sSql);
  773. $aGroupBy = array();
  774. $aLabels = array();
  775. $aValues = array();
  776. $iTotalCount = 0;
  777. foreach ($aRes as $iRow => $aRow)
  778. {
  779. $sValue = $aRow['grouped_by_1'];
  780. $aValues[$iRow] = $sValue;
  781. $sHtmlValue = $oGroupByExp->MakeValueLabel($this->m_oFilter, $sValue, $sValue);
  782. $aLabels[$iRow] = $sHtmlValue;
  783. $aGroupBy[$iRow] = (int) $aRow['_itop_count_'];
  784. $iTotalCount += $aRow['_itop_count_'];
  785. }
  786. $aData = array();
  787. $idx = 0;
  788. $aURLs = array();
  789. foreach($aGroupBy as $iRow => $iCount)
  790. {
  791. // Build the search for this subset
  792. $oSubsetSearch = clone $this->m_oFilter;
  793. $oCondition = new BinaryExpression($oGroupByExp, '=', new ScalarExpression($aValues[$iRow]));
  794. $oSubsetSearch->AddConditionExpression($oCondition);
  795. $aURLs[$idx] = $oSubsetSearch->serialize();
  796. $idx++;
  797. }
  798. $sURLList = '';
  799. foreach($aURLs as $index => $sURL)
  800. {
  801. $sURLList .= "\taURLs[$index] = '".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&format=html{$sContext}&filter=".urlencode($sURL)."';\n";
  802. }
  803. $oPage->add_script(
  804. <<<EOF
  805. function ofc_drill_down_{$sId}(index)
  806. {
  807. var aURLs = new Array();
  808. {$sURLList}
  809. window.location.href=aURLs[index];
  810. }
  811. EOF
  812. );
  813. }
  814. break;
  815. case 'open_flash_chart_ajax':
  816. require_once(APPROOT.'/pages/php-ofc-library/open-flash-chart.php');
  817. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  818. $sId = utils::ReadParam('id', '');
  819. $oChart = new open_flash_chart();
  820. switch($sChartType)
  821. {
  822. case 'bars':
  823. $oChartElement = new bar_glass();
  824. if (isset($aExtraParams['group_by']))
  825. {
  826. if (isset($aExtraParams['group_by_label']))
  827. {
  828. $oGroupByExp = Expression::FromOQL($aExtraParams['group_by']);
  829. $sGroupByLabel = $aExtraParams['group_by_label'];
  830. }
  831. else
  832. {
  833. // Backward compatibility: group_by is simply a field id
  834. $sAlias = $this->m_oFilter->GetClassAlias();
  835. $oGroupByExp = new FieldExpression($aExtraParams['group_by'], $sAlias);
  836. $sGroupByLabel = MetaModel::GetLabel($this->m_oFilter->GetClass(), $aExtraParams['group_by']);
  837. }
  838. $aGroupBy = array();
  839. $aGroupBy['grouped_by_1'] = $oGroupByExp;
  840. $sSql = MetaModel::MakeGroupByQuery($this->m_oFilter, $aQueryParams, $aGroupBy);
  841. $aRes = CMDBSource::QueryToArray($sSql);
  842. $aGroupBy = array();
  843. $aLabels = array();
  844. $iTotalCount = 0;
  845. foreach ($aRes as $iRow => $aRow)
  846. {
  847. $sValue = $aRow['grouped_by_1'];
  848. $sHtmlValue = $oGroupByExp->MakeValueLabel($this->m_oFilter, $sValue, $sValue);
  849. $aLabels[$iRow] = strip_tags($sHtmlValue);
  850. $aGroupBy[$iRow] = (int) $aRow['_itop_count_'];
  851. $iTotalCount += $aRow['_itop_count_'];
  852. }
  853. $aData = array();
  854. $aChartLabels = array();
  855. $maxValue = 0;
  856. foreach($aGroupBy as $iRow => $iCount)
  857. {
  858. $oBarValue = new bar_value($iCount);
  859. $oBarValue->on_click("ofc_drill_down_$sId");
  860. $aData[] = $oBarValue;
  861. if ($iCount > $maxValue) $maxValue = $iCount;
  862. $aChartLabels[] = $aLabels[$iRow];
  863. }
  864. $oYAxis = new y_axis();
  865. $aMagicValues = array(1,2,5,10);
  866. $iMultiplier = 1;
  867. $index = 0;
  868. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  869. while($maxValue > $iTop)
  870. {
  871. $index++;
  872. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  873. if (($index % count($aMagicValues)) == 0)
  874. {
  875. $iMultiplier = $iMultiplier * 10;
  876. }
  877. }
  878. //echo "oYAxis->set_range(0, $iTop, $iMultiplier);\n";
  879. $oYAxis->set_range(0, $iTop, $iMultiplier);
  880. $oChart->set_y_axis( $oYAxis );
  881. $oChartElement->set_values( $aData );
  882. $oXAxis = new x_axis();
  883. $oXLabels = new x_axis_labels();
  884. // set them vertical
  885. $oXLabels->set_vertical();
  886. // set the label text
  887. $oXLabels->set_labels($aChartLabels);
  888. // Add the X Axis Labels to the X Axis
  889. $oXAxis->set_labels( $oXLabels );
  890. $oChart->set_x_axis( $oXAxis );
  891. }
  892. break;
  893. case 'pie':
  894. default:
  895. $oChartElement = new pie();
  896. $oChartElement->set_start_angle( 35 );
  897. $oChartElement->set_animate( true );
  898. $oChartElement->set_tooltip( '#label# - #val# (#percent#)' );
  899. $oChartElement->set_colours( array('#FF8A00', '#909980', '#2C2B33', '#CCC08D', '#596664') );
  900. if (isset($aExtraParams['group_by']))
  901. {
  902. if (isset($aExtraParams['group_by_label']))
  903. {
  904. $oGroupByExp = Expression::FromOQL($aExtraParams['group_by']);
  905. $sGroupByLabel = $aExtraParams['group_by_label'];
  906. }
  907. else
  908. {
  909. // Backward compatibility: group_by is simply a field id
  910. $sAlias = $this->m_oFilter->GetClassAlias();
  911. $oGroupByExp = new FieldExpression($aExtraParams['group_by'], $sAlias);
  912. $sGroupByLabel = MetaModel::GetLabel($this->m_oFilter->GetClass(), $aExtraParams['group_by']);
  913. }
  914. $aGroupBy = array();
  915. $aGroupBy['grouped_by_1'] = $oGroupByExp;
  916. $sSql = MetaModel::MakeGroupByQuery($this->m_oFilter, $aQueryParams, $aGroupBy);
  917. $aRes = CMDBSource::QueryToArray($sSql);
  918. $aGroupBy = array();
  919. $aLabels = array();
  920. $iTotalCount = 0;
  921. foreach ($aRes as $iRow => $aRow)
  922. {
  923. $sValue = $aRow['grouped_by_1'];
  924. $sHtmlValue = $oGroupByExp->MakeValueLabel($this->m_oFilter, $sValue, $sValue);
  925. $aLabels[$iRow] = strip_tags($sHtmlValue);
  926. $aGroupBy[$iRow] = (int) $aRow['_itop_count_'];
  927. $iTotalCount += $aRow['_itop_count_'];
  928. }
  929. $aData = array();
  930. foreach($aGroupBy as $iRow => $iCount)
  931. {
  932. $PieValue = new pie_value($iCount, $aLabels[$iRow]); //@@ BUG: not passed via ajax !!!
  933. $PieValue->on_click("ofc_drill_down_$sId");
  934. $aData[] = $PieValue;
  935. }
  936. $oChartElement->set_values( $aData );
  937. $oChart->x_axis = null;
  938. }
  939. }
  940. if (isset($aExtraParams['chart_title']))
  941. {
  942. $oTitle = new title( Dict::S($aExtraParams['chart_title']) );
  943. $oChart->set_title( $oTitle );
  944. }
  945. $oChart->set_bg_colour('#FFFFFF');
  946. $oChart->add_element( $oChartElement );
  947. $sHtml = $oChart->toPrettyString();
  948. break;
  949. default:
  950. // Unsupported style, do nothing.
  951. $sHtml .= Dict::format('UI:Error:UnsupportedStyleOfBlock', $this->m_sStyle);
  952. }
  953. return $sHtml;
  954. }
  955. /**
  956. * Add a condition (restriction) to the current DBObjectSearch on which the display block is based
  957. * taking into account the hierarchical keys for which the condition is based on the 'below' operator
  958. */
  959. protected function AddCondition($sFilterCode, $condition)
  960. {
  961. $sClass = $this->m_oFilter->GetClass();
  962. $bConditionAdded = false;
  963. // If the condition is an external key with a class having a hierarchy, use a "below" criteria
  964. if (MetaModel::IsValidAttCode($sClass, $sFilterCode))
  965. {
  966. $oAttDef = MetaModel::GetAttributeDef($sClass, $sFilterCode);
  967. if ($oAttDef->IsExternalKey())
  968. {
  969. $sHierarchicalKeyCode = MetaModel::IsHierarchicalClass($oAttDef->GetTargetClass());
  970. if ($sHierarchicalKeyCode !== false)
  971. {
  972. $oFilter = new DBObjectSearch($oAttDef->GetTargetClass());
  973. $oFilter->AddCondition('id', $condition);
  974. $oHKFilter = new DBObjectSearch($oAttDef->GetTargetClass());
  975. $oHKFilter->AddCondition_PointingTo($oFilter, $sHierarchicalKeyCode, TREE_OPERATOR_BELOW); // Use the 'below' operator by default
  976. $this->m_oFilter->AddCondition_PointingTo($oHKFilter, $sFilterCode);
  977. $bConditionAdded = true;
  978. }
  979. }
  980. }
  981. // In all other cases, just add the condition directly
  982. if (!$bConditionAdded)
  983. {
  984. $this->m_oFilter->AddCondition($sFilterCode, $condition); // Use the default 'loose' operator
  985. }
  986. }
  987. }
  988. /**
  989. * Helper class to manage 'blocks' of HTML pieces that are parts of a page and contain some list of cmdb objects
  990. *
  991. * Each block is actually rendered as a <div></div> tag that can be rendered synchronously
  992. * or as a piece of Javascript/JQuery/Ajax that will get its content from another page (ajax.render.php).
  993. * The list of cmdbObjects to be displayed into the block is defined by a filter
  994. * Right now the type of display is either: list, count or details
  995. * - list produces a table listing the objects
  996. * - count produces a paragraphs with a sentence saying 'cont' objects found
  997. * - details display (as table) the details of each object found (best if only one)
  998. */
  999. class HistoryBlock extends DisplayBlock
  1000. {
  1001. public function GetRenderContent(WebPage $oPage, $aExtraParams = array(), $sId)
  1002. {
  1003. $sHtml = '';
  1004. $oSet = new CMDBObjectSet($this->m_oFilter, array('date'=>false));
  1005. $sHtml .= "<!-- filter: ".($this->m_oFilter->ToOQL())."-->\n";
  1006. switch($this->m_sStyle)
  1007. {
  1008. case 'toggle':
  1009. // First the latest change that the user is allowed to see
  1010. do
  1011. {
  1012. $oLatestChangeOp = $oSet->Fetch();
  1013. }
  1014. while(is_object($oLatestChangeOp) && ($oLatestChangeOp->GetDescription() == ''));
  1015. if (is_object($oLatestChangeOp))
  1016. {
  1017. // There is one change in the list... only when the object has been created !
  1018. $sDate = $oLatestChangeOp->GetAsHTML('date');
  1019. $oChange = MetaModel::GetObject('CMDBChange', $oLatestChangeOp->Get('change'));
  1020. $sUserInfo = $oChange->GetAsHTML('userinfo');
  1021. $sHtml .= $oPage->GetStartCollapsibleSection(Dict::Format('UI:History:LastModified_On_By', $sDate, $sUserInfo));
  1022. $sHtml .= $this->GetHistoryTable($oPage, $oSet);
  1023. $sHtml .= $oPage->GetEndCollapsibleSection();
  1024. }
  1025. break;
  1026. case 'table':
  1027. default:
  1028. $sHtml .= $this->GetHistoryTable($oPage, $oSet);
  1029. }
  1030. return $sHtml;
  1031. }
  1032. protected function GetHistoryTable(WebPage $oPage, DBObjectSet $oSet)
  1033. {
  1034. $sHtml = '';
  1035. // First the latest change that the user is allowed to see
  1036. $oSet->Rewind(); // Reset the pointer to the beginning of the set
  1037. $aChanges = array();
  1038. while($oChangeOp = $oSet->Fetch())
  1039. {
  1040. $sChangeDescription = $oChangeOp->GetDescription();
  1041. if ($sChangeDescription != '')
  1042. {
  1043. // The change is visible for the current user
  1044. $changeId = $oChangeOp->Get('change');
  1045. $aChanges[$changeId]['date'] = $oChangeOp->Get('date');
  1046. $aChanges[$changeId]['userinfo'] = $oChangeOp->Get('userinfo');
  1047. if (!isset($aChanges[$changeId]['log']))
  1048. {
  1049. $aChanges[$changeId]['log'] = array();
  1050. }
  1051. $aChanges[$changeId]['log'][] = $sChangeDescription;
  1052. }
  1053. }
  1054. $aAttribs = array('date' => array('label' => Dict::S('UI:History:Date'), 'description' => Dict::S('UI:History:Date+')),
  1055. 'userinfo' => array('label' => Dict::S('UI:History:User'), 'description' => Dict::S('UI:History:User+')),
  1056. 'log' => array('label' => Dict::S('UI:History:Changes'), 'description' => Dict::S('UI:History:Changes+')),
  1057. );
  1058. $aValues = array();
  1059. foreach($aChanges as $aChange)
  1060. {
  1061. $aValues[] = array('date' => $aChange['date'], 'userinfo' => htmlentities($aChange['userinfo'], ENT_QUOTES, 'UTF-8'), 'log' => "<ul><li>".implode('</li><li>', $aChange['log'])."</li></ul>");
  1062. }
  1063. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  1064. return $sHtml;
  1065. }
  1066. }
  1067. /**
  1068. * Displays the 'Actions' menu for a given (list of) object(s)
  1069. * The 'style' of the list (see constructor of DisplayBlock) can be either 'list' or 'details'
  1070. * For backward compatibility 'popup' is equivalent to 'list'...
  1071. */
  1072. class MenuBlock extends DisplayBlock
  1073. {
  1074. /**
  1075. * Renders the "Actions" popup menu for the given set of objects
  1076. *
  1077. * Note that the menu links containing (or ending) with a hash (#) will have their fragment
  1078. * part (whatever is after the hash) dynamically replaced (by javascript) when the menu is
  1079. * displayed, to correspond to the current hash/fragment in the page. This allows modifying
  1080. * an object in with the same tab active by default as the tab that was active when selecting
  1081. * the "Modify..." action.
  1082. */
  1083. public function GetRenderContent(WebPage $oPage, $aExtraParams = array(), $sId)
  1084. {
  1085. if ($this->m_sStyle == 'popup') // popup is a synonym of 'list' for backward compatibility
  1086. {
  1087. $this->m_sStyle = 'list';
  1088. }
  1089. $sHtml = '';
  1090. $oAppContext = new ApplicationContext();
  1091. $sContext = $oAppContext->GetForLink();
  1092. if (!empty($sContext))
  1093. {
  1094. $sContext = '&'.$sContext;
  1095. }
  1096. $sClass = $this->m_oFilter->GetClass();
  1097. $oSet = new CMDBObjectSet($this->m_oFilter);
  1098. $sFilter = $this->m_oFilter->serialize();
  1099. $sFilterDesc = $this->m_oFilter->ToOql(true);
  1100. $aActions = array();
  1101. $sUIPage = cmdbAbstractObject::ComputeStandardUIPage($sClass);
  1102. $sRootUrl = utils::GetAbsoluteUrlAppRoot();
  1103. // 1:n links, populate the target object as a default value when creating a new linked object
  1104. if (isset($aExtraParams['target_attr']))
  1105. {
  1106. $aExtraParams['default'][$aExtraParams['target_attr']] = $aExtraParams['object_id'];
  1107. }
  1108. $sDefault = '';
  1109. if (!empty($aExtraParams['default']))
  1110. {
  1111. foreach($aExtraParams['default'] as $sKey => $sValue)
  1112. {
  1113. $sDefault.= "&default[$sKey]=$sValue";
  1114. }
  1115. }
  1116. $bIsCreationAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_CREATE) == UR_ALLOWED_YES);
  1117. switch($oSet->Count())
  1118. {
  1119. case 0:
  1120. // No object in the set, the only possible action is "new"
  1121. if ($bIsCreationAllowed) { $aActions['UI:Menu:New'] = array ('label' => Dict::S('UI:Menu:New'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=new&class=$sClass{$sContext}{$sDefault}"); }
  1122. break;
  1123. case 1:
  1124. $oObj = $oSet->Fetch();
  1125. $id = $oObj->GetKey();
  1126. $bIsModifyAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES);
  1127. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  1128. $bIsBulkModifyAllowed = (!MetaModel::IsAbstract($sClass)) && UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  1129. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  1130. // Just one object in the set, possible actions are "new / clone / modify and delete"
  1131. if (!isset($aExtraParams['link_attr']))
  1132. {
  1133. if ($bIsModifyAllowed) { $aActions['UI:Menu:Modify'] = array ('label' => Dict::S('UI:Menu:Modify'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=modify&class=$sClass&id=$id{$sContext}#"); }
  1134. if ($bIsCreationAllowed) { $aActions['UI:Menu:New'] = array ('label' => Dict::S('UI:Menu:New'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=new&class=$sClass{$sContext}{$sDefault}"); }
  1135. if ($bIsDeleteAllowed) { $aActions['UI:Menu:Delete'] = array ('label' => Dict::S('UI:Menu:Delete'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=delete&class=$sClass&id=$id{$sContext}"); }
  1136. // Transitions / Stimuli
  1137. $aTransitions = $oObj->EnumTransitions();
  1138. if (count($aTransitions))
  1139. {
  1140. $this->AddMenuSeparator($aActions);
  1141. $aStimuli = Metamodel::EnumStimuli(get_class($oObj));
  1142. foreach($aTransitions as $sStimulusCode => $aTransitionDef)
  1143. {
  1144. $iActionAllowed = (get_class($aStimuli[$sStimulusCode]) == 'StimulusUserAction') ? UserRights::IsStimulusAllowed($sClass, $sStimulusCode, $oSet) : UR_ALLOWED_NO;
  1145. switch($iActionAllowed)
  1146. {
  1147. case UR_ALLOWED_YES:
  1148. $aActions[$sStimulusCode] = array('label' => $aStimuli[$sStimulusCode]->GetLabel(), 'url' => "{$sRootUrl}pages/UI.php?operation=stimulus&stimulus=$sStimulusCode&class=$sClass&id=$id{$sContext}");
  1149. break;
  1150. default:
  1151. // Do nothing
  1152. }
  1153. }
  1154. }
  1155. // Relations...
  1156. $aRelations = MetaModel::EnumRelations($sClass);
  1157. if (count($aRelations))
  1158. {
  1159. $this->AddMenuSeparator($aActions);
  1160. foreach($aRelations as $sRelationCode)
  1161. {
  1162. $aActions[$sRelationCode] = array ('label' => MetaModel::GetRelationVerbUp($sRelationCode), 'url' => "{$sRootUrl}pages/$sUIPage?operation=swf_navigator&relation=$sRelationCode&class=$sClass&id=$id{$sContext}");
  1163. }
  1164. }
  1165. $this->AddMenuSeparator($aActions);
  1166. // Static menus: Email this page & CSV Export
  1167. $sUrl = ApplicationContext::MakeObjectUrl($sClass, $id);
  1168. $aActions['UI:Menu:EMail'] = array ('label' => Dict::S('UI:Menu:EMail'), 'url' => "mailto:?subject=".urlencode($oObj->GetRawName())."&body=".urlencode($sUrl));
  1169. $aActions['UI:Menu:CSVExport'] = array ('label' => Dict::S('UI:Menu:CSVExport'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=search&filter=".urlencode($sFilter)."&format=csv{$sContext}");
  1170. // The style tells us whether the menu is displayed on a list of one object, or on the details of the given object
  1171. if ($this->m_sStyle == 'list')
  1172. {
  1173. // Actions specific to the list
  1174. $sOQL = addslashes($sFilterDesc);
  1175. $aActions['UI:Menu:AddToDashboard'] = array ('label' => Dict::S('UI:Menu:AddToDashboard'), 'url' => "#", 'onclick' => "return DashletCreationDlg('$sOQL')");
  1176. }
  1177. }
  1178. $this->AddMenuSeparator($aActions);
  1179. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  1180. {
  1181. $oSet->Rewind();
  1182. foreach($oExtensionInstance->EnumAllowedActions($oSet) as $sLabel => $sUrl)
  1183. {
  1184. $aActions[$sLabel] = array ('label' => $sLabel, 'url' => $sUrl);
  1185. }
  1186. }
  1187. break;
  1188. default:
  1189. // Check rights
  1190. // New / Modify
  1191. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  1192. $bIsBulkModifyAllowed = (!MetaModel::IsAbstract($sClass)) && UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  1193. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  1194. if (isset($aExtraParams['link_attr']))
  1195. {
  1196. $id = $aExtraParams['object_id'];
  1197. $sTargetAttr = $aExtraParams['target_attr'];
  1198. $oAttDef = MetaModel::GetAttributeDef($sClass, $sTargetAttr);
  1199. $sTargetClass = $oAttDef->GetTargetClass();
  1200. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  1201. 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}"); }
  1202. 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}"); }
  1203. //if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => 'Remove All...', 'url' => "#"); }
  1204. }
  1205. else
  1206. {
  1207. // many objects in the set, possible actions are: new / modify all / delete all
  1208. if ($bIsCreationAllowed) { $aActions['UI:Menu:New'] = array ('label' => Dict::S('UI:Menu:New'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=new&class=$sClass{$sContext}{$sDefault}"); }
  1209. 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}"); }
  1210. 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}"); }
  1211. // Stimuli
  1212. $aStates = MetaModel::EnumStates($sClass);
  1213. if (count($aStates) > 0)
  1214. {
  1215. // Life cycle actions may be available... if all objects are in the same state
  1216. $oSet->Rewind();
  1217. $aStates = array();
  1218. while($oObj = $oSet->Fetch())
  1219. {
  1220. $aStates[$oObj->GetState()] = $oObj->GetState();
  1221. }
  1222. $oSet->Rewind();
  1223. if (count($aStates) == 1)
  1224. {
  1225. // All objects are in the same state...
  1226. $sState = array_pop($aStates);
  1227. $aTransitions = Metamodel::EnumTransitions($sClass, $sState);
  1228. if (count($aTransitions))
  1229. {
  1230. $this->AddMenuSeparator($aActions);
  1231. $aStimuli = Metamodel::EnumStimuli($sClass);
  1232. foreach($aTransitions as $sStimulusCode => $aTransitionDef)
  1233. {
  1234. $oChecker = new StimulusChecker($this->m_oFilter, $sState, $sStimulusCode);
  1235. $iActionAllowed = (get_class($aStimuli[$sStimulusCode]) == 'StimulusUserAction') ? $oChecker->IsAllowed() : UR_ALLOWED_NO;
  1236. switch($iActionAllowed)
  1237. {
  1238. case UR_ALLOWED_YES:
  1239. case UR_ALLOWED_DEPENDS:
  1240. $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}");
  1241. break;
  1242. default:
  1243. // Do nothing
  1244. }
  1245. }
  1246. }
  1247. }
  1248. }
  1249. $this->AddMenuSeparator($aActions);
  1250. $sUrl = utils::GetAbsoluteUrlAppRoot();
  1251. $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}"));
  1252. $aActions['UI:Menu:CSVExport'] = array ('label' => Dict::S('UI:Menu:CSVExport'), 'url' => "{$sRootUrl}pages/$sUIPage?operation=search&filter=".urlencode($sFilter)."&format=csv{$sContext}");
  1253. $sOQL = addslashes($sFilterDesc);
  1254. $aActions['UI:Menu:AddToDashboard'] = array ('label' => Dict::S('UI:Menu:AddToDashboard'), 'url' => "#", 'onclick' => "return DashletCreationDlg('$sOQL')");
  1255. }
  1256. $this->AddMenuSeparator($aActions);
  1257. foreach (MetaModel::EnumPlugins('iApplicationUIExtension') as $oExtensionInstance)
  1258. {
  1259. $oSet->Rewind();
  1260. foreach($oExtensionInstance->EnumAllowedActions($oSet) as $sLabel => $data)
  1261. {
  1262. if (is_array($data))
  1263. {
  1264. // New plugins can provide javascript handlers via the 'onclick' property
  1265. //TODO: enable extension of different menus by checking the 'target' property ??
  1266. $aActions[$sLabel] = array ('label' => $sLabel, 'url' => isset($data['url']) ? $data['url'] : '#', 'url' => isset($data['onclick']) ? $data['onclick'] : '');
  1267. }
  1268. else
  1269. {
  1270. // Backward compatibility with old plugins
  1271. $aActions[$sLabel] = array ('label' => $sLabel, 'url' => $data);
  1272. }
  1273. }
  1274. }
  1275. }
  1276. $aFavoriteActions = array();
  1277. $aCallSpec = array($sClass, 'GetShortcutActions');
  1278. if (is_callable($aCallSpec))
  1279. {
  1280. $aShortcutActions = call_user_func($aCallSpec, $sClass);
  1281. foreach ($aActions as $key => $aAction)
  1282. {
  1283. if (in_array($key, $aShortcutActions))
  1284. {
  1285. $aFavoriteActions[] = $aAction;
  1286. unset($aActions[$key]);
  1287. }
  1288. }
  1289. }
  1290. else
  1291. {
  1292. $aShortcutActions = array();
  1293. }
  1294. if (count($aFavoriteActions) > 0)
  1295. {
  1296. $sHtml .= "<div class=\"itop_popup actions_menu\"><ul>\n<li>".Dict::S('UI:Menu:OtherActions')."\n<ul>\n";
  1297. }
  1298. else
  1299. {
  1300. $sHtml .= "<div class=\"itop_popup actions_menu\"><ul>\n<li>".Dict::S('UI:Menu:Actions')."\n<ul>\n";
  1301. }
  1302. $sPrevUrl = '';
  1303. foreach ($aActions as $key => $aAction)
  1304. {
  1305. if (in_array($key, $aShortcutActions))
  1306. {
  1307. $aFavoriteActions[] = $aAction;
  1308. }
  1309. else
  1310. {
  1311. $sClass = isset($aAction['class']) ? " class=\"{$aAction['class']}\"" : "";
  1312. $sOnClick = isset($aAction['onclick']) ? " onclick=\"{$aAction['onclick']}\"" : "";
  1313. if (empty($aAction['url']))
  1314. {
  1315. if ($sPrevUrl != '') // Don't output consecutively two separators...
  1316. {
  1317. $sHtml .= "<li>{$aAction['label']}</li>\n";
  1318. }
  1319. $sPrevUrl = '';
  1320. }
  1321. else
  1322. {
  1323. $sHtml .= "<li><a href=\"{$aAction['url']}\"$sClass $sOnClick>{$aAction['label']}</a></li>\n";
  1324. $sPrevUrl = $aAction['url'];
  1325. }
  1326. }
  1327. }
  1328. $sHtml .= "</ul>\n</li>\n</ul></div>";
  1329. foreach(array_reverse($aFavoriteActions) as $aAction)
  1330. {
  1331. $sHtml .= "<div class=\"actions_button\"><a href='{$aAction['url']}'>{$aAction['label']}</a></div>";
  1332. }
  1333. static $bPopupScript = false;
  1334. if (!$bPopupScript)
  1335. {
  1336. // Output this once per page...
  1337. $oPage->add_ready_script("$(\"div.itop_popup>ul\").popupmenu();\n");
  1338. $bPopupScript = true;
  1339. }
  1340. return $sHtml;
  1341. }
  1342. /**
  1343. * Appends a menu separator to the current list of actions
  1344. * @param Hash $aActions The current actions list
  1345. * @return void
  1346. */
  1347. protected function AddMenuSeparator(&$aActions)
  1348. {
  1349. $sSeparator = '<hr class="menu-separator"/>';
  1350. if (count($aActions) > 0) // Make sure that the separator is not the first item in the menu
  1351. {
  1352. $aKeys = array_keys($aActions);
  1353. $sLastKey = array_pop($aKeys);
  1354. if ($aActions[$sLastKey]['label'] != $sSeparator) // Make sure there are no 2 consecutive separators
  1355. {
  1356. $aActions['sep_'.(count($aActions)-1)] = array('label' => $sSeparator, 'url' => '');
  1357. }
  1358. }
  1359. }
  1360. }
  1361. ?>