webpage.class.inc.php 31 KB

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