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