]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget/TagCloud.php
Merge pull request #8134 from nupplaphil/task/di_l10n
[friendica.git] / src / Content / Widget / TagCloud.php
1 <?php
2
3 /*
4  * @file src/Content/Widget/TagCloud.php
5  */
6
7 namespace Friendica\Content\Widget;
8
9 use Friendica\Core\Renderer;
10 use Friendica\Database\DBA;
11 use Friendica\DI;
12 use Friendica\Model\Item;
13
14 /**
15  * TagCloud widget
16  *
17  * @author Rabuzarus
18  */
19 class TagCloud
20 {
21         /**
22          * Construct a tag/term cloud block for an user.
23          *
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.
29          *
30          * @return string       HTML formatted output.
31          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
32          */
33         public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
34         {
35                 $o = '';
36                 $r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
37                 if (count($r)) {
38                         $contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
39                         $url = DI::baseUrl()->remove($contact['url']);
40
41                         $tags = [];
42                         foreach ($r as $rr) {
43                                 $tag['level'] = $rr[2];
44                                 $tag['url'] = $url . '?tag=' . urlencode($rr[0]);
45                                 $tag['name'] = $rr[0];
46
47                                 $tags[] = $tag;
48                         }
49
50                         $tpl = Renderer::getMarkupTemplate('widget/tagcloud.tpl');
51                         $o = Renderer::replaceMacros($tpl, [
52                                 '$title' => DI::l10n()->t('Tags'),
53                                 '$tags' => $tags
54                         ]);
55                 }
56                 return $o;
57         }
58
59         /**
60          * Get alphabetical sorted array of used tags/terms of an user including
61          * a weighting by frequency of use.
62          *
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.
68          *
69          * @return array        Alphabetical sorted array of used tags of an user.
70          * @throws \Exception
71          */
72         private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
73         {
74                 $sql_options = Item::getPermissionsSQLByUserId($uid);
75                 $limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
76
77                 if ($flags) {
78                         if ($flags === 'wall') {
79                                 $sql_options .= ' AND `item`.`wall` ';
80                         }
81                 }
82
83                 if ($owner_id) {
84                         $sql_options .= ' AND `item`.`owner-id` = ' . intval($owner_id) . ' ';
85                 }
86
87                 // Fetch tags
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`
93                         $sql_options
94                         GROUP BY `term` ORDER BY `total` DESC $limit",
95                         $uid,
96                         $type,
97                         TERM_OBJ_POST
98                 );
99                 if (!DBA::isResult($tag_stmt)) {
100                         return [];
101                 }
102
103                 $r = DBA::toArray($tag_stmt);
104
105                 return self::tagCalc($r);
106         }
107
108         /**
109          * Calculate weighting of tags according to the frequency of use.
110          *
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.
113          */
114         private static function tagCalc(array $arr)
115         {
116                 $tags = [];
117                 $min = 1e9;
118                 $max = -1e9;
119                 $x = 0;
120
121                 if (!$arr) {
122                         return [];
123                 }
124
125                 foreach ($arr as $rr) {
126                         $tags[$x][0] = $rr['term'];
127                         $tags[$x][1] = log($rr['total']);
128                         $tags[$x][2] = 0;
129                         $min = min($min, $tags[$x][1]);
130                         $max = max($max, $tags[$x][1]);
131                         $x ++;
132                 }
133
134                 usort($tags, 'self::tagsSort');
135                 $range = max(.01, $max - $min) * 1.0001;
136
137                 for ($x = 0; $x < count($tags); $x ++) {
138                         $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
139                 }
140
141                 return $tags;
142         }
143
144         /**
145          * Compare function to sort tags/terms alphabetically.
146          *
147          * @param string $a
148          * @param string $b
149          *
150          * @return int
151          */
152         private static function tagsSort($a, $b)
153         {
154                 if (strtolower($a[0]) == strtolower($b[0])) {
155                         return 0;
156                 }
157                 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
158         }
159 }