]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget/TagCloud.php
7d96587c258fadd8d8ee1ff40583884648fb3ebd
[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 dba;
10 use Friendica\Core\L10n;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
13
14 require_once 'include/dba.php';
15 require_once 'include/security.php';
16
17 /**
18  * TagCloud widget
19  *
20  * @author Rabuzarus
21  */
22 class TagCloud
23 {
24         /**
25          * Construct a tag/term cloud block for an user.
26          *
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.
33          *
34          * @return string       HTML formatted output.
35          */
36         public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
37         {
38                 $o = '';
39                 $r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
40                 if (count($r)) {
41                         $contact = dba::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
42                         $url = System::removedBaseUrl($contact['url']);
43
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 = get_markup_template('tagblock_widget.tpl');
53                         $o = replace_macros($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 arr          Alphabetical sorted array of used tags of an user.
74          */
75         private static function tagadelic($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
76         {
77                 $item_condition = item_condition();
78                 $sql_options = item_permissions_sql($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                 $r = dba::p("SELECT `term`, COUNT(`term`) AS `total` FROM `term`
93                         LEFT JOIN `item` ON `term`.`oid` = `item`.`id`
94                         LEFT JOIN `user-item` ON `user-item`.`iid` = `item`.`id` AND `user-item`.`uid` = ?
95                         WHERE `term`.`uid` = ? AND `term`.`type` = ?
96                         AND `term`.`otype` = ?
97                         AND $item_condition $sql_options
98                         GROUP BY `term` ORDER BY `total` DESC $limit",
99                         $uid,
100                         $uid,
101                         $type,
102                         TERM_OBJ_POST
103                 );
104                 if (!DBM::is_result($r)) {
105                         return [];
106                 }
107
108                 return self::tagCalc($r);
109         }
110
111         /**
112          * Calculate weighting of tags according to the frequency of use.
113          *
114          * @brief Calculate weighting of tags according to the frequency of use.
115          * @param array $arr Array of tags/terms with tag/term name and total count of use.
116          * @return array     Alphabetical sorted array of used tags/terms of an user.
117          */
118         private static function tagCalc($arr)
119         {
120                 $tags = [];
121                 $min = 1e9;
122                 $max = -1e9;
123                 $x = 0;
124
125                 if (!$arr) {
126                         return [];
127                 }
128
129                 foreach ($arr as $rr) {
130                         $tags[$x][0] = $rr['term'];
131                         $tags[$x][1] = log($rr['total']);
132                         $tags[$x][2] = 0;
133                         $min = min($min, $tags[$x][1]);
134                         $max = max($max, $tags[$x][1]);
135                         $x ++;
136                 }
137
138                 usort($tags, 'self::tagsSort');
139                 $range = max(.01, $max - $min) * 1.0001;
140
141                 for ($x = 0; $x < count($tags); $x ++) {
142                         $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
143                 }
144
145                 return $tags;
146         }
147
148         /**
149          * Compare function to sort tags/terms alphabetically.
150          *
151          * @brief Compare function to sort tags/terms alphabetically.
152          * @param type $a
153          * @param type $b
154          *
155          * @return int
156          */
157         private static function tagsSort($a, $b)
158         {
159                 if (strtolower($a[0]) == strtolower($b[0])) {
160                         return 0;
161                 }
162                 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
163         }
164 }