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