]> git.mxchange.org Git - friendica.git/commitdiff
Add new Widget\ContactBlock
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 6 Jan 2019 03:04:42 +0000 (22:04 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 21 Jan 2019 15:54:42 +0000 (10:54 -0500)
src/Content/Widget/ContactBlock.php [new file with mode: 0644]

diff --git a/src/Content/Widget/ContactBlock.php b/src/Content/Widget/ContactBlock.php
new file mode 100644 (file)
index 0000000..12cd4cd
--- /dev/null
@@ -0,0 +1,111 @@
+<?php
+
+/*
+ * @file src/Content/Widget/ContactBlock.php
+ */
+
+namespace Friendica\Content\Widget;
+
+use Friendica\Content\Text\HTML;
+use Friendica\Core\Hook;
+use Friendica\Core\L10n;
+use Friendica\Core\PConfig;
+use Friendica\Core\Protocol;
+use Friendica\Core\Renderer;
+use Friendica\Database\DBA;
+use Friendica\Model\Contact;
+
+/**
+ * ContactBlock widget
+ *
+ * @author Hypolite Petovan
+ */
+class ContactBlock
+{
+       /**
+        * Get HTML for contact block
+        *
+        * @template contact_block.tpl
+        * @hook contact_block_end (contacts=>array, output=>string)
+        * @return string
+        */
+       public static function getHTML(array $profile)
+       {
+               $o = '';
+
+               $shown = PConfig::get($profile['uid'], 'system', 'display_friend_count', 24);
+               if ($shown == 0) {
+                       return $o;
+               }
+
+               if (!empty($profile['hide-friends'])) {
+                       return $o;
+               }
+
+               $contacts = [];
+
+               $total = DBA::count('contact', [
+                       'uid' => $profile['uid'],
+                       'self' => false,
+                       'blocked' => false,
+                       'pending' => false,
+                       'hidden' => false,
+                       'archive' => false,
+                       'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA],
+               ]);
+
+               $contacts_title = L10n::t('No contacts');
+
+               if (!$total) {
+                       $micropro = [];
+               } else {
+                       $contact_ids_stmt = DBA::select('contact', ['id'], [
+                               'uid' => $profile['uid'],
+                               'self' => false,
+                               'blocked' => false,
+                               'pending' => false,
+                               'hidden' => false,
+                               'archive' => false,
+                               'rel' => [Contact::FOLLOWER, Contact::FRIEND],
+                               'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA],
+                       ], ['limit' => $shown]);
+
+                       if (DBA::isResult($contact_ids_stmt)) {
+                               $contact_ids = [];
+                               while($contact = DBA::fetch($contact_ids_stmt)) {
+                                       $contact_ids[] = $contact["id"];
+                               }
+
+                               $contacts_stmt = DBA::select('contact', ['id', 'uid', 'addr', 'url', 'name', 'thumb', 'network'], ['id' => $contact_ids]);
+
+                               if (DBA::isResult($contacts_stmt)) {
+                                       $contacts_title = L10n::tt('%d Contact', '%d Contacts', $total);
+                                       $micropro = [];
+
+                                       while ($contact = DBA::fetch($contacts_stmt)) {
+                                               $contacts[] = $contact;
+                                               $micropro[] = HTML::micropro($contact, true, 'mpfriend');
+                                       }
+                               }
+
+                               DBA::close($contacts_stmt);
+                       }
+
+                       DBA::close($contact_ids_stmt);
+               }
+
+               $tpl = Renderer::getMarkupTemplate('contact_block.tpl');
+               $o = Renderer::replaceMacros($tpl, [
+                       '$contacts' => $contacts_title,
+                       '$nickname' => $profile['nickname'],
+                       '$viewcontacts' => L10n::t('View Contacts'),
+                       '$micropro' => $micropro,
+               ]);
+
+               $arr = ['contacts' => $contacts, 'output' => $o];
+
+               Hook::callAll('contact_block_end', $arr);
+
+               return $o;
+       }
+}