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