displayblock.class.inc.php 41 KB

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