4 * @file src/Content/Widget/TagCloud.php
7 namespace Friendica\Content\Widget;
9 use Friendica\Core\Renderer;
10 use Friendica\Database\DBA;
12 use Friendica\Model\Item;
22 * Construct a tag/term cloud block for an user.
24 * @param int $uid The user ID.
25 * @param int $count Max number of displayed tags/terms.
26 * @param int $owner_id The contact ID of the owner of the tagged items.
27 * @param string $flags Special item flags.
28 * @param int $type The tag/term type.
30 * @return string HTML formatted output.
31 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
33 public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
36 $r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
38 $contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
39 $url = DI::baseUrl()->remove($contact['url']);
43 $tag['level'] = $rr[2];
44 $tag['url'] = $url . '?tag=' . urlencode($rr[0]);
45 $tag['name'] = $rr[0];
50 $tpl = Renderer::getMarkupTemplate('widget/tagcloud.tpl');
51 $o = Renderer::replaceMacros($tpl, [
52 '$title' => DI::l10n()->t('Tags'),
60 * Get alphabetical sorted array of used tags/terms of an user including
61 * a weighting by frequency of use.
63 * @param int $uid The user ID.
64 * @param int $count Max number of displayed tags/terms.
65 * @param int $owner_id The contact id of the owner of the tagged items.
66 * @param string $flags Special item flags.
67 * @param int $type The tag/term type.
69 * @return array Alphabetical sorted array of used tags of an user.
72 private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
74 $sql_options = Item::getPermissionsSQLByUserId($uid);
75 $limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
78 if ($flags === 'wall') {
79 $sql_options .= ' AND `item`.`wall` ';
84 $sql_options .= ' AND `item`.`owner-id` = ' . intval($owner_id) . ' ';
88 $tag_stmt = DBA::p("SELECT `term`, COUNT(`term`) AS `total` FROM `term`
89 LEFT JOIN `item` ON `term`.`oid` = `item`.`id`
90 WHERE `term`.`uid` = ? AND `term`.`type` = ?
91 AND `term`.`otype` = ?
92 AND `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
94 GROUP BY `term` ORDER BY `total` DESC $limit",
99 if (!DBA::isResult($tag_stmt)) {
103 $r = DBA::toArray($tag_stmt);
105 return self::tagCalc($r);
109 * Calculate weighting of tags according to the frequency of use.
111 * @param array $arr Array of tags/terms with tag/term name and total count of use.
112 * @return array Alphabetical sorted array of used tags/terms of an user.
114 private static function tagCalc(array $arr)
125 foreach ($arr as $rr) {
126 $tags[$x][0] = $rr['term'];
127 $tags[$x][1] = log($rr['total']);
129 $min = min($min, $tags[$x][1]);
130 $max = max($max, $tags[$x][1]);
134 usort($tags, 'self::tagsSort');
135 $range = max(.01, $max - $min) * 1.0001;
137 for ($x = 0; $x < count($tags); $x ++) {
138 $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
145 * Compare function to sort tags/terms alphabetically.
152 private static function tagsSort($a, $b)
154 if (strtolower($a[0]) == strtolower($b[0])) {
157 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);