List.class.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * hold the PMA_List base class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * @todo add caching
  13. * @abstract
  14. * @package PhpMyAdmin
  15. * @since phpMyAdmin 2.9.10
  16. */
  17. abstract class PMA_List extends ArrayObject
  18. {
  19. /**
  20. * @var mixed empty item
  21. */
  22. protected $item_empty = '';
  23. public function __construct($array = array(), $flags = 0, $iterator_class = "ArrayIterator")
  24. {
  25. parent::__construct($array, $flags, $iterator_class);
  26. }
  27. /**
  28. * returns item only if there is only one in the list
  29. *
  30. * @return single item
  31. */
  32. public function getSingleItem()
  33. {
  34. if (count($this) === 1) {
  35. return reset($this);
  36. }
  37. return $this->getEmpty();
  38. }
  39. /**
  40. * defines what is an empty item (0, '', false or null)
  41. *
  42. * @return mixed an empty item
  43. */
  44. public function getEmpty()
  45. {
  46. return $this->item_empty;
  47. }
  48. /**
  49. * checks if the given db names exists in the current list, if there is
  50. * missing at least one item it returns false otherwise true
  51. *
  52. * @return boolean true if all items exists, otheriwse false
  53. */
  54. public function exists()
  55. {
  56. $this_elements = $this->getArrayCopy();
  57. foreach (func_get_args() as $result) {
  58. if (! in_array($result, $this_elements)) {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. /**
  65. * returns HTML <option>-tags to be used inside <select></select>
  66. *
  67. * @param mixed $selected the selected db or true for
  68. * selecting current db
  69. * @param boolean $include_information_schema whether include information schema
  70. *
  71. * @return string HTML option tags
  72. */
  73. public function getHtmlOptions($selected = '', $include_information_schema = true)
  74. {
  75. if (true === $selected) {
  76. $selected = $this->getDefault();
  77. }
  78. $options = '';
  79. foreach ($this as $each_item) {
  80. if (false === $include_information_schema
  81. && PMA_is_system_schema($each_item)
  82. ) {
  83. continue;
  84. }
  85. $options .= '<option value="' . htmlspecialchars($each_item) . '"';
  86. if ($selected === $each_item) {
  87. $options .= ' selected="selected"';
  88. }
  89. $options .= '>' . htmlspecialchars($each_item) . '</option>' . "\n";
  90. }
  91. return $options;
  92. }
  93. /**
  94. * returns default item
  95. *
  96. * @return string default item
  97. */
  98. public function getDefault()
  99. {
  100. return $this->getEmpty();
  101. }
  102. /**
  103. * builds up the list
  104. *
  105. * @return void
  106. */
  107. abstract public function build();
  108. }
  109. ?>