|
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\base\models\models\BaseBlameableModel; |
|
16
|
|
|
use rhosocial\base\models\queries\BaseUserQuery; |
|
17
|
|
|
use rhosocial\user\User; |
|
18
|
|
|
use Yii; |
|
19
|
|
|
use yii\base\Event; |
|
20
|
|
|
use yii\behaviors\BlameableBehavior; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class OrganizationSetting |
|
24
|
|
|
* |
|
25
|
|
|
* @property string $item |
|
26
|
|
|
* @property mixed $value |
|
27
|
|
|
* @property-read User $operator The user who last modified this setting. |
|
28
|
|
|
* Note: the return value may be null, please note that the case of invalid user. |
|
29
|
|
|
* |
|
30
|
|
|
* @package rhosocial\organization |
|
31
|
|
|
* @version 1.0 |
|
32
|
|
|
* @author vistart <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
class OrganizationSetting extends BaseBlameableModel |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var string Host class. |
|
38
|
|
|
* You must assign with your own [[Organization]] class. |
|
39
|
|
|
*/ |
|
40
|
|
|
public $hostClass = Organization::class; |
|
41
|
|
|
|
|
42
|
|
|
public $idAttribute = 'item'; |
|
43
|
|
|
public $idPreassigned = true; |
|
44
|
|
|
public $createdByAttribute = 'organization_guid'; |
|
45
|
|
|
public $updatedByAttribute = false; |
|
46
|
|
|
public $enableIP = false; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
public $operatorAttribute = 'operator_guid'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
public $contentAttribute = 'value'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get operator query. |
|
60
|
|
|
* If you want to get operator, please access [[$operator]] magic-property. |
|
61
|
|
|
* @return BaseUserQuery |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getOperator() |
|
64
|
|
|
{ |
|
65
|
|
|
if (empty($this->operatorAttribute) || !is_string($this->operatorAttribute)) { |
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
$userClass = Yii::$app->user->identityClass; |
|
69
|
|
|
$noInit = $userClass::buildNoInitModel(); |
|
70
|
|
|
/* @var $noInit User */ |
|
71
|
|
|
return $this->hasOne($userClass, [$noInit->guidAttribute => $this->operatorAttribute]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritdoc |
|
76
|
|
|
*/ |
|
77
|
31 |
|
public static function tableName() |
|
78
|
|
|
{ |
|
79
|
31 |
|
return '{{%organization_setting}}'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param Event $event |
|
84
|
|
|
* @return null|string |
|
85
|
|
|
*/ |
|
86
|
31 |
|
public function onAssignOperator($event) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
31 |
|
$identity = Yii::$app->user->identity; |
|
89
|
31 |
|
if (empty($identity)) { |
|
90
|
31 |
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
return $identity->getGUID(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritdoc |
|
97
|
|
|
*/ |
|
98
|
31 |
|
public function behaviors() |
|
99
|
|
|
{ |
|
100
|
31 |
|
$behaviors = parent::behaviors(); |
|
101
|
31 |
|
if (!empty($this->operatorAttribute) && is_string($this->operatorAttribute)) { |
|
102
|
31 |
|
$behaviors[] = [ |
|
103
|
31 |
|
'class' => BlameableBehavior::class, |
|
104
|
|
|
'createdByAttribute' => false, |
|
105
|
31 |
|
'updatedByAttribute' => $this->operatorAttribute, |
|
106
|
31 |
|
'value' => [$this, 'onAssignOperator'], |
|
107
|
|
|
]; |
|
108
|
|
|
} |
|
109
|
31 |
|
return $behaviors; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritdoc |
|
114
|
|
|
*/ |
|
115
|
31 |
|
public function rules() |
|
116
|
|
|
{ |
|
117
|
31 |
|
return array_merge(parent::rules(), [ |
|
118
|
31 |
|
[$this->idAttribute, 'string', 'max' => 255], |
|
119
|
31 |
|
[$this->operatorAttribute, 'safe'], |
|
120
|
|
|
]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @inheritdoc |
|
125
|
|
|
*/ |
|
126
|
|
|
public function attributeLabels() |
|
127
|
|
|
{ |
|
128
|
|
|
return [ |
|
129
|
|
|
$this->guidAttribute => Yii::t('user','GUID'), |
|
130
|
|
|
$this->createdByAttribute => Yii::t('organization', 'Organization GUID'), |
|
131
|
|
|
$this->idAttribute => 'Item', |
|
132
|
|
|
$this->contentAttribute => 'Value', |
|
133
|
|
|
$this->operatorAttribute => Yii::t('organization', 'Operator'), |
|
134
|
|
|
$this->createdAtAttribute => Yii::t('user', 'Creation Time'), |
|
135
|
|
|
$this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'), |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.