]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget/TagCloud.php
Fix mods/README.md format
[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 use Friendica\Util\Security;
15
16 require_once 'include/dba.php';
17
18 /**
19  * TagCloud widget
20  *
21  * @author Rabuzarus
22  */
23 class TagCloud
24 {
25         /**
26          * Construct a tag/term cloud block for an user.
27          *
28          * @brief Construct a tag/term cloud block for an user.
29          * @param int $uid      The user ID.
30          * @param int $count    Max number of displayed tags/terms.
31          * @param int $owner_id The contact ID of the owner of the tagged items.
32          * @param string $flags Special item flags.
33          * @param int $type     The tag/term type.
34          *
35          * @return string       HTML formatted output.
36          */
37         public static function getHTML($uid, $count = 0, $owner_id = 0, $flags = '', $type = TERM_HASHTAG)
38         {
39                 $o = '';
40                 $r = self::tagadelic($uid, $count, $owner_id, $flags, $type);
41                 if (count($r)) {
42                         $contact = DBA::selectFirst('contact', ['url'], ['uid' => $uid, 'self' => true]);
43                         $url = System::removedBaseUrl($contact['url']);
44
45                         foreach ($r as $rr) {
46                                 $tag['level'] = $rr[2];
47                                 $tag['url'] = $url . '?tag=' . urlencode($rr[0]);
48                                 $tag['name'] = $rr[0];
49
50                                 $tags[] = $tag;
51                         }
52
53                         $tpl = Renderer::getMarkupTemplate('tagblock_widget.tpl');
54                         $o = Renderer::replaceMacros($tpl, [
55                                 '$title' => L10n::t('Tags'),
56                                 '$tags' => $tags
57                         ]);
58                 }
59                 return $o;
60         }
61
62         /**
63          * Get alphabetical sorted array of used tags/terms of an user including
64          * a weighting by frequency of use.
65          *
66          * @brief Get alphabetical sorted array of used tags/terms of an user including
67          * a weighting by frequency of use.
68          * @param int $uid      The user ID.
69          * @param int $count    Max number of displayed tags/terms.
70          * @param int $owner_id The contact id of the owner of the tagged items.
71          * @param string $flags Special item flags.
72          * @param int $type     The tag/term type.
73          *
74          * @return arr          Alphabetical sorted array of used tags of an user.
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                 $r = 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($r)) {
104                         return [];
105                 }
106
107                 return self::tagCalc($r);
108         }
109
110         /**
111          * Calculate weighting of tags according to the frequency of use.
112          *
113          * @brief Calculate weighting of tags according to the frequency of use.
114          * @param array $arr Array of tags/terms with tag/term name and total count of use.
115          * @return array     Alphabetical sorted array of used tags/terms of an user.
116          */
117         private static function tagCalc($arr)
118         {
119                 $tags = [];
120                 $min = 1e9;
121                 $max = -1e9;
122                 $x = 0;
123
124                 if (!$arr) {
125                         return [];
126                 }
127
128                 foreach ($arr as $rr) {
129                         $tags[$x][0] = $rr['term'];
130                         $tags[$x][1] = log($rr['total']);
131                         $tags[$x][2] = 0;
132                         $min = min($min, $tags[$x][1]);
133                         $max = max($max, $tags[$x][1]);
134                         $x ++;
135                 }
136
137                 usort($tags, 'self::tagsSort');
138                 $range = max(.01, $max - $min) * 1.0001;
139
140                 for ($x = 0; $x < count($tags); $x ++) {
141                         $tags[$x][2] = 1 + floor(9 * ($tags[$x][1] - $min) / $range);
142                 }
143
144                 return $tags;
145         }
146
147         /**
148          * Compare function to sort tags/terms alphabetically.
149          *
150          * @brief Compare function to sort tags/terms alphabetically.
151          * @param type $a
152          * @param type $b
153          *
154          * @return int
155          */
156         private static function tagsSort($a, $b)
157         {
158                 if (strtolower($a[0]) == strtolower($b[0])) {
159                         return 0;
160                 }
161                 return ((strtolower($a[0]) < strtolower($b[0])) ? -1 : 1);
162         }
163 }