4 * @file src/Content/Widget/TagCloud.php
7 namespace Friendica\Content\Widget;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
13 use Friendica\Model\Item;
14 use Friendica\Util\Security;
24 * Construct a tag/term cloud block for an user.
26 * @brief Construct a tag/term cloud block for an user.
27 * @param int $uid The user ID.
28 * @param int $count Max number of displayed tags/terms.
29 * @param int $owner_id The contact ID of the owner of the tagged items.
30 * @param string $flags Special item flags.
31 * @param int $type The tag/term type.
33 * @return string HTML formatted output.
35 public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
38 $r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
40 $contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
41 $url = System::removedBaseUrl($contact['url']);
44 $tag['level'] = $rr[2];
45 $tag['url'] = $url . '?tag=' . urlencode($rr[0]);
46 $tag['name'] = $rr[0];
51 $tpl = Renderer::getMarkupTemplate('tagblock_widget.tpl');
52 $o = Renderer::replaceMacros($tpl, [
53 '$title' => L10n::t('Tags'),
61 * Get alphabetical sorted array of used tags/terms of an user including
62 * a weighting by frequency of use.
64 * @brief Get alphabetical sorted array of used tags/terms of an user including
65 * a weighting by frequency of use.
66 * @param int $uid The user ID.
67 * @param int $count Max number of displayed tags/terms.
68 * @param int $owner_id The contact id of the owner of the tagged items.
69 * @param string $flags Special item flags.
70 * @param int $type The tag/term type.
72 * @return arr Alphabetical sorted array of used tags of an user.
74 private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
76 $sql_options = Item::getPermissionsSQLByUserId($uid);
77 $limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
80 if ($flags === 'wall') {
81 $sql_options .= ' AND `item`.`wall` ';
86 $sql_options .= ' AND `item`.`owner-id` = ' . intval($owner_id) . ' ';
90 $r = DBA::p("SELECT `term`, COUNT(`term`) AS `total` FROM `term`
91 LEFT JOIN `item` ON `term`.`oid` = `item`.`id`
92 WHERE `term`.`uid` = ? AND `term`.`type` = ?
93 AND `term`.`otype` = ?
94 AND `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
96 GROUP BY `term` ORDER BY `total` DESC $limit",
101 if (!DBA::isResult($r)) {
105 return self::tagCalc($r);
109 * Calculate weighting of tags according to the frequency of use.
111 * @brief Calculate weighting of tags according to the frequency of use.
112 * @param array $arr Array of tags/terms with tag/term name and total count of use.
113 * @return array Alphabetical sorted array of used tags/terms of an user.
115 private static function tagCalc($arr)
126 foreach ($arr as $rr) {
127 $tags[$x][0] = $rr['term'];
128 $tags[$x][1] = log($rr['total']);
130 $min = min($min, $tags[$x][1]);
131 $max = max($max, $tags[$x][1]);
135 usort($tags, 'self::tagsSort');
136 $range = max(.01, $max - $min) * 1.0001;
138 for ($x = 0; $x < count($tags); $x ++) {
139 $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
146 * Compare function to sort tags/terms alphabetically.
148 * @brief Compare function to sort tags/terms alphabetically.
154 private static function tagsSort($a, $b)
156 if (strtolower($a[0]) == strtolower($b[0])) {
159 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);