webpage.class.inc.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. <?php
  2. // Copyright (C) 2010-2013 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. * Class WebPage
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * Generic interface common to CLI and Web pages
  26. */
  27. Interface Page
  28. {
  29. public function output();
  30. public function add($sText);
  31. public function p($sText);
  32. public function pre($sText);
  33. public function add_comment($sText);
  34. public function table($aConfig, $aData, $aParams = array());
  35. }
  36. /**
  37. * Simple helper class to ease the production of HTML pages
  38. *
  39. * This class provide methods to add content, scripts, includes... to a web page
  40. * and renders the full web page by putting the elements in the proper place & order
  41. * when the output() method is called.
  42. * Usage:
  43. * $oPage = new WebPage("Title of my page");
  44. * $oPage->p("Hello World !");
  45. * $oPage->output();
  46. */
  47. class WebPage implements Page
  48. {
  49. protected $s_title;
  50. protected $s_content;
  51. protected $s_deferred_content;
  52. protected $a_scripts;
  53. protected $a_dict_entries;
  54. protected $a_styles;
  55. protected $a_include_scripts;
  56. protected $a_include_stylesheets;
  57. protected $a_headers;
  58. protected $a_base;
  59. protected $iNextId;
  60. protected $iTransactionId;
  61. protected $sContentType;
  62. protected $sContentDisposition;
  63. protected $sContentFileName;
  64. protected $s_sOutputFormat;
  65. protected $a_OutputOptions;
  66. public function __construct($s_title)
  67. {
  68. $this->s_title = $s_title;
  69. $this->s_content = "";
  70. $this->s_deferred_content = '';
  71. $this->a_scripts = array();
  72. $this->a_dict_entries = array();
  73. $this->a_styles = array();
  74. $this->a_linked_scripts = array();
  75. $this->a_linked_stylesheets = array();
  76. $this->a_headers = array();
  77. $this->a_base = array( 'href' => '', 'target' => '');
  78. $this->iNextId = 0;
  79. $this->iTransactionId = 0;
  80. $this->sContentType = '';
  81. $this->sContentDisposition = '';
  82. $this->sContentFileName = '';
  83. $this->s_OutputFormat = utils::ReadParam('output_format', 'html');
  84. $this->a_OutputOptions = array();
  85. ob_start(); // Start capturing the output
  86. }
  87. /**
  88. * Change the title of the page after its creation
  89. */
  90. public function set_title($s_title)
  91. {
  92. $this->s_title = $s_title;
  93. }
  94. /**
  95. * Specify a default URL and a default target for all links on a page
  96. */
  97. public function set_base($s_href = '', $s_target = '')
  98. {
  99. $this->a_base['href'] = $s_href;
  100. $this->a_base['target'] = $s_target;
  101. }
  102. /**
  103. * Add any text or HTML fragment to the body of the page
  104. */
  105. public function add($s_html)
  106. {
  107. $this->s_content .= $s_html;
  108. }
  109. /**
  110. * Add any text or HTML fragment (identified by an ID) at the end of the body of the page
  111. * This is useful to add hidden content, DIVs or FORMs that should not
  112. * be embedded into each other.
  113. */
  114. public function add_at_the_end($s_html, $sId = '')
  115. {
  116. $this->s_deferred_content .= $s_html;
  117. }
  118. /**
  119. * Add a paragraph to the body of the page
  120. */
  121. public function p($s_html)
  122. {
  123. $this->add($this->GetP($s_html));
  124. }
  125. /**
  126. * Add a pre-formatted text to the body of the page
  127. */
  128. public function pre($s_html)
  129. {
  130. $this->add('<pre>'.$s_html.'</pre>');
  131. }
  132. /**
  133. * Add a comment
  134. */
  135. public function add_comment($sText)
  136. {
  137. $this->add('<!--'.$sText.'-->');
  138. }
  139. /**
  140. * Add a paragraph to the body of the page
  141. */
  142. public function GetP($s_html)
  143. {
  144. return "<p>$s_html</p>\n";
  145. }
  146. /**
  147. * Adds a tabular content to the web page
  148. * @param Hash $aConfig Configuration of the table: hash array of 'column_id' => 'Column Label'
  149. * @param Hash $aData Hash array. Data to display in the table: each row is made of 'column_id' => Data. A column 'pkey' is expected for each row
  150. * @param Hash $aParams Hash array. Extra parameters for the table.
  151. * @return void
  152. */
  153. public function table($aConfig, $aData, $aParams = array())
  154. {
  155. $this->add($this->GetTable($aConfig, $aData, $aParams));
  156. }
  157. public function GetTable($aConfig, $aData, $aParams = array())
  158. {
  159. $oAppContext = new ApplicationContext();
  160. static $iNbTables = 0;
  161. $iNbTables++;
  162. $sHtml = "";
  163. $sHtml .= "<table class=\"listResults\">\n";
  164. $sHtml .= "<thead>\n";
  165. $sHtml .= "<tr>\n";
  166. foreach($aConfig as $sName=>$aDef)
  167. {
  168. $sHtml .= "<th title=\"".$aDef['description']."\">".$aDef['label']."</th>\n";
  169. }
  170. $sHtml .= "</tr>\n";
  171. $sHtml .= "</thead>\n";
  172. $sHtml .= "<tbody>\n";
  173. foreach($aData as $aRow)
  174. {
  175. $sHtml .= $this->GetTableRow($aRow, $aConfig);
  176. }
  177. $sHtml .= "</tbody>\n";
  178. $sHtml .= "</table>\n";
  179. return $sHtml;
  180. }
  181. public function GetTableRow($aRow, $aConfig)
  182. {
  183. $sHtml = '';
  184. if (isset($aRow['@class'])) // Row specific class, for hilighting certain rows
  185. {
  186. $sHtml .= "<tr class=\"{$aRow['@class']}\">";
  187. }
  188. else
  189. {
  190. $sHtml .= "<tr>";
  191. }
  192. foreach($aConfig as $sName=>$aAttribs)
  193. {
  194. $sClass = isset($aAttribs['class']) ? 'class="'.$aAttribs['class'].'"' : '';
  195. $sValue = ($aRow[$sName] === '') ? '&nbsp;' : $aRow[$sName];
  196. $sHtml .= "<td $sClass>$sValue</td>";
  197. }
  198. $sHtml .= "</tr>";
  199. return $sHtml;
  200. }
  201. /**
  202. * Add some Javascript to the header of the page
  203. */
  204. public function add_script($s_script)
  205. {
  206. $this->a_scripts[] = $s_script;
  207. }
  208. /**
  209. * Add some Javascript to the header of the page
  210. */
  211. public function add_ready_script($s_script)
  212. {
  213. // Do nothing silently... this is not supported by this type of page...
  214. }
  215. /**
  216. * Add a dictionary entry for the Javascript side
  217. */
  218. public function add_dict_entry($s_entryId)
  219. {
  220. $this->a_dict_entries[$s_entryId] = Dict::S($s_entryId);
  221. }
  222. /**
  223. * Add some CSS definitions to the header of the page
  224. */
  225. public function add_style($s_style)
  226. {
  227. $this->a_styles[] = $s_style;
  228. }
  229. /**
  230. * Add a script (as an include, i.e. link) to the header of the page
  231. */
  232. public function add_linked_script($s_linked_script)
  233. {
  234. $this->a_linked_scripts[$s_linked_script] = $s_linked_script;
  235. }
  236. /**
  237. * Add a CSS stylesheet (as an include, i.e. link) to the header of the page
  238. */
  239. public function add_linked_stylesheet($s_linked_stylesheet, $s_condition = "")
  240. {
  241. $this->a_linked_stylesheets[] = array( 'link' => $s_linked_stylesheet, 'condition' => $s_condition);
  242. }
  243. /**
  244. * Add some custom header to the page
  245. */
  246. public function add_header($s_header)
  247. {
  248. $this->a_headers[] = $s_header;
  249. }
  250. /**
  251. * Add needed headers to the page so that it will no be cached
  252. */
  253. public function no_cache()
  254. {
  255. $this->add_header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  256. $this->add_header("Expires: Fri, 17 Jul 1970 05:00:00 GMT"); // Date in the past
  257. }
  258. /**
  259. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  260. */
  261. public function details($aFields)
  262. {
  263. $this->add($this->GetDetails($aFields));
  264. }
  265. /**
  266. * Records the current state of the 'html' part of the page output
  267. * @return mixed The current state of the 'html' output
  268. */
  269. public function start_capture()
  270. {
  271. return strlen($this->s_content);
  272. }
  273. /**
  274. * Returns the part of the html output that occurred since the call to start_capture
  275. * and removes this part from the current html output
  276. * @param $offset mixed The value returned by start_capture
  277. * @return string The part of the html output that was added since the call to start_capture
  278. */
  279. public function end_capture($offset)
  280. {
  281. $sCaptured = substr($this->s_content, $offset);
  282. $this->s_content = substr($this->s_content, 0, $offset);
  283. return $sCaptured;
  284. }
  285. /**
  286. * Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
  287. */
  288. public function GetDetails($aFields)
  289. {
  290. $sHtml = "<table class=\"details\">\n";
  291. $sHtml .= "<tbody>\n";
  292. foreach($aFields as $aAttrib)
  293. {
  294. $sHtml .= "<tr>\n";
  295. // By Rom, for csv import, proposed to show several values for column selection
  296. if (is_array($aAttrib['value']))
  297. {
  298. $sHtml .= "<td class=\"label\">".$aAttrib['label']."</td><td>".implode("</td><td>", $aAttrib['value'])."</td>\n";
  299. }
  300. else
  301. {
  302. $sHtml .= "<td class=\"label\">".$aAttrib['label']."</td><td>".$aAttrib['value']."</td>\n";
  303. }
  304. $sComment = (isset($aAttrib['comments'])) ? $aAttrib['comments'] : '&nbsp;';
  305. $sInfo = (isset($aAttrib['infos'])) ? $aAttrib['infos'] : '&nbsp;';
  306. $sHtml .= "<td>$sComment</td><td>$sInfo</td>\n";
  307. $sHtml .= "</tr>\n";
  308. }
  309. $sHtml .= "</tbody>\n";
  310. $sHtml .= "</table>\n";
  311. return $sHtml;
  312. }
  313. /**
  314. * Build a set of radio buttons suitable for editing a field/attribute of an object (including its validation)
  315. * @param $aAllowedValues hash Array of value => display_value
  316. * @param $value mixed Current value for the field/attribute
  317. * @param $iId mixed Unique Id for the input control in the page
  318. * @param $sFieldName string The name of the field, attr_<$sFieldName> will hold the value for the field
  319. * @param $bMandatory bool Whether or not the field is mandatory
  320. * @param $bVertical bool Disposition of the radio buttons vertical or horizontal
  321. * @param $sValidationField string HTML fragment holding the validation field (exclamation icon...)
  322. * @return string The HTML fragment corresponding to the radio buttons
  323. */
  324. public function GetRadioButtons($aAllowedValues, $value, $iId, $sFieldName, $bMandatory, $bVertical, $sValidationField)
  325. {
  326. $idx = 0;
  327. $sHTMLValue = '';
  328. foreach($aAllowedValues as $key => $display_value)
  329. {
  330. if ((count($aAllowedValues) == 1) && ($bMandatory == 'true') )
  331. {
  332. // When there is only once choice, select it by default
  333. $sSelected = ' checked';
  334. }
  335. else
  336. {
  337. $sSelected = ($value == $key) ? ' checked' : '';
  338. }
  339. $sHTMLValue .= "<input type=\"radio\" id=\"{$iId}_{$key}\" name=\"radio_$sFieldName\" onChange=\"$('#{$iId}').val(this.value).trigger('change');\" value=\"$key\"$sSelected><label class=\"radio\" for=\"{$iId}_{$key}\">&nbsp;$display_value</label>&nbsp;";
  340. if ($bVertical)
  341. {
  342. if ($idx == 0)
  343. {
  344. // Validation icon at the end of the first line
  345. $sHTMLValue .= "&nbsp;{$sValidationField}\n";
  346. }
  347. $sHTMLValue .= "<br>\n";
  348. }
  349. $idx++;
  350. }
  351. $sHTMLValue .= "<input type=\"hidden\" id=\"$iId\" name=\"$sFieldName\" value=\"$value\"/>";
  352. if (!$bVertical)
  353. {
  354. // Validation icon at the end of the line
  355. $sHTMLValue .= "&nbsp;{$sValidationField}\n";
  356. }
  357. return $sHTMLValue;
  358. }
  359. /**
  360. * Discard unexpected output data
  361. * This is a MUST when the Page output is DATA (download of a document, download CSV export, download ...)
  362. */
  363. public function TrashUnexpectedOutput()
  364. {
  365. // This protection is redundant with a protection implemented in MetaModel::IncludeModule
  366. // which detects such issues while loading module files
  367. // Here, the purpose is to detect and discard characters produced by the code execution (echo)
  368. $sPreviousContent = ob_get_clean();
  369. if (trim($sPreviousContent) != '')
  370. {
  371. if (Utils::GetConfig() && Utils::GetConfig()->Get('debug_report_spurious_chars'))
  372. {
  373. IssueLog::Error("Output already started before downloading file:\nContent was:'$sPreviousContent'\n");
  374. }
  375. }
  376. }
  377. /**
  378. * Outputs (via some echo) the complete HTML page by assembling all its elements
  379. */
  380. public function output()
  381. {
  382. foreach($this->a_headers as $s_header)
  383. {
  384. header($s_header);
  385. }
  386. $s_captured_output = ob_get_contents();
  387. ob_end_clean();
  388. echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  389. echo "<html>\n";
  390. echo "<head>\n";
  391. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
  392. echo "<title>".htmlentities($this->s_title, ENT_QUOTES, 'UTF-8')."</title>\n";
  393. echo $this->get_base_tag();
  394. foreach($this->a_linked_scripts as $s_script)
  395. {
  396. // Make sure that the URL to the script contains the application's version number
  397. // so that the new script do NOT get reloaded from the cache when the application is upgraded
  398. if (strpos($s_script, '?') === false)
  399. {
  400. $s_script .= "?itopversion=".ITOP_VERSION;
  401. }
  402. else
  403. {
  404. $s_script .= "&itopversion=".ITOP_VERSION;
  405. }
  406. echo "<script type=\"text/javascript\" src=\"$s_script\"></script>\n";
  407. }
  408. if (count($this->a_scripts)>0)
  409. {
  410. echo "<script type=\"text/javascript\">\n";
  411. foreach($this->a_scripts as $s_script)
  412. {
  413. echo "$s_script\n";
  414. }
  415. echo "</script>\n";
  416. }
  417. $this->output_dict_entries();
  418. foreach($this->a_linked_stylesheets as $a_stylesheet)
  419. {
  420. if ($a_stylesheet['condition'] != "")
  421. {
  422. echo "<!--[if {$a_stylesheet['condition']}]>\n";
  423. }
  424. echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$a_stylesheet['link']}\" />\n";
  425. if ($a_stylesheet['condition'] != "")
  426. {
  427. echo "<![endif]-->\n";
  428. }
  429. }
  430. if (count($this->a_styles)>0)
  431. {
  432. echo "<style>\n";
  433. foreach($this->a_styles as $s_style)
  434. {
  435. echo "$s_style\n";
  436. }
  437. echo "</style>\n";
  438. }
  439. if (class_exists('MetaModel') && MetaModel::GetConfig())
  440. {
  441. echo "<link rel=\"shortcut icon\" href=\"".utils::GetAbsoluteUrlAppRoot()."images/favicon.ico\" />\n";
  442. }
  443. echo "</head>\n";
  444. echo "<body>\n";
  445. echo self::FilterXSS($this->s_content);
  446. if (trim($s_captured_output) != "")
  447. {
  448. echo "<div class=\"raw_output\">".self::FilterXSS($s_captured_output)."</div>\n";
  449. }
  450. echo '<div id="at_the_end">'.self::FilterXSS($this->s_deferred_content).'</div>';
  451. echo "</body>\n";
  452. echo "</html>\n";
  453. if (class_exists('MetaModel'))
  454. {
  455. MetaModel::RecordQueryTrace();
  456. }
  457. if (class_exists('ExecutionKPI'))
  458. {
  459. ExecutionKPI::ReportStats();
  460. }
  461. }
  462. /**
  463. * Build a series of hidden field[s] from an array
  464. */
  465. public function add_input_hidden($sLabel, $aData)
  466. {
  467. foreach($aData as $sKey => $sValue)
  468. {
  469. // Note: protection added to protect against the Notice 'array to string conversion' that appeared with PHP 5.4
  470. // (this function seems unused though!)
  471. if (is_scalar($sValue))
  472. {
  473. $this->add("<input type=\"hidden\" name=\"".$sLabel."[$sKey]\" value=\"$sValue\">");
  474. }
  475. }
  476. }
  477. protected function get_base_tag()
  478. {
  479. $sTag = '';
  480. if (($this->a_base['href'] != '') || ($this->a_base['target'] != ''))
  481. {
  482. $sTag = '<base ';
  483. if (($this->a_base['href'] != ''))
  484. {
  485. $sTag .= "href =\"{$this->a_base['href']}\" ";
  486. }
  487. if (($this->a_base['target'] != ''))
  488. {
  489. $sTag .= "target =\"{$this->a_base['target']}\" ";
  490. }
  491. $sTag .= " />\n";
  492. }
  493. return $sTag;
  494. }
  495. /**
  496. * Get an ID (for any kind of HTML tag) that is guaranteed unique in this page
  497. * @return int The unique ID (in this page)
  498. */
  499. public function GetUniqueId()
  500. {
  501. return $this->iNextId++;
  502. }
  503. /**
  504. * Set the content-type (mime type) for the page's content
  505. * @param $sContentType string
  506. * @return void
  507. */
  508. public function SetContentType($sContentType)
  509. {
  510. $this->sContentType = $sContentType;
  511. }
  512. /**
  513. * Set the content-disposition (mime type) for the page's content
  514. * @param $sDisposition string The disposition: 'inline' or 'attachment'
  515. * @param $sFileName string The original name of the file
  516. * @return void
  517. */
  518. public function SetContentDisposition($sDisposition, $sFileName)
  519. {
  520. $this->sContentDisposition = $sDisposition;
  521. $this->sContentFileName = $sFileName;
  522. }
  523. /**
  524. * Set the transactionId of the current form
  525. * @param $iTransactionId integer
  526. * @return void
  527. */
  528. public function SetTransactionId($iTransactionId)
  529. {
  530. $this->iTransactionId = $iTransactionId;
  531. }
  532. /**
  533. * Returns the transactionId of the current form
  534. * @return integer The current transactionID
  535. */
  536. public function GetTransactionId()
  537. {
  538. return $this->iTransactionId;
  539. }
  540. public static function FilterXSS($sHTML)
  541. {
  542. return str_ireplace('<script', '&lt;script', $sHTML);
  543. }
  544. /**
  545. * What is the currently selected output format
  546. * @return string The selected output format: html, pdf...
  547. */
  548. public function GetOutputFormat()
  549. {
  550. return $this->s_OutputFormat;
  551. }
  552. /**
  553. * Check whether the desired output format is possible or not
  554. * @param string $sOutputFormat The desired output format: html, pdf...
  555. * @return bool True if the format is Ok, false otherwise
  556. */
  557. function IsOutputFormatAvailable($sOutputFormat)
  558. {
  559. $bResult = false;
  560. switch($sOutputFormat)
  561. {
  562. case 'html':
  563. $bResult = true; // Always supported
  564. break;
  565. case 'pdf':
  566. $bResult = @is_readable(APPROOT.'lib/MPDF/mpdf.php');
  567. break;
  568. }
  569. return $bResult;
  570. }
  571. /**
  572. * Retrieves the value of a named output option for the given format
  573. * @param string $sFormat The format: html or pdf
  574. * @param string $sOptionName The name of the option
  575. * @return mixed false if the option was never set or the options's value
  576. */
  577. public function GetOutputOption($sFormat, $sOptionName)
  578. {
  579. if (isset($this->a_OutputOptions[$sFormat][$sOptionName]))
  580. {
  581. return $this->a_OutputOptions[$sFormat][$sOptionName];
  582. }
  583. return false;
  584. }
  585. /**
  586. * Sets a named output option for the given format
  587. * @param string $sFormat The format for which to set the option: html or pdf
  588. * @param string $sOptionName the name of the option
  589. * @param mixed $sValue The value of the option
  590. */
  591. public function SetOutputOption($sFormat, $sOptionName, $sValue)
  592. {
  593. if (!isset($this->a_OutputOptions[$sFormat]))
  594. {
  595. $this->a_OutputOptions[$sFormat] = array($sOptionName => $sValue);
  596. }
  597. else
  598. {
  599. $this->a_OutputOptions[$sFormat][$sOptionName] = $sValue;
  600. }
  601. }
  602. public function RenderPopupMenuItems($aActions, $aFavoriteActions = array())
  603. {
  604. $sPrevUrl = '';
  605. $sHtml = '';
  606. foreach ($aActions as $aAction)
  607. {
  608. $sClass = isset($aAction['class']) ? " class=\"{$aAction['class']}\"" : "";
  609. $sOnClick = isset($aAction['onclick']) ? " onclick=\"{$aAction['onclick']}\"" : "";
  610. $sTarget = isset($aAction['target']) ? " target=\"{$aAction['target']}\"" : "";
  611. if (empty($aAction['url']))
  612. {
  613. if ($sPrevUrl != '') // Don't output consecutively two separators...
  614. {
  615. $sHtml .= "<li>{$aAction['label']}</li>";
  616. }
  617. $sPrevUrl = '';
  618. }
  619. else
  620. {
  621. $sHtml .= "<li><a $sTarget href=\"{$aAction['url']}\"$sClass $sOnClick>{$aAction['label']}</a></li>";
  622. $sPrevUrl = $aAction['url'];
  623. }
  624. }
  625. $sHtml .= "</ul></li></ul></div>";
  626. foreach(array_reverse($aFavoriteActions) as $aAction)
  627. {
  628. $sTarget = isset($aAction['target']) ? " target=\"{$aAction['target']}\"" : "";
  629. $sHtml .= "<div class=\"actions_button\"><a $sTarget href='{$aAction['url']}'>{$aAction['label']}</a></div>";
  630. }
  631. return $sHtml;
  632. }
  633. protected function output_dict_entries($bReturnOutput = false)
  634. {
  635. $sHtml = '';
  636. if (count($this->a_dict_entries)>0)
  637. {
  638. $sHtml .= "<script type=\"text/javascript\">\n";
  639. $sHtml .= "var Dict = {};\n";
  640. $sHtml .= "Dict._entries = {};\n";
  641. $sHtml .= "Dict.S = function(sEntry) {\n";
  642. $sHtml .= " if (sEntry in Dict._entries)\n";
  643. $sHtml .= " {\n";
  644. $sHtml .= " return Dict._entries[sEntry];\n";
  645. $sHtml .= " }\n";
  646. $sHtml .= " else\n";
  647. $sHtml .= " {\n";
  648. $sHtml .= " return sEntry;\n";
  649. $sHtml .= " }\n";
  650. $sHtml .= "};\n";
  651. foreach($this->a_dict_entries as $s_entry => $s_value)
  652. {
  653. $sHtml .= "Dict._entries['$s_entry'] = '".addslashes($s_value)."';\n";
  654. }
  655. $sHtml .= "</script>\n";
  656. }
  657. if ($bReturnOutput)
  658. {
  659. return $sHtml;
  660. }
  661. else
  662. {
  663. echo $sHtml;
  664. }
  665. }
  666. }
  667. interface iTabbedPage
  668. {
  669. public function AddTabContainer($sTabContainer, $sPrefix = '');
  670. public function AddToTab($sTabContainer, $sTabLabel, $sHtml);
  671. public function SetCurrentTabContainer($sTabContainer = '');
  672. public function SetCurrentTab($sTabLabel = '');
  673. /**
  674. * Add a tab which content will be loaded asynchronously via the supplied URL
  675. *
  676. * Limitations:
  677. * Cross site scripting is not not allowed for security reasons. Use a normal tab with an IFRAME if you want to pull content from another server.
  678. * Static content cannot be added inside such tabs.
  679. *
  680. * @param string $sTabLabel The (localised) label of the tab
  681. * @param string $sUrl The URL to load (on the same server)
  682. * @param boolean $bCache Whether or not to cache the content of the tab once it has been loaded. flase will cause the tab to be reloaded upon each activation.
  683. * @since 2.0.3
  684. */
  685. public function AddAjaxTab($sTabLabel, $sUrl, $bCache = true);
  686. public function GetCurrentTab();
  687. public function RemoveTab($sTabLabel, $sTabContainer = null);
  688. /**
  689. * Finds the tab whose title matches a given pattern
  690. * @return mixed The name of the tab as a string or false if not found
  691. */
  692. public function FindTab($sPattern, $sTabContainer = null);
  693. }
  694. /**
  695. * Helper class to implement JQueryUI tabs inside a page
  696. */
  697. class TabManager
  698. {
  699. protected $m_aTabs;
  700. protected $m_sCurrentTabContainer;
  701. protected $m_sCurrentTab;
  702. public function __construct()
  703. {
  704. $this->m_aTabs = array();
  705. $this->m_sCurrentTabContainer = '';
  706. $this->m_sCurrentTab = '';
  707. }
  708. public function AddTabContainer($sTabContainer, $sPrefix = '')
  709. {
  710. $this->m_aTabs[$sTabContainer] = array('prefix' => $sPrefix, 'tabs' => array());
  711. return "\$Tabs:$sTabContainer\$";
  712. }
  713. public function AddToCurrentTab($sHtml)
  714. {
  715. $this->AddToTab($this->m_sCurrentTabContainer, $this->m_sCurrentTab, $sHtml);
  716. }
  717. public function GetCurrentTabLength($sHtml)
  718. {
  719. $iLength = isset($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html']) ? strlen($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html']): 0;
  720. return $iLength;
  721. }
  722. public function TruncateTab($sTabContainer, $sTab, $iLength)
  723. {
  724. $this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html'] = substr($this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$this->m_sCurrentTab]['html'], 0, $iLength);
  725. }
  726. public function TabExists($sTabContainer, $sTab)
  727. {
  728. return isset($this->m_aTabs[$sTabContainer]['tabs'][$sTab]);
  729. }
  730. public function TabsContainerCount()
  731. {
  732. return count($this->m_aTabs);
  733. }
  734. public function AddToTab($sTabContainer, $sTabLabel, $sHtml)
  735. {
  736. if (!isset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]))
  737. {
  738. // Set the content of the tab
  739. $this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel] = array(
  740. 'type' => 'html',
  741. 'html' => $sHtml,
  742. );
  743. }
  744. else
  745. {
  746. if ($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['type'] != 'html')
  747. {
  748. throw new Exception("Cannot add HTML content to the tab '$sTabLabel' of type '{$this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['type']}'");
  749. }
  750. // Append to the content of the tab
  751. $this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]['html'] .= $sHtml;
  752. }
  753. return ''; // Nothing to add to the page for now
  754. }
  755. public function SetCurrentTabContainer($sTabContainer = '')
  756. {
  757. $sPreviousTabContainer = $this->m_sCurrentTabContainer;
  758. $this->m_sCurrentTabContainer = $sTabContainer;
  759. return $sPreviousTabContainer;
  760. }
  761. public function SetCurrentTab($sTabLabel = '')
  762. {
  763. $sPreviousTab = $this->m_sCurrentTab;
  764. $this->m_sCurrentTab = $sTabLabel;
  765. return $sPreviousTab;
  766. }
  767. /**
  768. * Add a tab which content will be loaded asynchronously via the supplied URL
  769. *
  770. * Limitations:
  771. * Cross site scripting is not not allowed for security reasons. Use a normal tab with an IFRAME if you want to pull content from another server.
  772. * Static content cannot be added inside such tabs.
  773. *
  774. * @param string $sTabLabel The (localised) label of the tab
  775. * @param string $sUrl The URL to load (on the same server)
  776. * @param boolean $bCache Whether or not to cache the content of the tab once it has been loaded. flase will cause the tab to be reloaded upon each activation.
  777. * @since 2.0.3
  778. */
  779. public function AddAjaxTab($sTabLabel, $sUrl, $bCache = true)
  780. {
  781. // Set the content of the tab
  782. $this->m_aTabs[$this->m_sCurrentTabContainer]['tabs'][$sTabLabel] = array(
  783. 'type' => 'ajax',
  784. 'url' => $sUrl,
  785. 'cache' => $bCache,
  786. );
  787. return ''; // Nothing to add to the page for now
  788. }
  789. public function GetCurrentTabContainer()
  790. {
  791. return $this->m_sCurrentTabContainer;
  792. }
  793. public function GetCurrentTab()
  794. {
  795. return $this->m_sCurrentTab;
  796. }
  797. public function RemoveTab($sTabLabel, $sTabContainer = null)
  798. {
  799. if ($sTabContainer == null)
  800. {
  801. $sTabContainer = $this->m_sCurrentTabContainer;
  802. }
  803. if (isset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]))
  804. {
  805. // Delete the content of the tab
  806. unset($this->m_aTabs[$sTabContainer]['tabs'][$sTabLabel]);
  807. // If we just removed the active tab, let's reset the active tab
  808. if (($this->m_sCurrentTabContainer == $sTabContainer) && ($this->m_sCurrentTab == $sTabLabel))
  809. {
  810. $this->m_sCurrentTab = '';
  811. }
  812. }
  813. }
  814. /**
  815. * Finds the tab whose title matches a given pattern
  816. * @return mixed The name of the tab as a string or false if not found
  817. */
  818. public function FindTab($sPattern, $sTabContainer = null)
  819. {
  820. $return = false;
  821. if ($sTabContainer == null)
  822. {
  823. $sTabContainer = $this->m_sCurrentTabContainer;
  824. }
  825. foreach($this->m_aTabs[$sTabContainer]['tabs'] as $sTabLabel => $void)
  826. {
  827. if (preg_match($sPattern, $sTabLabel))
  828. {
  829. $result = $sTabLabel;
  830. break;
  831. }
  832. }
  833. return $result;
  834. }
  835. /**
  836. * Make the given tab the active one, as if it were clicked
  837. * DOES NOT WORK: apparently in the *old* version of jquery
  838. * that we are using this is not supported... TO DO upgrade
  839. * the whole jquery bundle...
  840. */
  841. public function SelectTab($sTabContainer, $sTabLabel)
  842. {
  843. $container_index = 0;
  844. $tab_index = 0;
  845. foreach($this->m_aTabs as $sCurrentTabContainerName => $aTabs)
  846. {
  847. if ($sTabContainer == $sCurrentTabContainerName)
  848. {
  849. foreach($aTabs['tabs'] as $sCurrentTabLabel => $void)
  850. {
  851. if ($sCurrentTabLabel == $sTabLabel)
  852. {
  853. break;
  854. }
  855. $tab_index++;
  856. }
  857. break;
  858. }
  859. $container_index++;
  860. }
  861. $sSelector = '#tabbedContent_'.$container_index.' > ul';
  862. return "window.setTimeout(\"$('$sSelector').tabs('select', $tab_index);\", 100);"; // Let the time to the tabs widget to initialize
  863. }
  864. public function RenderIntoContent($sContent)
  865. {
  866. // Render the tabs in the page (if any)
  867. foreach($this->m_aTabs as $sTabContainerName => $aTabs)
  868. {
  869. $sTabs = '';
  870. $sPrefix = $aTabs['prefix'];
  871. $container_index = 0;
  872. if (count($aTabs['tabs']) > 0)
  873. {
  874. $sTabs = "<!-- tabs -->\n<div id=\"tabbedContent_{$sPrefix}{$container_index}\" class=\"light\">\n";
  875. $sTabs .= "<ul>\n";
  876. // Display the unordered list that will be rendered as the tabs
  877. $i = 0;
  878. foreach($aTabs['tabs'] as $sTabName => $aTabData)
  879. {
  880. switch($aTabData['type'])
  881. {
  882. case 'ajax':
  883. $sTabs .= "<li data-cache=\"".($aTabData['cache'] ? 'true' : 'false')."\"><a href=\"{$aTabData['url']}\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  884. break;
  885. case 'html':
  886. default:
  887. $sTabs .= "<li><a href=\"#tab_{$sPrefix}{$container_index}$i\" class=\"tab\"><span>".htmlentities($sTabName, ENT_QUOTES, 'UTF-8')."</span></a></li>\n";
  888. }
  889. $i++;
  890. }
  891. $sTabs .= "</ul>\n";
  892. // Now add the content of the tabs themselves
  893. $i = 0;
  894. foreach($aTabs['tabs'] as $sTabName => $aTabData)
  895. {
  896. switch($aTabData['type'])
  897. {
  898. case 'ajax':
  899. // Nothing to add
  900. break;
  901. case 'html':
  902. default:
  903. $sTabs .= "<div id=\"tab_{$sPrefix}{$container_index}$i\">".$aTabData['html']."</div>\n";
  904. }
  905. $i++;
  906. }
  907. $sTabs .= "</div>\n<!-- end of tabs-->\n";
  908. }
  909. $sContent = str_replace("\$Tabs:$sTabContainerName\$", $sTabs, $sContent);
  910. $container_index++;
  911. }
  912. return $sContent;
  913. }
  914. }