Completed
Push — master ( 3fed4b...dafce2 )
by Nate
16:31 queued 05:53
created

Organization::getElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
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\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
                $this->dateJoined = Db::prepareDateForDb(new \DateTime());
44
            }
45
        }
46
47
        return parent::beforeSave($insert);
48
    }
49
50
    /**
51
     * Returns the organizations's element.
52
     *
53
     * @return ActiveQueryInterface
54
     */
55
    public function getElement(): ActiveQueryInterface
56
    {
57
        return $this->hasOne(ElementRecord::class, ['id' => 'id']);
58
    }
59
60
    /**
61
     * Returns the organization's types.
62
     *
63
     * @return ActiveQueryInterface
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