PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/php-getid3-2.0.0b5/sample_apps/morg/morg/abstraction.php

#
PHP | 2615 lines | 1545 code | 515 blank | 555 comment | 273 complexity | 0ede6eef6952d729c8d9a1a1ab4f07a1 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5.1.4 |
  4. // +----------------------------------------------------------------------+
  5. // | Placed in public domain by Allan Hansen, 2003-2006. |
  6. // | Share and enjoy! |
  7. // +----------------------------------------------------------------------+
  8. // | Updates and other scripts by Allan Hansen here: |
  9. // | http://www.artemis.dk/php/ |
  10. // +----------------------------------------------------------------------+
  11. // | abstraction.php |
  12. // | Abstract PHP classes to generate and output HTML/XML. |
  13. // +----------------------------------------------------------------------+
  14. // | Authors: Allan Hansen <ah@artemis.dk> |
  15. // +----------------------------------------------------------------------+
  16. //
  17. // $Id: abstraction.php,v 1.7 2007/01/03 16:15:59 ah Exp $
  18. /**
  19. * HTML/XML Generator.
  20. *
  21. * @version 1
  22. */
  23. class xml_gen
  24. {
  25. /**
  26. * Generate XML for plain link.
  27. *
  28. * If $uri is false a grey text will be displayed instead.
  29. *
  30. * @param string uri URI to open
  31. * @param string text Text or HTML used for linking
  32. * @param string attr Additional HTML attributes, e.g. "target=main width='100%'".
  33. */
  34. public static function a($uri, $text, $attr = false)
  35. {
  36. if (!$uri) {
  37. return "<span style=\"color: #999999\" $attr><font color=\"#999999\">$text</font></span>";
  38. }
  39. // Replace & with &amp; in uri
  40. $uri = str_replace('&', '&amp;', $uri);
  41. return "<a href=\"$uri\" $attr>$text</a>";
  42. }
  43. /**
  44. * Generate XML code for image.
  45. *
  46. * Automatically locates file- looks for it in any ../ (max 20 levels)
  47. * Can extract width and height automatically.
  48. * Border defaults to 0. Can be overridden with $attr
  49. *
  50. * @param string filename Relative filename of picture or uri.
  51. * @param string width Forced width of picture.
  52. * @param string height Forced height of picture.
  53. * @param string attr Additional HTML attributes, e.g. "target=main width='100%'".
  54. * @return string Generated XML.
  55. */
  56. public static function img($filename, $attr = false, $width = false, $height = false)
  57. {
  58. // If filename is a local file, locate it somewhere in (../)*
  59. if (!strstr($filename, "http://") && !strstr($filename, "?")) {
  60. $name = preg_replace("/^\//", "", $filename);
  61. $i= 21;
  62. while (--$i && !file_exists($name)) {
  63. $name = '../'.$name;
  64. }
  65. if (!file_exists($name)) {
  66. return false;
  67. }
  68. $fn_size = $name;
  69. // Replace relative filename with correct path
  70. if (@$filename[0] != "/") {
  71. $filename = $name;
  72. }
  73. }
  74. // Get height and width
  75. if (!empty($fn_size) && (!$height || !$width)) {
  76. $sz = GetImageSize($fn_size);
  77. if (!$width) {
  78. $width= $sz[0];
  79. }
  80. if (!$height) {
  81. $height= $sz[1];
  82. }
  83. }
  84. // Add border code?
  85. $border= (stristr(" $attr", " border=")) ? '' : "border='0'";
  86. // Add alt code?
  87. $alt= (stristr(" $attr", " alt=")) ? '' : 'alt=""';
  88. // Replace & with &amp; in filename
  89. $filename = str_replace('&', '&amp;', $filename);
  90. // Output
  91. $result = "<img src=\"$filename\"";
  92. if ($width) {
  93. $result .= " width=\"$width\"";
  94. }
  95. if ($height) {
  96. $result .= " height=\"$height\"";
  97. }
  98. $result .= " $attr $border $alt />";
  99. return $result;
  100. }
  101. /**
  102. * Generate XML for pictorial link with image swapping.
  103. *
  104. * If uri is null, plain picture is displayed
  105. * Width and height of images are auto detected.
  106. * If using image swapping, images are preloaded with javascript.
  107. *
  108. * @param string uri URI to open
  109. * @param string filename Filename for image to display.
  110. * @param string attr Additional HTML attributes, e.g. "target=main width='100%'"
  111. * @param string m_over Filename for image to display when mouse is over image.
  112. * Can contain onmouse* -- will merge with our code (our code
  113. * will be executed first!). Except for onmouse* $attr is only
  114. * added to <img tag. Cannot contain onmouse* code. Refer to $onmouse_merge
  115. * @param array otherswap Swap other image: array ($name, $normal, $m_over).
  116. * @param array onmouse_merge Addition onmouseover/out code to merge with our:
  117. * array ($m_name_merge, $m_out_merge).
  118. */
  119. public static function a_img($uri, $filename, $attr = false, $m_over = false, $otherswap = false, $onmouse_merge = false)
  120. {
  121. // $uri is false, just output picture
  122. if (!$uri) {
  123. return xml_gen::img($filename);
  124. }
  125. // Number of times xml_gen::a_img() has been called with $m_over.
  126. static $called;
  127. // Init
  128. $swap_out = false;
  129. $swap_over = false;
  130. // Generate onmouse* code
  131. if ($m_over || $otherswap) {
  132. // Increment called var
  133. $called++;
  134. // Has mouse over?
  135. if ($m_over) {
  136. $swap_out = "swp('spluf$called','spl_f$called'); ";
  137. $swap_over = "swp('spluf$called','splof$called'); ";
  138. }
  139. // Otherswap?
  140. if ($otherswap) {
  141. list($os_name, $os_normal, $os_m_over)= $otherswap;
  142. if (!$os_name || !$os_normal || !$os_m_over) {
  143. die("$otherswap requires array with three string.");
  144. }
  145. $swap_out .= "swp('$os_name','spl_q$called'); ";
  146. $swap_over .= "swp('$os_name','sploq$called'); ";
  147. }
  148. }
  149. // Onmouse* merge`?
  150. if ($onmouse_merge) {
  151. list($merge_over, $merge_out)= $onmouse_merge;
  152. if (!$merge_over || !$merge_out) {
  153. die("$onmouse_merge requires array with two strings.");
  154. }
  155. $swap_out .= $merge_out;
  156. $swap_over .= $merge_over;
  157. }
  158. // Set name only if swapping
  159. $name = ($m_over || $otherswap) ? "name='spluf$called'" : "";
  160. // Output link and picture
  161. $result = xml_gen::a($uri, xml_gen::img($filename, "$name $attr"), "onclick='this.blur()' onmouseout=\"$swap_out\" onmouseover=\"$swap_over\"");
  162. // If no m_over, return result
  163. if (!$m_over && !$otherswap) {
  164. return $result;
  165. }
  166. // Else preload images
  167. $result .= "<script type='text/javascript'>\n<!--\n\n";
  168. // On first a_img() call, we insert the swp() javascript function.
  169. if ($called == 1) {
  170. $result .= "function swp(id,name) { if (document.images) document.images[id].src = eval(name+'.src'); }\n\n";
  171. }
  172. // Insert preload code.
  173. if ($m_over) {
  174. $result .= "if (document.images) {
  175. spl_f$called = new Image; spl_f$called.src = '$filename';
  176. splof$called = new Image; splof$called.src = '$m_over';
  177. }";
  178. }
  179. // Insert preload code - otherswap
  180. if ($otherswap) {
  181. $result .= "if (document.images) {
  182. spl_q$called = new Image; spl_q$called.src = '$os_normal';
  183. sploq$called = new Image; sploq$called.src = '$os_m_over';
  184. }";
  185. }
  186. return $result . "\n\n// -->\n</script>";
  187. }
  188. /**
  189. * Generate XML for pictorial link with image swapping -or- Simple picture -depending- on $condition.
  190. *
  191. * @param bool condition Show Picture or xml_gen::a_img?
  192. * @param string selected Filename for image to display if $condition == true. - use m_over if set to null
  193. * @param string uri URI to open
  194. * @param string filename Filename for image to display if $condition == false.
  195. * @param string m_over Filename for image to display when mouse is over image.
  196. * @param string attr Additional HTML attributes, e.g. "target=main width='100%'".
  197. * Except for onmouse* $attr is only added to <img tag.
  198. * @param string[] otherswap Swap other image: array ($name, $normal, $m_over).
  199. * @see xml_gen::a_img
  200. */
  201. public static function a_img_cond($condition, $selected=null, $uri, $filename, $m_over = false, $attr = false, $otherswap = false)
  202. {
  203. if ($condition) {
  204. if (is_null($selected)) {
  205. return xml_gen::a_img($uri, $m_over, $attr, $m_over, $otherswap);
  206. }
  207. else {
  208. return xml_gen::a_img($uri, $selected, $attr, $m_over, $otherswap);
  209. }
  210. }
  211. else {
  212. return xml_gen::a_img($uri, $filename, $attr, $m_over, $otherswap);
  213. }
  214. }
  215. /**
  216. * Generate XML code for flash object.
  217. */
  218. function flash($filename, $width = false, $height = false, $attr = null, $bgcolor = '#ffffff', $requied_flash_version = 6, $use_swfobject = true, $swfobject_name = "mymovie")
  219. {
  220. // If filename is a local file, locate it somewhere in (../)*
  221. if (!strstr($filename, "http://") && !strstr($filename, "?")) {
  222. $name = preg_replace("/^\//", "", $filename);
  223. $i= 21;
  224. while (--$i && !file_exists($name)) {
  225. $name = '../'.$name;
  226. }
  227. if (!file_exists($name)) {
  228. return false;
  229. }
  230. $fn_size = $name;
  231. // Replace relative filename with correct path
  232. if (@$filename[0] != "/") {
  233. $filename = $name;
  234. }
  235. }
  236. // Get height and width
  237. if (!empty($fn_size) && (!$height || !$width)) {
  238. $sz = GetImageSize($fn_size);
  239. if (!$width) {
  240. $width= $sz[0];
  241. }
  242. if (!$height) {
  243. $height= $sz[1];
  244. }
  245. }
  246. if ($use_swfobject) {
  247. $div_name = "flashcontent_".strtr($filename, "/.=?%;", "______");
  248. return '
  249. <div width="'.$width.'" height="'.$height.'" id="'.$div_name.'"><table cellpadding=5 cellspacing=0 border=0 width="'.$width.'" height="'.$height.'"></tr><td bgcolor="'.$bgcolor.'"><b>Requires flash version '.$requied_flash_version.'</b></td></tr></table></div>
  250. <script type="text/javascript">
  251. <!--
  252. var so = new SWFObject("'.$filename.'", "'.$swfobject_name.'", "'.$width.'", "'.$height.'", "'.$requied_flash_version.'", "'.$bgcolor.'");
  253. so.addParam("quality", "high");
  254. so.write("'.$div_name.'");
  255. // -->
  256. </script>
  257. ';
  258. } else {
  259. return "
  260. <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=$requied_flash_version,0,0,0' width='$width' height='$height' $attr>
  261. <param name='movie' value='$filename'>
  262. <param name='quality' value='high'>
  263. <param name='bgcolor' value='$bgcolor'>
  264. <embed src='$filename' quality='high' bgcolor='$bgcolor' width='$width' height='$height' type='application/x-shockwave-flash' pluginspace='http://www.macromedia.com/go/getflashplayer'></embed>
  265. </object>
  266. ";
  267. }
  268. }
  269. /**
  270. * Generate XML for <span>.
  271. */
  272. public static function span($content, $attr = null)
  273. {
  274. return "<span $attr>$content</span>";
  275. }
  276. /**
  277. * Generate XML for <div>.
  278. */
  279. public static function div($content, $attr = null)
  280. {
  281. return "<div $attr>$content</div>";
  282. }
  283. /**
  284. * Generate XML for bold text.
  285. */
  286. public static function b($text)
  287. {
  288. return "<b>$text</b>";
  289. }
  290. /**
  291. * Generate XML for italic text.
  292. */
  293. public static function i($text)
  294. {
  295. return "<i>$text</i>";
  296. }
  297. /**
  298. * Generate XML for pre formatted text.
  299. */
  300. public static function pre($text, $attr = null)
  301. {
  302. return "<pre $attr>$text</pre>\n";
  303. }
  304. /**
  305. * Generate XML for paragraph.
  306. */
  307. public static function p($text, $attr = null)
  308. {
  309. if (is_array($text)) {
  310. $text = implode("<br>", $text);
  311. }
  312. return "<p $attr>$text</p>\n";
  313. }
  314. /**
  315. * Generate XML for error paragraph.
  316. */
  317. public static function p_err($text)
  318. {
  319. if (is_array($text)) {
  320. $text = implode("<br>", $text);
  321. }
  322. return xml_gen::p($text, "class='error'");
  323. }
  324. /**
  325. * Generate XML for text as heading 1.
  326. */
  327. public static function h1($text, $attr='')
  328. {
  329. return "<h1 $attr>$text</h1>";
  330. }
  331. /**
  332. * Generate XML for text as heading 2.
  333. */
  334. public static function h2($text, $attr='')
  335. {
  336. return "<h2 $attr>$text</h2>";
  337. }
  338. /**
  339. * Generate XML for text as heading 3.
  340. */
  341. public static function h3($text, $attr='')
  342. {
  343. return "<h3 $attr>$text</h3>";
  344. }
  345. /**
  346. * Generate XML for text as heading 4.
  347. */
  348. public static function h4($text, $attr='')
  349. {
  350. return "<h4 $attr>$text</h4>";
  351. }
  352. /**
  353. * Generate XML for spaces.
  354. */
  355. public static function space($n=1)
  356. {
  357. return str_repeat("&nbsp;", $n);
  358. }
  359. /**
  360. * Generate XML for linefeeds.
  361. */
  362. public static function br($n = 1, $abs = null)
  363. {
  364. if ($abs) {
  365. $abs= " clear=all";
  366. }
  367. return str_repeat("<br$abs />\n", $n);
  368. }
  369. /**
  370. * Generate XML for hr tag.
  371. */
  372. public static function hr($attr = null)
  373. {
  374. return "<hr $attr />";
  375. }
  376. /**
  377. * Generate XML for unordered list.
  378. */
  379. public static function ul($elements, $attr = null)
  380. {
  381. return "<ul $attr>$elements</ul>\n";
  382. }
  383. /**
  384. * Generate XML for list index.
  385. */
  386. public static function li($text, $attr = null)
  387. {
  388. return "<li $attr />$text\n";
  389. }
  390. /**
  391. * Generate XML for iframe
  392. *
  393. * Defaults to no scrolling and no frameborder.
  394. * Automatic height and width possible for local uris only.
  395. *
  396. * @param string src Iframe source (src)
  397. * @param mixed width Iframe width - set to 'auto' to automatically adjust width.
  398. * @param mixed height Iframe height - set to 'auto' to automatically adjust height
  399. * @param string attr Additional HTML attributes, e.g. "target=main width='100%'".
  400. */
  401. public static function iframe($src, $width=null, $height=null, $attr=null)
  402. {
  403. if (!strstr($attr, 'frameborder')) {
  404. $attr .= " frameborder='0'";
  405. }
  406. if (!strstr($attr, 'scrolling')) {
  407. $attr .= " scrolling='no'";
  408. }
  409. if ($width == 'auto' || $height == 'auto') {
  410. $on_load = '';
  411. if ($width == 'auto') {
  412. $on_load .= 'this.width=this.contentWindow ? this.contentWindow.document.body.scrollWidth : this.document.body.scrollWidth;';
  413. }
  414. if ($height == 'auto') {
  415. $on_load .= 'this.height=this.contentWindow ? this.contentWindow.document.body.scrollHeight : this.document.body.scrollHeight;';
  416. }
  417. $attr .= "onLoad='$on_load'";
  418. }
  419. $result = "<iframe src='$src'";
  420. if ($width) {
  421. $result .= " width='$width'";
  422. }
  423. if ($height) {
  424. $result .= " height='$height'";
  425. }
  426. return "$result $attr>An iframe capable browser is required to view this web site</iframe>";
  427. }
  428. /**
  429. * Generate XML for invisible table with desired width and height.
  430. */
  431. public static function spacer($width, $height)
  432. {
  433. return "<table cellpadding='0' cellspacing='0' border='0' width='$width' style='height:${height}px'><tr><td style='Font: 1px Arial'>&nbsp;</td></tr></table>";
  434. }
  435. /**
  436. * Generate js onclick for confirmations for <a
  437. */
  438. public static function js_confirm($str)
  439. {
  440. $str = xml_gen::js_string($str);
  441. return "onclick='return confirm(\"$str\");'";
  442. }
  443. /**
  444. * Generate js onclick for confirmations for <input type=button
  445. */
  446. public static function button_js_confirm_href($str, $url)
  447. {
  448. $str = xml_gen::js_string($str);
  449. return "onclick='if (confirm(\"$str\")) location.href=\"$url\";'";
  450. }
  451. /**
  452. * Prepare string for javascript
  453. */
  454. public static function js_string($str)
  455. {
  456. $str = str_replace('"', '\"', $str);
  457. $str = str_replace("'", '"+String.fromCharCode(39)+"', $str);
  458. return $str;
  459. }
  460. }
  461. //////////////////////////////////////////////////////////////////////////////
  462. // Tables ////////////////////////////////////////////////////////////////////
  463. //////////////////////////////////////////////////////////////////////////////
  464. /**
  465. * HTML Table Generator.
  466. *
  467. * @version 2
  468. */
  469. class table
  470. {
  471. protected $curr_col; // Current column we are in
  472. protected $curr_row; // Current row we are in
  473. protected $rowspan; // Array to handle rowspan
  474. protected $closed; // Is current cell closed?
  475. protected $columns; // Number of column in table
  476. protected $aligns; // Alignments from constructor
  477. protected $widths; // widths from constructor
  478. protected $classes; // classes from constructor
  479. protected $header; // Does table has header row
  480. protected $set_widths; // Set only widths on first row with no colspan
  481. protected $next_row_attr = ''; // Set id= on next <tr>
  482. /**
  483. * Constructor - define table layout here.
  484. *
  485. * @param int colums Number of colums in table
  486. * @param string attr Additional HTML attributes. cellpadding, cellspacing and border all defaults to 0.
  487. * @param string classes classes (set on <tr>) to alternate. "header;alternate" or ";alternate".
  488. * @param string aligns Alignments of columns. List seperated by ;. I.e. "left;right;center;left".
  489. * @param string widths widths of columns. List seperated by ;. I.e. "100;20%;20".
  490. * Alternate is ; seperated too, I.e. "header;odd;even".
  491. */
  492. public function __construct($columns, $attr = false, $classes = false, $aligns = false, $widths = false)
  493. {
  494. echo "\n\n<table";
  495. // Default cellpadding: 0
  496. if (!stristr(" $attr", " cellpadding=")) {
  497. echo " cellpadding='0'";
  498. }
  499. // Default cellspacing: 0
  500. if (!stristr(" $attr", " cellspacing=")) {
  501. echo " cellspacing='0'";
  502. }
  503. // Default border: 0
  504. if (!stristr(" $attr", " border=")) {
  505. echo " border='0'";
  506. }
  507. echo " $attr>";
  508. $this->closed = true; // Table is closed
  509. $this->curr_col = -1; // We are at column -1 (before <tr>)
  510. $this->curr_row = 0; // We are in row 0
  511. $this->columns = $columns;
  512. $this->header = $classes && $classes[0] != ';';
  513. $this->aligns = explode(";", $aligns);
  514. $this->widths = explode(";", $widths);
  515. $this->classes = explode(";", $classes);
  516. $this->set_widths = 1;
  517. $this->rowspan = array ();
  518. }
  519. /**
  520. * Destructor - finalise table.
  521. */
  522. public function done()
  523. {
  524. $this->end_row();
  525. echo "</table>\n\n";
  526. }
  527. /**
  528. * Output data cell(s).
  529. *
  530. * @param mixed data String: Data to output. Can be blank... just output after function call.
  531. * Array: Each element will be output in a new cell.
  532. * @param string attr Additional HTML attributes. Set col/rowspan, override/set class, width, align...
  533. */
  534. public function data($data = '', $attr = false)
  535. {
  536. if (is_array($data)) {
  537. foreach ($data as $element) {
  538. $this->data($element);
  539. }
  540. return;
  541. }
  542. // Before <tr>?
  543. if ($this->curr_col == -1) {
  544. $this->new_row();
  545. }
  546. // After last column?
  547. elseif ($this->curr_col + $this->rowspan[0] >= $this->columns) {
  548. $this->end_row();
  549. $this->new_row();
  550. }
  551. // After <td>?
  552. if ($this->curr_col > 0) {
  553. echo "</td>\n";
  554. $this->closed = true;
  555. }
  556. // Begin new cell
  557. echo "<td";
  558. $this->closed= false;
  559. // Override width
  560. if (!stristr(" $attr", ' width=') && !stristr(" $attr", ' colspan=')) {
  561. if ($this->curr_row == $this->set_widths && sizeof($this->widths > $this->curr_col) && !empty($this->widths[$this->curr_col])) {
  562. echo " width='".$this->widths[$this->curr_col]."'";
  563. }
  564. }
  565. // Override alignment
  566. if (!stristr(" $attr", ' align=')) {
  567. if (sizeof($this->aligns > $this->curr_col) && !empty($this->aligns[$this->curr_col])) {
  568. echo " align='".$this->aligns[$this->curr_col] . "'";
  569. }
  570. }
  571. // Output data
  572. echo " $attr>$data";
  573. // Advance internal pointer
  574. $this->curr_col++;
  575. // Colspan?
  576. $colspan = 1;
  577. if (eregi(" colspan=([\"'0-9]+) ", " $attr ", $regs)) {
  578. $colspan = str_replace("'", '', str_replace('"', '', $regs[1]));
  579. if ($colspan > 1) {
  580. $this->curr_col += $colspan - 1;
  581. }
  582. if ($this->curr_row == $this->set_widths) {
  583. $this->set_widths++;
  584. }
  585. }
  586. // rowspan?
  587. if (eregi(" rowspan=([\"'0-9]+) ", " $attr ", $regs)) {
  588. $rowspan = str_replace("'", '', str_replace('"', '', $regs[1]));
  589. while ($rowspan > 1) {
  590. array_push($this->rowspan, 0); // add a elements to our array... we may not need it... who cares?
  591. @$this->rowspan[$rowspan - 1] += $colspan;
  592. $rowspan--;
  593. }
  594. }
  595. }
  596. /**
  597. * Set attributes on next <tr>.
  598. */
  599. public function row_attr($attr)
  600. {
  601. $this->next_row_attr = ' ' . $attr;
  602. }
  603. /**
  604. * Finalise current row. Fill with empty cells if needed.
  605. */
  606. public function end_row()
  607. {
  608. if ($this->curr_col == -1 && $this->closed) {
  609. return;
  610. }
  611. // Before last column?
  612. if ($this->curr_col + @$this->rowspan[0] < $this->columns) {
  613. $colspan = $this->columns - $this->curr_col - @$this->rowspan[0] + 1;
  614. $this->data('', "colspan=$colspan");
  615. }
  616. if (!$this->closed) {
  617. echo "</td>\n";
  618. }
  619. echo "</tr>\n";
  620. $this->curr_col = -1;
  621. ksort($this->rowspan);
  622. array_shift($this->rowspan);
  623. }
  624. /**
  625. * Advance to new row.
  626. */
  627. protected function new_row()
  628. {
  629. echo "\n<tr";
  630. // Override class
  631. if (!stristr(" $this->next_row_attr", ' class=')) {
  632. echo $this->get_class();
  633. }
  634. echo $this->next_row_attr;
  635. $this->next_row_attr = '';
  636. echo ">\n";
  637. $this->curr_row++;
  638. $this->curr_col = 0;
  639. array_push($this->rowspan, 0);
  640. }
  641. /**
  642. * Get class for current row.
  643. */
  644. protected function get_class()
  645. {
  646. $sz = sizeof($this->classes);
  647. if (!$sz) {
  648. return;
  649. }
  650. $class = $this->classes[0];
  651. if ($this->curr_row == 0 && $this->header && $class) {
  652. return " class=\"$class\"";
  653. }
  654. $offset = 1;
  655. if ($this->header) {
  656. $offset--;
  657. }
  658. $offs = 1+ ($offset+$this->curr_row-1) % ($sz-1);
  659. $class = isset($this->classes[$offs]) ? $this->classes[$offs] : null;
  660. if ($class) {
  661. return " class=\"$class\"";
  662. }
  663. }
  664. }
  665. //////////////////////////////////////////////////////////////////////////////
  666. // Forms /////////////////////////////////////////////////////////////////////
  667. //////////////////////////////////////////////////////////////////////////////
  668. /**
  669. * HTML Form Generator.
  670. *
  671. * @version 3
  672. */
  673. class form
  674. {
  675. protected $elements; // Array of elements: array ( array("fisk", "radio"), ...
  676. protected $last_date_type; // YMD order of last date field
  677. protected $focus_element = -1; // Element on which to focus with javascript.
  678. protected $check_code; // Javascript code for CheckFormX()
  679. protected $extra_code_pre; // Extra javascript when drawing form
  680. protected $extra_code_post; // Extra javascript before submit
  681. protected $values; // Object or array containing default values.
  682. public $name; // Name of form.
  683. public $class_fields = "field"; // Standard class for textfields, textareas
  684. public $class_fields_readonly = "field_readonly"; // Standard class for readonly fields
  685. public $class_fields_disabled = "field_disabled"; // Standard class for disabled fields
  686. public $class_selects = "select"; // Standard class for selectboxes
  687. public $class_selects_readonly = "select_readonly"; // Standard class for readonly selectboxes
  688. public $class_selects_disabled = "select_disabled"; // Standard class for disabled selectboxes
  689. public $class_buttons = "button"; // Standard class for buttons
  690. public $class_checkboxes = "checkbox"; // Standard class for checkboxes
  691. public $class_radiobuttons = "radiobutton"; // Standard class for radiobuttons
  692. public $class_labels = "label"; // Standard class for labels (checkboxes and radiobuttons)
  693. public $class_labels_readonly = "label_readonly"; // Standard class for "readonly" labels
  694. public $class_datefields = "datefield"; // Standard class for datefield combi - do not set width
  695. public $class_dual_select_marked = "marked"; // Standard class for dual select - marked option
  696. public $option_dhtml_options = false; // Add options to <select> with DHTML. Faster when number of
  697. // options exceeds some 2000-4000.
  698. /**
  699. * Constructor - define form here.
  700. *
  701. * @param string action URL to POST or GET - defaults to $_SERVER["REQUEST_URI"]
  702. * @param string name HTML name of form.
  703. * @param string attr Additional HTML attributes, e.g. "target=main"
  704. */
  705. public function __construct($action = null, $name = null, $attr = null)
  706. {
  707. // Default
  708. if (is_null($action)) {
  709. $action = $_SERVER["REQUEST_URI"];
  710. }
  711. // Init
  712. $this->elements = array ();
  713. // Generate name if none is specified.
  714. if (!$name) {
  715. $name = "Form" . uniqid(42);
  716. }
  717. $this->name = $name;
  718. // Determine method - look for method='get' in $attr, set to post if not present
  719. if (!stristr(" $attr", " method='get'") && !stristr(" $attr", ' method="get"') && !stristr(" $attr", ' method=get')) {
  720. $attr = "method='post' enctype='multipart/form-data' $attr";
  721. $this->values = &$_POST;
  722. }
  723. else {
  724. $this->values = &$_GET;
  725. }
  726. // Output form header
  727. echo "\n\n<form action=\"$action\" name='$name' $attr onsubmit=\"return Check$name();\">\n";
  728. // Output hidden field to fix msie error
  729. $this->hidden("field_to_fix_msie_bug_on_post_special_chars", 42);
  730. }
  731. /**
  732. * Destructor - finalise form.
  733. */
  734. public function done()
  735. {
  736. // Output form footer
  737. echo "\n</form>\n\n";
  738. // Output javascript
  739. echo "<script type='text/javascript'>\n<!--\n\n";
  740. // Focus
  741. if ($this->focus_element != -1) {
  742. // Get element name from focus_element number
  743. foreach ($this->elements as $element_num => $array) {
  744. list($element, $type) = $array;
  745. if (!$this->focus_element) {
  746. break;
  747. }
  748. $this->focus_element--;
  749. }
  750. // Set focus on selected or first element in form
  751. echo "if (!document.$this->name.elements[$element_num].disabled)\n";
  752. echo " document.$this->name.elements[$element_num].focus();\n\n";
  753. }
  754. // Output Additional code
  755. echo $this->extra_code_pre;
  756. // Output CheckForm code
  757. echo "function Check" . $this->name . "()\n{\n";
  758. echo $this->check_code;
  759. echo "\n";
  760. echo $this->extra_code_post;
  761. echo "\n";
  762. echo " return true;\n";
  763. echo "}\n\n\n// -->\n</script>\n\n";
  764. }
  765. /**
  766. * Set object or array to get default values from.
  767. *
  768. * Defaults to $_REQUEST.
  769. *
  770. * @param mixed from Array or object.
  771. */
  772. public function values(&$from)
  773. {
  774. $this->values = &$from;
  775. }
  776. /**
  777. * Output a hidden field.
  778. *
  779. * Value comes from array or object defined with values.
  780. * Default from $_REQUEST[$name].
  781. *
  782. * @param string name Name of hidden field.
  783. * @param string value Static value, ignore $_REQUEST[$name] and $GLOBALS[$name]
  784. */
  785. public function hidden($name, $value = null, $attr = null)
  786. {
  787. // Get value
  788. if (is_null($value)) {
  789. $value = $this->get($name);
  790. }
  791. // Value is array - make multiple hidden fields
  792. if (strstr($name, "[]")) {
  793. // no empty arrays
  794. if (is_array($value)) {
  795. foreach ($value as $key => $val) {
  796. $this->hidden(str_replace("[]", "[$key]", $name), $val);
  797. }
  798. }
  799. return;
  800. }
  801. // Convert " to HTML
  802. $value = str_replace('"', '&quot;', $value);
  803. // Output hidden field
  804. echo "<input type='hidden' name='$name' value=\"$value\" $attr />";
  805. // Store element in elements array
  806. array_push($this->elements, array ($name, "hidden"));
  807. }
  808. /**
  809. * Output a radio button.
  810. *
  811. * Checked button comes from array or object defined with values.
  812. * Default from $_REQUEST[$name].
  813. *
  814. * @param string name Name of radio group.
  815. * @param string value Value for this radio button.
  816. * @param string label Text label.
  817. * @param string attr Additional HTML attributes, e.g. "id=main"
  818. */
  819. public function radio($name, $value, $label = null, $attr = false)
  820. {
  821. // Get value from name
  822. $val = $this->get($name);
  823. $check = $val == $value;
  824. // Convert $check to HTML
  825. $check = ($check) ? "checked" : "";
  826. if ($label) {
  827. // Render label grey if attr contains "disabled".
  828. if (stristr(" $attr ", " disabled ")) {
  829. $label = "<span style=\"color: #999999\"><font color=\"#999999\">$label</font></span>";
  830. }
  831. elseif ($this->class_labels) {
  832. $label = "<span class='$this->class_labels'>$label</span>";
  833. }
  834. }
  835. // Get box class
  836. $class = empty($this->class_radiobuttons) ? "" : "class='$this->class_radiobuttons'";
  837. // Output radio button
  838. echo "\n<input type='radio' name='$name' value=\"$value\" $check $class $attr />$label";
  839. // Store element in elements array
  840. array_push($this->elements, array ($name, "radio"));
  841. }
  842. /**
  843. * Output a checkbox
  844. *
  845. * Checked comes from array or object defined with values.
  846. * Default from $_REQUEST[$name].
  847. *
  848. * @param string name Name of check box.
  849. * @param string label Text label.
  850. * @param string attr Additional HTML attributes, e.g. "id=main"
  851. */
  852. public function checkbox($name, $label = null, $attr = false)
  853. {
  854. $value = $this->get($name);
  855. $value = !empty($value);
  856. // Convert $check and value to HTML
  857. $check = $value ? "checked" : "";
  858. // Render label grey if attr contains "disabled".
  859. if ($label) {
  860. if (stristr(" $attr ", " disabled ")) {
  861. $label = "<span style=\"color: #999999\"><font color=\"#999999\">$label</font></span>";
  862. }
  863. elseif ($this->class_labels) {
  864. $label = "<span class='$this->class_labels'>$label</span>";
  865. }
  866. }
  867. // Get box class
  868. $class = empty($this->class_checkboxes) ? "" : "class='$this->class_checkboxes'";
  869. // Output checkbox
  870. echo "\n<input type='checkbox' name='$name' $check $attr $class />$label";
  871. // Store element in elements array
  872. array_push($this->elements, array ($name, "checkbox"));
  873. }
  874. /**
  875. * Output a select box
  876. *
  877. * Selected element comes from array or object defined with values.
  878. * Default from $_REQUEST[$name].
  879. *
  880. * @param string name Name of select box.
  881. * @param array items Associative array with items: $value => $label.
  882. * @param string attr Additional HTML attributes, e.g. "id=main"
  883. */
  884. public function select($name, $items, $attr = false)
  885. {
  886. $select = $this->get($name);
  887. // convert select to array
  888. if (!is_array($select)) {
  889. $select = array (0 => $select);
  890. }
  891. // Use class_select_disabled if no class or style is specified in attr and field readonly.
  892. if ($this->class_selects_disabled && stristr(" $attr", " disabled") && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  893. $attr .= " class='$this->class_selects_disabled'";
  894. }
  895. // Use class_select_readonly if no class or style is specified in attr and field readonly.
  896. if ($this->class_selects_readonly && stristr(" $attr", " readonly") && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  897. $attr .= " class='$this->class_selects_readonly'";
  898. }
  899. // Use class_selects if no class or style is specified in attr.
  900. if ($this->class_selects && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  901. $attr .= " class='$this->class_selects'";
  902. }
  903. // Output select - the normal HTML way
  904. if (!$this->option_dhtml_options) {
  905. echo "<select name='$name' $attr>";
  906. // Output box elements
  907. foreach ($items as $value => $label) {
  908. $selected = in_array($value, $select) ? "selected='selected'" : "";
  909. echo "<option value=\"$value\" $selected>$label</option>";
  910. }
  911. echo "</select>";
  912. }
  913. // Output box elements - the DHTML way
  914. else {
  915. echo "<select name='$name' $attr></select>\n";
  916. echo "<script type='text/javascript'>\n";
  917. // Generate box elements
  918. $i = 0;
  919. foreach ($items as $value => $label) {
  920. $value = str_replace('"', '\"', $value);
  921. $value = str_replace("'", '"+String.fromCharCode(39)+"', $value);
  922. $label = str_replace('"', '\"', $label);
  923. $label = str_replace("'", '"+String.fromCharCode(39)+"', $label);
  924. echo "document.$this->name.${name}[$i]= new Option(\"$label\",\"$value\");\n";
  925. if (in_array($value, $select)) {
  926. echo "document.$this->name.$name.selectedIndex = $i;\n";
  927. }
  928. $i++;
  929. }
  930. echo "\n</script>";
  931. }
  932. // Store element in elements array
  933. array_push($this->elements, array ($name, "select"));
  934. }
  935. /**
  936. * Output a submit button.
  937. *
  938. * @param string label Label on button.
  939. * @param string attr Additional HTML attributes, e.g. "id=main"
  940. * @param string $change_action Change action to this value before submit.
  941. */
  942. public function submit($label, $attr = false, $change_action = false)
  943. {
  944. // Use class_buttons if no class or style is specified in attr.
  945. if ($this->class_buttons && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  946. $attr .= " class='$this->class_buttons'";
  947. }
  948. // Change form action
  949. if ($change_action) {
  950. $change_action = "onclick=\"$this->name.action='$change_action';\"";
  951. }
  952. // Output button
  953. echo "<input type='submit' value=\"$label\" $attr $change_action />";
  954. }
  955. /**
  956. * Output a reset button.
  957. *
  958. * @param string label Label on button.
  959. * @param string attr Additional HTML attributes, e.g. "id=main"
  960. */
  961. public function reset($label, $attr = false)
  962. {
  963. // Use class_buttons if no class or style is specified in attr.
  964. if ($this->class_buttons && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  965. $attr .= " class='$this->class_buttons'";
  966. }
  967. // Output button
  968. echo "<input type='reset' value=\"$label\" $attr />";
  969. }
  970. /**
  971. * Output a button.
  972. *
  973. * @param string label Label on button.
  974. * @param string attr Additional HTML attributes, e.g. "id=main"
  975. */
  976. public function button($label, $attr = false)
  977. {
  978. // Use class_buttons if no class or style is specified in attr.
  979. if ($this->class_buttons && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  980. $attr .= " class='$this->class_buttons'";
  981. }
  982. // Output button
  983. echo "<input type='button' value=\"$label\" $attr />";
  984. }
  985. /**
  986. * Output an image submit button.
  987. *
  988. * @param string label Label on button.
  989. * @param string attr Additional HTML attributes, e.g. "id=main"
  990. * @param string $change_action Change action to this value before submit.
  991. */
  992. public function image($image, $text, $attr = false, $change_action = false)
  993. {
  994. // Change form action
  995. if ($change_action) {
  996. $change_action = "onclick=\"$this->name.action='$change_action';\"";
  997. }
  998. // Output button
  999. echo "<input type='image' value='$text' src='$image' $attr $change_action />";
  1000. }
  1001. /**
  1002. * Output a text field.
  1003. *
  1004. * Value comes from array or object defined with values.
  1005. * Default from $_REQUEST[$name].
  1006. *
  1007. * @param string name Name of text field.
  1008. * @param string attr Additional HTML attributes, e.g. "id=main"
  1009. */
  1010. public function text($name, $attr = false)
  1011. {
  1012. $value = $this->get($name);
  1013. // Convert " to HTML
  1014. $value = str_replace('"', '&quot;', $value);
  1015. // Use class_fields_disabled if no class or style is specified in attr and field readonly.
  1016. if ($this->class_fields_disabled && stristr(" $attr", " disabled") && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1017. $attr .= " class='$this->class_fields_disabled'";
  1018. }
  1019. // Use class_fields_readonly if no class or style is specified in attr and field readonly.
  1020. if ($this->class_fields_readonly && stristr(" $attr", " readonly") && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1021. $attr .= " class='$this->class_fields_readonly'";
  1022. }
  1023. // Use class_fields if no class or style is specified in attr.
  1024. if ($this->class_fields && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1025. $attr .= " class='$this->class_fields'";
  1026. }
  1027. // Output text field
  1028. echo "<input type='text' name='$name' value=\"$value\" $attr />";
  1029. // Store element in elements array
  1030. array_push($this->elements, array ($name, "text"));
  1031. }
  1032. /**
  1033. * Output a password field.
  1034. *
  1035. * Value comes from array or object defined with values.
  1036. * Default from $_REQUEST[$name].
  1037. *
  1038. * @param string name Name of password field.
  1039. * @param string attr Additional HTML attributes, e.g. "id=main"
  1040. */
  1041. public function password($name, $attr = false)
  1042. {
  1043. // Get value
  1044. $value = $this->get($name);
  1045. // Convert " to HTML
  1046. $value = str_replace('"', '&quot;', $value);
  1047. // Use class_fields_disabled if no class or style is specified in attr and field readonly.
  1048. if ($this->class_fields_disabled && stristr(" $attr", " disabled") && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1049. $attr .= " class='$this->class_fields_disabled'";
  1050. }
  1051. // Use class_fields_readonly if no class or style is specified in attr and field readonly.
  1052. if ($this->class_fields_readonly && stristr(" $attr", " readonly") && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1053. $attr .= " class='$this->class_fields_readonly'";
  1054. }
  1055. // Use class_fields if no class or style is specified in attr.
  1056. if ($this->class_fields && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1057. $attr .= " class='".$this->class_fields."'";
  1058. }
  1059. // Output text field
  1060. echo "<input type='password' name='$name' value=\"$value\" $attr />";
  1061. // Store element in elements array
  1062. array_push($this->elements, array ($name, "password"));
  1063. }
  1064. /**
  1065. * Output a text area.
  1066. *
  1067. * Value comes from array or object defined with values.
  1068. * Default from $_REQUEST[$name].
  1069. *
  1070. * @param string name Name of text area.
  1071. * @param string attr Additional HTML attributes, e.g. "id=main"
  1072. */
  1073. public function textarea($name, $attr = false)
  1074. {
  1075. // Get value
  1076. $value = $this->get($name);
  1077. // Convert " to HTML
  1078. $value = str_replace('"', '&quot;', $value);
  1079. // Use class_fields_disabled if no class or style is specified in attr and field readonly.
  1080. if ($this->class_fields_disabled && stristr(" $attr", " disabled") && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1081. $attr .= " class='$this->class_fields_disabled'";
  1082. }
  1083. // Use class_fields_readonly if no class or style is specified in attr and field readonly.
  1084. if ($this->class_fields_readonly && stristr(" $attr", " readonly") && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1085. $attr .= " class='$this->class_fields_readonly'";
  1086. }
  1087. // Use class_fields if no class or style is specified in attr.
  1088. if ($this->class_fields && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1089. $attr .= " class='$this->class_fields'";
  1090. }
  1091. // Add required rows attribute if not specified in attr
  1092. if (!stristr(" $attr", " rows=")) {
  1093. $attr .= " rows='3'";
  1094. }
  1095. // Add required cols attribute if not specified in attr
  1096. if (!stristr(" $attr", " cols=")) {
  1097. $attr .= " cols='40'";
  1098. }
  1099. // Output text field
  1100. echo "<textarea name='$name' $attr>$value</textarea>";
  1101. // Store element in elements array
  1102. array_push($this->elements, array ($name, "textarea"));
  1103. }
  1104. /**
  1105. * Output a file upload field.
  1106. *
  1107. * @param string name Name of file upload field.
  1108. * @param string attr Additional HTML attributes, e.g. "id=main"
  1109. */
  1110. public function file_upload($name, $attr = false)
  1111. {
  1112. // Use class_fields_disabled if no class or style is specified in attr and field readonly.
  1113. if ($this->class_fields_disabled && stristr(" $attr", " disabled") && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1114. $attr .= " class='$this->class_fields_disabled'";
  1115. }
  1116. // Use class_fields_readonly if no class or style is specified in attr and field readonly.
  1117. if ($this->class_fields_readonly && stristr(" $attr", " readonly") && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1118. $attr .= " class='$this->class_fields_readonly'";
  1119. }
  1120. // Use class_fields if no class or style is specified in attr.
  1121. if ($this->class_fields && !stristr(" $attr", " class=") && !stristr(" $attr", " style=")) {
  1122. $attr .= " class='$this->class_fields'";
  1123. }
  1124. // Output text field
  1125. echo "<input type='file' name='$name' $attr />";
  1126. // Store element in elements array
  1127. array_push($this->elements, array ($name, "file"));
  1128. }
  1129. /**
  1130. * Output a two field dual select box for multiple choices
  1131. *
  1132. * @param string name Name of hidden field receiving selected values.
  1133. * @param array items Associative array with items: $value => $label.
  1134. * @param string attr Additional HTML attributes for <select
  1135. *
  1136. * Note: After posting $_POST[$name] will contain comma separated list of selected keys in $items.
  1137. * Input (selected values at start) in form->values->$name can be comma separated like above or array like normal select.
  1138. */
  1139. public function dual_select($name, $items, $attr = false)
  1140. {
  1141. // Build array of selected values
  1142. $select = $this->get($name);

Large files files are truncated, but you can click here to view the full file