Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.2KB

  1. <?php
  2. /**
  3. * @file src/Content/ForumManager.php
  4. * @brief ForumManager class with its methods related to forum functionality
  5. */
  6. namespace Friendica\Content;
  7. use Friendica\Core\Protocol;
  8. use Friendica\Content\Feature;
  9. use Friendica\Content\Text\HTML;
  10. use Friendica\Core\L10n;
  11. use Friendica\Core\Renderer;
  12. use Friendica\Core\System;
  13. use Friendica\Database\DBA;
  14. use Friendica\Model\Contact;
  15. use Friendica\Util\Proxy as ProxyUtils;
  16. /**
  17. * @brief This class handles methods related to the forum functionality
  18. */
  19. class ForumManager
  20. {
  21. /**
  22. * @brief Function to list all forums a user is connected with
  23. *
  24. * @param int $uid of the profile owner
  25. * @param boolean $lastitem Sort by lastitem
  26. * @param boolean $showhidden Show frorums which are not hidden
  27. * @param boolean $showprivate Show private groups
  28. *
  29. * @return array
  30. * 'url' => forum url
  31. * 'name' => forum name
  32. * 'id' => number of the key from the array
  33. * 'micro' => contact photo in format micro
  34. * 'thumb' => contact photo in format thumb
  35. */
  36. public static function getList($uid, $lastitem, $showhidden = true, $showprivate = false)
  37. {
  38. if ($lastitem) {
  39. $params = ['order' => ['last-item' => true]];
  40. } else {
  41. $params = ['order' => ['name']];
  42. }
  43. $condition_str = "`network` = ? AND `uid` = ? AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `success_update` > `failure_update` AND ";
  44. if ($showprivate) {
  45. $condition_str .= '(`forum` OR `prv`)';
  46. } else {
  47. $condition_str .= '`forum`';
  48. }
  49. if (!$showhidden) {
  50. $condition_str .= ' AND NOT `hidden`';
  51. }
  52. $forumlist = [];
  53. $fields = ['id', 'url', 'name', 'micro', 'thumb'];
  54. $condition = [$condition_str, Protocol::DFRN, $uid];
  55. $contacts = DBA::select('contact', $fields, $condition, $params);
  56. if (!$contacts) {
  57. return($forumlist);
  58. }
  59. while ($contact = DBA::fetch($contacts)) {
  60. $forumlist[] = [
  61. 'url' => $contact['url'],
  62. 'name' => $contact['name'],
  63. 'id' => $contact['id'],
  64. 'micro' => $contact['micro'],
  65. 'thumb' => $contact['thumb'],
  66. ];
  67. }
  68. DBA::close($contacts);
  69. return($forumlist);
  70. }
  71. /**
  72. * @brief Forumlist widget
  73. *
  74. * Sidebar widget to show subcribed friendica forums. If activated
  75. * in the settings, it appears at the notwork page sidebar
  76. *
  77. * @param int $uid The ID of the User
  78. * @param int $cid The contact id which is used to mark a forum as "selected"
  79. * @return string
  80. */
  81. public static function widget($uid, $cid = 0)
  82. {
  83. $o = '';
  84. //sort by last updated item
  85. $lastitem = true;
  86. $contacts = self::getList($uid, $lastitem, true, true);
  87. $total = count($contacts);
  88. $visible_forums = 10;
  89. if (DBA::isResult($contacts)) {
  90. $id = 0;
  91. foreach ($contacts as $contact) {
  92. $selected = (($cid == $contact['id']) ? ' forum-selected' : '');
  93. $entry = [
  94. 'url' => 'network?f=&cid=' . $contact['id'],
  95. 'external_url' => Contact::magicLink($contact['url']),
  96. 'name' => $contact['name'],
  97. 'cid' => $contact['id'],
  98. 'selected' => $selected,
  99. 'micro' => System::removedBaseUrl(ProxyUtils::proxifyUrl($contact['micro'], false, ProxyUtils::SIZE_MICRO)),
  100. 'id' => ++$id,
  101. ];
  102. $entries[] = $entry;
  103. }
  104. $tpl = Renderer::getMarkupTemplate('widget_forumlist.tpl');
  105. $o .= Renderer::replaceMacros(
  106. $tpl,
  107. [
  108. '$title' => L10n::t('Forums'),
  109. '$forums' => $entries,
  110. '$link_desc' => L10n::t('External link to forum'),
  111. '$total' => $total,
  112. '$visible_forums' => $visible_forums,
  113. '$showmore' => L10n::t('show more')]
  114. );
  115. }
  116. return $o;
  117. }
  118. /**
  119. * @brief Format forumlist as contact block
  120. *
  121. * This function is used to show the forumlist in
  122. * the advanced profile.
  123. *
  124. * @param int $uid The ID of the User
  125. * @return string
  126. */
  127. public static function profileAdvanced($uid)
  128. {
  129. $profile = intval(Feature::isEnabled($uid, 'forumlist_profile'));
  130. if (! $profile) {
  131. return;
  132. }
  133. $o = '';
  134. // place holder in case somebody wants configurability
  135. $show_total = 9999;
  136. //don't sort by last updated item
  137. $lastitem = false;
  138. $contacts = self::getList($uid, $lastitem, false, false);
  139. $total_shown = 0;
  140. $forumlist = '';
  141. foreach ($contacts as $contact) {
  142. $forumlist .= HTML::micropro($contact, false, 'forumlist-profile-advanced');
  143. $total_shown ++;
  144. if ($total_shown == $show_total) {
  145. break;
  146. }
  147. }
  148. if (count($contacts) > 0) {
  149. $o .= $forumlist;
  150. return $o;
  151. }
  152. }
  153. /**
  154. * @brief count unread forum items
  155. *
  156. * Count unread items of connected forums and private groups
  157. *
  158. * @return array
  159. * 'id' => contact id
  160. * 'name' => contact/forum name
  161. * 'count' => counted unseen forum items
  162. */
  163. public static function countUnseenItems()
  164. {
  165. $r = q(
  166. "SELECT `contact`.`id`, `contact`.`name`, COUNT(*) AS `count` FROM `item`
  167. INNER JOIN `contact` ON `item`.`contact-id` = `contact`.`id`
  168. WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted` AND `item`.`unseen`
  169. AND `contact`.`network`= 'dfrn' AND (`contact`.`forum` OR `contact`.`prv`)
  170. AND NOT `contact`.`blocked` AND NOT `contact`.`hidden`
  171. AND NOT `contact`.`pending` AND NOT `contact`.`archive`
  172. AND `contact`.`success_update` > `failure_update`
  173. GROUP BY `contact`.`id` ",
  174. intval(local_user())
  175. );
  176. return $r;
  177. }
  178. }