NodeColumn.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Functionality for the navigation tree
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Navigation\Nodes;
  7. use PhpMyAdmin\Html\Generator;
  8. use PhpMyAdmin\Url;
  9. use function strlen;
  10. use function substr;
  11. /**
  12. * Represents a columns node in the navigation tree
  13. */
  14. class NodeColumn extends Node
  15. {
  16. /**
  17. * Initialises the class
  18. *
  19. * @param array $item array to identify the column node
  20. * @param int $type Type of node, may be one of CONTAINER or OBJECT
  21. * @param bool $isGroup Whether this object has been created
  22. * while grouping nodes
  23. */
  24. public function __construct($item, $type = Node::OBJECT, $isGroup = false)
  25. {
  26. $this->displayName = $this->getDisplayName($item);
  27. parent::__construct($item['name'], $type, $isGroup);
  28. $this->icon = Generator::getImage($this->getColumnIcon($item['key']), __('Column'));
  29. $this->links = [
  30. 'text' => Url::getFromRoute('/table/structure/change', [
  31. 'server' => $GLOBALS['server'],
  32. 'change_column' => 1,
  33. ]) . '&amp;db=%3$s&amp;table=%2$s&amp;field=%1$s',
  34. 'icon' => Url::getFromRoute('/table/structure/change', [
  35. 'server' => $GLOBALS['server'],
  36. 'change_column' => 1,
  37. ]) . '&amp;db=%3$s&amp;table=%2$s&amp;field=%1$s',
  38. 'title' => __('Structure'),
  39. ];
  40. }
  41. /**
  42. * Get customized Icon for columns in navigation tree
  43. *
  44. * @param string $key The key type - (primary, foreign etc.)
  45. *
  46. * @return string Icon name for required key.
  47. */
  48. private function getColumnIcon($key)
  49. {
  50. switch ($key) {
  51. case 'PRI':
  52. $retval = 'b_primary';
  53. break;
  54. case 'UNI':
  55. $retval = 'bd_primary';
  56. break;
  57. default:
  58. $retval = 'pause';
  59. break;
  60. }
  61. return $retval;
  62. }
  63. /**
  64. * Get displayable name for navigation tree (key_type, data_type, default)
  65. *
  66. * @param array<string, mixed> $item Item is array containing required info
  67. *
  68. * @return string Display name for navigation tree
  69. */
  70. private function getDisplayName($item)
  71. {
  72. $retval = $item['name'];
  73. $flag = 0;
  74. foreach ($item as $key => $value) {
  75. if (empty($value) || $key === 'name') {
  76. continue;
  77. }
  78. $flag == 0 ? $retval .= ' (' : $retval .= ', ';
  79. $flag = 1;
  80. $retval .= $this->getTruncateValue($key, $value);
  81. }
  82. return $retval . ')';
  83. }
  84. /**
  85. * Get truncated value for display in node column view
  86. *
  87. * @param string $key key to identify default,datatype etc
  88. * @param string $value value corresponding to key
  89. *
  90. * @return string truncated value
  91. */
  92. public function getTruncateValue($key, $value)
  93. {
  94. $retval = '';
  95. switch ($key) {
  96. case 'default':
  97. strlen($value) > 6 ?
  98. $retval .= substr($value, 0, 6) . '...' :
  99. $retval = $value;
  100. break;
  101. default:
  102. $retval = $value;
  103. break;
  104. }
  105. return $retval;
  106. }
  107. }