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.

337 lines
8.3KB

  1. <?php
  2. /**
  3. * @file src/Content/Widget.php
  4. */
  5. namespace Friendica\Content;
  6. use Friendica\Content\ContactSelector;
  7. use Friendica\Content\Feature;
  8. use Friendica\Core\Addon;
  9. use Friendica\Core\Config;
  10. use Friendica\Core\L10n;
  11. use Friendica\Core\PConfig;
  12. use Friendica\Core\Protocol;
  13. use Friendica\Core\Renderer;
  14. use Friendica\Core\System;
  15. use Friendica\Database\DBA;
  16. use Friendica\Model\Contact;
  17. use Friendica\Model\FileTag;
  18. use Friendica\Model\GContact;
  19. use Friendica\Model\Profile;
  20. use Friendica\Util\Strings;
  21. use Friendica\Util\XML;
  22. class Widget
  23. {
  24. /**
  25. * Return the follow widget
  26. *
  27. * @param string $value optional, default empty
  28. */
  29. public static function follow($value = "")
  30. {
  31. return Renderer::replaceMacros(Renderer::getMarkupTemplate('follow.tpl'), array(
  32. '$connect' => L10n::t('Add New Contact'),
  33. '$desc' => L10n::t('Enter address or web location'),
  34. '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
  35. '$value' => $value,
  36. '$follow' => L10n::t('Connect')
  37. ));
  38. }
  39. /**
  40. * Return Find People widget
  41. */
  42. public static function findPeople()
  43. {
  44. $a = get_app();
  45. $global_dir = Config::get('system', 'directory');
  46. if (Config::get('system', 'invitation_only')) {
  47. $x = PConfig::get(local_user(), 'system', 'invites_remaining');
  48. if ($x || is_site_admin()) {
  49. $a->page['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
  50. . L10n::tt('%d invitation available', '%d invitations available', $x)
  51. . '</div>';
  52. }
  53. }
  54. $nv = [];
  55. $nv['findpeople'] = L10n::t('Find People');
  56. $nv['desc'] = L10n::t('Enter name or interest');
  57. $nv['label'] = L10n::t('Connect/Follow');
  58. $nv['hint'] = L10n::t('Examples: Robert Morgenstein, Fishing');
  59. $nv['findthem'] = L10n::t('Find');
  60. $nv['suggest'] = L10n::t('Friend Suggestions');
  61. $nv['similar'] = L10n::t('Similar Interests');
  62. $nv['random'] = L10n::t('Random Profile');
  63. $nv['inv'] = L10n::t('Invite Friends');
  64. $nv['directory'] = L10n::t('Global Directory');
  65. $nv['global_dir'] = $global_dir;
  66. $nv['local_directory'] = L10n::t('Local Directory');
  67. $aside = [];
  68. $aside['$nv'] = $nv;
  69. return Renderer::replaceMacros(Renderer::getMarkupTemplate('peoplefind.tpl'), $aside);
  70. }
  71. /**
  72. * Return unavailable networks
  73. */
  74. public static function unavailableNetworks()
  75. {
  76. // Always hide content from these networks
  77. $networks = ['face', 'apdn'];
  78. if (!Addon::isEnabled("statusnet")) {
  79. $networks[] = Protocol::STATUSNET;
  80. }
  81. if (!Addon::isEnabled("pumpio")) {
  82. $networks[] = Protocol::PUMPIO;
  83. }
  84. if (!Addon::isEnabled("twitter")) {
  85. $networks[] = Protocol::TWITTER;
  86. }
  87. if (Config::get("system", "ostatus_disabled")) {
  88. $networks[] = Protocol::OSTATUS;
  89. }
  90. if (!Config::get("system", "diaspora_enabled")) {
  91. $networks[] = Protocol::DIASPORA;
  92. }
  93. if (!Addon::isEnabled("pnut")) {
  94. $networks[] = Protocol::PNUT;
  95. }
  96. if (!sizeof($networks)) {
  97. return "";
  98. }
  99. $network_filter = implode("','", $networks);
  100. $network_filter = "AND `network` NOT IN ('$network_filter')";
  101. return $network_filter;
  102. }
  103. /**
  104. * Return networks widget
  105. *
  106. * @param string $baseurl baseurl
  107. * @param string $selected optional, default empty
  108. */
  109. public static function networks($baseurl, $selected = '')
  110. {
  111. if (!local_user()) {
  112. return '';
  113. }
  114. if (!Feature::isEnabled(local_user(), 'networks')) {
  115. return '';
  116. }
  117. $extra_sql = self::unavailableNetworks();
  118. $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND `network` != '' $extra_sql ORDER BY `network`",
  119. local_user()
  120. );
  121. $nets = array();
  122. while ($rr = DBA::fetch($r)) {
  123. $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
  124. }
  125. DBA::close($r);
  126. if (count($nets) < 2) {
  127. return '';
  128. }
  129. return Renderer::replaceMacros(Renderer::getMarkupTemplate('nets.tpl'), array(
  130. '$title' => L10n::t('Protocols'),
  131. '$desc' => '',
  132. '$sel_all' => (($selected == '') ? 'selected' : ''),
  133. '$all' => L10n::t('All Protocols'),
  134. '$nets' => $nets,
  135. '$base' => $baseurl,
  136. ));
  137. }
  138. /**
  139. * Return file as widget
  140. *
  141. * @param string $baseurl baseurl
  142. * @param string $selected optional, default empty
  143. */
  144. public static function fileAs($baseurl, $selected = '')
  145. {
  146. if (!local_user()) {
  147. return '';
  148. }
  149. $saved = PConfig::get(local_user(), 'system', 'filetags');
  150. if (!strlen($saved)) {
  151. return;
  152. }
  153. $matches = false;
  154. $terms = array();
  155. $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
  156. if ($cnt) {
  157. foreach ($matches as $mtch)
  158. {
  159. $unescaped = XML::escape(FileTag::decode($mtch[1]));
  160. $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
  161. }
  162. }
  163. return Renderer::replaceMacros(Renderer::getMarkupTemplate('fileas_widget.tpl'), array(
  164. '$title' => L10n::t('Saved Folders'),
  165. '$desc' => '',
  166. '$sel_all' => (($selected == '') ? 'selected' : ''),
  167. '$all' => L10n::t('Everything'),
  168. '$terms' => $terms,
  169. '$base' => $baseurl,
  170. ));
  171. }
  172. /**
  173. * Return categories widget
  174. *
  175. * @param string $baseurl baseurl
  176. * @param string $selected optional, default empty
  177. */
  178. public static function categories($baseurl, $selected = '')
  179. {
  180. $a = get_app();
  181. if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
  182. return '';
  183. }
  184. $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
  185. if (!strlen($saved)) {
  186. return;
  187. }
  188. $matches = false;
  189. $terms = array();
  190. $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
  191. if ($cnt) {
  192. foreach ($matches as $mtch) {
  193. $unescaped = XML::escape(FileTag::decode($mtch[1]));
  194. $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
  195. }
  196. }
  197. return Renderer::replaceMacros(Renderer::getMarkupTemplate('categories_widget.tpl'), array(
  198. '$title' => L10n::t('Categories'),
  199. '$desc' => '',
  200. '$sel_all' => (($selected == '') ? 'selected' : ''),
  201. '$all' => L10n::t('Everything'),
  202. '$terms' => $terms,
  203. '$base' => $baseurl,
  204. ));
  205. }
  206. /**
  207. * Return common friends visitor widget
  208. *
  209. * @param string $profile_uid uid
  210. */
  211. public static function commonFriendsVisitor($profile_uid)
  212. {
  213. if (local_user() == $profile_uid) {
  214. return;
  215. }
  216. $cid = $zcid = 0;
  217. if (!empty($_SESSION['remote'])) {
  218. foreach ($_SESSION['remote'] as $visitor) {
  219. if ($visitor['uid'] == $profile_uid) {
  220. $cid = $visitor['cid'];
  221. break;
  222. }
  223. }
  224. }
  225. if (!$cid) {
  226. if (Profile::getMyURL()) {
  227. $contact = DBA::selectFirst('contact', ['id'],
  228. ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
  229. if (DBA::isResult($contact)) {
  230. $cid = $contact['id'];
  231. } else {
  232. $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
  233. if (DBA::isResult($gcontact)) {
  234. $zcid = $gcontact['id'];
  235. }
  236. }
  237. }
  238. }
  239. if ($cid == 0 && $zcid == 0) {
  240. return;
  241. }
  242. if ($cid) {
  243. $t = GContact::countCommonFriends($profile_uid, $cid);
  244. } else {
  245. $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
  246. }
  247. if (!$t) {
  248. return;
  249. }
  250. if ($cid) {
  251. $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
  252. } else {
  253. $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
  254. }
  255. return Renderer::replaceMacros(Renderer::getMarkupTemplate('remote_friends_common.tpl'), array(
  256. '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
  257. '$base' => System::baseUrl(),
  258. '$uid' => $profile_uid,
  259. '$cid' => (($cid) ? $cid : '0'),
  260. '$linkmore' => (($t > 5) ? 'true' : ''),
  261. '$more' => L10n::t('show more'),
  262. '$items' => $r)
  263. );
  264. }
  265. /**
  266. * Insert a tag cloud widget for the present profile.
  267. *
  268. * @brief Insert a tag cloud widget for the present profile.
  269. * @param int $limit Max number of displayed tags.
  270. * @return string HTML formatted output.
  271. */
  272. public static function tagCloud($limit = 50)
  273. {
  274. $a = get_app();
  275. if (!$a->profile['profile_uid'] || !$a->profile['url']) {
  276. return '';
  277. }
  278. if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
  279. $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
  280. if (!$owner_id) {
  281. return '';
  282. }
  283. return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');
  284. }
  285. return '';
  286. }
  287. }