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