]> git.mxchange.org Git - friendica.git/commitdiff
Rework Module\Profile\Contacts class
authorHypolite Petovan <hypolite@mrpetovan.com>
Wed, 5 Aug 2020 03:02:43 +0000 (23:02 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Wed, 5 Aug 2020 03:02:43 +0000 (23:02 -0400)
- Simplify imports
- Use router parameters
- Use exceptions
- Simplify querying and result processing
- Add no result label

src/Module/Profile/Contacts.php
view/templates/profile/contacts.tpl
view/theme/frio/templates/profile/contacts.tpl

index e55c0d9a845e959582d7ef203087dbcfaec7f6c4..b1d8fa6c2fa812c674771d5b425d2350f5a2297d 100644 (file)
@@ -28,57 +28,53 @@ use Friendica\Core\Renderer;
 use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\DI;
-use Friendica\Model\Profile;
-use Friendica\Module\BaseProfile;
-use Friendica\Module\Contact as ModuleContact;
+use Friendica\Model;
+use Friendica\Module;
+use Friendica\Network\HTTPException;
 
-class Contacts extends BaseProfile
+class Contacts extends Module\BaseProfile
 {
        public static function content(array $parameters = [])
        {
                if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
-                       throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
+                       throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
                }
 
                $a = DI::app();
 
-               //@TODO: Get value from router parameters
-               $nickname = $a->argv[1];
-               $type = ($a->argv[3] ?? '') ?: 'all';
+               $nickname = $parameters['nickname'];
+               $type = $parameters['type'] ?? 'all';
 
-               Nav::setSelected('home');
+               Model\Profile::load($a, $nickname);
 
-               $user = DBA::selectFirst('user', [], ['nickname' => $nickname, 'blocked' => false]);
-               if (!DBA::isResult($user)) {
-                       throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
+               if (empty($a->profile)) {
+                       throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
                }
 
-               $a->profile_uid  = $user['uid'];
+               if (!empty($a->profile['hide-friends'])) {
+                       throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
+               }
 
-               Profile::load($a, $nickname);
+               Nav::setSelected('home');
 
                $is_owner = $a->profile['uid'] == local_user();
 
                $o = self::getTabsHTML($a, 'contacts', $is_owner, $nickname);
 
-               if (!count($a->profile) || $a->profile['hide-friends']) {
-                       notice(DI::l10n()->t('Permission denied.'));
-                       return $o;
-               }
-
                $condition = [
                        'uid'     => $a->profile['uid'],
                        'blocked' => false,
                        'pending' => false,
                        'hidden'  => false,
                        'archive' => false,
+                       'self'    => false,
                        'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED]
                ];
 
                switch ($type) {
-                       case 'followers': $condition['rel'] = [1, 3]; break;
-                       case 'following': $condition['rel'] = [2, 3]; break;
-                       case 'mutuals': $condition['rel'] = 3; break;
+                       case 'followers': $condition['rel'] = [Model\Contact::FOLLOWER, Model\Contact::FRIEND]; break;
+                       case 'following': $condition['rel'] = [Model\Contact::SHARING,  Model\Contact::FRIEND]; break;
+                       case 'mutuals':   $condition['rel'] = Model\Contact::FRIEND; break;
                }
 
                $total = DBA::count('contact', $condition);
@@ -87,42 +83,44 @@ class Contacts extends BaseProfile
 
                $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
 
-               $contacts_stmt = DBA::select('contact', [], $condition, $params);
-
-               if (!DBA::isResult($contacts_stmt)) {
-                       notice(DI::l10n()->t('No contacts.'));
-                       return $o;
-               }
-
-               $contacts = [];
-
-               while ($contact = DBA::fetch($contacts_stmt)) {
-                       if ($contact['self']) {
-                               continue;
-                       }
-                       $contacts[] = ModuleContact::getContactTemplateVars($contact);
-               }
-
-               DBA::close($contacts_stmt);
+               $contacts = array_map(
+                       [Module\Contact::class, 'getContactTemplateVars'],
+                       Model\Contact::selectToArray([], $condition, $params)
+               );
 
+               $desc = '';
                switch ($type) {
-                       case 'followers':    $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total); break;
-                       case 'following':    $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total); break;
-                       case 'mutuals':      $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total); break;
-
-                       case 'all': default: $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total); break;
+                       case 'followers':
+                               $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total);
+                               break;
+                       case 'following':
+                               $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total);
+                               break;
+                       case 'mutuals':
+                               $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
+                               $desc = DI::l10n()->t(
+                                       'These contacts both follow and are followed by <strong>%s</strong>.',
+                                       htmlentities($a->profile['name'], ENT_COMPAT, 'UTF-8')
+                               );
+                               break;
+                       case 'all':
+                       default:
+                               $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total);
+                               break;
                }
 
                $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
                $o .= Renderer::replaceMacros($tpl, [
                        '$title'    => $title,
+                       '$desc'     => $desc,
                        '$nickname' => $nickname,
                        '$type'     => $type,
 
-                       '$all_label' => DI::l10n()->t('All contacts'),
+                       '$all_label'       => DI::l10n()->t('All contacts'),
                        '$followers_label' => DI::l10n()->t('Followers'),
                        '$following_label' => DI::l10n()->t('Following'),
-                       '$mutuals_label' => DI::l10n()->t('Mutual friends'),
+                       '$mutuals_label'   => DI::l10n()->t('Mutual friends'),
+                       '$noresult_label'  => DI::l10n()->t('No contacts.'),
 
                        '$contacts' => $contacts,
                        '$paginate' => $pager->renderFull($total),
index 4e78a7a7f83ffb79c13846b97df354d07ae38c9c..90ef867c7754092442bf4636454d09e723431354 100644 (file)
@@ -1,18 +1,26 @@
 <div class="generic-page-wrapper">
        {{include file="section_title.tpl"}}
 
+{{if $desc}}
+       <p>{{$desc nofilter}}</p>
+{{/if}}
+
        <ul role="menubar" class="tabs">
                <li role="menuitem"><a href="profile/{{$nickname}}/contacts" class="tab button{{if !$type || $type == 'all'}} active{{/if}}">{{$all_label}}</a></li>
                <li role="menuitem"><a href="profile/{{$nickname}}/contacts/followers" class="tab button{{if $type == 'followers'}} active{{/if}}">{{$followers_label}}</a></li>
                <li role="menuitem"><a href="profile/{{$nickname}}/contacts/following" class="tab button{{if $type == 'following'}} active{{/if}}">{{$following_label}}</a></li>
                <li role="menuitem"><a href="profile/{{$nickname}}/contacts/mutuals" class="tab button{{if $type == 'mutuals'}} active{{/if}}">{{$mutuals_label}}</a></li>
        </ul>
-
+{{if $contacts}}
        <div id="viewcontact_wrapper-{{$id}}">
-{{foreach $contacts as $contact}}
+       {{foreach $contacts as $contact}}
                {{include file="contact_template.tpl"}}
-{{/foreach}}
+       {{/foreach}}
        </div>
+{{else}}
+       <div class="alert alert-info" role="alert">{{$noresult_label}}</div>
+{{/if}}
+
        <div class="clear"></div>
        <div id="view-contact-end"></div>
 
index 69d34d05abd1b59001b7d1b8728519717701acf3..40c0fb6e33315354edc2b7c16e0e655ab6a8c9cb 100644 (file)
@@ -1,18 +1,33 @@
 <div class="generic-page-wrapper">
        {{include file="section_title.tpl"}}
 
+{{if $desc}}
+       <p>{{$desc nofilter}}</p>
+{{/if}}
+
        <ul class="nav nav-tabs">
-               <li role="presentation"{{if !$type || $type == 'all'}} class="active"{{/if}}><a href="profile/{{$nickname}}/contacts">{{$all_label}}</a></li>
-               <li role="presentation"{{if $type == 'followers'}} class="active"{{/if}}><a href="profile/{{$nickname}}/contacts/followers">{{$followers_label}}</a></li>
-               <li role="presentation"{{if $type == 'following'}} class="active"{{/if}}><a href="profile/{{$nickname}}/contacts/following">{{$following_label}}</a></li>
-               <li role="presentation"{{if $type == 'mutuals'}} class="active"{{/if}}><a href="profile/{{$nickname}}/contacts/mutuals">{{$mutuals_label}}</a></li>
+               <li role="presentation"{{if !$type || $type == 'all'}} class="active"{{/if}}>
+                       <a href="profile/{{$nickname}}/contacts">{{$all_label}}</a>
+               </li>
+               <li role="presentation"{{if $type == 'followers'}} class="active"{{/if}}>
+                       <a href="profile/{{$nickname}}/contacts/followers">{{$followers_label}}</a>
+               </li>
+               <li role="presentation"{{if $type == 'following'}} class="active"{{/if}}>
+                       <a href="profile/{{$nickname}}/contacts/following">{{$following_label}}</a>
+               </li>
+               <li role="presentation"{{if $type == 'mutuals'}} class="active"{{/if}}>
+                       <a href="profile/{{$nickname}}/contacts/mutuals">{{$mutuals_label}}</a>
+               </li>
        </ul>
-
+{{if $contacts}}
        <ul id="viewcontact_wrapper{{if $id}}-{{$id}}{{/if}}" class="viewcontact_wrapper media-list">
-{{foreach $contacts as $contact}}
+       {{foreach $contacts as $contact}}
                <li>{{include file="contact_template.tpl"}}</li>
-{{/foreach}}
+       {{/foreach}}
        </ul>
+{{else}}
+       <div class="alert alert-info" role="alert">{{$noresult_label}}</div>
+{{/if}}
        <div class="clear"></div>
        <div id="view-contact-end"></div>