4 * @file src/Content/Widget/TagCloud.php
7 namespace Friendica\Content\Widget;
9 use Friendica\Core\L10n;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\Database\DBM;
14 require_once 'include/dba.php';
15 require_once 'include/security.php';
25 * Construct a tag/term cloud block for an user.
27 * @brief Construct a tag/term cloud block for an user.
28 * @param int $uid The user ID.
29 * @param int $count Max number of displayed tags/terms.
30 * @param int $owner_id The contact ID of the owner of the tagged items.
31 * @param string $flags Special item flags.
32 * @param int $type The tag/term type.
34 * @return string HTML formatted output.
36 public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
39 $r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
41 $contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
42 $url = System::removedBaseUrl($contact['url']);
45 $tag['level'] = $rr[2];
46 $tag['url'] = $url . '?tag=' . urlencode($rr[0]);
47 $tag['name'] = $rr[0];
52 $tpl = get_markup_template('tagblock_widget.tpl');
53 $o = replace_macros($tpl, [
54 '$title' => L10n::t('Tags'),
62 * Get alphabetical sorted array of used tags/terms of an user including
63 * a weighting by frequency of use.
65 * @brief Get alphabetical sorted array of used tags/terms of an user including
66 * a weighting by frequency of use.
67 * @param int $uid The user ID.
68 * @param int $count Max number of displayed tags/terms.
69 * @param int $owner_id The contact id of the owner of the tagged items.
70 * @param string $flags Special item flags.
71 * @param int $type The tag/term type.
73 * @return arr Alphabetical sorted array of used tags of an user.
75 private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
77 $sql_options = item_permissions_sql($uid);
78 $limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
81 if ($flags === 'wall') {
82 $sql_options .= ' AND `item`.`wall` ';
87 $sql_options .= ' AND `item`.`owner-id` = ' . intval($owner_id) . ' ';
91 $r = DBA::p("SELECT `term`, COUNT(`term`) AS `total` FROM `term`
92 LEFT JOIN `item` ON `term`.`oid` = `item`.`id`
93 WHERE `term`.`uid` = ? AND `term`.`type` = ?
94 AND `term`.`otype` = ?
95 AND `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
97 GROUP BY `term` ORDER BY `total` DESC $limit",
102 if (!DBM::is_result($r)) {
106 return self::tagCalc($r);
110 * Calculate weighting of tags according to the frequency of use.
112 * @brief Calculate weighting of tags according to the frequency of use.
113 * @param array $arr Array of tags/terms with tag/term name and total count of use.
114 * @return array Alphabetical sorted array of used tags/terms of an user.
116 private static function tagCalc($arr)
127 foreach ($arr as $rr) {
128 $tags[$x][0] = $rr['term'];
129 $tags[$x][1] = log($rr['total']);
131 $min = min($min, $tags[$x][1]);
132 $max = max($max, $tags[$x][1]);
136 usort($tags, 'self::tagsSort');
137 $range = max(.01, $max - $min) * 1.0001;
139 for ($x = 0; $x < count($tags); $x ++) {
140 $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
147 * Compare function to sort tags/terms alphabetically.
149 * @brief Compare function to sort tags/terms alphabetically.
155 private static function tagsSort($a, $b)
157 if (strtolower($a[0]) == strtolower($b[0])) {
160 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);