get_image.js.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Provides the functionality for retreiving images
  5. * which may be actual images or an icon from a sprite
  6. *
  7. * @package PhpMyAdmin
  8. */
  9. chdir('..');
  10. // Send correct type:
  11. header('Content-Type: text/javascript; charset=UTF-8');
  12. header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 3600) . ' GMT');
  13. // Avoid loading the full common.inc.php because this would add many
  14. // non-js-compatible stuff like DOCTYPE
  15. define('PMA_MINIMUM_COMMON', true);
  16. require_once './libraries/common.inc.php';
  17. // Get the data for the sprites, if it's available
  18. if (is_readable($_SESSION['PMA_Theme']->getPath() . '/sprites.lib.php')) {
  19. include $_SESSION['PMA_Theme']->getPath() . '/sprites.lib.php';
  20. }
  21. $sprites = array();
  22. if (function_exists('PMA_sprites')) {
  23. $sprites = PMA_sprites();
  24. }
  25. // We only need the keys from the array of sprites data,
  26. // since they contain the (partial) class names
  27. $keys = array();
  28. foreach ($sprites as $key => $value) {
  29. $keys[] = "'$key'";
  30. }
  31. ?>
  32. /**
  33. * Returns an HTML IMG tag for a particular image from a theme,
  34. * which may be an actual file or an icon from a sprite
  35. *
  36. * @param string image The name of the file to get
  37. * @param string alternate Used to set 'alt' and 'title' attributes of the image
  38. * @param object attributes An associative array of other attributes
  39. *
  40. * @return Object The requested image, this object has two methods:
  41. * .toString() - Returns the IMG tag for the requested image
  42. * .attr(name) - Returns a particular attribute of the IMG
  43. * tag given it's name
  44. * .attr(name, value) - Sets a particular attribute of the IMG
  45. * tag to the given value
  46. * And one property:
  47. * .isSprite - Whether the image is a sprite or not
  48. */
  49. function PMA_getImage(image, alternate, attributes) {
  50. var in_array = function (needle, haystack) {
  51. for (var i in haystack) {
  52. if (haystack[i] == needle) {
  53. return true;
  54. }
  55. }
  56. return false;
  57. };
  58. var sprites = [
  59. <?php echo implode($keys, ",\n ") . "\n"; ?>
  60. ];
  61. // custom image object, it will eventually be returned by this functions
  62. var retval = {
  63. data: {
  64. // this is private
  65. alt: '',
  66. title: '',
  67. src: (typeof PMA_TEST_THEME == 'undefined' ? '' : '../')
  68. + 'themes/dot.gif'
  69. },
  70. isSprite: true,
  71. attr: function (name, value) {
  72. if (value == undefined) {
  73. if (this.data[name] == undefined) {
  74. return '';
  75. } else {
  76. return this.data[name];
  77. }
  78. } else {
  79. this.data[name] = value;
  80. }
  81. },
  82. toString: function () {
  83. var retval = '<' + 'img';
  84. for (var i in this.data) {
  85. retval += ' ' + i + '="' + this.data[i] + '"';
  86. }
  87. retval += ' /' + '>';
  88. return retval;
  89. }
  90. };
  91. // initialise missing parameters
  92. if (attributes == undefined) {
  93. attributes = {};
  94. }
  95. if (alternate == undefined) {
  96. alternate = '';
  97. }
  98. // set alt
  99. if (attributes.alt != undefined) {
  100. retval.attr('alt', attributes.alt);
  101. } else {
  102. retval.attr('alt', alternate);
  103. }
  104. // set title
  105. if (attributes.title != undefined) {
  106. retval.attr('title', attributes.title);
  107. } else {
  108. retval.attr('title', alternate);
  109. }
  110. // set src
  111. var klass = image.replace('.gif', '').replace('.png', '');
  112. if (in_array(klass, sprites)) {
  113. // it's an icon from a sprite
  114. retval.attr('class', 'icon ic_' + klass);
  115. } else {
  116. // it's an image file
  117. retval.isSprite = false;
  118. retval.attr(
  119. 'src',
  120. "<?php echo $_SESSION['PMA_Theme']->getImgPath(); ?>" + image
  121. );
  122. }
  123. // set all other attrubutes
  124. for (var i in attributes) {
  125. if (i == 'src') {
  126. // do not allow to override the 'src' attribute
  127. continue;
  128. } else if (i == 'class') {
  129. retval.attr(i, retval.attr('class') + ' ' + attributes[i]);
  130. } else {
  131. retval.attr(i, attributes[i]);
  132. }
  133. }
  134. return retval;
  135. }
  136. //