itopwebpage.class.inc.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. * Class iTopWebPage
  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/nicewebpage.class.inc.php");
  25. require_once("../application/usercontext.class.inc.php");
  26. require_once("../application/applicationcontext.class.inc.php");
  27. /**
  28. * Web page with some associated CSS and scripts (jquery) for a fancier display
  29. */
  30. class iTopWebPage extends NiceWebPage
  31. {
  32. private $m_sMenu;
  33. private $m_currentOrganization;
  34. private $m_aTabs;
  35. private $m_sCurrentTabContainer;
  36. private $m_sCurrentTab;
  37. public function __construct($sTitle, $currentOrganization)
  38. {
  39. parent::__construct($sTitle);
  40. $this->m_sCurrentTabContainer = '';
  41. $this->m_sCurrentTab = '';
  42. $this->m_aTabs = array();
  43. $this->m_sMenu = "";
  44. $oAppContext = new ApplicationContext();
  45. $sExtraParams = $oAppContext->GetForLink();
  46. $this->add_header("Content-type: text/html; charset=utf-8");
  47. $this->add_header("Cache-control: no-cache");
  48. $this->m_currentOrganization = $currentOrganization;
  49. $this->add_linked_script("../js/jquery.dimensions.js");
  50. $this->add_linked_script("../js/splitter.js");
  51. $this->add_linked_script("../js/jquery.tablehover.js");
  52. $this->add_linked_script("../js/jquery.treeview.js");
  53. $this->add_linked_script("../js/jquery.autocomplete.js");
  54. $this->add_linked_script("../js/jquery.bgiframe.js");
  55. $this->add_linked_script("../js/jquery.positionBy.js");
  56. $this->add_linked_script("../js/jquery.popupmenu.js");
  57. $this->add_linked_script("../js/date.js");
  58. $this->add_linked_script("../js/jquery.date.picker.js");
  59. $this->add_linked_script("../js/jquery.tablesorter.min.js");
  60. $this->add_linked_script("../js/jquery.blockUI.js");
  61. $this->add_linked_script("../js/utils.js");
  62. //$this->add_linked_script("../js/jquery-ui-personalized-1.5.3.js");
  63. $this->add_linked_script("../js/swfobject.js");
  64. $this->add_linked_stylesheet("../css/jquery.treeview.css");
  65. $this->add_linked_stylesheet("../css/jquery.autocomplete.css");
  66. $this->add_linked_stylesheet("../css/date.picker.css");
  67. $this->add_ready_script(
  68. <<<EOF
  69. // Vertical splitter. The min/max/starting sizes for the left (A) pane
  70. // are set here. All values are in pixels.
  71. $("#MySplitter").splitter({
  72. type: "v",
  73. minA: 100, initA: 250, maxA: 500,
  74. accessKey: "|"
  75. });
  76. // Horizontal splitter, nested in the right pane of the vertical splitter.
  77. if ( $("#TopPane").length > 0)
  78. {
  79. $("#RightPane").splitter({
  80. type: "h" //,
  81. //minA: 100, initA: 150, maxA: 500,
  82. //accessKey: "_"
  83. });
  84. }
  85. // Manually set the outer splitter's height to fill the browser window.
  86. // This must be re-done any time the browser window is resized.
  87. $(window).bind("resize", function(){
  88. var ms = $("#MySplitter");
  89. var top = ms.offset().top; // from dimensions.js
  90. var wh = $(window).height();
  91. // Account for margin or border on the splitter container
  92. var mrg = parseInt(ms.css("marginBottom")) || 0;
  93. var brd = parseInt(ms.css("borderBottomWidth")) || 0;
  94. ms.css("height", (wh-top-mrg-brd)+"px");
  95. // IE fires resize for splitter; others don't so do it here
  96. if ( !jQuery.browser.msie )
  97. ms.trigger("resize");
  98. }).trigger("resize");
  99. var ms = $("#MySplitter");
  100. ms.trigger("resize");
  101. if ( $("#TopPane").length > 0)
  102. {
  103. $("#RightPane").trigger("resize");
  104. }
  105. $("div[id^=tabbedContent] > ul").tabs( 1, { fxFade: true, fxSpeed: 'fast' } ); // tabs
  106. $("table.listResults").tableHover(); // hover tables
  107. $(".listResults").tablesorter( { headers: { 0:{sorter: false }}, widgets: ['zebra']} ); // sortable and zebra tables
  108. $(".date-pick").datePicker( {clickInput: false, createButton: true, startDate: '2000-01-01'} ); // Date picker
  109. $('#ModalDlg').jqm({ajax: '@href', trigger: 'a.jqmTrigger', overlay:70, modal:true, toTop:true}); // jqModal Window
  110. //$('.display_block').draggable(); // make the blocks draggable
  111. EOF
  112. );
  113. $this->add_script("
  114. // For automplete
  115. function findValue(li) {
  116. if( li == null ) return alert(\"No match!\");
  117. // if coming from an AJAX call, let's use the CityId as the value
  118. if( !!li.extra ) var sValue = li.extra[0];
  119. // otherwise, let's just display the value in the text box
  120. else var sValue = li.selectValue;
  121. //alert(\"The value you selected was: \" + sValue);
  122. }
  123. function selectItem(li) {
  124. findValue(li);
  125. }
  126. function formatItem(row) {
  127. return row[0];
  128. }
  129. function goBack()
  130. {
  131. window.history.back();
  132. }
  133. ");
  134. $this->DisplayMenu();
  135. }
  136. public function AddToMenu($sHtml)
  137. {
  138. $this->m_sMenu .= $sHtml;
  139. }
  140. public function DisplayMenu()
  141. {
  142. // Combo box to select the organization
  143. $this->AddToMenu("<div id=\"OrganizationSelection\">
  144. <form style=\"display:inline\" action=\"{$_SERVER['PHP_SELF']}\"><select style=\"width:150px;font-size:x-small\" name=\"org_id\" title=\"Pick an organization\" onChange=\"this.form.submit();\">\n");
  145. // List of visible Organizations
  146. $oContext = new UserContext();
  147. $oSearchFilter = $oContext->NewFilter("bizOrganization");
  148. $oSet = new CMDBObjectSet($oSearchFilter);
  149. $sSelected = ($this->m_currentOrganization == '') ? ' selected' : '';
  150. $this->AddToMenu("<option value=\"\"$sSelected>".Dict::S('UI:AllOrganizations')."</option>");
  151. if ($oSet->Count() > 0)
  152. while($oOrg = $oSet->Fetch())
  153. {
  154. if ($this->m_currentOrganization == $oOrg->GetKey())
  155. {
  156. $oCurrentOrganization = $oOrg;
  157. $sSelected = " selected";
  158. }
  159. else
  160. {
  161. $sSelected = "";
  162. }
  163. $this->AddToMenu("<option value=\"".$oOrg->GetKey()."\"$sSelected>".$oOrg->Get('name')."</option>\n");
  164. }
  165. $this->AddToMenu("</select>\n");
  166. // Add other dimensions/context information to this form
  167. $oAppContext = new ApplicationContext();
  168. $oAppContext->Reset('org_id'); // Org id is handled above and we want to be able to change it here !
  169. $this->AddToMenu($oAppContext->GetForForm());
  170. $this->AddToMenu("</form>\n");
  171. $this->AddToMenu("</div>\n");
  172. $this->AddToMenu("<ul id=\"browser\" class=\"dir\">\n");
  173. // Display the menu
  174. $oAppContext = new ApplicationContext();
  175. // 1) Application defined menus
  176. $oSearchFilter = $oContext->NewFilter("menuNode");
  177. $oSearchFilter->AddCondition('parent_id', 0, '=');
  178. $oSearchFilter->AddCondition('type', 'application', '=');
  179. // There may be more criteria added later to have a specific menu based on the user's profile
  180. $oSet = new CMDBObjectSet($oSearchFilter, array('rank' => true));
  181. while ($oRootMenuNode = $oSet->Fetch())
  182. {
  183. $oRootMenuNode->DisplayMenu($this, 'application', $oAppContext->GetAsHash());
  184. }
  185. // 2) User defined menus (Bookmarks)
  186. $oSearchFilter = $oContext->NewFilter("menuNode");
  187. $oSearchFilter->AddCondition('parent_id', 0, '=');
  188. $oSearchFilter->AddCondition('type', 'user', '=');
  189. $oSearchFilter->AddCondition('user_id', UserRights::GetUserId(), '=');
  190. // There may be more criteria added later to have a specific menu based on the user's profile
  191. $oSet = new CMDBObjectSet($oSearchFilter, array('rank' => true));
  192. while ($oRootMenuNode = $oSet->Fetch())
  193. {
  194. $oRootMenuNode->DisplayMenu($this, 'user', $oAppContext->GetAsHash());
  195. }
  196. // 3) Administrator menu
  197. if (userRights::IsAdministrator())
  198. {
  199. $oSearchFilter = $oContext->NewFilter("menuNode");
  200. $oSearchFilter->AddCondition('parent_id', 0, '=');
  201. $oSearchFilter->AddCondition('type', 'administrator', '=');
  202. // There may be more criteria added later to have a specific menu based on the user's profile
  203. $oSet = new CMDBObjectSet($oSearchFilter, array('rank' => true));
  204. while ($oRootMenuNode = $oSet->Fetch())
  205. {
  206. $oRootMenuNode->DisplayMenu($this, 'administrator', $oAppContext->GetAsHash());
  207. }
  208. }
  209. $this->AddToMenu("</ul>\n");
  210. }
  211. /**
  212. * Outputs (via some echo) the complete HTML page by assembling all its elements
  213. */
  214. public function output()
  215. {
  216. foreach($this->a_headers as $s_header)
  217. {
  218. header($s_header);
  219. }
  220. $s_captured_output = ob_get_contents();
  221. ob_end_clean();
  222. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  223. echo "<html>\n";
  224. echo "<head>\n";
  225. echo "<title>{$this->s_title}</title>\n";
  226. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
  227. echo $this->get_base_tag();
  228. // Stylesheets MUST be loaded before any scripts otherwise
  229. // jQuery scripts may face some spurious problems (like failing on a 'reload')
  230. foreach($this->a_linked_stylesheets as $a_stylesheet)
  231. {
  232. if ($a_stylesheet['condition'] != "")
  233. {
  234. echo "<!--[if {$a_stylesheet['condition']}]>\n";
  235. }
  236. echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$a_stylesheet['link']}\" />\n";
  237. if ($a_stylesheet['condition'] != "")
  238. {
  239. echo "<![endif]-->\n";
  240. }
  241. }
  242. foreach($this->a_linked_scripts as $s_script)
  243. {
  244. echo "<script type=\"text/javascript\" src=\"$s_script\"></script>\n";
  245. }
  246. if (count($this->m_aReadyScripts)>0)
  247. {
  248. $this->add_script("\$(document).ready(function() {\n".implode("\n", $this->m_aReadyScripts)."\n});");
  249. }
  250. if (count($this->a_scripts)>0)
  251. {
  252. echo "<script type=\"text/javascript\">\n";
  253. foreach($this->a_scripts as $s_script)
  254. {
  255. echo "$s_script\n";
  256. }
  257. echo "</script>\n";
  258. }
  259. if (count($this->a_styles)>0)
  260. {
  261. echo "<style>\n";
  262. foreach($this->a_styles as $s_style)
  263. {
  264. echo "$s_style\n";
  265. }
  266. echo "</style>\n";
  267. }
  268. echo "<link rel=\"search\" type=\"application/opensearchdescription+xml\" title=\"iTop\" href=\"./opensearch.xml.php\" />\n";
  269. echo "</head>\n";
  270. echo "<body>\n";
  271. // Display the header
  272. if (ITOP_REVISION == '$WCREV$')
  273. {
  274. // This is NOT a version built using the buil system, just display the main version
  275. $sVersionString = "iTop Version ".ITOP_VERSION;
  276. }
  277. else
  278. {
  279. // This is a build made from SVN, let display the full information
  280. $sVersionString = "iTop Version ".ITOP_VERSION." revision ".ITOP_REVISION.", built on: ".ITOP_BUILD_DATE;
  281. }
  282. echo "<div id=\"Header\">\n";
  283. echo "<div class=\"iTopLogo\" title=\"$sVersionString\"><span>iTop</span></div>\n";
  284. //echo "<div id=\"GlobalSearch\"><div style=\"border: 1px solid #999; padding:1px; background-color:#fff;\"><img src=\"../images/magnifier.gif\"/><input style=\"border:0\" type=\"text\" size=\"15\" title=\"Global Search\"></input></div></div>\n";
  285. $sText = Utils::ReadParam('text', '');
  286. $sOnClick = "";
  287. if (empty($sText))
  288. {
  289. // if no search text is supplied then
  290. // 1) the search text is filled with "your search"
  291. // 2) clicking on it will erase it
  292. $sText = Dict::S("UI:YourSearch");
  293. $sOnClick = " onclick=\"this.value='';this.onclick=null;\"";
  294. }
  295. $sUserName = UserRights::GetUser();
  296. $sIsAdmin = UserRights::IsAdministrator() ? '(Administrator)' : '';
  297. if (UserRights::IsAdministrator())
  298. {
  299. $sLogonMessage = Dict::Format('UI:LoggedAsMessage+Admin', $sUserName);
  300. }
  301. else
  302. {
  303. $sLogonMessage = Dict::Format('UI:LoggedAsMessage', $sUserName);
  304. }
  305. $sLogOffBtn = Dict::S('UI:Button:Logoff');
  306. $sSearchBtn = Dict::S('UI:Button:GlobalSearch');
  307. echo "<div id=\"Login\" style=\"position:absolute; top:18px; right:16px; width:600px;\">{$sLogonMessage}&nbsp;&nbsp;";
  308. echo "<form action=\"../pages/UI.php\" method=\"post\" style=\"display:inline\">\n";
  309. echo "<input type=\"submit\" value=\"$sLogOffBtn\" />\n";
  310. echo "<input type=\"hidden\" name=\"loginop\" value=\"logoff\" />\n";
  311. echo "</form>\n";
  312. echo "<form action=\"../pages/UI.php\" style=\"display:inline\"><div style=\"padding:1px; background-color:#fff;display:inline;\"><img src=\"../images/magnifier.gif\"/><input style=\"border:0\" type=\"text\" size=\"15\" title=\"Global Search\" name=\"text\" value=\"$sText\"$sOnClick></input></div><input type=\"submit\" value=\"$sSearchBtn\" />
  313. <input type=\"hidden\" name=\"operation\" value=\"full_text\" /></form>\n";
  314. echo "</div>\n";
  315. echo "</div>\n";
  316. // Display the menu
  317. echo "<div id=\"MySplitter\">\n";
  318. echo " <div id=\"LeftPane\">\n";
  319. echo $this->m_sMenu;
  320. echo " </div> <!-- LeftPane -->\n";
  321. echo "<div id=\"RightPane\">\n";
  322. // Render the tabs in the page (if any)
  323. foreach($this->m_aTabs as $sTabContainerName => $m_aTabs)
  324. {
  325. $sTabs = '';
  326. $container_index = 0;
  327. if (count($m_aTabs) > 0)
  328. {
  329. $sTabs = "<!-- tabs -->\n<div id=\"tabbedContent_{$container_index}\" class=\"light\">\n";
  330. $sTabs .= "<ul>\n";
  331. // Display the unordered list that will be rendered as the tabs
  332. $i = 0;
  333. foreach($m_aTabs as $sTabName => $sTabContent)
  334. {
  335. $sTabs .= "<li><a href=\"#fragment_$i\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  336. $i++;
  337. }
  338. $sTabs .= "</ul>\n";
  339. // Now add the content of the tabs themselves
  340. $i = 0;
  341. foreach($m_aTabs as $sTabName => $sTabContent)
  342. {
  343. $sTabs .= "<div id=\"fragment_$i\">".$sTabContent."</div>\n";
  344. $i++;
  345. }
  346. $sTabs .= "</div>\n<!-- end of tabs-->\n";
  347. }
  348. $this->s_content = str_replace("\$Tabs:$sTabContainerName\$", $sTabs, $this->s_content);
  349. $container_index++;
  350. }
  351. // Display the page's content
  352. echo $this->s_content;
  353. // Add the captured output
  354. if (trim($s_captured_output) != "")
  355. {
  356. echo "<div class=\"raw_output\">$s_captured_output</div>\n";
  357. }
  358. echo $this->s_deferred_content;
  359. echo "<div class=\"jqmWindow\" id=\"ex2\">Please wait...</div>\n"; // jqModal Window
  360. echo "</div> <!-- RightPane -->\n";
  361. echo "</div> <!-- Splitter -->\n";
  362. echo "<div class=\"jqmWindow\" id=\"ModalDlg\"></div>";
  363. echo "</body>\n";
  364. echo "</html>\n";
  365. }
  366. public function AddTabContainer($sTabContainer)
  367. {
  368. $this->m_aTabs[$sTabContainer] = array();
  369. $this->add("\$Tabs:$sTabContainer\$");
  370. }
  371. public function AddToTab($sTabContainer, $sTabLabel, $sHtml)
  372. {
  373. if (!isset($this->m_aTabs[$sTabContainer][$sTabLabel]))
  374. {
  375. // Set the content of the tab
  376. $this->m_aTabs[$sTabContainer][$sTabLabel] = $sHtml;
  377. }
  378. else
  379. {
  380. // Append to the content of the tab
  381. $this->m_aTabs[$sTabContainer][$sTabLabel] .= $sHtml;
  382. }
  383. }
  384. public function SetCurrentTabContainer($sTabContainer = '')
  385. {
  386. $sPreviousTabContainer = $this->m_sCurrentTabContainer;
  387. $this->m_sCurrentTabContainer = $sTabContainer;
  388. return $sPreviousTabContainer;
  389. }
  390. public function SetCurrentTab($sTabLabel = '')
  391. {
  392. $sPreviousTab = $this->m_sCurrentTab;
  393. $this->m_sCurrentTab = $sTabLabel;
  394. return $sPreviousTab;
  395. }
  396. /**
  397. * Make the given tab the active one, as if it were clicked
  398. * DOES NOT WORK: apparently in the *old* version of jquery
  399. * that we are using this is not supported... TO DO upgrade
  400. * the whole jquery bundle...
  401. */
  402. public function SelectTab($sTabContainer, $sTabLabel)
  403. {
  404. $container_index = 0;
  405. $tab_index = 0;
  406. foreach($this->m_aTabs as $sCurrentTabContainerName => $aTabs)
  407. {
  408. if ($sTabContainer == $sCurrentTabContainerName)
  409. {
  410. foreach($aTabs as $sCurrentTabLabel => $void)
  411. {
  412. if ($sCurrentTabLabel == $sTabLabel)
  413. {
  414. break;
  415. }
  416. $tab_index++;
  417. }
  418. break;
  419. }
  420. $container_index++;
  421. }
  422. $sSelector = '#tabbedContent_'.$container_index.' > ul';
  423. $this->add_ready_script("$('$sSelector').tabs('select', $tab_index);");
  424. }
  425. public function StartCollapsibleSection($sSectionLabel, $bOpen = false)
  426. {
  427. $this->add($this->GetStartCollapsibleSection($sSectionLabel, $bOpen));
  428. }
  429. public function GetStartCollapsibleSection($sSectionLabel, $bOpen = false)
  430. {
  431. $sHtml = '';
  432. static $iSectionId = 0;
  433. $sImgStyle = $bOpen ? ' open' : '';
  434. $sHtml .= "<a id=\"LnkCollapse_$iSectionId\" class=\"CollapsibleLabel{$sImgStyle}\" href=\"#\">$sSectionLabel</a></br>\n";
  435. $sStyle = $bOpen ? '' : 'style="display:none" ';
  436. $sHtml .= "<div id=\"Collapse_$iSectionId\" $sStyle>";
  437. $this->add_ready_script("\$(\"#LnkCollapse_$iSectionId\").click(function() {\$(\"#Collapse_$iSectionId\").slideToggle('normal'); $(\"#LnkCollapse_$iSectionId\").toggleClass('open');});");
  438. //$this->add_ready_script("$('#LnkCollapse_$iSectionId').hide();");
  439. $iSectionId++;
  440. return $sHtml;
  441. }
  442. public function EndCollapsibleSection()
  443. {
  444. $this->add($this->GetEndCollapsibleSection());
  445. }
  446. public function GetEndCollapsibleSection()
  447. {
  448. return "</div>";
  449. }
  450. public function add($sHtml)
  451. {
  452. if (!empty($this->m_sCurrentTabContainer) && !empty($this->m_sCurrentTab))
  453. {
  454. $this->AddToTab($this->m_sCurrentTabContainer, $this->m_sCurrentTab, $sHtml);
  455. }
  456. else
  457. {
  458. parent::add($sHtml);
  459. }
  460. }
  461. /*
  462. public function AddSearchForm($sClassName, $bOpen = false)
  463. {
  464. $iSearchSectionId = 0;
  465. $sStyle = $bOpen ? 'SearchDrawer' : 'SearchDrawer DrawerClosed';
  466. $this->add("<div id=\"Search_$iSearchSectionId\" class=\"$sStyle\">\n");
  467. $this->add("<h1>Search form for ".Metamodel::GetName($sClassName)."</h1>\n");
  468. $this->add_ready_script("\$(\"#LnkSearch_$iSearchSectionId\").click(function() {\$(\"#Search_$iSearchSectionId\").slideToggle('normal'); $(\"#LnkSearch_$iSearchSectionId\").toggleClass('open');});");
  469. $oFilter = new DBObjectSearch($sClassName);
  470. $sFilter = $oFilter->serialize();
  471. $oSet = new CMDBObjectSet($oFilter);
  472. cmdbAbstractObject::DisplaySearchForm($this, $oSet, array('operation' => 'search', 'filter' => $sFilter, 'search_form' => true));
  473. $this->add("</div>\n");
  474. $this->add("<div class=\"HRDrawer\"/></div>\n");
  475. $this->add("<div id=\"LnkSearch_$iSearchSectionId\" class=\"DrawerHandle\">Search</div>\n");
  476. $iSearchSectionId++;
  477. }
  478. */
  479. }
  480. ?>