conversationtree.php 4.0 KB

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