1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Class ilContactUserActionProvider
6 * @author Michael Jansen <mjansen@databay.de>
7 */
8class ilContactUserActionProvider extends ilUserActionProvider
9{
10    /** @var ilObjUser */
11    protected $user;
12
13    /** @var string[] */
14    private $stateToPermLinkMap = [
15        'ilBuddySystemLinkedRelationState' => '_contact_approved',
16        'ilBuddySystemIgnoredRequestRelationState' => '_contact_ignored'
17    ];
18
19    /**
20     * ilContactUserActionProvider constructor.
21     */
22    public function __construct()
23    {
24        global $DIC;
25
26        parent::__construct();
27
28        $this->user = $DIC['ilUser'];
29    }
30
31    /**
32     * @inheritDoc
33     */
34    public function getComponentId()
35    {
36        return 'contact';
37    }
38
39    /**
40     * @inheritDoc
41     */
42    public function getActionTypes()
43    {
44        $this->lng->loadLanguageModule('buddysystem');
45        return [
46            'handle_req' => $this->lng->txt('buddy_handle_contact_request')
47        ];
48    }
49
50    /**
51     * @inheritDoc
52     */
53    public function collectActionsForTargetUser($a_target_user)
54    {
55        $coll = ilUserActionCollection::getInstance();
56
57        if (!ilBuddySystem::getInstance()->isEnabled()) {
58            return $coll;
59        }
60
61        if (ilObjUser::_isAnonymous($this->getUserId()) || $this->user->isAnonymous()) {
62            return $coll;
63        }
64
65        $buddyList = ilBuddyList::getInstanceByGlobalUser();
66        $requested_contacts = $buddyList->getRequestRelationsForOwner()->getKeys();
67
68        if (in_array($a_target_user, $requested_contacts)) {
69            $this->lng->loadLanguageModule('buddysystem');
70
71            $relation = $buddyList->getRelationByUserId($a_target_user);
72            foreach ($relation->getCurrentPossibleTargetStates() as $target_state) {
73                $f = new ilUserAction();
74                $f->setText(
75                    $this->lng->txt(
76                        'buddy_bs_act_btn_txt_requested_to_' .
77                        ilStr::convertUpperCamelCaseToUnderscoreCase($target_state->getName())
78                    )
79                );
80                $f->setType('handle_req');
81                $f->setHref(
82                    ilLink::_getStaticLink(
83                        $a_target_user,
84                        'usr',
85                        true,
86                        $this->stateToPermLinkMap[get_class($target_state)]
87                    )
88                );
89                $f->setData([
90                    'current-state' => get_class($relation->getState()),
91                    'target-state' => get_class($target_state),
92                    'buddy-id' => $a_target_user,
93                    'action' => $target_state->getAction()
94                ]);
95                $coll->addAction($f);
96            }
97        }
98
99        return $coll;
100    }
101}
102