]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget/TagCloud.php
Refactor namespaces
[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                         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('tagblock_widget.tpl');
52                         $o = Renderer::replaceMacros($tpl, [
53                                 '$title' => 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          * @brief Get alphabetical sorted array of used tags/terms of an user including
65          * a weighting by frequency of use.
66          * @param int    $uid      The user ID.
67          * @param int    $count    Max number of displayed tags/terms.
68          * @param int    $owner_id The contact id of the owner of the tagged items.
69          * @param string $flags    Special item flags.
70          * @param int    $type     The tag/term type.
71          *
72          * @return array        Alphabetical sorted array of used tags of an user.
73          * @throws \Exception
74          */
75         private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
76         {
77                 $sql_options = Item::getPermissionsSQLByUserId($uid);
78                 $limit = $count ? sprintf('LIMIT %d', intval($count)) : '';
79
80                 if ($flags) {
81                         if ($flags === 'wall') {
82                                 $sql_options .= ' AND `item`.`wall` ';
83                         }
84                 }
85
86                 if ($owner_id) {
87                         $sql_options .= ' AND `item`.`owner-id` = ' . intval($owner_id) . ' ';
88                 }
89
90                 // Fetch tags
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`
96                         $sql_options
97                         GROUP BY `term` ORDER BY `total` DESC $limit",
98                         $uid,
99                         $type,
100                         TERM_OBJ_POST
101                 );
102                 if (!DBA::isResult($r)) {
103                         return [];
104                 }
105
106                 return self::tagCalc($r);
107         }
108
109         /**
110          * Calculate weighting of tags according to the frequency of use.
111          *
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.
115          */
116         private static function tagCalc($arr)
117         {
118                 $tags = [];
119                 $min = 1e9;
120                 $max = -1e9;
121                 $x = 0;
122
123                 if (!$arr) {
124                         return [];
125                 }
126
127                 foreach ($arr as $rr) {
128                         $tags[$x][0] = $rr['term'];
129                         $tags[$x][1] = log($rr['total']);
130                         $tags[$x][2] = 0;
131                         $min = min($min, $tags[$x][1]);
132                         $max = max($max, $tags[$x][1]);
133                         $x ++;
134                 }
135
136                 usort($tags, 'self::tagsSort');
137                 $range = max(.01, $max - $min) * 1.0001;
138
139                 for ($x = 0; $x < count($tags); $x ++) {
140                         $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
141                 }
142
143                 return $tags;
144         }
145
146         /**
147          * Compare function to sort tags/terms alphabetically.
148          *
149          * @brief Compare function to sort tags/terms alphabetically.
150          * @param string $a
151          * @param string $b
152          *
153          * @return int
154          */
155         private static function tagsSort($a, $b)
156         {
157                 if (strtolower($a[0]) == strtolower($b[0])) {
158                         return 0;
159                 }
160                 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
161         }
162 }