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\user\models\invitation\registration; |
14
|
|
|
|
15
|
|
|
use rhosocial\base\models\queries\BaseBlameableQuery; |
16
|
|
|
use rhosocial\user\models\invitation\Invitation; |
17
|
|
|
use rhosocial\user\User; |
18
|
|
|
use Yii; |
19
|
|
|
use yii\base\InvalidParamException; |
20
|
|
|
use yii\db\IntegrityException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Trait UserInvitationRegistrationTrait |
24
|
|
|
* @property-read Invitation[] invitationRegistrations |
25
|
|
|
* @property-read User[] invitationRegistrationInvitees |
26
|
|
|
* |
27
|
|
|
* @package rhosocial\user\models\invitation\registration |
28
|
|
|
* @version 1.0 |
29
|
|
|
* @author vistart <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
trait UserInvitationRegistrationTrait |
32
|
|
|
{ |
33
|
|
|
public $invitationRegistrationClass = Registration::class; |
34
|
|
|
/** |
35
|
|
|
* @param array $associatedModels |
36
|
|
|
* @param array $authRoles |
37
|
|
|
* @param User $inviter |
38
|
|
|
* @return boolean |
39
|
|
|
* @throws \Exception |
40
|
|
|
* @throws InvalidParamException |
41
|
|
|
*/ |
42
|
|
|
public function registerAccordingToInvitation($associatedModels = [], $authRoles = [], $inviter) |
43
|
|
|
{ |
44
|
|
|
if (!$inviter) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
if ($inviter instanceof User && $inviter->getIsNewRecord()) { |
48
|
|
|
throw new InvalidParamException("Inviter cannot be a new user."); |
49
|
|
|
} |
50
|
|
|
$transaction = Yii::$app->db->beginTransaction(); |
51
|
|
|
try { |
52
|
|
|
$result = $this->register($associatedModels, $authRoles); |
|
|
|
|
53
|
|
|
if ($result instanceof \Exception) { |
54
|
|
|
throw $result; |
55
|
|
|
} |
56
|
|
|
if ($result !== true) { |
57
|
|
|
throw new IntegrityException("Registration Failed."); |
58
|
|
|
} |
59
|
|
|
$invitation = $inviter->createInvitationRegistration($this); |
60
|
|
|
$result = $invitation->save(); |
61
|
|
|
if (!$result) { |
62
|
|
|
throw new IntegrityException("Record Invitation Failed."); |
63
|
|
|
} |
64
|
|
|
$transaction->commit(); |
65
|
|
|
} catch (\Exception $ex) { |
66
|
|
|
$transaction->rollBack(); |
67
|
|
|
throw $ex; |
68
|
|
|
} |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create a registration invitation instance. |
74
|
|
|
* @param string|User $invitee The invited person. |
75
|
|
|
* @return Registration |
76
|
|
|
*/ |
77
|
|
|
public function createInvitationRegistration($invitee) |
78
|
|
|
{ |
79
|
|
|
return $this->create($this->invitationRegistrationClass, ['invitee' => $invitee]); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Check whether this user enables the invitation from registration feature or not. |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
|
|
public function hasEnabledInvitationRegistration() |
87
|
|
|
{ |
88
|
|
|
if ($this->invitationRegistrationClass === false || !is_string($this->invitationRegistrationClass) || !class_exists($this->invitationRegistrationClass)) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return BaseBlameableQuery |
96
|
|
|
*/ |
97
|
|
|
public function getInvitationRegistrations() |
98
|
|
|
{ |
99
|
|
|
if (!$this->hasEnabledInvitationRegistration()) { |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
$irClass = $this->invitationRegistrationClass; |
103
|
|
|
$noInit = $irClass::buildNoInitModel(); |
104
|
|
|
/* @var $noInit Registration */ |
105
|
|
|
return $this->hasMany($irClass, [$noInit->createdByAttribute => $this->guidAttribute]); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return User[] |
110
|
|
|
*/ |
111
|
|
|
public function getInvitationRegistrationInvitees() |
112
|
|
|
{ |
113
|
|
|
return $this->hasMany(static::class, [$this->guidAttribute => 'invitee_guid'])->via('invitationRegistrations'); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.