displayblock.class.inc.php 54 KB

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