]> git.mxchange.org Git - friendica.git/blob - src/Content/ContactSelector.php
Issue 7563: Use icons instead of names on posts
[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  * @brief 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 => L10n::t('Frequently'),
59                         1 => L10n::t('Hourly'),
60                         2 => L10n::t('Twice daily'),
61                         3 => L10n::t('Daily'),
62                         4 => L10n::t('Weekly'),
63                         5 => 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'], ['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
109          * @param string $profile optional, default empty
110          * @return string
111          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
112          */
113         public static function networkToName($network, $profile = "")
114         {
115                 $nets = [
116                         Protocol::DFRN      =>   L10n::t('DFRN'),
117                         Protocol::OSTATUS   =>   L10n::t('OStatus'),
118                         Protocol::FEED      =>   L10n::t('RSS/Atom'),
119                         Protocol::MAIL      =>   L10n::t('Email'),
120                         Protocol::DIASPORA  =>   L10n::t('Diaspora'),
121                         Protocol::ZOT       =>   L10n::t('Zot!'),
122                         Protocol::LINKEDIN  =>   L10n::t('LinkedIn'),
123                         Protocol::XMPP      =>   L10n::t('XMPP/IM'),
124                         Protocol::MYSPACE   =>   L10n::t('MySpace'),
125                         Protocol::GPLUS     =>   L10n::t('Google+'),
126                         Protocol::PUMPIO    =>   L10n::t('pump.io'),
127                         Protocol::TWITTER   =>   L10n::t('Twitter'),
128                         Protocol::DIASPORA2 =>   L10n::t('Diaspora Connector'),
129                         Protocol::STATUSNET =>   L10n::t('GNU Social Connector'),
130                         Protocol::ACTIVITYPUB => L10n::t('ActivityPub'),
131                         Protocol::PNUT      =>   L10n::t('pnut'),
132                 ];
133
134                 Hook::callAll('network_to_name', $nets);
135
136                 $search  = array_keys($nets);
137                 $replace = array_values($nets);
138
139                 $networkname = str_replace($search, $replace, $network);
140
141                 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
142                         $server_url = self::getServerURLForProfile($profile);
143
144                         // Now query the GServer for the platform name
145                         $gserver = DBA::selectFirst('gserver', ['platform', 'network'], ['nurl' => $server_url]);
146
147                         if (DBA::isResult($gserver)) {
148                                 if (!empty($gserver['platform'])) {
149                                         $platform = $gserver['platform'];
150                                 } elseif (!empty($gserver['network']) && ($gserver['network'] != Protocol::ACTIVITYPUB)) {
151                                         $platform = self::networkToName($gserver['network']);
152                                 }
153
154                                 if (!empty($platform)) {
155                                         $networkname = $platform;
156
157                                         if ($network == Protocol::ACTIVITYPUB) {
158                                                 $networkname .= ' (AP)';
159                                         }
160                                 }
161                         }
162                 }
163
164                 return $networkname;
165         }
166
167         /**
168          * @param string $network network
169          * @param string $profile optional, default empty
170          * @return string
171          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
172          */
173         public static function networkToIcon($network, $profile = "")
174         {
175                 $nets = [
176                         Protocol::DFRN      =>   'friendica',
177                         Protocol::OSTATUS   =>   'gnu-social',
178                         Protocol::FEED      =>   'rss',
179                         Protocol::MAIL      =>   '',
180                         Protocol::DIASPORA  =>   'diaspora',
181                         Protocol::ZOT       =>   'hubzilla',
182                         Protocol::LINKEDIN  =>   'linkedin',
183                         Protocol::XMPP      =>   'xmpp',
184                         Protocol::MYSPACE   =>   '',
185                         Protocol::GPLUS     =>   'google-plus',
186                         Protocol::PUMPIO    =>   '',
187                         Protocol::TWITTER   =>   'twitter',
188                         Protocol::DIASPORA2 =>   'diaspora',
189                         Protocol::STATUSNET =>   'gnu-social',
190                         Protocol::ACTIVITYPUB => 'activitypub',
191                         Protocol::PNUT      =>   '',
192                 ];
193
194                 $search  = array_keys($nets);
195                 $replace = array_values($nets);
196
197                 $networkicon = str_replace($search, $replace, $network);
198
199                 if ((in_array($network, Protocol::FEDERATED)) && ($profile != "")) {
200                         $server_url = self::getServerURLForProfile($profile);
201
202                         // Now query the GServer for the platform name
203                         $gserver = DBA::selectFirst('gserver', ['platform'], ['nurl' => $server_url]);
204
205                         if (DBA::isResult($gserver) && !empty($gserver['platform'])) {
206                                 switch (strtolower($gserver['platform'])) {
207                                         case 'friendica':
208                                                 $networkicon = 'friendica';
209                                                 break;
210                                         case 'hubzilla':
211                                                 $networkicon = 'hubzilla';
212                                                 break;
213                                         case 'mastodon':
214                                                 $networkicon = 'mastodon';
215                                                 break;
216                                         case 'pleroma':
217                                                 $networkicon = 'pleroma';
218                                                 break;
219                                 }
220                         }
221                 }
222
223                 if (empty($networkicon)) {
224                         $networkicon = 'file-text-o';
225                 }
226
227                 return $networkicon;
228         }
229
230         /**
231          * @param string $current optional, default empty
232          * @param string $suffix  optionsl, default empty
233          * @return string
234          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
235          */
236         public static function gender($current = "", $suffix = "")
237         {
238                 $o = '';
239                 $select = [
240                         ''                 => L10n::t('No answer'),
241                         'Male'             => L10n::t('Male'),
242                         'Female'           => L10n::t('Female'),
243                         'Currently Male'   => L10n::t('Currently Male'),
244                         'Currently Female' => L10n::t('Currently Female'),
245                         'Mostly Male'      => L10n::t('Mostly Male'),
246                         'Mostly Female'    => L10n::t('Mostly Female'),
247                         'Transgender'      => L10n::t('Transgender'),
248                         'Intersex'         => L10n::t('Intersex'),
249                         'Transsexual'      => L10n::t('Transsexual'),
250                         'Hermaphrodite'    => L10n::t('Hermaphrodite'),
251                         'Neuter'           => L10n::t('Neuter'),
252                         'Non-specific'     => L10n::t('Non-specific'),
253                         'Other'            => L10n::t('Other'),
254                         'Undecided'        => L10n::t('Undecided'),
255                 ];
256
257                 Hook::callAll('gender_selector', $select);
258
259                 $o .= "<select name=\"gender$suffix\" id=\"gender-select$suffix\" size=\"1\" >";
260                 foreach ($select as $neutral => $selection) {
261                         if ($selection !== 'NOTRANSLATION') {
262                                 $selected = (($neutral == $current) ? ' selected="selected" ' : '');
263                                 $o .= "<option value=\"$neutral\" $selected >$selection</option>";
264                         }
265                 }
266                 $o .= '</select>';
267                 return $o;
268         }
269
270         /**
271          * @param string $current optional, default empty
272          * @param string $suffix  optionsl, default empty
273          * @return string
274          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
275          */
276         public static function sexualPreference($current = "", $suffix = "")
277         {
278                 $o = '';
279                 $select = [
280                         ''              => L10n::t('No answer'),
281                         'Males'         => L10n::t('Males'),
282                         'Females'       => L10n::t('Females'),
283                         'Gay'           => L10n::t('Gay'),
284                         'Lesbian'       => L10n::t('Lesbian'),
285                         'No Preference' => L10n::t('No Preference'),
286                         'Bisexual'      => L10n::t('Bisexual'),
287                         'Autosexual'    => L10n::t('Autosexual'),
288                         'Abstinent'     => L10n::t('Abstinent'),
289                         'Virgin'        => L10n::t('Virgin'),
290                         'Deviant'       => L10n::t('Deviant'),
291                         'Fetish'        => L10n::t('Fetish'),
292                         'Oodles'        => L10n::t('Oodles'),
293                         'Nonsexual'     => L10n::t('Nonsexual'),
294                 ];
295
296                 Hook::callAll('sexpref_selector', $select);
297
298                 $o .= "<select name=\"sexual$suffix\" id=\"sexual-select$suffix\" size=\"1\" >";
299                 foreach ($select as $neutral => $selection) {
300                         if ($selection !== 'NOTRANSLATION') {
301                                 $selected = (($neutral == $current) ? ' selected="selected" ' : '');
302                                 $o .= "<option value=\"$neutral\" $selected >$selection</option>";
303                         }
304                 }
305                 $o .= '</select>';
306                 return $o;
307         }
308
309         /**
310          * @param string $current optional, default empty
311          * @return string
312          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
313          */
314         public static function maritalStatus($current = "")
315         {
316                 $o = '';
317                 $select = [
318                         ''                     => L10n::t('No answer'),
319                         'Single'               => L10n::t('Single'),
320                         'Lonely'               => L10n::t('Lonely'),
321                         'In a relation'        => L10n::t('In a relation'),
322                         'Has crush'            => L10n::t('Has crush'),
323                         'Infatuated'           => L10n::t('Infatuated'),
324                         'Dating'               => L10n::t('Dating'),
325                         'Unfaithful'           => L10n::t('Unfaithful'),
326                         'Sex Addict'           => L10n::t('Sex Addict'),
327                         'Friends'              => L10n::t('Friends'),
328                         'Friends/Benefits'     => L10n::t('Friends/Benefits'),
329                         'Casual'               => L10n::t('Casual'),
330                         'Engaged'              => L10n::t('Engaged'),
331                         'Married'              => L10n::t('Married'),
332                         'Imaginarily married'  => L10n::t('Imaginarily married'),
333                         'Partners'             => L10n::t('Partners'),
334                         'Cohabiting'           => L10n::t('Cohabiting'),
335                         'Common law'           => L10n::t('Common law'),
336                         'Happy'                => L10n::t('Happy'),
337                         'Not looking'          => L10n::t('Not looking'),
338                         'Swinger'              => L10n::t('Swinger'),
339                         'Betrayed'             => L10n::t('Betrayed'),
340                         'Separated'            => L10n::t('Separated'),
341                         'Unstable'             => L10n::t('Unstable'),
342                         'Divorced'             => L10n::t('Divorced'),
343                         'Imaginarily divorced' => L10n::t('Imaginarily divorced'),
344                         'Widowed'              => L10n::t('Widowed'),
345                         'Uncertain'            => L10n::t('Uncertain'),
346                         'It\'s complicated'    => L10n::t('It\'s complicated'),
347                         'Don\'t care'          => L10n::t('Don\'t care'),
348                         'Ask me'               => L10n::t('Ask me'),
349                 ];
350
351                 Hook::callAll('marital_selector', $select);
352
353                 $o .= '<select name="marital" id="marital-select" size="1" >';
354                 foreach ($select as $neutral => $selection) {
355                         if ($selection !== 'NOTRANSLATION') {
356                                 $selected = (($neutral == $current) ? ' selected="selected" ' : '');
357                                 $o .= "<option value=\"$neutral\" $selected >$selection</option>";
358                         }
359                 }
360                 $o .= '</select>';
361                 return $o;
362         }
363 }