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