Organization::getElement()   A
last analyzed

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 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\records;
10
11
use craft\base\ElementInterface;
12
use craft\helpers\Db;
13
use craft\records\Element as ElementRecord;
14
use craft\records\User as UserRecord;
15
use flipbox\craft\ember\records\ActiveRecordWithId;
16
use flipbox\organizations\records\OrganizationTypeAssociation as OrganizationTypeRecord;
17
use flipbox\organizations\records\UserAssociation as OrganizationUserRecord;
18
use yii\db\ActiveQueryInterface;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @property string $dateJoined
25
 * @property ElementInterface $element
26
 * @property OrganizationType[] $types
27
 * @property UserRecord[] $users
28
 */
29
class Organization extends ActiveRecordWithId
30
{
31
    /**
32
     * The table name
33
     */
34
    const TABLE_ALIAS = 'organizations';
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function beforeSave($insert)
40
    {
41
        if ($this->getIsNewRecord()) {
42
            if (!$this->dateJoined) {
43
                /** @noinspection PhpUnhandledExceptionInspection */
44
                $this->dateJoined = Db::prepareDateForDb(new \DateTime());
45
            }
46
        }
47
48
        return parent::beforeSave($insert);
49
    }
50
51
    /**
52
     * Returns the organizations's element.
53
     *
54
     * @return ActiveQueryInterface
55
     */
56
    public function getElement(): ActiveQueryInterface
57
    {
58
        return $this->hasOne(ElementRecord::class, ['id' => 'id']);
59
    }
60
61
    /**
62
     * @return ActiveQueryInterface
63
     * @throws \yii\base\InvalidConfigException
64
     */
65
    public function getTypes(): ActiveQueryInterface
66
    {
67
        // Todo - apply order by
68
        return $this->hasMany(OrganizationType::class, ['id' => 'typeId'])
69
            ->viaTable(
70
                OrganizationTypeRecord::tableName(),
71
                ['organizationId' => 'id']
72
            );
73
    }
74
75
    /**
76
     * Returns the organizations's users.
77
     *
78
     * @return ActiveQueryInterface
79
     */
80
    public function getUserAssociations(): ActiveQueryInterface
81
    {
82
        return $this->hasMany(OrganizationUserRecord::class, ['organizationId' => 'id'])
83
            ->orderBy(['sortOrder' => SORT_ASC]);
84
    }
85
86
    /**
87
     * Returns the organizations's users.
88
     *
89
     * @return ActiveQueryInterface
90
     */
91
    public function getUsers(): ActiveQueryInterface
92
    {
93
        // Todo - apply order by
94
        return $this->hasMany(UserRecord::class, ['id' => 'userId'])
95
            ->via('userAssociations');
96
    }
97
}
98