]> git.mxchange.org Git - friendica.git/blob - src/Module/Conversation/Network.php
Move network module to src/
[friendica.git] / src / Module / Conversation / Network.php
1 <?php
2
3 namespace Friendica\Module\Conversation;
4
5 use Friendica\BaseModule;
6 use Friendica\Content\BoundariesPager;
7 use Friendica\Content\ForumManager;
8 use Friendica\Content\Nav;
9 use Friendica\Content\Widget;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Core\ACL;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Renderer;
14 use Friendica\Database\DBA;
15 use Friendica\DI;
16 use Friendica\Model\Contact;
17 use Friendica\Model\Group;
18 use Friendica\Model\Item;
19 use Friendica\Model\Profile;
20 use Friendica\Model\User;
21 use Friendica\Module\Contact as ModuleContact;
22 use Friendica\Module\Security\Login;
23 use Friendica\Util\DateTimeFormat;
24
25 class Network extends BaseModule
26 {
27         /** @var int */
28         private static $groupId;
29         /** @var int */
30         private static $forumContactId;
31         /** @var string */
32         private static $selectedTab;
33         /** @var mixed */
34         private static $min_id;
35         /** @var mixed */
36         private static $max_id;
37         /** @var int */
38         private static $accountType;
39         /** @var string */
40         private static $network;
41         /** @var int */
42         private static $itemsPerPage;
43         /** @var string */
44         private static $dateFrom;
45         /** @var string */
46         private static $dateTo;
47         /** @var int */
48         private static $star;
49         /** @var int */
50         private static $mention;
51         /** @var string */
52         protected static $order;
53
54         public static function content(array $parameters = [])
55         {
56                 if (!local_user()) {
57                         return Login::form();
58                 }
59
60                 self::parseRequest($parameters, $_GET);
61
62                 $module = 'network';
63
64                 DI::page()['aside'] .= Widget::accounts($module . '/accounttype', self::$accountType);
65                 DI::page()['aside'] .= Group::sidebarWidget($module, $module . '/group', 'standard', self::$groupId);
66                 DI::page()['aside'] .= ForumManager::widget($module . '/forum', local_user(), self::$forumContactId);
67                 DI::page()['aside'] .= Widget::postedByYear($module . '/archive', local_user(), false);
68                 DI::page()['aside'] .= Widget::networks($module, !self::$forumContactId ? self::$network : '');
69                 DI::page()['aside'] .= Widget\SavedSearches::getHTML(DI::args()->getQueryString());
70                 DI::page()['aside'] .= Widget::fileAs('filed', null);
71
72                 $arr = ['query' => DI::args()->getQueryString()];
73                 Hook::callAll('network_content_init', $arr);
74
75                 $o = '';
76
77                 // Fetch a page full of parent items for this page
78                 $params = ['limit' => self::$itemsPerPage];
79                 $table = 'network-thread-view';
80
81                 $items = self::getItems($table, $params);
82
83                 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll') && ($_GET['mode'] ?? '') != 'minimal') {
84                         $tpl = Renderer::getMarkupTemplate('infinite_scroll_head.tpl');
85                         $o .= Renderer::replaceMacros($tpl, ['$reload_uri' => DI::args()->getQueryString()]);
86                 }
87
88                 if (!(isset($_GET['mode']) AND ($_GET['mode'] == 'raw'))) {
89                         $o .= self::getTabsHTML(self::$selectedTab);
90
91                         Nav::setSelected(DI::args()->get(0));
92
93                         $content = '';
94
95                         if (self::$forumContactId) {
96                                 // If self::$forumContactId belongs to a communitity forum or a privat goup,.add a mention to the status editor
97                                 $condition = ["`id` = ? AND (`forum` OR `prv`)", self::$forumContactId];
98                                 $contact = DBA::selectFirst('contact', ['addr'], $condition);
99                                 if (!empty($contact['addr'])) {
100                                         $content = '!' . $contact['addr'];
101                                 }
102                         }
103
104                         $a = DI::app();
105
106                         $default_permissions = [];
107                         if (self::$groupId) {
108                                 $default_permissions['allow_gid'] = [self::$groupId];
109                         }
110
111                         $allowedCids = [];
112                         if (self::$forumContactId) {
113                                 $allowedCids[] = (int) self::$forumContactId;
114                         } elseif (self::$network) {
115                                 $condition = [
116                                         'uid'     => local_user(),
117                                         'network' => self::$network,
118                                         'self'    => false,
119                                         'blocked' => false,
120                                         'pending' => false,
121                                         'archive' => false,
122                                         'rel'     => [Contact::SHARING, Contact::FRIEND],
123                                 ];
124                                 $contactStmt = DBA::select('contact', ['id'], $condition);
125                                 while ($contact = DBA::fetch($contactStmt)) {
126                                         $allowedCids[] = (int) $contact['id'];
127                                 }
128                                 DBA::close($contactStmt);
129                         }
130
131                         if (count($allowedCids)) {
132                                 $default_permissions['allow_cid'] = $allowedCids;
133                         }
134
135                         $x = [
136                                 'is_owner' => true,
137                                 'allow_location' => $a->user['allow_location'],
138                                 'default_location' => $a->user['default-location'],
139                                 'nickname' => $a->user['nickname'],
140                                 'lockstate' => (self::$groupId || self::$forumContactId || self::$network || (is_array($a->user) &&
141                                         (strlen($a->user['allow_cid']) || strlen($a->user['allow_gid']) ||
142                                                 strlen($a->user['deny_cid']) || strlen($a->user['deny_gid']))) ? 'lock' : 'unlock'),
143                                 'default_perms' => ACL::getDefaultUserPermissions($a->user),
144                                 'acl' => ACL::getFullSelectorHTML(DI::page(), $a->user, true, $default_permissions),
145                                 'bang' => ((self::$groupId || self::$forumContactId || self::$network) ? '!' : ''),
146                                 'visitor' => 'block',
147                                 'profile_uid' => local_user(),
148                                 'content' => $content,
149                         ];
150
151                         $o .= status_editor($a, $x);
152                 }
153
154                 if (self::$groupId) {
155                         $group = DBA::selectFirst('group', ['name'], ['id' => self::$groupId, 'uid' => local_user()]);
156                         if (!DBA::isResult($group)) {
157                                 notice(DI::l10n()->t('No such group'));
158                         }
159
160                         $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
161                                 '$title' => DI::l10n()->t('Group: %s', $group['name'])
162                         ]) . $o;
163                 } elseif (self::$forumContactId) {
164                         $contact = Contact::getById(self::$forumContactId);
165                         if (DBA::isResult($contact)) {
166                                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('viewcontact_template.tpl'), [
167                                         'contacts' => [ModuleContact::getContactTemplateVars($contact)],
168                                         'id' => DI::args()->get(0),
169                                 ]) . $o;
170                         } else {
171                                 notice(DI::l10n()->t('Invalid contact.'));
172                         }
173                 } elseif (!DI::config()->get('theme', 'hide_eventlist')) {
174                         $o .= Profile::getBirthdays();
175                         $o .= Profile::getEventsReminderHTML();
176                 }
177
178                 if (self::$order === 'received') {
179                         $ordering = '`received`';
180                 } else {
181                         $ordering = '`commented`';
182                 }
183
184                 $o .= conversation(DI::app(), $items, 'network', false, false, $ordering, local_user());
185
186                 if (DI::pConfig()->get(local_user(), 'system', 'infinite_scroll')) {
187                         $o .= HTML::scrollLoader();
188                 } else {
189                         $pager = new BoundariesPager(
190                                 DI::l10n(),
191                                 DI::args()->getQueryString(),
192                                 $items[0][self::$order],
193                                 $items[count($items) - 1][self::$order],
194                                 self::$itemsPerPage
195                         );
196
197                         $o .= $pager->renderMinimal(count($items));
198                 }
199
200                 return $o;
201         }
202
203         /**
204          * Sets items as seen
205          *
206          * @param array $condition The array with the SQL condition
207          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
208          */
209         private static function setItemsSeenByCondition(array $condition)
210         {
211                 if (empty($condition)) {
212                         return;
213                 }
214
215                 $unseen = Item::exists($condition);
216
217                 if ($unseen) {
218                         Item::update(['unseen' => false], $condition);
219                 }
220         }
221
222         /**
223          * Get the network tabs menu
224          *
225          * @param string $selectedTab
226          * @return string Html of the network tabs
227          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
228          */
229         private static function getTabsHTML(string $selectedTab)
230         {
231                 $cmd = DI::args()->getCommand();
232
233                 // tabs
234                 $tabs = [
235                         [
236                                 'label' => DI::l10n()->t('Latest Activity'),
237                                 'url'   => $cmd . '?' . http_build_query(['order' => 'commented']),
238                                 'sel'   => !$selectedTab || $selectedTab == 'commented' ? 'active' : '',
239                                 'title' => DI::l10n()->t('Sort by latest activity'),
240                                 'id'    => 'activity-order-tab',
241                                 'accesskey' => 'e',
242                         ],
243                         [
244                                 'label' => DI::l10n()->t('Latest Posts'),
245                                 'url'   => $cmd . '?' . http_build_query(['order' => 'received']),
246                                 'sel'   => $selectedTab == 'received' ? 'active' : '',
247                                 'title' => DI::l10n()->t('Sort by post received date'),
248                                 'id'    => 'post-order-tab',
249                                 'accesskey' => 't',
250                         ],
251                         [
252                                 'label' => DI::l10n()->t('Personal'),
253                                 'url'   => $cmd . '?' . http_build_query(['mention' => true]),
254                                 'sel'   => $selectedTab == 'mention' ? 'active' : '',
255                                 'title' => DI::l10n()->t('Posts that mention or involve you'),
256                                 'id'    => 'personal-tab',
257                                 'accesskey' => 'r',
258                         ],
259                         [
260                                 'label' => DI::l10n()->t('Starred'),
261                                 'url'   => $cmd . '?' . http_build_query(['star' => true]),
262                                 'sel'   => $selectedTab == 'star' ? 'active' : '',
263                                 'title' => DI::l10n()->t('Favourite Posts'),
264                                 'id'    => 'starred-posts-tab',
265                                 'accesskey' => 'm',
266                         ],
267                 ];
268
269                 $arr = ['tabs' => $tabs];
270                 Hook::callAll('network_tabs', $arr);
271
272                 $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
273
274                 return Renderer::replaceMacros($tpl, ['$tabs' => $arr['tabs']]);
275         }
276
277         protected static function parseRequest(array $parameters, array $get)
278         {
279                 self::$groupId = $parameters['group_id'] ?? 0;
280
281                 self::$forumContactId = $parameters['contact_id'] ?? 0;
282
283                 self::$selectedTab = DI::pConfig()->get(local_user(), 'network.view', 'selected_tab', '');
284
285                 if (!empty($get['star'])) {
286                         self::$selectedTab = 'star';
287                 }
288
289                 if (!empty($get['mention'])) {
290                         self::$selectedTab = 'mention';
291                 }
292
293                 if (!empty($get['order'])) {
294                         self::$selectedTab = $get['order'];
295                 }
296
297                 DI::pConfig()->set(local_user(), 'network.view', 'selected_tab', self::$selectedTab);
298
299                 self::$star    = intval($get['star']      ?? 0);
300                 self::$mention = intval($_GET['mention']      ?? 0);
301                 self::$order   = in_array(self::$selectedTab, ['received', 'commented', 'created', 'uriid']) ? self::$selectedTab : 'commented';
302
303                 self::$accountType = User::getAccountTypeByString($parameters['accounttype'] ?? '') ?? '';
304
305                 self::$network = $get['nets'] ?? '';
306
307                 self::$dateFrom = $parameters['from'] ?? '';
308                 self::$dateTo = $parameters['to'] ?? '';
309
310                 if (DI::mode()->isMobile()) {
311                         self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
312                                 DI::config()->get('system', 'itemspage_network_mobile'));
313                 } else {
314                         self::$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
315                                 DI::config()->get('system', 'itemspage_network'));
316                 }
317
318                 self::$min_id = $_GET['min_id'] ?? null;
319                 self::$max_id = $_GET['max_id'] ?? null;
320
321                 switch (self::$selectedTab) {
322                         case 'received':
323                                 self::$max_id = $_GET['last_received'] ?? self::$max_id;
324                                 break;
325                         case 'commented':
326                                 self::$max_id = $_GET['last_commented'] ?? self::$max_id;
327                                 break;
328                         case 'created':
329                                 self::$max_id = $_GET['last_created'] ?? self::$max_id;
330                                 break;
331                         case 'uriid':
332                                 self::$max_id = $_GET['last_uriid'] ?? self::$max_id;
333                                 break;
334                 }
335         }
336
337         protected static function getItems(string $table, array $params, array $conditionFields = [])
338         {
339                 $conditionFields['uid'] = local_user();
340                 $conditionStrings = [];
341
342                 if (self::$accountType) {
343                         $conditionFields['contact-type'] = self::$accountType;
344                 }
345
346                 if (self::$star) {
347                         $conditionFields['starred'] = true;
348                 }
349                 if (self::$mention) {
350                         $conditionFields['mention'] = true;
351                 }
352                 if (self::$network) {
353                         $conditionFields['network'] = self::$network;
354                 }
355
356                 if (self::$dateFrom) {
357                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` <= ? ", DateTimeFormat::convert(self::$dateFrom, 'UTC', date_default_timezone_get())]);
358                 }
359                 if (self::$dateTo) {
360                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` >= ? ", DateTimeFormat::convert(self::$dateTo, 'UTC', date_default_timezone_get())]);
361                 }
362
363                 if (self::$groupId) {
364                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`contact-id` IN (SELECT `contact-id` FROM `group_member` WHERE `gid` = ?)", self::$groupId]);
365                 } elseif (self::$forumContactId) {
366                         $conditionFields['contact-id'] = self::$forumContactId;
367                 }
368
369                 // Currently only the order modes "received" and "commented" are in use
370                 if (isset(self::$max_id)) {
371                         switch (self::$order) {
372                                 case 'received':
373                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` < ?", self::$max_id]);
374                                         break;
375                                 case 'commented':
376                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` < ?", self::$max_id]);
377                                         break;
378                                 case 'created':
379                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` < ?", self::$max_id]);
380                                         break;
381                                 case 'uriid':
382                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` < ?", self::$max_id]);
383                                         break;
384                         }
385                 }
386
387                 if (isset(self::$min_id)) {
388                         switch (self::$order) {
389                                 case 'received':
390                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`received` > ?", self::$min_id]);
391                                         break;
392                                 case 'commented':
393                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`commented` > ?", self::$min_id]);
394                                         break;
395                                 case 'created':
396                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`created` > ?", self::$min_id]);
397                                         break;
398                                 case 'uriid':
399                                         $conditionStrings = DBA::mergeConditions($conditionStrings, ["`uri-id` > ?", self::$min_id]);
400                                         break;
401                         }
402                 }
403
404                 if (isset(self::$min_id) && !isset(self::$max_id)) {
405                         // min_id quirk: querying in reverse order with min_id gets the most recent rows, regardless of how close
406                         // they are to min_id. We change the query ordering to get the expected data, and we need to reverse the
407                         // order of the results.
408                         $params['order'] = [self::$order => false];
409                 } else {
410                         $params['order'] = [self::$order => true];
411                 }
412
413                 $items = DBA::selectToArray($table, [], DBA::mergeConditions($conditionFields, $conditionStrings), $params);
414
415                 // min_id quirk, continued
416                 if (isset(self::$min_id) && !isset(self::$max_id)) {
417                         $items = array_reverse($items);
418                 }
419
420                 $parents_str = '';
421                 if (DBA::isResult($items)) {
422                         $parents_arr = [];
423
424                         foreach ($items as $item) {
425                                 if (!in_array($item['parent'], $parents_arr) && ($item['parent'] > 0)) {
426                                         $parents_arr[] = $item['parent'];
427                                 }
428                         }
429                         $parents_str = implode(', ', $parents_arr);
430                 }
431
432                 // We aren't going to try and figure out at the item, group, and page
433                 // level which items you've seen and which you haven't. If you're looking
434                 // at the top level network page just mark everything seen.
435                 if (!self::$groupId && !self::$forumContactId && self::$selectedTab != 'star') {
436                         $condition = ['unseen' => true, 'uid' => local_user()];
437                         self::setItemsSeenByCondition($condition);
438                 } elseif ($parents_str) {
439                         $condition = ["`uid` = ? AND `unseen` AND `parent` IN (" . DBA::escape($parents_str) . ")", local_user()];
440                         self::setItemsSeenByCondition($condition);
441                 }
442
443                 return $items;
444         }
445 }