]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
Merge pull request #4288 from zeroadam/Addon
[friendica.git] / src / Content / Widget.php
1 <?php
2 /**
3  * @file src/Content/Widget.php
4  */
5 namespace Friendica\Content;
6
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Feature;
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\PConfig;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
14 use Friendica\Model\GContact;
15 use Friendica\Model\Profile;
16
17 use dba;
18
19 require_once 'boot.php';
20
21 class Widget
22 {
23         /**
24          * Return the follow widget
25          *
26          * @param string $value optional, default empty
27          */
28         public static function follow($value = "")
29         {
30                 return replace_macros(get_markup_template('follow.tpl'), array(
31                         '$connect' => t('Add New Contact'),
32                         '$desc' => t('Enter address or web location'),
33                         '$hint' => t('Example: bob@example.com, http://example.com/barbara'),
34                         '$value' => $value,
35                         '$follow' => t('Connect')
36                 ));
37         }
38
39         /**
40          * Return Find People widget
41          */
42         public static function findPeople()
43         {
44                 $a = get_app();
45                 $global_dir = Config::get('system', 'directory');
46
47                 if (Config::get('system', 'invitation_only')) {
48                         $x = PConfig::get(local_user(), 'system', 'invites_remaining');
49                         if ($x || is_site_admin()) {
50                                 $a->page['aside'] .= '<div class="side-link" id="side-invite-remain">'
51                                         . tt('%d invitation available', '%d invitations available', $x)
52                                         . '</div>' . $inv;
53                         }
54                 }
55
56                 return replace_macros(get_markup_template('peoplefind.tpl'), array(
57                         '$findpeople' => t('Find People'),
58                         '$desc' => t('Enter name or interest'),
59                         '$label' => t('Connect/Follow'),
60                         '$hint' => t('Examples: Robert Morgenstein, Fishing'),
61                         '$findthem' => t('Find'),
62                         '$suggest' => t('Friend Suggestions'),
63                         '$similar' => t('Similar Interests'),
64                         '$random' => t('Random Profile'),
65                         '$inv' => t('Invite Friends'),
66                         '$directory' => t('View Global Directory'),
67                         '$global_dir' => $global_dir
68                 ));
69         }
70
71         /**
72          * Return unavailable networks
73          */
74         public static function unavailableNetworks()
75         {
76                 $networks = array();
77
78                 if (!Addon::isEnabled("appnet")) {
79                         $networks[] = NETWORK_APPNET;
80                 }
81
82                 if (!Addon::isEnabled("fbpost") && !Addon::isEnabled("facebook")) {
83                         $networks[] = NETWORK_FACEBOOK;
84                 }
85
86                 if (!Addon::isEnabled("statusnet")) {
87                         $networks[] = NETWORK_STATUSNET;
88                 }
89
90                 if (!Addon::isEnabled("pumpio")) {
91                         $networks[] = NETWORK_PUMPIO;
92                 }
93
94                 if (!Addon::isEnabled("twitter")) {
95                         $networks[] = NETWORK_TWITTER;
96                 }
97
98                 if (Config::get("system", "ostatus_disabled")) {
99                         $networks[] = NETWORK_OSTATUS;
100                 }
101
102                 if (!Config::get("system", "diaspora_enabled")) {
103                         $networks[] = NETWORK_DIASPORA;
104                 }
105
106                 if (!Addon::isEnabled("pnut")) {
107                         $networks[] = NETWORK_PNUT;
108                 }
109
110                 if (!sizeof($networks)) {
111                         return "";
112                 }
113
114                 $network_filter = implode("','", $networks);
115
116                 $network_filter = "AND `network` NOT IN ('$network_filter')";
117
118                 return $network_filter;
119         }
120
121         /**
122          * Return networks widget
123          *
124          * @param string $baseurl  baseurl
125          * @param string $selected optional, default empty
126          */
127         public static function networks($baseurl, $selected = '')
128         {
129                 if (!local_user()) {
130                         return '';
131                 }
132
133                 if (!Feature::isEnabled(local_user(), 'networks')) {
134                         return '';
135                 }
136
137                 $extra_sql = self::unavailableNetworks();
138
139                 $r = dba::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND `network` != '' $extra_sql ORDER BY `network`",
140                         local_user()
141                 );
142
143                 $nets = array();
144                 while ($rr = dba::fetch($r)) {
145                         /// @TODO If 'network' is not there, this triggers an E_NOTICE
146                         if ($rr['network']) {
147                                 $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
148                         }
149                 }
150                 dba::close($r);
151
152                 if (count($nets) < 2) {
153                         return '';
154                 }
155
156                 return replace_macros(get_markup_template('nets.tpl'), array(
157                         '$title' => t('Networks'),
158                         '$desc' => '',
159                         '$sel_all' => (($selected == '') ? 'selected' : ''),
160                         '$all' => t('All Networks'),
161                         '$nets' => $nets,
162                         '$base' => $baseurl,
163                 ));
164         }
165
166         /**
167          * Return file as widget
168          *
169          * @param string $baseurl  baseurl
170          * @param string $selected optional, default empty
171          */
172         public static function fileAs($baseurl, $selected = '')
173         {
174                 if (!local_user()) {
175                         return '';
176                 }
177
178                 if (!Feature::isEnabled(local_user(), 'filing')) {
179                         return '';
180                 }
181
182                 $saved = PConfig::get(local_user(), 'system', 'filetags');
183                 if (!strlen($saved)) {
184                         return;
185                 }
186
187                 $matches = false;
188                 $terms = array();
189                 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
190                 if ($cnt) {
191                         foreach ($matches as $mtch) {
192                                 $unescaped = xmlify(file_tag_decode($mtch[1]));
193                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
194                         }
195                 }
196
197                 return replace_macros(get_markup_template('fileas_widget.tpl'), array(
198                         '$title' => t('Saved Folders'),
199                         '$desc' => '',
200                         '$sel_all' => (($selected == '') ? 'selected' : ''),
201                         '$all' => t('Everything'),
202                         '$terms' => $terms,
203                         '$base' => $baseurl,
204                 ));
205         }
206
207         /**
208          * Return categories widget
209          *
210          * @param string $baseurl  baseurl
211          * @param string $selected optional, default empty
212          */
213         public static function categories($baseurl, $selected = '')
214         {
215                 $a = get_app();
216
217                 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
218                         return '';
219                 }
220
221                 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
222                 if (!strlen($saved)) {
223                         return;
224                 }
225
226                 $matches = false;
227                 $terms = array();
228                 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
229
230                 if ($cnt) {
231                         foreach ($matches as $mtch) {
232                                 $unescaped = xmlify(file_tag_decode($mtch[1]));
233                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
234                         }
235                 }
236
237                 return replace_macros(get_markup_template('categories_widget.tpl'), array(
238                         '$title' => t('Categories'),
239                         '$desc' => '',
240                         '$sel_all' => (($selected == '') ? 'selected' : ''),
241                         '$all' => t('Everything'),
242                         '$terms' => $terms,
243                         '$base' => $baseurl,
244                 ));
245         }
246
247         /**
248          * Return common friends visitor widget
249          *
250          * @param string $profile_uid uid
251          */
252         public static function commonFriendsVisitor($profile_uid)
253         {
254                 if (local_user() == $profile_uid) {
255                         return;
256                 }
257
258                 $cid = $zcid = 0;
259
260                 if (is_array($_SESSION['remote'])) {
261                         foreach ($_SESSION['remote'] as $visitor) {
262                                 if ($visitor['uid'] == $profile_uid) {
263                                         $cid = $visitor['cid'];
264                                         break;
265                                 }
266                         }
267                 }
268
269                 if (!$cid) {
270                         if (Profile::getMyURL()) {
271                                 $contact = dba::selectFirst('contact', ['id'],
272                                                 ['nurl' => normalise_link(Profile::getMyURL()), 'uid' => $profile_uid]);
273                                 if (DBM::is_result($contact)) {
274                                         $cid = $contact['id'];
275                                 } else {
276                                         $gcontact = dba::selectFirst('gcontact', ['id'], ['nurl' => normalise_link(Profile::getMyURL())]);
277                                         if (DBM::is_result($gcontact)) {
278                                                 $zcid = $gcontact['id'];
279                                         }
280                                 }
281                         }
282                 }
283
284                 if ($cid == 0 && $zcid == 0) {
285                         return;
286                 }
287
288                 if ($cid) {
289                         $t = GContact::countCommonFriends($profile_uid, $cid);
290                 } else {
291                         $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
292                 }
293
294                 if (!$t) {
295                         return;
296                 }
297
298                 if ($cid) {
299                         $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
300                 } else {
301                         $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
302                 }
303
304                 return replace_macros(get_markup_template('remote_friends_common.tpl'), array(
305                         '$desc' => tt("%d contact in common", "%d contacts in common", $t),
306                         '$base' => System::baseUrl(),
307                         '$uid' => $profile_uid,
308                         '$cid' => (($cid) ? $cid : '0'),
309                         '$linkmore' => (($t > 5) ? 'true' : ''),
310                         '$more' => t('show more'),
311                         '$items' => $r)
312                 );
313         }
314 }