]> git.mxchange.org Git - friendica.git/blob - src/Content/ContactSelector.php
cleanup namespace usages for L10n
[friendica.git] / src / Content / ContactSelector.php
1 <?php
2 /**
3  * @file src/Content/ContactSelector.php
4  */
5 namespace Friendica\Content;
6
7 use Friendica\Core\Hook;
8 use Friendica\Core\Protocol;
9 use Friendica\Database\DBA;
10 use Friendica\Util\Network;
11 use Friendica\Util\Strings;
12
13 /**
14  * ContactSelector class
15  */
16 class ContactSelector
17 {
18         /**
19          * @param string $current     current
20          * @param string $foreign_net network
21          * @return string
22          * @throws \Exception
23          */
24         public static function profileAssign($current, $foreign_net)
25         {
26                 $o = '';
27
28                 $disabled = (($foreign_net) ? ' disabled="true" ' : '');
29
30                 $o .= "<select id=\"contact-profile-selector\" class=\"form-control\" $disabled name=\"profile-assign\" >\r\n";
31
32                 $s = DBA::select('profile', ['id', 'profile-name', 'is-default'], ['uid' => $_SESSION['uid']]);
33                 $r = DBA::toArray($s);
34
35                 if (DBA::isResult($r)) {
36                         foreach ($r as $rr) {
37                                 $selected = (($rr['id'] == $current || ($current == 0 && $rr['is-default'] == 1)) ? " selected=\"selected\" " : "");
38                                 $o .= "<option value=\"{$rr['id']}\" $selected >{$rr['profile-name']}</option>\r\n";
39                         }
40                 }
41                 $o .= "</select>\r\n";
42                 return $o;
43         }
44
45         /**
46          * @param string  $current  current
47          * @param boolean $disabled optional, default false
48          * @return object
49          */
50         public static function pollInterval($current, $disabled = false)
51         {
52                 $dis = (($disabled) ? ' disabled="disabled" ' : '');
53                 $o = '';
54                 $o .= "<select id=\"contact-poll-interval\" name=\"poll\" $dis />" . "\r\n";
55
56                 $rep = [
57                         0 => DI::l10n()->t('Frequently'),
58                         1 => DI::l10n()->t('Hourly'),
59                         2 => DI::l10n()->t('Twice daily'),
60                         3 => DI::l10n()->t('Daily'),
61                         4 => DI::l10n()->t('Weekly'),
62                         5 => DI::l10n()->t('Monthly')
63                 ];
64
65                 foreach ($rep as $k => $v) {
66                         $selected = (($k == $current) ? " selected=\"selected\" " : "");
67                         $o .= "<option value=\"$k\" $selected >$v</option>\r\n";
68                 }
69                 $o .= "</select>\r\n";
70                 return $o;
71         }
72
73         /**
74          * @param string $profile Profile URL
75          * @return string Server URL
76          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
77          */
78         private static function getServerURLForProfile($profile)
79         {
80                 $server_url = '';
81
82                 // Fetch the server url from the contact table
83                 $contact = DBA::selectFirst('contact', ['baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($profile)]);
84                 if (DBA::isResult($contact) && !empty($contact['baseurl'])) {
85                         $server_url = Strings::normaliseLink($contact['baseurl']);
86                 }
87
88                 if (empty($server_url)) {
89                         // Fetch the server url from the gcontact table
90                         $gcontact = DBA::selectFirst('gcontact', ['server_url'], ['nurl' => Strings::normaliseLink($profile)]);
91                         if (!empty($gcontact) && !empty($gcontact['server_url'])) {
92                                 $server_url = Strings::normaliseLink($gcontact['server_url']);
93                         }
94                 }
95
96                 if (empty($server_url)) {
97                         // Create the server url out of the profile url
98                         $parts = parse_url($profile);
99                         unset($parts['path']);
100                         $server_url = Strings::normaliseLink(Network::unparseURL($parts));
101                 }
102
103                 return $server_url;
104         }
105
106         /**
107          * @param string $network  network of the contact
108          * @param string $profile  optional, default empty
109          * @param string $protocol (Optional) Protocol that is used for the transmission
110          * @return string
111          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
112          */
113         public static function networkToName($network, $profile = '', $protocol = '')
114         {
115                 $nets = [
116                         Protocol::DFRN      =>   DI::l10n()->t('DFRN'),
117                         Protocol::OSTATUS   =>   DI::l10n()->t('OStatus'),
118                         Protocol::FEED      =>   DI::l10n()->t('RSS/Atom'),
119                         Protocol::MAIL      =>   DI::l10n()->t('Email'),
120                         Protocol::DIASPORA  =>   DI::l10n()->t('Diaspora'),
121                         Protocol::ZOT       =>   DI::l10n()->t('Zot!'),
122                         Protocol::LINKEDIN  =>   DI::l10n()->t('LinkedIn'),
123                         Protocol::XMPP      =>   DI::l10n()->t('XMPP/IM'),
124                         Protocol::MYSPACE   =>   DI::l10n()->t('MySpace'),
125                         Protocol::GPLUS     =>   DI::l10n()->t('Google+'),
126                         Protocol::PUMPIO    =>   DI::l10n()->t('pump.io'),
127                         Protocol::TWITTER   =>   DI::l10n()->t('Twitter'),
128                         Protocol::DISCOURSE =>   DI::l10n()->t('Discourse'),
129                         Protocol::DIASPORA2 =>   DI::l10n()->t('Diaspora Connector'),
130                         Protocol::STATUSNET =>   DI::l10n()->t('GNU Social Connector'),
131                         Protocol::ACTIVITYPUB => DI::l10n()->t('ActivityPub'),
132                         Protocol::PNUT      =>   DI::l10n()->t('pnut'),
133                 ];
134
135                 Hook::callAll('network_to_name', $nets);
136
137                 $search  = array_keys($nets);
138                 $replace = array_values($nets);
139
140                 $networkname = str_replace($search, $replace, $network);
141
142                 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
143                         $server_url = self::getServerURLForProfile($profile);
144
145                         // Now query the GServer for the platform name
146                         $gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['nurl' => $server_url]);
147
148                         if (DBA::isResult($gserver)) {
149                                 if (!empty($gserver['platform'])) {
150                                         $platform = $gserver['platform'];
151                                 } elseif (!empty($gserver['network']) && ($gserver['network'] != Protocol::ACTIVITYPUB)) {
152                                         $platform = self::networkToName($gserver['network']);
153                                 }
154
155                                 if (!empty($platform)) {
156                                         $networkname = $platform;
157
158                                         if ($network == Protocol::ACTIVITYPUB) {
159                                                 $networkname .= ' (AP)';
160                                         }
161                                 }
162                         }
163                 }
164
165                 if (!empty($protocol) && ($protocol != $network)) {
166                         $networkname = DI::l10n()->t('%s (via %s)', $networkname, self::networkToName($protocol));
167                 }
168
169                 return $networkname;
170         }
171
172         /**
173          * @param string $network network
174          * @param string $profile optional, default empty
175          * @return string
176          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
177          */
178         public static function networkToIcon($network, $profile = "")
179         {
180                 $nets = [
181                         Protocol::DFRN      =>   'friendica',
182                         Protocol::OSTATUS   =>   'gnu-social', // There is no generic OStatus icon
183                         Protocol::FEED      =>   'rss',
184                         Protocol::MAIL      =>   'inbox',
185                         Protocol::DIASPORA  =>   'diaspora',
186                         Protocol::ZOT       =>   'hubzilla',
187                         Protocol::LINKEDIN  =>   'linkedin',
188                         Protocol::XMPP      =>   'xmpp',
189                         Protocol::MYSPACE   =>   'file-text-o', /// @todo
190                         Protocol::GPLUS     =>   'google-plus',
191                         Protocol::PUMPIO    =>   'file-text-o', /// @todo
192                         Protocol::TWITTER   =>   'twitter',
193                         Protocol::DISCOURSE =>   'dot-circle-o', /// @todo
194                         Protocol::DIASPORA2 =>   'diaspora',
195                         Protocol::STATUSNET =>   'gnu-social',
196                         Protocol::ACTIVITYPUB => 'activitypub',
197                         Protocol::PNUT      =>   'file-text-o', /// @todo
198                 ];
199
200                 $platform_icons = ['diaspora' => 'diaspora', 'friendica' => 'friendica', 'friendika' => 'friendica',
201                         'GNU Social' => 'gnu-social', 'gnusocial' => 'gnu-social', 'hubzilla' => 'hubzilla',
202                         'mastodon' => 'mastodon', 'peertube' => 'peertube', 'pixelfed' => 'pixelfed',
203                         'pleroma' => 'pleroma', 'red' => 'hubzilla', 'redmatrix' => 'hubzilla',
204                         'socialhome' => 'social-home', 'wordpress' => 'wordpress'];
205
206                 $search  = array_keys($nets);
207                 $replace = array_values($nets);
208
209                 $network_icon = str_replace($search, $replace, $network);
210
211                 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
212                         $server_url = self::getServerURLForProfile($profile);
213
214                         // Now query the GServer for the platform name
215                         $gserver = DBA::selectFirst('gserver', ['platform'], ['nurl' => $server_url]);
216
217                         if (DBA::isResult($gserver) && !empty($gserver['platform'])) {
218                                 $network_icon = $platform_icons[strtolower($gserver['platform'])] ?? $network_icon;
219                         }
220                 }
221
222                 return $network_icon;
223         }
224
225         /**
226          * @param string $current optional, default empty
227          * @param string $suffix  optionsl, default empty
228          * @return string
229          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
230          */
231         public static function gender($current = "", $suffix = "")
232         {
233                 $o = '';
234                 $select = [
235                         ''                 => DI::l10n()->t('No answer'),
236                         'Male'             => DI::l10n()->t('Male'),
237                         'Female'           => DI::l10n()->t('Female'),
238                         'Currently Male'   => DI::l10n()->t('Currently Male'),
239                         'Currently Female' => DI::l10n()->t('Currently Female'),
240                         'Mostly Male'      => DI::l10n()->t('Mostly Male'),
241                         'Mostly Female'    => DI::l10n()->t('Mostly Female'),
242                         'Transgender'      => DI::l10n()->t('Transgender'),
243                         'Intersex'         => DI::l10n()->t('Intersex'),
244                         'Transsexual'      => DI::l10n()->t('Transsexual'),
245                         'Hermaphrodite'    => DI::l10n()->t('Hermaphrodite'),
246                         'Neuter'           => DI::l10n()->t('Neuter'),
247                         'Non-specific'     => DI::l10n()->t('Non-specific'),
248                         'Other'            => DI::l10n()->t('Other'),
249                         'Undecided'        => DI::l10n()->t('Undecided'),
250                 ];
251
252                 Hook::callAll('gender_selector', $select);
253
254                 $o .= "<select name=\"gender$suffix\" id=\"gender-select$suffix\" size=\"1\" >";
255                 foreach ($select as $neutral => $selection) {
256                         if ($selection !== 'NOTRANSLATION') {
257                                 $selected = (($neutral == $current) ? ' selected="selected" ' : '');
258                                 $o .= "<option value=\"$neutral\" $selected >$selection</option>";
259                         }
260                 }
261                 $o .= '</select>';
262                 return $o;
263         }
264
265         /**
266          * @param string $current optional, default empty
267          * @param string $suffix  optionsl, default empty
268          * @return string
269          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
270          */
271         public static function sexualPreference($current = "", $suffix = "")
272         {
273                 $o = '';
274                 $select = [
275                         ''              => DI::l10n()->t('No answer'),
276                         'Males'         => DI::l10n()->t('Males'),
277                         'Females'       => DI::l10n()->t('Females'),
278                         'Gay'           => DI::l10n()->t('Gay'),
279                         'Lesbian'       => DI::l10n()->t('Lesbian'),
280                         'No Preference' => DI::l10n()->t('No Preference'),
281                         'Bisexual'      => DI::l10n()->t('Bisexual'),
282                         'Autosexual'    => DI::l10n()->t('Autosexual'),
283                         'Abstinent'     => DI::l10n()->t('Abstinent'),
284                         'Virgin'        => DI::l10n()->t('Virgin'),
285                         'Deviant'       => DI::l10n()->t('Deviant'),
286                         'Fetish'        => DI::l10n()->t('Fetish'),
287                         'Oodles'        => DI::l10n()->t('Oodles'),
288                         'Nonsexual'     => DI::l10n()->t('Nonsexual'),
289                 ];
290
291                 Hook::callAll('sexpref_selector', $select);
292
293                 $o .= "<select name=\"sexual$suffix\" id=\"sexual-select$suffix\" size=\"1\" >";
294                 foreach ($select as $neutral => $selection) {
295                         if ($selection !== 'NOTRANSLATION') {
296                                 $selected = (($neutral == $current) ? ' selected="selected" ' : '');
297                                 $o .= "<option value=\"$neutral\" $selected >$selection</option>";
298                         }
299                 }
300                 $o .= '</select>';
301                 return $o;
302         }
303
304         /**
305          * @param string $current optional, default empty
306          * @return string
307          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
308          */
309         public static function maritalStatus($current = "")
310         {
311                 $o = '';
312                 $select = [
313                         ''                     => DI::l10n()->t('No answer'),
314                         'Single'               => DI::l10n()->t('Single'),
315                         'Lonely'               => DI::l10n()->t('Lonely'),
316                         'In a relation'        => DI::l10n()->t('In a relation'),
317                         'Has crush'            => DI::l10n()->t('Has crush'),
318                         'Infatuated'           => DI::l10n()->t('Infatuated'),
319                         'Dating'               => DI::l10n()->t('Dating'),
320                         'Unfaithful'           => DI::l10n()->t('Unfaithful'),
321                         'Sex Addict'           => DI::l10n()->t('Sex Addict'),
322                         'Friends'              => DI::l10n()->t('Friends'),
323                         'Friends/Benefits'     => DI::l10n()->t('Friends/Benefits'),
324                         'Casual'               => DI::l10n()->t('Casual'),
325                         'Engaged'              => DI::l10n()->t('Engaged'),
326                         'Married'              => DI::l10n()->t('Married'),
327                         'Imaginarily married'  => DI::l10n()->t('Imaginarily married'),
328                         'Partners'             => DI::l10n()->t('Partners'),
329                         'Cohabiting'           => DI::l10n()->t('Cohabiting'),
330                         'Common law'           => DI::l10n()->t('Common law'),
331                         'Happy'                => DI::l10n()->t('Happy'),
332                         'Not looking'          => DI::l10n()->t('Not looking'),
333                         'Swinger'              => DI::l10n()->t('Swinger'),
334                         'Betrayed'             => DI::l10n()->t('Betrayed'),
335                         'Separated'            => DI::l10n()->t('Separated'),
336                         'Unstable'             => DI::l10n()->t('Unstable'),
337                         'Divorced'             => DI::l10n()->t('Divorced'),
338                         'Imaginarily divorced' => DI::l10n()->t('Imaginarily divorced'),
339                         'Widowed'              => DI::l10n()->t('Widowed'),
340                         'Uncertain'            => DI::l10n()->t('Uncertain'),
341                         'It\'s complicated'    => DI::l10n()->t('It\'s complicated'),
342                         'Don\'t care'          => DI::l10n()->t('Don\'t care'),
343                         'Ask me'               => DI::l10n()->t('Ask me'),
344                 ];
345
346                 Hook::callAll('marital_selector', $select);
347
348                 $o .= '<select name="marital" id="marital-select" size="1" >';
349                 foreach ($select as $neutral => $selection) {
350                         if ($selection !== 'NOTRANSLATION') {
351                                 $selected = (($neutral == $current) ? ' selected="selected" ' : '');
352                                 $o .= "<option value=\"$neutral\" $selected >$selection</option>";
353                         }
354                 }
355                 $o .= '</select>';
356                 return $o;
357         }
358 }