Completed
Push — develop ( fec580...e0c8ea )
by Nate
02:55
created

UsersAttributeTrait::dissociateUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\elements;
10
11
use Craft;
12
use craft\db\Query;
13
use craft\elements\db\UserQuery;
14
use craft\elements\User;
15
use craft\helpers\ArrayHelper;
16
use flipbox\craft\ember\helpers\QueryHelper;
17
use flipbox\organizations\managers\UsersToOrganizationAssociatedManager;
18
use flipbox\organizations\records\UserAssociation;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @mixin Organization
25
 */
26
trait UsersAttributeTrait
27
{
28
    /**
29
     * @var UsersToOrganizationAssociatedManager
30
     */
31
    private $userManager;
32
33
    /**
34
     * @return UsersToOrganizationAssociatedManager
35
     */
36
    public function getUserManager(): UsersToOrganizationAssociatedManager
37
    {
38
        if (null === $this->userManager) {
39
            $this->userManager = new UsersToOrganizationAssociatedManager($this);
40
        }
41
42
        return $this->userManager;
43
    }
44
45
    /**
46
     * @param array $sourceElements
47
     * @return array
48
     */
49
    private static function eagerLoadingUsersMap(array $sourceElements)
50
    {
51
        // Get the source element IDs
52
        $sourceElementIds = ArrayHelper::getColumn($sourceElements, 'id');
53
54
        $map = (new Query())
55
            ->select(['organizationId as source', 'userId as target'])
56
            ->from(UserAssociation::tableName())
57
            ->where(['organizationId' => $sourceElementIds])
58
            ->all();
59
60
        return [
61
            'elementType' => User::class,
62
            'map' => $map
63
        ];
64
    }
65
66
    /************************************************************
67
     * REQUEST
68
     ************************************************************/
69
70
    /**
71
     * AssociateUserToOrganization an array of users from request input
72
     *
73
     * @param string $identifier
74
     * @return $this
75
     */
76
    public function setUsersFromRequest(string $identifier = 'users')
77
    {
78
        if (null !== ($users = Craft::$app->getRequest()->getBodyParam($identifier))) {
79
            $this->getUserManager()->setMany((array)$users);
80
        }
81
82
        return $this;
83
    }
84
85
86
    /************************************************************
87
     * USERS QUERY
88
     ************************************************************/
89
90
    /**
91
     * @param array $criteria
92
     * @return UserQuery
93
     */
94
    public function userQuery($criteria = []): UserQuery
95
    {
96
        /** @noinspection PhpUndefinedMethodInspection */
97
        $query = User::find()
98
            ->organization($this)
99
            ->orderBy([
100
                'userOrder' => SORT_ASC,
101
                'username' => SORT_ASC,
102
            ]);
103
104
        if (!empty($criteria)) {
105
            QueryHelper::configure(
106
                $query,
107
                $criteria
108
            );
109
        }
110
111
        return $query;
112
    }
113
114
    /**
115
     * Get an array of users associated to an organization
116
     *
117
     * @return User[]
118
     */
119
    public function getUsers(): array
120
    {
121
        return ArrayHelper::getColumn(
122
            $this->getUserManager()->findAll(),
123
            'user'
124
        );
125
    }
126
}
127