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.

339 lines
8.2 KiB

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