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\actions\organizations; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\craft\ember\actions\records\SaveRecordTrait; |
13
|
|
|
use flipbox\organizations\records\UserAssociation; |
14
|
|
|
use yii\base\Action; |
15
|
|
|
use yii\db\ActiveRecord; |
16
|
|
|
use yii\web\HttpException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 3.0.0 |
21
|
|
|
*/ |
22
|
|
|
class AssociateUserToOrganization extends Action |
23
|
|
|
{ |
24
|
|
|
use SaveRecordTrait, LookupAssociationTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
public $validBodyParams = [ |
30
|
|
|
'sortOrder' => 'userOrder', |
31
|
|
|
'state' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $user |
36
|
|
|
* @param string $organization |
37
|
|
|
* @return null|\yii\base\Model|\yii\web\Response |
38
|
|
|
* @throws HttpException |
39
|
|
|
*/ |
40
|
|
|
public function run( |
41
|
|
|
string $user, |
42
|
|
|
string $organization |
43
|
|
|
) { |
44
|
|
|
if (null === ($user = $this->findUser($user))) { |
45
|
|
|
return $this->handleNotFoundResponse(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (null === ($organization = $this->findOrganization($organization))) { |
49
|
|
|
return $this->handleNotFoundResponse(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->runInternal( |
53
|
|
|
$organization->getUsers()->findOrCreate($user) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
* @param UserAssociation $record |
|
|
|
|
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
protected function performAction(UserAssociation $association): bool |
63
|
|
|
{ |
64
|
|
|
return $association->save(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Body params that should be set on the record. |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
protected function validBodyParams(): array |
73
|
|
|
{ |
74
|
|
|
return $this->validBodyParams; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param UserAssociation $record |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
protected function populate(UserAssociation $record): ActiveRecord |
82
|
|
|
{ |
83
|
|
|
$record->setAttributes( |
84
|
|
|
$this->attributeValuesFromBody() |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$types = Craft::$app->getRequest()->getBodyParam('types', []); |
88
|
|
|
if (!empty($types)) { |
89
|
|
|
$record->getTypes()->clear()->add($types); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $record; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.