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.

199 lines
4.8KB

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