applicationextension.inc.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. <?php
  2. // Copyright (C) 2010-2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Management of application plugins
  20. *
  21. * Definition of interfaces that can be implemented to customize iTop.
  22. * You may implement such interfaces in a module file (e.g. main.mymodule.php)
  23. *
  24. * @package Extensibility
  25. * @copyright Copyright (C) 2010-2012 Combodo SARL
  26. * @license http://opensource.org/licenses/AGPL-3.0
  27. * @api
  28. */
  29. /**
  30. * Implement this interface to change the behavior of the GUI for some objects.
  31. *
  32. * All methods are invoked by iTop for a given object. There are basically two usages:
  33. *
  34. * 1) To tweak the form of an object, you will have to implement a specific behavior within:
  35. *
  36. * * OnDisplayProperties (bEditMode = true)
  37. * * OnFormSubmit
  38. * * OnFormCancel
  39. *
  40. * 2) To tune the display of the object details, you can use:
  41. *
  42. * * OnDisplayProperties
  43. * * OnDisplayRelations
  44. * * GetIcon
  45. * * GetHilightClass
  46. *
  47. * Please note that some of the APIs can be called several times for a single page displayed.
  48. * Therefore it is not recommended to perform too many operations, such as querying the database.
  49. * A recommended pattern is to cache data by the mean of static members.
  50. *
  51. * @package Extensibility
  52. * @api
  53. */
  54. interface iApplicationUIExtension
  55. {
  56. /**
  57. * Invoked when an object is being displayed (wiew or edit)
  58. *
  59. * The method is called right after the main tab has been displayed.
  60. * You can add output to the page, either to change the display, or to add a form input
  61. *
  62. * Example:
  63. * <code>
  64. * if ($bEditMode)
  65. * {
  66. * $oPage->p('Age of the captain: &lt;input type="text" name="captain_age"/&gt;');
  67. * }
  68. * else
  69. * {
  70. * $oPage->p('Age of the captain: '.$iCaptainAge);
  71. * }
  72. * </code>
  73. *
  74. * @param DBObject $oObject The object being displayed
  75. * @param WebPage $oPage The output context
  76. * @param boolean $bEditMode True if the edition form is being displayed
  77. * @return void
  78. */
  79. public function OnDisplayProperties($oObject, WebPage $oPage, $bEditMode = false);
  80. /**
  81. * Invoked when an object is being displayed (wiew or edit)
  82. *
  83. * The method is called rigth after all the tabs have been displayed
  84. *
  85. * @param DBObject $oObject The object being displayed
  86. * @param WebPage $oPage The output context
  87. * @param boolean $bEditMode True if the edition form is being displayed
  88. * @return void
  89. */
  90. public function OnDisplayRelations($oObject, WebPage $oPage, $bEditMode = false);
  91. /**
  92. * Invoked when the end-user clicks on Modify from the object edition form
  93. *
  94. * The method is called after the changes from the standard form have been
  95. * taken into account, and before saving the changes into the database.
  96. *
  97. * @param DBObject $oObject The object being edited
  98. * @param string $sFormPrefix Prefix given to the HTML form inputs
  99. * @return void
  100. */
  101. public function OnFormSubmit($oObject, $sFormPrefix = '');
  102. /**
  103. * Invoked when the end-user clicks on Cancel from the object edition form
  104. *
  105. * Implement here any cleanup. This is necessary when you have injected some
  106. * javascript into the edition form, and if that code requires to store temporary data
  107. * (this is the case when a file must be uploaded).
  108. *
  109. * @param string $sTempId Unique temporary identifier made of session_id and transaction_id. It identifies the object in a unique way.
  110. * @return void
  111. */
  112. public function OnFormCancel($sTempId);
  113. /**
  114. * Not yet called by the framework!
  115. *
  116. * Sorry, the verb has been reserved. You must implement it, but it is not called as of now.
  117. *
  118. * @param DBObject $oObject The object being displayed
  119. * @return type desc
  120. */
  121. public function EnumUsedAttributes($oObject); // Not yet implemented
  122. /**
  123. * Not yet called by the framework!
  124. *
  125. * Sorry, the verb has been reserved. You must implement it, but it is not called as of now.
  126. *
  127. * @param DBObject $oObject The object being displayed
  128. * @return string Path of the icon, relative to the modules directory.
  129. */
  130. public function GetIcon($oObject); // Not yet implemented
  131. /**
  132. * Invoked when the object is displayed alone or within a list
  133. *
  134. * Returns a value influencing the appearance of the object depending on its
  135. * state.
  136. *
  137. * Possible values are:
  138. *
  139. * * HILIGHT_CLASS_CRITICAL
  140. * * HILIGHT_CLASS_WARNING
  141. * * HILIGHT_CLASS_OK
  142. * * HILIGHT_CLASS_NONE
  143. *
  144. * @param DBObject $oObject The object being displayed
  145. * @return integer The value representing the mood of the object
  146. */
  147. public function GetHilightClass($oObject);
  148. /**
  149. * Called when building the Actions menu for a single object or a list of objects
  150. *
  151. * Use this to add items to the Actions menu. You will have to specify a label and an URL.
  152. *
  153. * Example:
  154. * <code>
  155. * $oObject = $oSet->fetch();
  156. * if ($oObject instanceof Sheep)
  157. * {
  158. * return array('View in my app' => 'http://myserver/view_sheeps?id='.$oObject->Get('name'));
  159. * }
  160. * else
  161. * {
  162. * return array();
  163. * }
  164. * </code>
  165. *
  166. * See also iPopupMenuExtension for greater flexibility
  167. *
  168. * @param DBObjectSet $oSet A set of persistent objects (DBObject)
  169. * @return string[string]
  170. */
  171. public function EnumAllowedActions(DBObjectSet $oSet);
  172. }
  173. /**
  174. * Implement this interface to perform specific things when objects are manipulated
  175. *
  176. * Note that those methods will be called when objects are manipulated, either in a programmatic way
  177. * or through the GUI.
  178. *
  179. * @package Extensibility
  180. * @api
  181. */
  182. interface iApplicationObjectExtension
  183. {
  184. /**
  185. * Invoked to determine wether an object has been modified in memory
  186. *
  187. * The GUI calls this verb to determine the message that will be displayed to the end-user.
  188. * Anyhow, this API can be called in other contexts such as the CSV import tool.
  189. *
  190. * If the extension returns false, then the framework will perform the usual evaluation.
  191. * Otherwise, the answer is definitively "yes, the object has changed".
  192. *
  193. * @param DBObject $oObject The target object
  194. * @return boolean True if something has changed for the target object
  195. */
  196. public function OnIsModified($oObject);
  197. /**
  198. * Invoked to determine wether an object can be written to the database
  199. *
  200. * The GUI calls this verb and reports any issue.
  201. * Anyhow, this API can be called in other contexts such as the CSV import tool.
  202. *
  203. * @param DBObject $oObject The target object
  204. * @return string[] A list of errors message. An error message is made of one line and it can be displayed to the end-user.
  205. */
  206. public function OnCheckToWrite($oObject);
  207. /**
  208. * Invoked to determine wether an object can be deleted from the database
  209. *
  210. * The GUI calls this verb and stops the deletion process if any issue is reported.
  211. *
  212. * Please not that it is not possible to cascade deletion by this mean: only stopper issues can be handled.
  213. *
  214. * @param DBObject $oObject The target object
  215. * @return string[] A list of errors message. An error message is made of one line and it can be displayed to the end-user.
  216. */
  217. public function OnCheckToDelete($oObject);
  218. /**
  219. * Invoked when an object is updated into the database
  220. *
  221. * The method is called right <b>after</b> the object has been written to the database.
  222. *
  223. * @param DBObject $oObject The target object
  224. * @param CMDBChange|null $oChange A change context. Since 2.0 it is fine to ignore it, as the framework does maintain this information once for all the changes made within the current page
  225. * @return void
  226. */
  227. public function OnDBUpdate($oObject, $oChange = null);
  228. /**
  229. * Invoked when an object is created into the database
  230. *
  231. * The method is called right <b>after</b> the object has been written to the database.
  232. *
  233. * @param DBObject $oObject The target object
  234. * @param CMDBChange|null $oChange A change context. Since 2.0 it is fine to ignore it, as the framework does maintain this information once for all the changes made within the current page
  235. * @return void
  236. */
  237. public function OnDBInsert($oObject, $oChange = null);
  238. /**
  239. * Invoked when an object is deleted from the database
  240. *
  241. * The method is called right <b>before</b> the object will be deleted from the database.
  242. *
  243. * @param DBObject $oObject The target object
  244. * @param CMDBChange|null $oChange A change context. Since 2.0 it is fine to ignore it, as the framework does maintain this information once for all the changes made within the current page
  245. * @return void
  246. */
  247. public function OnDBDelete($oObject, $oChange = null);
  248. }
  249. /**
  250. * New extension to add menu items in the "popup" menus inside iTop. Provides a greater flexibility than
  251. * iApplicationUIExtension::EnumAllowedActions.
  252. *
  253. * To add some menus into iTop, declare a class that implements this interface, it will be called automatically
  254. * by the application, as long as the class definition is included somewhere in the code
  255. *
  256. * @package Extensibility
  257. * @api
  258. * @since 2.0
  259. */
  260. interface iPopupMenuExtension
  261. {
  262. /**
  263. * Insert an item into the Actions menu of a list
  264. *
  265. * $param is a DBObjectSet containing the list of objects
  266. */
  267. const MENU_OBJLIST_ACTIONS = 1;
  268. /**
  269. * Insert an item into the Toolkit menu of a list
  270. *
  271. * $param is a DBObjectSet containing the list of objects
  272. */
  273. const MENU_OBJLIST_TOOLKIT = 2;
  274. /**
  275. * Insert an item into the Actions menu on an object details page
  276. *
  277. * $param is a DBObject instance: the object currently displayed
  278. */
  279. const MENU_OBJDETAILS_ACTIONS = 3;
  280. /**
  281. * Insert an item into the Dashboard menu
  282. *
  283. * The dashboad menu is shown on the top right corner when a dashboard
  284. * is being displayed.
  285. *
  286. * $param is a Dashboard instance: the dashboard currently displayed
  287. */
  288. const MENU_DASHBOARD_ACTIONS = 4;
  289. /**
  290. * Insert an item into the User menu (upper right corner)
  291. *
  292. * $param is null
  293. */
  294. const MENU_USER_ACTIONS = 5;
  295. /**
  296. * Insert an item into the Action menu on an object item in an objects list in the portal
  297. *
  298. * $param is an array('portal_id' => $sPortalId, 'object' => $oObject) containing the portal id and a DBObject instance (the object on the current line)
  299. */
  300. const PORTAL_OBJLISTITEM_ACTIONS = 7;
  301. /**
  302. * Insert an item into the Action menu on an object details page in the portal
  303. *
  304. * $param is an array('portal_id' => $sPortalId, 'object' => $oObject) containing the portal id and a DBObject instance (the object currently displayed)
  305. */
  306. const PORTAL_OBJDETAILS_ACTIONS = 8;
  307. /**
  308. * Insert an item into the Actions menu of a list in the portal
  309. * Note: This is not implemented yet !
  310. *
  311. * $param is an array('portal_id' => $sPortalId, 'object_set' => $oSet) containing DBObjectSet containing the list of objects
  312. * @todo
  313. */
  314. const PORTAL_OBJLIST_ACTIONS = 6;
  315. /**
  316. * Insert an item into the user menu of the portal
  317. * Note: This is not implemented yet !
  318. *
  319. * $param is the portal id
  320. * @todo
  321. */
  322. const PORTAL_USER_ACTIONS = 9;
  323. /**
  324. * Insert an item into the navigation menu of the portal
  325. * Note: This is not implemented yet !
  326. *
  327. * $param is the portal id
  328. * @todo
  329. */
  330. const PORTAL_MENU_ACTIONS = 10;
  331. /**
  332. * Get the list of items to be added to a menu.
  333. *
  334. * This method is called by the framework for each menu.
  335. * The items will be inserted in the menu in the order of the returned array.
  336. * @param int $iMenuId The identifier of the type of menu, as listed by the constants MENU_xxx
  337. * @param mixed $param Depends on $iMenuId, see the constants defined above
  338. * @return object[] An array of ApplicationPopupMenuItem or an empty array if no action is to be added to the menu
  339. */
  340. public static function EnumItems($iMenuId, $param);
  341. }
  342. /**
  343. * Base class for the various types of custom menus
  344. *
  345. * @package Extensibility
  346. * @internal
  347. * @since 2.0
  348. */
  349. abstract class ApplicationPopupMenuItem
  350. {
  351. /** @ignore */
  352. protected $sUID;
  353. /** @ignore */
  354. protected $sLabel;
  355. /** @ignore */
  356. protected $aCssClasses;
  357. /**
  358. * Constructor
  359. *
  360. * @param string $sUID The unique identifier of this menu in iTop... make sure you pass something unique enough
  361. * @param string $sLabel The display label of the menu (must be localized)
  362. * @param array $aCssClasses The CSS classes to add to the menu
  363. */
  364. public function __construct($sUID, $sLabel)
  365. {
  366. $this->sUID = $sUID;
  367. $this->sLabel = $sLabel;
  368. $this->aCssClasses = array();
  369. }
  370. /**
  371. * Get the UID
  372. *
  373. * @return string The unique identifier
  374. * @ignore
  375. */
  376. public function GetUID()
  377. {
  378. return $this->sUID;
  379. }
  380. /**
  381. * Get the label
  382. *
  383. * @return string The label
  384. * @ignore
  385. */
  386. public function GetLabel()
  387. {
  388. return $this->sLabel;
  389. }
  390. /**
  391. * Get the CSS classes
  392. *
  393. * @return array
  394. * @ignore
  395. */
  396. public function GetCssClasses()
  397. {
  398. return $this->aCssClasses;
  399. }
  400. /**
  401. * @param $aCssClasses
  402. */
  403. public function SetCssClasses($aCssClasses)
  404. {
  405. $this->aCssClasses = $aCssClasses;
  406. }
  407. /**
  408. * Adds a CSS class to the CSS classes that will be put on the menu item
  409. *
  410. * @param $sCssClass
  411. */
  412. public function AddCssClass($sCssClass)
  413. {
  414. $this->aCssClasses[] = $sCssClass;
  415. }
  416. /**
  417. * Returns the components to create a popup menu item in HTML
  418. * @return Hash A hash array: array('label' => , 'url' => , 'target' => , 'onclick' => )
  419. * @ignore
  420. */
  421. abstract public function GetMenuItem();
  422. /** @ignore */
  423. public function GetLinkedScripts()
  424. {
  425. return array();
  426. }
  427. }
  428. /**
  429. * Class for adding an item into a popup menu that browses to the given URL
  430. *
  431. * @package Extensibility
  432. * @api
  433. * @since 2.0
  434. */
  435. class URLPopupMenuItem extends ApplicationPopupMenuItem
  436. {
  437. /** @ignore */
  438. protected $sURL;
  439. /** @ignore */
  440. protected $sTarget;
  441. /**
  442. * Constructor
  443. *
  444. * @param string $sUID The unique identifier of this menu in iTop... make sure you pass something unique enough
  445. * @param string $sLabel The display label of the menu (must be localized)
  446. * @param string $sURL If the menu is an hyperlink, provide the absolute hyperlink here
  447. * @param string $sTarget In case the menu is an hyperlink and a specific target is needed (_blank for example), pass it here
  448. */
  449. public function __construct($sUID, $sLabel, $sURL, $sTarget = '_top')
  450. {
  451. parent::__construct($sUID, $sLabel);
  452. $this->sURL = $sURL;
  453. $this->sTarget = $sTarget;
  454. }
  455. /** @ignore */
  456. public function GetMenuItem()
  457. {
  458. return array ('label' => $this->GetLabel(), 'url' => $this->sURL, 'target' => $this->sTarget, 'css_classes' => $this->aCssClasses);
  459. }
  460. }
  461. /**
  462. * Class for adding an item into a popup menu that triggers some Javascript code
  463. *
  464. * @package Extensibility
  465. * @api
  466. * @since 2.0
  467. */
  468. class JSPopupMenuItem extends ApplicationPopupMenuItem
  469. {
  470. /** @ignore */
  471. protected $sJSCode;
  472. /** @ignore */
  473. protected $aIncludeJSFiles;
  474. /**
  475. * Class for adding an item that triggers some Javascript code
  476. * @param string $sUID The unique identifier of this menu in iTop... make sure you pass something unique enough
  477. * @param string $sLabel The display label of the menu (must be localized)
  478. * @param string $sJSCode In case the menu consists in executing some havascript code inside the page, pass it here. If supplied $sURL ans $sTarget will be ignored
  479. * @param array $aIncludeJSFiles An array of file URLs to be included (once) to provide some JS libraries for the page.
  480. */
  481. public function __construct($sUID, $sLabel, $sJSCode, $aIncludeJSFiles = array())
  482. {
  483. parent::__construct($sUID, $sLabel);
  484. $this->sJSCode = $sJSCode;
  485. $this->aIncludeJSFiles = $aIncludeJSFiles;
  486. }
  487. /** @ignore */
  488. public function GetMenuItem()
  489. {
  490. // Note: the semicolumn is a must here!
  491. return array ('label' => $this->GetLabel(), 'onclick' => $this->sJSCode.'; return false;', 'url' => '#', 'css_classes' => $this->aCssClasses);
  492. }
  493. /** @ignore */
  494. public function GetLinkedScripts()
  495. {
  496. return $this->aIncludeJSFiles;
  497. }
  498. }
  499. /**
  500. * Class for adding a separator (horizontal line, not selectable) the output
  501. * will automatically reduce several consecutive separators to just one
  502. *
  503. * @package Extensibility
  504. * @api
  505. * @since 2.0
  506. */
  507. class SeparatorPopupMenuItem extends ApplicationPopupMenuItem
  508. {
  509. static $idx = 0;
  510. /**
  511. * Constructor
  512. */
  513. public function __construct()
  514. {
  515. parent::__construct('_separator_'.(self::$idx++), '');
  516. }
  517. /** @ignore */
  518. public function GetMenuItem()
  519. {
  520. return array ('label' => '<hr class="menu-separator">', 'url' => '', 'css_classes' => $this->aCssClasses);
  521. }
  522. }
  523. /**
  524. * Class for adding an item as a button that browses to the given URL
  525. *
  526. * @package Extensibility
  527. * @api
  528. * @since 2.0
  529. */
  530. class URLButtonItem extends URLPopupMenuItem
  531. {
  532. }
  533. /**
  534. * Class for adding an item as a button that runs some JS code
  535. *
  536. * @package Extensibility
  537. * @api
  538. * @since 2.0
  539. */
  540. class JSButtonItem extends JSPopupMenuItem
  541. {
  542. }
  543. /**
  544. * Implement this interface to add content to any iTopWebPage
  545. *
  546. * There are 3 places where content can be added:
  547. *
  548. * * The north pane: (normaly empty/hidden) at the top of the page, spanning the whole
  549. * width of the page
  550. * * The south pane: (normaly empty/hidden) at the bottom of the page, spanning the whole
  551. * width of the page
  552. * * The admin banner (two tones gray background) at the left of the global search.
  553. * Limited space, use it for short messages
  554. *
  555. * Each of the methods of this interface is supposed to return the HTML to be inserted at
  556. * the specified place and can use the passed iTopWebPage object to add javascript or CSS definitions
  557. *
  558. * @package Extensibility
  559. * @api
  560. * @since 2.0
  561. */
  562. interface iPageUIExtension
  563. {
  564. /**
  565. * Add content to the North pane
  566. * @param iTopWebPage $oPage The page to insert stuff into.
  567. * @return string The HTML content to add into the page
  568. */
  569. public function GetNorthPaneHtml(iTopWebPage $oPage);
  570. /**
  571. * Add content to the South pane
  572. * @param iTopWebPage $oPage The page to insert stuff into.
  573. * @return string The HTML content to add into the page
  574. */
  575. public function GetSouthPaneHtml(iTopWebPage $oPage);
  576. /**
  577. * Add content to the "admin banner"
  578. * @param iTopWebPage $oPage The page to insert stuff into.
  579. * @return string The HTML content to add into the page
  580. */
  581. public function GetBannerHtml(iTopWebPage $oPage);
  582. }
  583. /**
  584. * Implement this interface to add content to any enhanced portal page
  585. *
  586. * IMPORTANT! Experimental API, may be removed at anytime, we don't recommend to use it just now!
  587. *
  588. * @package Extensibility
  589. * @api
  590. * @since 2.4
  591. */
  592. interface iPortalUIExtension
  593. {
  594. const ENUM_PORTAL_EXT_UI_BODY = 'Body';
  595. const ENUM_PORTAL_EXT_UI_NAVIGATION_MENU = 'NavigationMenu';
  596. const ENUM_PORTAL_EXT_UI_MAIN_CONTENT = 'MainContent';
  597. /**
  598. * Returns an array of CSS file urls
  599. *
  600. * @param \Silex\Application $oApp
  601. * @return array
  602. */
  603. public function GetCSSFiles(\Silex\Application $oApp);
  604. /**
  605. * Returns inline (raw) CSS
  606. *
  607. * @param \Silex\Application $oApp
  608. * @return string
  609. */
  610. public function GetCSSInline(\Silex\Application $oApp);
  611. /**
  612. * Returns an array of JS file urls
  613. *
  614. * @param \Silex\Application $oApp
  615. * @return array
  616. */
  617. public function GetJSFiles(\Silex\Application $oApp);
  618. /**
  619. * Returns raw JS code
  620. *
  621. * @param \Silex\Application $oApp
  622. * @return string
  623. */
  624. public function GetJSInline(\Silex\Application $oApp);
  625. /**
  626. * Returns raw HTML code to put at the end of the <body> tag
  627. *
  628. * @param \Silex\Application $oApp
  629. * @return string
  630. */
  631. public function GetBodyHTML(\Silex\Application $oApp);
  632. /**
  633. * Returns raw HTML code to put at the end of the #main-wrapper element
  634. *
  635. * @param \Silex\Application $oApp
  636. * @return string
  637. */
  638. public function GetMainContentHTML(\Silex\Application $oApp);
  639. /**
  640. * Returns raw HTML code to put at the end of the #topbar and #sidebar elements
  641. *
  642. * @param \Silex\Application $oApp
  643. * @return string
  644. */
  645. public function GetNavigationMenuHTML(\Silex\Application $oApp);
  646. }
  647. /**
  648. * IMPORTANT! Experimental API, may be removed at anytime, we don't recommend to use it just now!
  649. */
  650. abstract class AbstractPortalUIExtension implements iPortalUIExtension
  651. {
  652. /**
  653. * @inheritDoc
  654. */
  655. public function GetCSSFiles(\Silex\Application $oApp)
  656. {
  657. return array();
  658. }
  659. /**
  660. * @inheritDoc
  661. */
  662. public function GetCSSInline(\Silex\Application $oApp)
  663. {
  664. return null;
  665. }
  666. /**
  667. * @inheritDoc
  668. */
  669. public function GetJSFiles(\Silex\Application $oApp)
  670. {
  671. return array();
  672. }
  673. /**
  674. * @inheritDoc
  675. */
  676. public function GetJSInline(\Silex\Application $oApp)
  677. {
  678. return null;
  679. }
  680. /**
  681. * @inheritDoc
  682. */
  683. public function GetBodyHTML(\Silex\Application $oApp)
  684. {
  685. return null;
  686. }
  687. /**
  688. * @inheritDoc
  689. */
  690. public function GetMainContentHTML(\Silex\Application $oApp)
  691. {
  692. return null;
  693. }
  694. /**
  695. * @inheritDoc
  696. */
  697. public function GetNavigationMenuHTML(\Silex\Application $oApp)
  698. {
  699. return null;
  700. }
  701. }
  702. /**
  703. * Implement this interface to add new operations to the REST/JSON web service
  704. *
  705. * @package Extensibility
  706. * @api
  707. * @since 2.0.1
  708. */
  709. interface iRestServiceProvider
  710. {
  711. /**
  712. * Enumerate services delivered by this class
  713. * @param string $sVersion The version (e.g. 1.0) supported by the services
  714. * @return array An array of hash 'verb' => verb, 'description' => description
  715. */
  716. public function ListOperations($sVersion);
  717. /**
  718. * Enumerate services delivered by this class
  719. * @param string $sVersion The version (e.g. 1.0) supported by the services
  720. * @return RestResult The standardized result structure (at least a message)
  721. * @throws Exception in case of internal failure.
  722. */
  723. public function ExecOperation($sVersion, $sVerb, $aParams);
  724. }
  725. /**
  726. * Minimal REST response structure. Derive this structure to add response data and error codes.
  727. *
  728. * @package Extensibility
  729. * @api
  730. * @since 2.0.1
  731. */
  732. class RestResult
  733. {
  734. /**
  735. * Result: no issue has been encountered
  736. */
  737. const OK = 0;
  738. /**
  739. * Result: missing/wrong credentials or the user does not have enough rights to perform the requested operation
  740. */
  741. const UNAUTHORIZED = 1;
  742. /**
  743. * Result: the parameter 'version' is missing
  744. */
  745. const MISSING_VERSION = 2;
  746. /**
  747. * Result: the parameter 'json_data' is missing
  748. */
  749. const MISSING_JSON = 3;
  750. /**
  751. * Result: the input structure is not a valid JSON string
  752. */
  753. const INVALID_JSON = 4;
  754. /**
  755. * Result: the parameter 'auth_user' is missing, authentication aborted
  756. */
  757. const MISSING_AUTH_USER = 5;
  758. /**
  759. * Result: the parameter 'auth_pwd' is missing, authentication aborted
  760. */
  761. const MISSING_AUTH_PWD = 6;
  762. /**
  763. * Result: no operation is available for the specified version
  764. */
  765. const UNSUPPORTED_VERSION = 10;
  766. /**
  767. * Result: the requested operation is not valid for the specified version
  768. */
  769. const UNKNOWN_OPERATION = 11;
  770. /**
  771. * Result: the requested operation cannot be performed because it can cause data (integrity) loss
  772. */
  773. const UNSAFE = 12;
  774. /**
  775. * Result: the operation could not be performed, see the message for troubleshooting
  776. */
  777. const INTERNAL_ERROR = 100;
  778. /**
  779. * Default constructor - ok!
  780. *
  781. * @param DBObject $oObject The object being reported
  782. * @param string $sAttCode The attribute code (must be valid)
  783. * @return string A scalar representation of the value
  784. */
  785. public function __construct()
  786. {
  787. $this->code = RestResult::OK;
  788. }
  789. public $code;
  790. public $message;
  791. }
  792. /**
  793. * Helpers for implementing REST services
  794. *
  795. * @package Extensibility
  796. * @api
  797. */
  798. class RestUtils
  799. {
  800. /**
  801. * Registering tracking information. Any further object modification be associated with the given comment, when the modification gets recorded into the DB
  802. *
  803. * @param StdClass $oData Structured input data. Must contain 'comment'.
  804. * @return void
  805. * @throws Exception
  806. * @api
  807. */
  808. public static function InitTrackingComment($oData)
  809. {
  810. $sComment = self::GetMandatoryParam($oData, 'comment');
  811. CMDBObject::SetTrackInfo($sComment);
  812. }
  813. /**
  814. * Read a mandatory parameter from from a Rest/Json structure.
  815. *
  816. * @param StdClass $oData Structured input data. Must contain the entry defined by sParamName.
  817. * @param string $sParamName Name of the parameter to fetch from the input data
  818. * @return void
  819. * @throws Exception If the parameter is missing
  820. * @api
  821. */
  822. public static function GetMandatoryParam($oData, $sParamName)
  823. {
  824. if (isset($oData->$sParamName))
  825. {
  826. return $oData->$sParamName;
  827. }
  828. else
  829. {
  830. throw new Exception("Missing parameter '$sParamName'");
  831. }
  832. }
  833. /**
  834. * Read an optional parameter from from a Rest/Json structure.
  835. *
  836. * @param StdClass $oData Structured input data.
  837. * @param string $sParamName Name of the parameter to fetch from the input data
  838. * @param mixed $default Default value if the parameter is not found in the input data
  839. * @return void
  840. * @throws Exception
  841. * @api
  842. */
  843. public static function GetOptionalParam($oData, $sParamName, $default)
  844. {
  845. if (isset($oData->$sParamName))
  846. {
  847. return $oData->$sParamName;
  848. }
  849. else
  850. {
  851. return $default;
  852. }
  853. }
  854. /**
  855. * Read a class from a Rest/Json structure.
  856. *
  857. * @param StdClass $oData Structured input data. Must contain the entry defined by sParamName.
  858. * @param string $sParamName Name of the parameter to fetch from the input data
  859. * @return void
  860. * @throws Exception If the parameter is missing or the class is unknown
  861. * @api
  862. */
  863. public static function GetClass($oData, $sParamName)
  864. {
  865. $sClass = self::GetMandatoryParam($oData, $sParamName);
  866. if (!MetaModel::IsValidClass($sClass))
  867. {
  868. throw new Exception("$sParamName: '$sClass' is not a valid class'");
  869. }
  870. return $sClass;
  871. }
  872. /**
  873. * Read a list of attribute codes from a Rest/Json structure.
  874. *
  875. * @param string $sClass Name of the class
  876. * @param StdClass $oData Structured input data.
  877. * @param string $sParamName Name of the parameter to fetch from the input data
  878. * @return An array of class => list of attributes (see RestResultWithObjects::AddObject that uses it)
  879. * @throws Exception
  880. * @api
  881. */
  882. public static function GetFieldList($sClass, $oData, $sParamName)
  883. {
  884. $sFields = self::GetOptionalParam($oData, $sParamName, '*');
  885. $aShowFields = array();
  886. if ($sFields == '*')
  887. {
  888. foreach (MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  889. {
  890. $aShowFields[$sClass][] = $sAttCode;
  891. }
  892. }
  893. elseif ($sFields == '*+')
  894. {
  895. foreach (MetaModel::EnumChildClasses($sClass, ENUM_CHILD_CLASSES_ALL) as $sRefClass)
  896. {
  897. foreach (MetaModel::ListAttributeDefs($sRefClass) as $sAttCode => $oAttDef)
  898. {
  899. $aShowFields[$sRefClass][] = $sAttCode;
  900. }
  901. }
  902. }
  903. else
  904. {
  905. foreach(explode(',', $sFields) as $sAttCode)
  906. {
  907. $sAttCode = trim($sAttCode);
  908. if (($sAttCode != 'id') && (!MetaModel::IsValidAttCode($sClass, $sAttCode)))
  909. {
  910. throw new Exception("$sParamName: invalid attribute code '$sAttCode'");
  911. }
  912. $aShowFields[$sClass][] = $sAttCode;
  913. }
  914. }
  915. return $aShowFields;
  916. }
  917. /**
  918. * Read and interpret object search criteria from a Rest/Json structure
  919. *
  920. * @param string $sClass Name of the class
  921. * @param StdClass $oCriteria Hash of attribute code => value (can be a substructure or a scalar, depending on the nature of the attriute)
  922. * @return object The object found
  923. * @throws Exception If the input structure is not valid or it could not find exactly one object
  924. */
  925. protected static function FindObjectFromCriteria($sClass, $oCriteria)
  926. {
  927. $aCriteriaReport = array();
  928. if (isset($oCriteria->finalclass))
  929. {
  930. if (!MetaModel::IsValidClass($oCriteria->finalclass))
  931. {
  932. throw new Exception("finalclass: Unknown class '".$oCriteria->finalclass."'");
  933. }
  934. if (!MetaModel::IsParentClass($sClass, $oCriteria->finalclass))
  935. {
  936. throw new Exception("finalclass: '".$oCriteria->finalclass."' is not a child class of '$sClass'");
  937. }
  938. $sClass = $oCriteria->finalclass;
  939. }
  940. $oSearch = new DBObjectSearch($sClass);
  941. foreach ($oCriteria as $sAttCode => $value)
  942. {
  943. $realValue = static::MakeValue($sClass, $sAttCode, $value);
  944. $oSearch->AddCondition($sAttCode, $realValue, '=');
  945. if (is_object($value) || is_array($value))
  946. {
  947. $value = json_encode($value);
  948. }
  949. $aCriteriaReport[] = "$sAttCode: $value ($realValue)";
  950. }
  951. $oSet = new DBObjectSet($oSearch);
  952. $iCount = $oSet->Count();
  953. if ($iCount == 0)
  954. {
  955. throw new Exception("No item found with criteria: ".implode(', ', $aCriteriaReport));
  956. }
  957. elseif ($iCount > 1)
  958. {
  959. throw new Exception("Several items found ($iCount) with criteria: ".implode(', ', $aCriteriaReport));
  960. }
  961. $res = $oSet->Fetch();
  962. return $res;
  963. }
  964. /**
  965. * Find an object from a polymorph search specification (Rest/Json)
  966. *
  967. * @param string $sClass Name of the class
  968. * @param mixed $key Either search criteria (substructure), or an object or an OQL string.
  969. * @param bool $bAllowNullValue Allow the cases such as key = 0 or key = {null} and return null then
  970. * @return DBObject The object found
  971. * @throws Exception If the input structure is not valid or it could not find exactly one object
  972. * @api
  973. */
  974. public static function FindObjectFromKey($sClass, $key, $bAllowNullValue = false)
  975. {
  976. if (is_object($key))
  977. {
  978. $res = static::FindObjectFromCriteria($sClass, $key);
  979. }
  980. elseif (is_numeric($key))
  981. {
  982. if ($bAllowNullValue && ($key == 0))
  983. {
  984. $res = null;
  985. }
  986. else
  987. {
  988. $res = MetaModel::GetObject($sClass, $key, false);
  989. if (is_null($res))
  990. {
  991. throw new Exception("Invalid object $sClass::$key");
  992. }
  993. }
  994. }
  995. elseif (is_string($key))
  996. {
  997. // OQL
  998. $oSearch = DBObjectSearch::FromOQL($key);
  999. $oSet = new DBObjectSet($oSearch);
  1000. $iCount = $oSet->Count();
  1001. if ($iCount == 0)
  1002. {
  1003. throw new Exception("No item found for query: $key");
  1004. }
  1005. elseif ($iCount > 1)
  1006. {
  1007. throw new Exception("Several items found ($iCount) for query: $key");
  1008. }
  1009. $res = $oSet->Fetch();
  1010. }
  1011. else
  1012. {
  1013. throw new Exception("Wrong format for key");
  1014. }
  1015. return $res;
  1016. }
  1017. /**
  1018. * Search objects from a polymorph search specification (Rest/Json)
  1019. *
  1020. * @param string $sClass Name of the class
  1021. * @param mixed $key Either search criteria (substructure), or an object or an OQL string.
  1022. * @return DBObjectSet The search result set
  1023. * @throws Exception If the input structure is not valid
  1024. */
  1025. public static function GetObjectSetFromKey($sClass, $key)
  1026. {
  1027. if (is_object($key))
  1028. {
  1029. if (isset($key->finalclass))
  1030. {
  1031. $sClass = $key->finalclass;
  1032. if (!MetaModel::IsValidClass($sClass))
  1033. {
  1034. throw new Exception("finalclass: Unknown class '$sClass'");
  1035. }
  1036. }
  1037. $oSearch = new DBObjectSearch($sClass);
  1038. foreach ($key as $sAttCode => $value)
  1039. {
  1040. $realValue = static::MakeValue($sClass, $sAttCode, $value);
  1041. $oSearch->AddCondition($sAttCode, $realValue, '=');
  1042. }
  1043. }
  1044. elseif (is_numeric($key))
  1045. {
  1046. $oSearch = new DBObjectSearch($sClass);
  1047. $oSearch->AddCondition('id', $key);
  1048. }
  1049. elseif (is_string($key))
  1050. {
  1051. // OQL
  1052. $oSearch = DBObjectSearch::FromOQL($key);
  1053. $oObjectSet = new DBObjectSet($oSearch);
  1054. }
  1055. else
  1056. {
  1057. throw new Exception("Wrong format for key");
  1058. }
  1059. $oObjectSet = new DBObjectSet($oSearch);
  1060. return $oObjectSet;
  1061. }
  1062. /**
  1063. * Interpret the Rest/Json value and get a valid attribute value
  1064. *
  1065. * @param string $sClass Name of the class
  1066. * @param string $sAttCode Attribute code
  1067. * @param mixed $value Depending on the type of attribute (a scalar, or search criteria, or list of related objects...)
  1068. * @return mixed The value that can be used with DBObject::Set()
  1069. * @throws Exception If the specification of the value is not valid.
  1070. * @api
  1071. */
  1072. public static function MakeValue($sClass, $sAttCode, $value)
  1073. {
  1074. try
  1075. {
  1076. if (!MetaModel::IsValidAttCode($sClass, $sAttCode))
  1077. {
  1078. throw new Exception("Unknown attribute");
  1079. }
  1080. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  1081. if ($oAttDef instanceof AttributeExternalKey)
  1082. {
  1083. $oExtKeyObject = static::FindObjectFromKey($oAttDef->GetTargetClass(), $value, true /* allow null */);
  1084. $value = ($oExtKeyObject != null) ? $oExtKeyObject->GetKey() : 0;
  1085. }
  1086. elseif ($oAttDef instanceof AttributeLinkedSet)
  1087. {
  1088. if (!is_array($value))
  1089. {
  1090. throw new Exception("A link set must be defined by an array of objects");
  1091. }
  1092. $sLnkClass = $oAttDef->GetLinkedClass();
  1093. $aLinks = array();
  1094. foreach($value as $oValues)
  1095. {
  1096. $oLnk = static::MakeObjectFromFields($sLnkClass, $oValues);
  1097. $aLinks[] = $oLnk;
  1098. }
  1099. $value = DBObjectSet::FromArray($sLnkClass, $aLinks);
  1100. }
  1101. else
  1102. {
  1103. $value = $oAttDef->FromJSONToValue($value);
  1104. }
  1105. }
  1106. catch (Exception $e)
  1107. {
  1108. throw new Exception("$sAttCode: ".$e->getMessage(), $e->getCode());
  1109. }
  1110. return $value;
  1111. }
  1112. /**
  1113. * Interpret a Rest/Json structure that defines attribute values, and build an object
  1114. *
  1115. * @param string $sClass Name of the class
  1116. * @param array $aFields A hash of attribute code => value specification.
  1117. * @return DBObject The newly created object
  1118. * @throws Exception If the specification of the values is not valid
  1119. * @api
  1120. */
  1121. public static function MakeObjectFromFields($sClass, $aFields)
  1122. {
  1123. $oObject = MetaModel::NewObject($sClass);
  1124. foreach ($aFields as $sAttCode => $value)
  1125. {
  1126. $realValue = static::MakeValue($sClass, $sAttCode, $value);
  1127. try
  1128. {
  1129. $oObject->Set($sAttCode, $realValue);
  1130. }
  1131. catch (Exception $e)
  1132. {
  1133. throw new Exception("$sAttCode: ".$e->getMessage(), $e->getCode());
  1134. }
  1135. }
  1136. return $oObject;
  1137. }
  1138. /**
  1139. * Interpret a Rest/Json structure that defines attribute values, and update the given object
  1140. *
  1141. * @param DBObject $oObject The object being modified
  1142. * @param array $aFields A hash of attribute code => value specification.
  1143. * @return DBObject The object modified
  1144. * @throws Exception If the specification of the values is not valid
  1145. * @api
  1146. */
  1147. public static function UpdateObjectFromFields($oObject, $aFields)
  1148. {
  1149. $sClass = get_class($oObject);
  1150. foreach ($aFields as $sAttCode => $value)
  1151. {
  1152. $realValue = static::MakeValue($sClass, $sAttCode, $value);
  1153. try
  1154. {
  1155. $oObject->Set($sAttCode, $realValue);
  1156. }
  1157. catch (Exception $e)
  1158. {
  1159. throw new Exception("$sAttCode: ".$e->getMessage(), $e->getCode());
  1160. }
  1161. }
  1162. return $oObject;
  1163. }
  1164. }