1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.me/ |
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
10
|
|
|
* @license https://vistart.me/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace rhosocial\organization; |
14
|
|
|
|
15
|
|
|
use rhosocial\organization\queries\MemberQuery; |
16
|
|
|
use rhosocial\organization\queries\OrganizationQuery; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\base\InvalidConfigException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @property string $guidAttribute GUID Attribute. |
22
|
|
|
* @property-read Member[] $ofMembers |
23
|
|
|
* @property-read Organization[] $atOrganizations |
24
|
|
|
* |
25
|
|
|
* @version 1.0 |
26
|
|
|
* @author vistart <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
trait UserOrganizationTrait |
29
|
|
|
{ |
30
|
|
|
public $organizationClass = Organization::class; |
31
|
|
|
public $departmentClass = Department::class; |
32
|
|
|
public $memberClass = Member::class; |
33
|
|
|
private $noInitOrganization; |
34
|
|
|
private $noInitMember; |
35
|
|
|
/** |
36
|
|
|
* @return Organization |
37
|
|
|
*/ |
38
|
|
|
protected function getNoInitOrganization() |
39
|
|
|
{ |
40
|
|
|
if (!$this->noInitOrganization) { |
41
|
|
|
$class = $this->organizationClass; |
42
|
|
|
$this->noInitOrganization = $class::buildNoInitModel(); |
43
|
|
|
} |
44
|
|
|
return $this->noInitOrganization; |
45
|
|
|
} |
46
|
|
|
/** |
47
|
|
|
* @return Member |
48
|
|
|
*/ |
49
|
|
|
protected function getNoInitMember() |
50
|
|
|
{ |
51
|
|
|
if (!$this->noInitMember) { |
52
|
|
|
$class = $this->memberClass; |
53
|
|
|
$this->noInitMember = $class::buildNoInitModel(); |
54
|
|
|
} |
55
|
|
|
return $this->noInitMember; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
* @return MemberQuery |
61
|
|
|
*/ |
62
|
|
|
public function getOfMembers() |
63
|
|
|
{ |
64
|
|
|
return $this->hasMany($this->memberClass, [$this->guidAttribute => $this->getNoInitMember()->memberAttribute])->inverseOf('memberUser'); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* @return OrganizationQuery |
70
|
|
|
*/ |
71
|
|
|
public function getAtOrganizations() |
72
|
|
|
{ |
73
|
|
|
return $this->hasMany($this->organizationClass, [$this->guidAttribute => $this->getNoInitOrganization()->guidAttribute])->via('ofMembers'); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set up organization. |
78
|
|
|
* @param string $name |
79
|
|
|
* @param string $nickname |
80
|
|
|
* @param integer $gravatar_type |
81
|
|
|
* @param string $gravatar |
82
|
|
|
* @param string $timezone |
83
|
|
|
* @param string $description |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
|
|
public function setUpOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$transaction = Yii::$app->db->beginTransaction(); |
89
|
|
|
try { |
90
|
|
|
$models = $this->createOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = ''); |
91
|
|
|
if (!array_key_exists('organization', $models) || !($models['organization'] instanceof Organization)) { |
92
|
|
|
throw new InvalidConfigException('Invalid Organization Model.'); |
93
|
|
|
} |
94
|
|
|
$result = $models['organization']->register($models['associatedModels']); |
95
|
|
|
if ($result instanceof \Exception) { |
96
|
|
|
throw $result; |
97
|
|
|
} |
98
|
|
|
if ($result !== true) { |
99
|
|
|
throw new \Exception('Failed to set up.'); |
100
|
|
|
} |
101
|
|
|
$transaction->commit(); |
102
|
|
|
} catch (\Exception $ex) { |
103
|
|
|
$transaction->rollBack(); |
104
|
|
|
Yii::error($ex->getMessage(), __METHOD__); |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set Up Department. |
112
|
|
|
* @param BaseOrganization $organization |
113
|
|
|
* @param type $department |
114
|
|
|
*/ |
115
|
|
|
public function setUpDepartment($organization, $department) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* |
122
|
|
|
* @param string $name |
123
|
|
|
* @param string $nickname |
124
|
|
|
* @param string $gravatar_type |
125
|
|
|
* @param string $gravatar |
126
|
|
|
* @param string $timezone |
127
|
|
|
* @param string $description |
128
|
|
|
*/ |
129
|
|
|
public function createOrganization($name, $nickname = '', $gravatar_type = 0, $gravatar = '', $timezone = 'UTC', $description = '') |
130
|
|
|
{ |
131
|
|
|
$class = $this->organizationClass; |
132
|
|
|
$organization = new $class(); |
133
|
|
|
/* @var $organization BaseOrganization */ |
134
|
|
|
$profileConfig = [ |
135
|
|
|
'name' => $name, |
136
|
|
|
'nickname' => $nickname, |
137
|
|
|
'gravatar_type' => $gravatar_type, |
138
|
|
|
'gravatar' => $gravatar, |
139
|
|
|
'timezone' => $timezone, |
140
|
|
|
'description' => $description, |
141
|
|
|
]; |
142
|
|
|
$profile = $organization->createProfile($profileConfig); |
143
|
|
|
$member = $organization->createMemberModelWithUser($this); |
144
|
|
|
return ['organization' => $organization, 'associatedModels' => ['profile' => $profile, 'creator'=> $member]]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.