displayblock.class.inc.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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('../application/webpage.class.inc.php');
  25. require_once('../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. $aExtraParams = array_merge($aExtraParams, $this->m_aParams);
  174. $aExtraParams['block_id'] = $sId;
  175. if (!$this->m_bAsynchronous)
  176. {
  177. // render now
  178. $oPage->add("<div id=\"$sId\" class=\"display_block\">\n");
  179. $this->RenderContent($oPage, $aExtraParams);
  180. $oPage->add("</div>\n");
  181. }
  182. else
  183. {
  184. // render it as an Ajax (asynchronous) call
  185. $sFilter = $this->m_oFilter->serialize();
  186. $oPage->add("<div id=\"$sId\" class=\"display_block loading\">\n");
  187. $oPage->p("<img src=\"../images/indicator_arrows.gif\"> Loading...");
  188. $oPage->add("</div>\n");
  189. $oPage->add('
  190. <script language="javascript">
  191. $.post("ajax.render.php?style='.$this->m_sStyle.'",
  192. { operation: "ajax", filter: "$sFilter" },
  193. function(data){
  194. $("#'.$sId.'").empty();
  195. $("#'.$sId.'").append(data);
  196. $("#'.$sId.'").removeClass("loading");
  197. }
  198. );
  199. </script>'); // TO DO: add support for $aExtraParams in asynchronous/Ajax mode
  200. }
  201. */
  202. }
  203. public function GetDisplay(WebPage $oPage, $sId, $aExtraParams = array())
  204. {
  205. $sHtml = '';
  206. $aExtraParams = array_merge($aExtraParams, $this->m_aParams);
  207. $aExtraParams['block_id'] = $sId;
  208. $sExtraParams = addslashes(str_replace('"', "'", json_encode($aExtraParams))); // JSON encode, change the style of the quotes and escape them
  209. $bAutoReload = false;
  210. if (isset($aExtraParams['auto_reload']))
  211. {
  212. switch($aExtraParams['auto_reload'])
  213. {
  214. case 'fast':
  215. $bAutoReload = true;
  216. $iReloadInterval = utils::GetConfig()->GetFastReloadInterval()*1000;
  217. break;
  218. case 'standard':
  219. case 'true':
  220. case true:
  221. $bAutoReload = true;
  222. $iReloadInterval = utils::GetConfig()->GetStandardReloadInterval()*1000;
  223. break;
  224. default:
  225. if (is_numeric($aExtraParams['auto_reload']))
  226. {
  227. $bAutoReload = true;
  228. $iReloadInterval = $aExtraParams['auto_reload']*1000;
  229. }
  230. else
  231. {
  232. // incorrect config, ignore it
  233. $bAutoReload = false;
  234. }
  235. }
  236. }
  237. $sFilter = $this->m_oFilter->serialize(); // Used either for asynchronous or auto_reload
  238. if (!$this->m_bAsynchronous)
  239. {
  240. // render now
  241. $sHtml .= "<div id=\"$sId\" class=\"display_block\">\n";
  242. $sHtml .= $this->GetRenderContent($oPage, $aExtraParams);
  243. $sHtml .= "</div>\n";
  244. }
  245. else
  246. {
  247. // render it as an Ajax (asynchronous) call
  248. $sHtml .= "<div id=\"$sId\" class=\"display_block loading\">\n";
  249. $sHtml .= $oPage->GetP("<img src=\"../images/indicator_arrows.gif\"> ".Dict::S('UI:Loading'));
  250. $sHtml .= "</div>\n";
  251. $sHtml .= '
  252. <script language="javascript">
  253. $.post("ajax.render.php?style='.$this->m_sStyle.'",
  254. { operation: "ajax", filter: "'.$sFilter.'", extra_params: "'.$sExtraParams.'" },
  255. function(data){
  256. $("#'.$sId.'").empty();
  257. $("#'.$sId.'").append(data);
  258. $("#'.$sId.'").removeClass("loading");
  259. // Check each "listResults" table for a checkbox in the first column and make the first column sortable only if it does not contain a checkbox in the header
  260. $("#'.$sId.'".listResults").each( function()
  261. {
  262. var table = $(this);
  263. var id = $(this).parent();
  264. var checkbox = (table.find(\'th:first :checkbox\').length > 0);
  265. if (checkbox)
  266. {
  267. // There is a checkbox in the first column, do not make it sortable
  268. table.tablesorter( { headers: { 0: {sorter: false}}, widgets: [\'myZebra\', \'truncatedList\']} ); // sortable and zebra tables
  269. }
  270. else
  271. {
  272. // There is NO checkbox in the first column, all columns are considered sortable
  273. table.tablesorter( { widgets: [\'myZebra\', \'truncatedList\']} ); // sortable and zebra tables
  274. }
  275. });
  276. }
  277. );
  278. </script>';
  279. }
  280. if ($bAutoReload)
  281. {
  282. $sHtml .= '
  283. <script language="javascript">
  284. setInterval("ReloadBlock(\''.$sId.'\', \''.$this->m_sStyle.'\', \''.$sFilter.'\', \"'.$sExtraParams.'\")", '.$iReloadInterval.');
  285. </script>';
  286. }
  287. return $sHtml;
  288. }
  289. public function RenderContent(WebPage $oPage, $aExtraParams = array())
  290. {
  291. $oPage->add($this->GetRenderContent($oPage, $aExtraParams));
  292. }
  293. public function GetRenderContent(WebPage $oPage, $aExtraParams = array())
  294. {
  295. $sHtml = '';
  296. // Add the extra params into the filter if they make sense for such a filter
  297. $bDoSearch = utils::ReadParam('dosearch', false);
  298. if ($this->m_oSet == null)
  299. {
  300. $aQueryParams = array();
  301. if (isset($aExtraParams['query_params']))
  302. {
  303. $aQueryParams = $aExtraParams['query_params'];
  304. }
  305. if ($this->m_sStyle != 'links')
  306. {
  307. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  308. foreach($aFilterCodes as $sFilterCode)
  309. {
  310. $sExternalFilterValue = utils::ReadParam($sFilterCode, '');
  311. if (isset($aExtraParams[$sFilterCode]))
  312. {
  313. $this->m_oFilter->AddCondition($sFilterCode, trim($aExtraParams[$sFilterCode])); // Use the default 'loose' operator
  314. }
  315. else if ($bDoSearch && $sExternalFilterValue != "")
  316. {
  317. $this->m_oFilter->AddCondition($sFilterCode, trim($sExternalFilterValue)); // Use the default 'loose' operator
  318. }
  319. }
  320. }
  321. $this->m_oSet = new CMDBObjectSet($this->m_oFilter, array(), $aQueryParams);
  322. }
  323. switch($this->m_sStyle)
  324. {
  325. case 'count':
  326. if (isset($aExtraParams['group_by']))
  327. {
  328. $sGroupByField = $aExtraParams['group_by'];
  329. $aGroupBy = array();
  330. $sLabels = array();
  331. while($oObj = $this->m_oSet->Fetch())
  332. {
  333. $sValue = $oObj->Get($sGroupByField);
  334. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  335. $sLabels[$sValue] = $oObj->GetAsHtml($sGroupByField);
  336. }
  337. $sFilter = urlencode($this->m_oFilter->serialize());
  338. $aData = array();
  339. $oAppContext = new ApplicationContext();
  340. $sParams = $oAppContext->GetForLink();
  341. foreach($aGroupBy as $sValue => $iCount)
  342. {
  343. $aData[] = array ( 'group' => $sLabels[$sValue],
  344. 'value' => "<a href=\"./UI.php?operation=search&dosearch=1&$sParams&filter=$sFilter&$sGroupByField=".urlencode($sValue)."\">$iCount</a>"); // TO DO: add the context information
  345. }
  346. $aAttribs =array(
  347. 'group' => array('label' => MetaModel::GetLabel($this->m_oFilter->GetClass(), $sGroupByField), 'description' => ''),
  348. 'value' => array('label'=> Dict::S('UI:GroupBy:Count'), 'description' => Dict::S('UI:GroupBy:Count+'))
  349. );
  350. $sHtml .= $oPage->GetTable($aAttribs, $aData);
  351. }
  352. else
  353. {
  354. // Simply count the number of elements in the set
  355. $iCount = $this->m_oSet->Count();
  356. $sFormat = 'UI:CountOfObjects';
  357. if (isset($aExtraParams['format']))
  358. {
  359. $sFormat = $aExtraParams['format'];
  360. }
  361. $sHtml .= $oPage->GetP(Dict::Format($sFormat, $iCount));
  362. }
  363. break;
  364. case 'join':
  365. $aDisplayAliases = isset($aExtraParams['display_aliases']) ? explode(',', $aExtraParams['display_aliases']): array();
  366. if (!isset($aExtraParams['group_by']))
  367. {
  368. $sHtml .= $oPage->GetP(Dict::S('UI:Error:MandatoryTemplateParameter_group_by'));
  369. }
  370. else
  371. {
  372. $aGroupByFields = array();
  373. $aGroupBy = explode(',', $aExtraParams['group_by']);
  374. foreach($aGroupBy as $sGroupBy)
  375. {
  376. $aMatches = array();
  377. if (preg_match('/^(.+)\.(.+)$/', $sGroupBy, $aMatches) > 0)
  378. {
  379. $aGroupByFields[] = array('alias' => $aMatches[1], 'att_code' => $aMatches[2]);
  380. }
  381. }
  382. if (count($aGroupByFields) == 0)
  383. {
  384. $sHtml .= $oPage->GetP(Dict::Format('UI:Error:InvalidGroupByFields', $aExtraParams['group_by']));
  385. }
  386. else
  387. {
  388. $aResults = array();
  389. $aCriteria = array();
  390. while($aObjects = $this->m_oSet->FetchAssoc())
  391. {
  392. $aKeys = array();
  393. foreach($aGroupByFields as $aField)
  394. {
  395. $aKeys[$aField['alias'].'.'.$aField['att_code']] = $aObjects[$aField['alias']]->Get($aField['att_code']);
  396. }
  397. $sCategory = implode($aKeys, ' ');
  398. $aResults[$sCategory][] = $aObjects;
  399. $aCriteria[$sCategory] = $aKeys;
  400. }
  401. $sHtml .= "<table>\n";
  402. // Construct a new (parametric) query that will return the content of this block
  403. $oBlockFilter = clone $this->m_oFilter;
  404. $aExpressions = array();
  405. $index = 0;
  406. foreach($aGroupByFields as $aField)
  407. {
  408. $aExpressions[] = '`'.$aField['alias'].'`.`'.$aField['att_code'].'` = :param'.$index++;
  409. }
  410. $sExpression = implode(' AND ', $aExpressions);
  411. $oExpression = Expression::FromOQL($sExpression);
  412. $oBlockFilter->AddConditionExpression($oExpression);
  413. $aExtraParams['menu'] = false;
  414. foreach($aResults as $sCategory => $aObjects)
  415. {
  416. $sHtml .= "<tr><td><h1>$sCategory</h1></td></tr>\n";
  417. if (count($aDisplayAliases) == 1)
  418. {
  419. $aSimpleArray = array();
  420. foreach($aObjects as $aRow)
  421. {
  422. $aSimpleArray[] = $aRow[$aDisplayAliases[0]];
  423. }
  424. $oSet = CMDBObjectSet::FromArray($this->m_oFilter->GetClass(), $aSimpleArray);
  425. $sHtml .= "<tr><td>".cmdbAbstractObject::GetDisplaySet($oPage, $oSet, $aExtraParams)."</td></tr>\n";
  426. }
  427. else
  428. {
  429. $index = 0;
  430. $aArgs = array();
  431. foreach($aGroupByFields as $aField)
  432. {
  433. $aArgs['param'.$index] = $aCriteria[$sCategory][$aField['alias'].'.'.$aField['att_code']];
  434. $index++;
  435. }
  436. $oSet = new CMDBObjectSet($oBlockFilter, array(), $aArgs);
  437. $sHtml .= "<tr><td>".cmdbAbstractObject::GetDisplayExtendedSet($oPage, $oSet, $aExtraParams)."</td></tr>\n";
  438. }
  439. }
  440. $sHtml .= "</table>\n";
  441. }
  442. }
  443. break;
  444. case 'list':
  445. $aClasses = $this->m_oSet->GetSelectedClasses();
  446. $aAuthorizedClasses = array();
  447. if (count($aClasses) > 1)
  448. {
  449. // Check the classes that can be read (i.e authorized) by this user...
  450. foreach($aClasses as $sAlias => $sClassName)
  451. {
  452. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_READ, $this->m_oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  453. {
  454. $aAuthorizedClasses[$sAlias] = $sClassName;
  455. }
  456. }
  457. if (count($aAuthorizedClasses) > 0)
  458. {
  459. if($this->m_oSet->Count() > 0)
  460. {
  461. $sHtml .= cmdbAbstractObject::GetDisplayExtendedSet($oPage, $this->m_oSet, $aExtraParams);
  462. }
  463. else
  464. {
  465. // Empty set
  466. $sHtml .= $oPage->GetP(Dict::S('UI:NoObjectToDisplay'));
  467. }
  468. }
  469. else
  470. {
  471. // Not authorized
  472. $sHtml .= $oPage->GetP(Dict::S('UI:NoObjectToDisplay'));
  473. }
  474. }
  475. else
  476. {
  477. // The list is made of only 1 class of objects, actions on the list are possible
  478. if ( ($this->m_oSet->Count()> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) )
  479. {
  480. $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $aExtraParams);
  481. }
  482. else
  483. {
  484. $sHtml .= $oPage->GetP(Dict::S('UI:NoObjectToDisplay'));
  485. $sClass = $this->m_oFilter->GetClass();
  486. $bDisplayMenu = isset($aExtraParams['menu']) ? $aExtraParams['menu'] == true : true;
  487. if ($bDisplayMenu)
  488. {
  489. if ((UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  490. {
  491. $oAppContext = new ApplicationContext();
  492. $sParams = $oAppContext->GetForLink();
  493. // 1:n links, populate the target object as a default value when creating a new linked object
  494. if (isset($aExtraParams['target_attr']))
  495. {
  496. $aExtraParams['default'][$aExtraParams['target_attr']] = $aExtraParams['object_id'];
  497. }
  498. $sDefault = '';
  499. if (!empty($aExtraParams['default']))
  500. {
  501. foreach($aExtraParams['default'] as $sKey => $sValue)
  502. {
  503. $sDefault.= "&default[$sKey]=$sValue";
  504. }
  505. }
  506. $sHtml .= $oPage->GetP("<a href=\"./UI.php?operation=new&class=$sClass&$sParams{$sDefault}\">".Dict::Format('UI:ClickToCreateNew', Metamodel::GetName($sClass))."</a>\n");
  507. }
  508. }
  509. }
  510. }
  511. break;
  512. case 'links':
  513. //$bDashboardMode = isset($aExtraParams['dashboard']) ? ($aExtraParams['dashboard'] == 'true') : false;
  514. //$bSelectMode = isset($aExtraParams['select']) ? ($aExtraParams['select'] == 'true') : false;
  515. if ( ($this->m_oSet->Count()> 0) && (UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_READ, $this->m_oSet) == UR_ALLOWED_YES) )
  516. {
  517. //$sLinkage = isset($aExtraParams['linkage']) ? $aExtraParams['linkage'] : '';
  518. $sHtml .= cmdbAbstractObject::GetDisplaySet($oPage, $this->m_oSet, $aExtraParams);
  519. }
  520. else
  521. {
  522. $sClass = $this->m_oFilter->GetClass();
  523. $oAttDef = MetaModel::GetAttributeDef($sClass, $this->m_aParams['target_attr']);
  524. $sTargetClass = $oAttDef->GetTargetClass();
  525. $sHtml .= $oPage->GetP(Dict::Format('UI:NoObject_Class_ToDisplay', MetaModel::GetName($sTargetClass)));
  526. $bDisplayMenu = isset($this->m_aParams['menu']) ? $this->m_aParams['menu'] == true : true;
  527. if ($bDisplayMenu)
  528. {
  529. if ((UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES))
  530. {
  531. $oAppContext = new ApplicationContext();
  532. $sParams = $oAppContext->GetForLink();
  533. $sDefaults = '';
  534. if (isset($this->m_aParams['default']))
  535. {
  536. foreach($this->m_aParams['default'] as $sName => $sValue)
  537. {
  538. $sDefaults .= '&'.urlencode($sName).'='.urlencode($sValue);
  539. }
  540. }
  541. $sHtml .= $oPage->GetP("<a href=\"../pages/UI.php?operation=modify_links&class=$sClass&sParams&link_attr=".$aExtraParams['link_attr']."&id=".$aExtraParams['object_id']."&target_class=$sTargetClass&addObjects=true$sDefaults\">".Dict::Format('UI:ClickToCreateNew', Metamodel::GetName($sClass))."</a>\n");
  542. }
  543. }
  544. }
  545. break;
  546. case 'details':
  547. while($oObj = $this->m_oSet->Fetch())
  548. {
  549. $sHtml .= $oObj->GetDetails($oPage); // Still used ???
  550. }
  551. break;
  552. case 'actions':
  553. $sClass = $this->m_oFilter->GetClass();
  554. $oAppContext = new ApplicationContext();
  555. $bContextFilter = isset($aExtraParams['context_filter']) ? isset($aExtraParams['context_filter']) != 0 : false;
  556. if ($bContextFilter)
  557. {
  558. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  559. foreach($oAppContext->GetNames() as $sFilterCode)
  560. {
  561. $sContextParamValue = trim(utils::ReadParam($sFilterCode, null));
  562. if (!is_null($sContextParamValue) && ! empty($sContextParamValue) && MetaModel::IsValidFilterCode($sClass, $sFilterCode))
  563. {
  564. $this->m_oFilter->AddCondition($sFilterCode, $sContextParamValue); // Use the default 'loose' operator
  565. }
  566. }
  567. $aQueryParams = array();
  568. if (isset($aExtraParams['query_params']))
  569. {
  570. $aQueryParams = $aExtraParams['query_params'];
  571. }
  572. $this->m_oSet = new CMDBObjectSet($this->m_oFilter, array(), $aQueryParams);
  573. }
  574. $iCount = $this->m_oSet->Count();
  575. $sHyperlink = '../pages/UI.php?operation=search&filter='.$this->m_oFilter->serialize();
  576. $sHtml .= '<p><a class="actions" href="'.$sHyperlink.'">';
  577. $sHtml .= MetaModel::GetClassIcon($sClass, true, 'float;left;margin-right:10px;');
  578. $sHtml .= MetaModel::GetName($sClass).': '.$iCount.'</a></p>';
  579. $sParams = $oAppContext->GetForLink();
  580. $sHtml .= '<p>';
  581. if (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY))
  582. {
  583. $sHtml .= "<a href=\"../pages/UI.php?operation=new&class={$sClass}&$sParams\">".Dict::Format('UI:ClickToCreateNew', MetaModel::GetName($sClass))."</a><br/>\n";
  584. }
  585. $sHtml .= "<a href=\"../pages/UI.php?operation=search_form&class={$sClass}&$sParams\">".Dict::Format('UI:SearchFor_Class', MetaModel::GetName($sClass))."</a>\n";
  586. $sHtml .= '</p>';
  587. break;
  588. case 'summary':
  589. $sClass = $this->m_oFilter->GetClass();
  590. $oAppContext = new ApplicationContext();
  591. $sTitle = isset($aExtraParams['title[block]']) ? $aExtraParams['title[block]'] : '';
  592. $sLabel = isset($aExtraParams['label[block]']) ? $aExtraParams['label[block]'] : '';
  593. $sStateAttrCode = isset($aExtraParams['status[block]']) ? $aExtraParams['status[block]'] : 'status';
  594. $sStatesList = isset($aExtraParams['status_codes[block]']) ? $aExtraParams['status_codes[block]'] : '';
  595. $bContextFilter = isset($aExtraParams['context_filter']) ? isset($aExtraParams['context_filter']) != 0 : false;
  596. if ($bContextFilter)
  597. {
  598. $aFilterCodes = array_keys(MetaModel::GetClassFilterDefs($this->m_oFilter->GetClass()));
  599. foreach($oAppContext->GetNames() as $sFilterCode)
  600. {
  601. $sContextParamValue = trim(utils::ReadParam($sFilterCode, null));
  602. if (!is_null($sContextParamValue) && ! empty($sContextParamValue) && MetaModel::IsValidFilterCode($sClass, $sFilterCode))
  603. {
  604. $this->m_oFilter->AddCondition($sFilterCode, $sContextParamValue); // Use the default 'loose' operator
  605. }
  606. }
  607. $aQueryParams = array();
  608. if (isset($aExtraParams['query_params']))
  609. {
  610. $aQueryParams = $aExtraParams['query_params'];
  611. }
  612. $this->m_oSet = new CMDBObjectSet($this->m_oFilter, array(), $aQueryParams);
  613. }
  614. // Summary details
  615. $aCounts = array();
  616. $aStateLabels = array();
  617. if (!empty($sStateAttrCode) && !empty($sStatesList))
  618. {
  619. $aStates = explode(',', $sStatesList);
  620. $oAttDef = MetaModel::GetAttributeDef($sClass, $sStateAttrCode);
  621. foreach($aStates as $sStateValue)
  622. {
  623. $oFilter = clone($this->m_oFilter);
  624. $oFilter->AddCondition($sStateAttrCode, $sStateValue, '=');
  625. $oSet = new DBObjectSet($oFilter);
  626. $aCounts[$sStateValue] = $oSet->Count();
  627. $aStateLabels[$sStateValue] = Dict::S("Class:".$oAttDef->GetHostClass()."/Attribute:$sStateAttrCode/Value:$sStateValue");
  628. if ($aCounts[$sStateValue] == 0)
  629. {
  630. $aCounts[$sStateValue] = '-';
  631. }
  632. else
  633. {
  634. $sHyperlink = '../pages/UI.php?operation=search&filter='.$oFilter->serialize();
  635. $aCounts[$sStateValue] = "<a href=\"$sHyperlink\">{$aCounts[$sStateValue]}</a>";
  636. }
  637. }
  638. }
  639. $sHtml .= '<div class="summary-details"><table><tr><th>'.implode('</th><th>', $aStateLabels).'</th></tr>';
  640. $sHtml .= '<tr><td>'.implode('</td><td>', $aCounts).'</td></tr></table></div>';
  641. // Title & summary
  642. $iCount = $this->m_oSet->Count();
  643. $sHyperlink = '../pages/UI.php?operation=search&filter='.$this->m_oFilter->serialize();
  644. $sHtml .= '<h1>'.Dict::S(str_replace('_', ':', $sTitle)).'</h1>';
  645. $sHtml .= '<a class="summary" href="'.$sHyperlink.'">'.Dict::Format(str_replace('_', ':', $sLabel), $iCount).'</a>';
  646. break;
  647. case 'bare_details':
  648. while($oObj = $this->m_oSet->Fetch())
  649. {
  650. $sHtml .= $oObj->GetBareProperties($oPage);
  651. }
  652. break;
  653. case 'csv':
  654. $sHtml .= "<textarea style=\"width:95%;height:98%\">\n";
  655. $sHtml .= cmdbAbstractObject::GetSetAsCSV($this->m_oSet);
  656. $sHtml .= "</textarea>\n";
  657. break;
  658. case 'modify':
  659. if ((UserRights::IsActionAllowed($this->m_oSet->GetClass(), UR_ACTION_MODIFY, $this->m_oSet) == UR_ALLOWED_YES))
  660. {
  661. while($oObj = $this->m_oSet->Fetch())
  662. {
  663. $sHtml .= $oObj->GetModifyForm($oPage);
  664. }
  665. }
  666. break;
  667. case 'search':
  668. static $iSearchSectionId = 1;
  669. $sStyle = (isset($aExtraParams['open']) && ($aExtraParams['open'] == 'true')) ? 'SearchDrawer' : 'SearchDrawer DrawerClosed';
  670. $sHtml .= "<div id=\"Search_$iSearchSectionId\" class=\"$sStyle\">\n";
  671. $oPage->add_ready_script(
  672. <<<EOF
  673. $("#LnkSearch_$iSearchSectionId").click( function() {
  674. $("#Search_$iSearchSectionId").slideToggle('normal', function() { $("#Search_$iSearchSectionId").parent().resize(); } );
  675. $("#LnkSearch_$iSearchSectionId").toggleClass('open');
  676. });
  677. EOF
  678. );
  679. $sHtml .= cmdbAbstractObject::GetSearchForm($oPage, $this->m_oSet, $aExtraParams);
  680. $sHtml .= "</div>\n";
  681. $sHtml .= "<div class=\"HRDrawer\"></div>\n";
  682. $sHtml .= "<div id=\"LnkSearch_$iSearchSectionId\" class=\"DrawerHandle\">".Dict::S('UI:SearchToggle')."</div>\n";
  683. $iSearchSectionId++;
  684. break;
  685. case 'open_flash_chart':
  686. static $iChartCounter = 0;
  687. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  688. $sTitle = isset($aExtraParams['chart_title']) ? $aExtraParams['chart_title'] : '';
  689. $sGroupBy = isset($aExtraParams['group_by']) ? $aExtraParams['group_by'] : '';
  690. $sFilter = $this->m_oFilter->serialize();
  691. $sHtml .= "<div id=\"my_chart_{$iChartCounter}\">If the chart does not display, <a href=\"http://get.adobe.com/flash/\" target=\"_blank\">install Flash</a></div>\n";
  692. $oPage->add_script("function ofc_resize(left, width, top, height) { /* do nothing special */ }");
  693. $oPage->add_ready_script("swfobject.embedSWF(\"../images/open-flash-chart.swf\", \"my_chart_{$iChartCounter}\", \"100%\", \"300\",\"9.0.0\", \"expressInstall.swf\",
  694. {\"data-file\":\"".urlencode("../pages/ajax.render.php?operation=open_flash_chart&params[group_by]=$sGroupBy&params[chart_type]=$sChartType&params[chart_title]=$sTitle&filter=".$sFilter)."\"}, {wmode: 'transparent'} );\n");
  695. $iChartCounter++;
  696. break;
  697. case 'open_flash_chart_ajax':
  698. include '../pages/php-ofc-library/open-flash-chart.php';
  699. $sChartType = isset($aExtraParams['chart_type']) ? $aExtraParams['chart_type'] : 'pie';
  700. $oChart = new open_flash_chart();
  701. switch($sChartType)
  702. {
  703. case 'bars':
  704. $oChartElement = new bar_glass();
  705. if (isset($aExtraParams['group_by']))
  706. {
  707. $sGroupByField = $aExtraParams['group_by'];
  708. $aGroupBy = array();
  709. while($oObj = $this->m_oSet->Fetch())
  710. {
  711. $sValue = $oObj->Get($sGroupByField);
  712. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  713. }
  714. $sFilter = urlencode($this->m_oFilter->serialize());
  715. $aData = array();
  716. $aLabels = array();
  717. foreach($aGroupBy as $sValue => $iValue)
  718. {
  719. $aData[] = $iValue;
  720. $aLabels[] = $sValue;
  721. }
  722. $maxValue = max($aData);
  723. $oYAxis = new y_axis();
  724. $aMagicValues = array(1,2,5,10);
  725. $iMultiplier = 1;
  726. $index = 0;
  727. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  728. while($maxValue > $iTop)
  729. {
  730. $index++;
  731. $iTop = $aMagicValues[$index % count($aMagicValues)]*$iMultiplier;
  732. if (($index % count($aMagicValues)) == 0)
  733. {
  734. $iMultiplier = $iMultiplier * 10;
  735. }
  736. }
  737. //echo "oYAxis->set_range(0, $iTop, $iMultiplier);\n";
  738. $oYAxis->set_range(0, $iTop, $iMultiplier);
  739. $oChart->set_y_axis( $oYAxis );
  740. $oChartElement->set_values( $aData );
  741. $oXAxis = new x_axis();
  742. $oXLabels = new x_axis_labels();
  743. // set them vertical
  744. $oXLabels->set_vertical();
  745. // set the label text
  746. $oXLabels->set_labels($aLabels);
  747. // Add the X Axis Labels to the X Axis
  748. $oXAxis->set_labels( $oXLabels );
  749. $oChart->set_x_axis( $oXAxis );
  750. }
  751. break;
  752. case 'pie':
  753. default:
  754. $oChartElement = new pie();
  755. $oChartElement->set_start_angle( 35 );
  756. $oChartElement->set_animate( true );
  757. $oChartElement->set_tooltip( '#label# - #val# (#percent#)' );
  758. $oChartElement->set_colours( array('#FF8A00', '#909980', '#2C2B33', '#CCC08D', '#596664') );
  759. if (isset($aExtraParams['group_by']))
  760. {
  761. $sGroupByField = $aExtraParams['group_by'];
  762. $aGroupBy = array();
  763. while($oObj = $this->m_oSet->Fetch())
  764. {
  765. $sValue = $oObj->Get($sGroupByField);
  766. $aGroupBy[$sValue] = isset($aGroupBy[$sValue]) ? $aGroupBy[$sValue]+1 : 1;
  767. }
  768. $sFilter = urlencode($this->m_oFilter->serialize());
  769. $aData = array();
  770. foreach($aGroupBy as $sValue => $iValue)
  771. {
  772. $aData[] = new pie_value($iValue, $sValue); //@@ BUG: not passed via ajax !!!
  773. }
  774. $oChartElement->set_values( $aData );
  775. $oChart->x_axis = null;
  776. }
  777. }
  778. if (isset($aExtraParams['chart_title']))
  779. {
  780. $oTitle = new title( Dict::S($aExtraParams['chart_title']) );
  781. $oChart->set_title( $oTitle );
  782. }
  783. $oChart->set_bg_colour('#FFFFFF');
  784. $oChart->add_element( $oChartElement );
  785. $sHtml = $oChart->toPrettyString();
  786. break;
  787. default:
  788. // Unsupported style, do nothing.
  789. $sHtml .= Dict::format('UI:Error:UnsupportedStyleOfBlock', $this->m_sStyle);
  790. }
  791. return $sHtml;
  792. }
  793. }
  794. /**
  795. * Helper class to manage 'blocks' of HTML pieces that are parts of a page and contain some list of cmdb objects
  796. *
  797. * Each block is actually rendered as a <div></div> tag that can be rendered synchronously
  798. * or as a piece of Javascript/JQuery/Ajax that will get its content from another page (ajax.render.php).
  799. * The list of cmdbObjects to be displayed into the block is defined by a filter
  800. * Right now the type of display is either: list, count or details
  801. * - list produces a table listing the objects
  802. * - count produces a paragraphs with a sentence saying 'cont' objects found
  803. * - details display (as table) the details of each object found (best if only one)
  804. */
  805. class HistoryBlock extends DisplayBlock
  806. {
  807. public function GetRenderContent(WebPage $oPage, $aExtraParams = array())
  808. {
  809. $sHtml = '';
  810. $oSet = new CMDBObjectSet($this->m_oFilter, array('date'=>false));
  811. $sHtml .= "<!-- filter: ".($this->m_oFilter->ToOQL())."-->\n";
  812. switch($this->m_sStyle)
  813. {
  814. case 'toggle':
  815. // First the latest change that the user is allowed to see
  816. do
  817. {
  818. $oLatestChangeOp = $oSet->Fetch();
  819. }
  820. while(is_object($oLatestChangeOp) && ($oLatestChangeOp->GetDescription() == ''));
  821. if (is_object($oLatestChangeOp))
  822. {
  823. // There is one change in the list... only when the object has been created !
  824. $sDate = $oLatestChangeOp->GetAsHTML('date');
  825. $oChange = MetaModel::GetObject('CMDBChange', $oLatestChangeOp->Get('change'));
  826. $sUserInfo = $oChange->GetAsHTML('userinfo');
  827. $sHtml .= $oPage->GetStartCollapsibleSection(Dict::Format('UI:History:LastModified_On_By', $sDate, $sUserInfo));
  828. $sHtml .= $this->GetHistoryTable($oPage, $oSet);
  829. $sHtml .= $oPage->GetEndCollapsibleSection();
  830. }
  831. break;
  832. case 'table':
  833. default:
  834. $sHtml .= $this->GetHistoryTable($oPage, $oSet);
  835. }
  836. return $sHtml;
  837. }
  838. protected function GetHistoryTable(WebPage $oPage, DBObjectSet $oSet)
  839. {
  840. $sHtml = '';
  841. // First the latest change that the user is allowed to see
  842. $oSet->Rewind(); // Reset the pointer to the beginning of the set
  843. $aChanges = array();
  844. while($oChangeOp = $oSet->Fetch())
  845. {
  846. $sChangeDescription = $oChangeOp->GetDescription();
  847. if ($sChangeDescription != '')
  848. {
  849. // The change is visible for the current user
  850. $changeId = $oChangeOp->Get('change');
  851. $aChanges[$changeId]['date'] = $oChangeOp->Get('date');
  852. $aChanges[$changeId]['userinfo'] = $oChangeOp->Get('userinfo');
  853. if (!isset($aChanges[$changeId]['log']))
  854. {
  855. $aChanges[$changeId]['log'] = array();
  856. }
  857. $aChanges[$changeId]['log'][] = $sChangeDescription;
  858. }
  859. }
  860. $aAttribs = array('date' => array('label' => Dict::S('UI:History:Date'), 'description' => Dict::S('UI:History:Date+')),
  861. 'userinfo' => array('label' => Dict::S('UI:History:User'), 'description' => Dict::S('UI:History:User+')),
  862. 'log' => array('label' => Dict::S('UI:History:Changes'), 'description' => Dict::S('UI:History:Changes+')),
  863. );
  864. $aValues = array();
  865. foreach($aChanges as $aChange)
  866. {
  867. $aValues[] = array('date' => $aChange['date'], 'userinfo' => $aChange['userinfo'], 'log' => "<ul><li>".implode('</li><li>', $aChange['log'])."</li></ul>");
  868. }
  869. $sHtml .= $oPage->GetTable($aAttribs, $aValues);
  870. return $sHtml;
  871. }
  872. }
  873. class MenuBlock extends DisplayBlock
  874. {
  875. /**
  876. * Renders the "Actions" popup menu for the given set of objects
  877. *
  878. * Note that the menu links containing (or ending) with a hash (#) will have their fragment
  879. * part (whatever is after the hash) dynamically replaced (by javascript) when the menu is
  880. * displayed, to correspond to the current hash/fragment in the page. This allows modifying
  881. * an object in with the same tab active by default as the tab that was active when selecting
  882. * the "Modify..." action.
  883. */
  884. public function GetRenderContent(WebPage $oPage, $aExtraParams = array())
  885. {
  886. $sHtml = '';
  887. $oAppContext = new ApplicationContext();
  888. $sContext = $oAppContext->GetForLink();
  889. $sClass = $this->m_oFilter->GetClass();
  890. $oSet = new CMDBObjectSet($this->m_oFilter);
  891. $sFilter = $this->m_oFilter->serialize();
  892. $aActions = array();
  893. $sUIPage = cmdbAbstractObject::ComputeUIPage($sClass);
  894. // 1:n links, populate the target object as a default value when creating a new linked object
  895. if (isset($aExtraParams['target_attr']))
  896. {
  897. $aExtraParams['default'][$aExtraParams['target_attr']] = $aExtraParams['object_id'];
  898. }
  899. $sDefault = '';
  900. if (!empty($aExtraParams['default']))
  901. {
  902. foreach($aExtraParams['default'] as $sKey => $sValue)
  903. {
  904. $sDefault.= "&default[$sKey]=$sValue";
  905. }
  906. }
  907. switch($oSet->Count())
  908. {
  909. case 0:
  910. // No object in the set, the only possible action is "new"
  911. $bIsModifyAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY) == UR_ALLOWED_YES);
  912. if ($bIsModifyAllowed) { $aActions[] = array ('label' => Dict::S('UI:Menu:New'), 'url' => "../page/$sUIPage?operation=new&class=$sClass&$sContext{$sDefault}"); }
  913. break;
  914. case 1:
  915. $oObj = $oSet->Fetch();
  916. $id = $oObj->GetKey();
  917. $bIsModifyAllowed = (UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet) == UR_ALLOWED_YES);
  918. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  919. $bIsBulkModifyAllowed = (!MetaModel::IsAbstract($sClass)) && UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  920. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  921. // Just one object in the set, possible actions are "new / clone / modify and delete"
  922. if (!isset($aExtraParams['link_attr']))
  923. {
  924. $sUrl = utils::GetAbsoluteUrl(false);
  925. if ($bIsModifyAllowed) { $aActions[] = array ('label' => Dict::S('UI:Menu:Modify'), 'url' => "../pages/$sUIPage?operation=modify&class=$sClass&id=$id&$sContext#"); }
  926. if ($bIsModifyAllowed) { $aActions[] = array ('label' => Dict::S('UI:Menu:New'), 'url' => "../pages/$sUIPage?operation=new&class=$sClass&$sContext{$sDefault}"); }
  927. if ($bIsDeleteAllowed) { $aActions[] = array ('label' => Dict::S('UI:Menu:Delete'), 'url' => "../pages/$sUIPage?operation=delete&class=$sClass&id=$id&$sContext"); }
  928. // Transitions / Stimuli
  929. $aTransitions = $oObj->EnumTransitions();
  930. if (count($aTransitions))
  931. {
  932. $aActions[] = array('label' => '<hr>', 'url' => ""); // Separator
  933. $aStimuli = Metamodel::EnumStimuli($sClass);
  934. foreach($aTransitions as $sStimulusCode => $aTransitionDef)
  935. {
  936. $iActionAllowed = (get_class($aStimuli[$sStimulusCode]) == 'StimulusUserAction') ? UserRights::IsStimulusAllowed($sClass, $sStimulusCode, $oSet) : UR_ALLOWED_NO;
  937. switch($iActionAllowed)
  938. {
  939. case UR_ALLOWED_YES:
  940. $aActions[] = array('label' => $aStimuli[$sStimulusCode]->GetLabel(), 'url' => "../pages/UI.php?operation=stimulus&stimulus=$sStimulusCode&class=$sClass&id=$id&$sContext");
  941. break;
  942. default:
  943. // Do nothing
  944. }
  945. }
  946. }
  947. // Relations...
  948. $aRelations = MetaModel::EnumRelations($sClass);
  949. if (count($aRelations))
  950. {
  951. $aActions[] = array('label' => '<hr>', 'url' => ""); // Separator
  952. foreach($aRelations as $sRelationCode)
  953. {
  954. $aActions[] = array ('label' => MetaModel::GetRelationVerbUp($sRelationCode), 'url' => "../pages/$sUIPage?operation=swf_navigator&relation=$sRelationCode&class=$sClass&id=$id&$sContext");
  955. }
  956. }
  957. $aActions[] = array('label' => '<hr>', 'url' => ""); // Separator
  958. // Static menus: Email this page & CSV Export
  959. $aActions[] = array ('label' => Dict::S('UI:Menu:EMail'), 'url' => "mailto:?subject=".$oObj->GetName()."&body=".urlencode("$sUrl?operation=details&class=$sClass&id=$id&$sContext"));
  960. $aActions[] = array ('label' => Dict::S('UI:Menu:CSVExport'), 'url' => "../pages/$sUIPage?operation=search&filter=$sFilter&format=csv&$sContext");
  961. }
  962. else
  963. {
  964. // List of links, the only actions are 'Add...' and 'Manage...'
  965. $id = $aExtraParams['object_id'];
  966. $sTargetAttr = $aExtraParams['target_attr'];
  967. $oAttDef = MetaModel::GetAttributeDef($sClass, $sTargetAttr);
  968. $sTargetClass = $oAttDef->GetTargetClass();
  969. if ($bIsModifyAllowed) { $aActions[] = array ('label' => Dict::S('UI:Menu:Add'), 'url' => "../pages/$sUIPage?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&target_class=$sTargetClass&id=$id&addObjects=true&$sContext"); }
  970. if ($bIsModifyAllowed) { $aActions[] = array ('label' => Dict::S('UI:Menu:Manage'), 'url' => "../pages/$sUIPage?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&target_class=$sTargetClass&id=$id&sContext"); }
  971. }
  972. break;
  973. default:
  974. // Check rights
  975. // New / Modify
  976. $bIsModifyAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_MODIFY, $oSet);
  977. $bIsBulkModifyAllowed = (!MetaModel::IsAbstract($sClass)) && UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_MODIFY, $oSet);
  978. $bIsBulkDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_DELETE, $oSet);
  979. if (isset($aExtraParams['link_attr']))
  980. {
  981. $id = $aExtraParams['object_id'];
  982. $sTargetAttr = $aExtraParams['target_attr'];
  983. $oAttDef = MetaModel::GetAttributeDef($sClass, $sTargetAttr);
  984. $sTargetClass = $oAttDef->GetTargetClass();
  985. $bIsDeleteAllowed = UserRights::IsActionAllowed($sClass, UR_ACTION_DELETE, $oSet);
  986. if ($bIsModifyAllowed) { $aActions[] = array ('label' => Dict::S('UI:Menu:Add'), 'url' => "../pages/$sUIPage?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&target_class=$sTargetClass&id=$id&addObjects=true&$sContext"); }
  987. if ($bIsBulkModifyAllowed) { $aActions[] = array ('label' => Dict::S('UI:Menu:Manage'), 'url' => "../pages/$sUIPage?operation=modify_links&class=$sClass&link_attr=".$aExtraParams['link_attr']."&target_class=$sTargetClass&id=$id&sContext"); }
  988. //if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => 'Remove All...', 'url' => "#"); }
  989. }
  990. else
  991. {
  992. // many objects in the set, possible actions are: new / modify all / delete all
  993. $sUrl = utils::GetAbsoluteUrl();
  994. if ($bIsModifyAllowed) { $aActions[] = array ('label' => Dict::S('UI:Menu:New'), 'url' => "../pages/$sUIPage?operation=new&class=$sClass&$sContext{$sDefault}"); }
  995. //if ($bIsBulkModifyAllowed) { $aActions[] = array ('label' => 'Modify All...', 'url' => "../pages/$sUIPage?operation=modify_all&filter=$sFilter&$sContext"); }
  996. if ($bIsBulkDeleteAllowed) { $aActions[] = array ('label' => Dict::S('UI:Menu:BulkDelete'), 'url' => "../pages/$sUIPage?operation=select_for_deletion&filter=$sFilter&$sContext"); }
  997. $aActions[] = array('label' => '<hr>', 'url' => ""); // Separator
  998. $aActions[] = array ('label' => Dict::S('UI:Menu:EMail'), 'url' => "mailto:?subject=".$oSet->GetFilter()->__DescribeHTML()."&body=".urlencode("$sUrl?operation=search&filter=$sFilter&$sContext"));
  999. $aActions[] = array ('label' => Dict::S('UI:Menu:CSVExport'), 'url' => "../pages/$sUIPage?operation=search&filter=$sFilter&format=csv&$sContext");
  1000. }
  1001. }
  1002. $sHtml .= "<div class=\"itop_popup\"><ul>\n<li>".Dict::S('UI:Menu:Actions')."\n<ul>\n";
  1003. foreach ($aActions as $aAction)
  1004. {
  1005. $sClass = isset($aAction['class']) ? " class=\"{$aAction['class']}\"" : "";
  1006. if (empty($aAction['url']))
  1007. {
  1008. $sHtml .= "<li>{$aAction['label']}</li>\n";
  1009. }
  1010. else
  1011. {
  1012. $sHtml .= "<li><a href=\"{$aAction['url']}\"$sClass>{$aAction['label']}</a></li>\n";
  1013. }
  1014. }
  1015. $sHtml .= "</ul>\n</li>\n</ul></div>\n";
  1016. static $bPopupScript = false;
  1017. if (!$bPopupScript)
  1018. {
  1019. // Output this once per page...
  1020. $oPage->add_ready_script("$(\"div.itop_popup>ul\").popupmenu();\n");
  1021. $bPopupScript = true;
  1022. }
  1023. return $sHtml;
  1024. }
  1025. }
  1026. ?>