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\organization\validators; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\organization\Organization as OrganizationPlugin; |
13
|
|
|
use flipbox\organization\records\Organization; |
14
|
|
|
use yii\validators\Validator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class Owner extends Validator |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
public $skipOnEmpty = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Organization $object |
27
|
|
|
* @param $attribute |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function validateAttribute($object, $attribute) |
32
|
|
|
{ |
33
|
|
|
$value = $object->{$attribute}; |
34
|
|
|
|
35
|
|
|
if (OrganizationPlugin::getInstance()->getSettings()->requireOwner && $this->isEmpty($value)) { |
36
|
|
|
// Invalid status message |
37
|
|
|
$message = Craft::t('organization', 'Owner is required.'); |
38
|
|
|
|
39
|
|
|
// Add error |
40
|
|
|
$this->addError($object, $attribute, $message); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if ($this->isEmpty($value)) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// If a user doesn't exist, than this means we have an invalid owner element |
48
|
|
|
if (!$userModel = Craft::$app->getUsers()->getUserById($value)) { |
49
|
|
|
// Invalid status message |
50
|
|
|
$message = Craft::t('organization', 'Owner is of invalid type.'); |
51
|
|
|
|
52
|
|
|
// Add error |
53
|
|
|
$this->addError($object, $attribute, $message); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Are they already a member of another organization? |
57
|
|
|
if (OrganizationPlugin::getInstance()->getSettings()->uniqueOwner) { |
58
|
|
|
$query = OrganizationPlugin::getInstance()->getOrganization()->getQuery([ |
59
|
|
|
'id' => 'not ' . $object->id, |
60
|
|
|
'status' => null, |
61
|
|
|
'owner' => $userModel->id |
62
|
|
|
]); |
63
|
|
|
|
64
|
|
|
if ($query->count()) { |
65
|
|
|
// Invalid status message |
66
|
|
|
$message = Craft::t('organization', 'Owner is already in use.'); |
67
|
|
|
|
68
|
|
|
// Add error |
69
|
|
|
$this->addError($object, $attribute, $message); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|