conversationtree.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. /**
  18. * Conversation tree
  19. *
  20. * The widget class for displaying a hierarchical list of notices.
  21. *
  22. * @category Widget
  23. * @package ConversationTreePlugin
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. */
  28. class ConversationTree extends NoticeList
  29. {
  30. public $tree = null;
  31. public $table = null;
  32. /**
  33. * Show the tree of notices
  34. *
  35. * @return int
  36. */
  37. public function show(): int
  38. {
  39. $cnt = $this->_buildTree();
  40. $this->out->elementStart('div', ['id' => 'notices_primary']);
  41. // TRANS: Header on conversation page. Hidden by default (h2).
  42. $this->out->element('h2', null, _('Notices'));
  43. $this->out->elementStart('ol', ['class' => 'notices xoxo old-school']);
  44. if (array_key_exists('root', $this->tree)) {
  45. $rootid = $this->tree['root'][0];
  46. $this->showNoticePlus($rootid);
  47. }
  48. $this->out->elementEnd('ol');
  49. $this->out->elementEnd('div');
  50. return $cnt;
  51. }
  52. public function _buildTree(): int
  53. {
  54. $cnt = 0;
  55. $this->tree = [];
  56. $this->table = [];
  57. while ($this->notice->fetch()) {
  58. $cnt++;
  59. $id = $this->notice->id;
  60. $notice = clone($this->notice);
  61. $this->table[$id] = $notice;
  62. if (is_null($notice->reply_to)) {
  63. $this->tree['root'] = [$notice->id];
  64. } elseif (array_key_exists($notice->reply_to, $this->tree)) {
  65. $this->tree[$notice->reply_to][] = $notice->id;
  66. } else {
  67. $this->tree[$notice->reply_to] = [$notice->id];
  68. }
  69. }
  70. return $cnt;
  71. }
  72. /**
  73. * Shows a notice plus its list of children.
  74. *
  75. * @param integer $id ID of the notice to show
  76. *
  77. * @return void
  78. */
  79. public function showNoticePlus($id): void
  80. {
  81. $notice = $this->table[$id];
  82. $this->out->elementStart('li', ['class' => 'h-entry notice',
  83. 'id' => 'notice-' . $id]);
  84. $item = $this->newListItem($notice);
  85. $item->show();
  86. if (array_key_exists($id, $this->tree)) {
  87. $children = $this->tree[$id];
  88. $this->out->elementStart('ol', ['class' => 'notices threaded-replies xoxo']);
  89. sort($children);
  90. foreach ($children as $child) {
  91. $this->showNoticePlus($child);
  92. }
  93. $this->out->elementEnd('ol');
  94. }
  95. $this->out->elementEnd('li');
  96. }
  97. /**
  98. * Override parent class to return our preferred item.
  99. *
  100. * @param Notice $notice Notice to display
  101. *
  102. * @return ConversationTreeItem a list item to show
  103. */
  104. public function newListItem(Notice $notice): ConversationTreeItem
  105. {
  106. return new ConversationTreeItem($notice, $this->out);
  107. }
  108. }