-
Notifications
You must be signed in to change notification settings - Fork 50
/
Config.php
160 lines (133 loc) · 6.01 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace humhub\modules\mail\models;
use DateInterval;
use DateTime;
use humhub\modules\user\models\User;
use Yii;
use humhub\modules\mail\Module;
/**
* ConfigureForm defines the configurable fields.
*
* @package humhub\modules\mail\models
* @author Akopian Gaik
*/
class Config extends \yii\base\Model
{
public $showInTopNav;
public $newUserRestrictionEnabled = 0;
public $newUserSinceDays = 3;
public $newUserConversationRestriction = 2;
public $newUserMessageRestriction = 5;
public $userConversationRestriction = 15;
public $userMessageRestriction = null;
public function init()
{
parent::init();
$module = $this->getModule();
$this->showInTopNav = !$module->hideInTopNav();
$this->newUserRestrictionEnabled = (int) $module->settings->get('newUserRestrictionEnabled', $this->newUserRestrictionEnabled);
$this->newUserSinceDays = (int) $module->settings->get('newUserSinceDays', $this->newUserSinceDays);
$this->newUserConversationRestriction = (int) $module->settings->get('newUserConversationRestriction', $this->newUserConversationRestriction);
$this->newUserMessageRestriction = (int) $module->settings->get('newUserMessageRestriction', $this->newUserMessageRestriction);
$this->userConversationRestriction = (int) $module->settings->get('userConversationRestriction', $this->userConversationRestriction);
$this->userMessageRestriction = (int) $module->settings->get('userMessageRestriction', $this->userMessageRestriction);
}
/**
* @return Module
*/
public static function getModule()
{
return Yii::$app->getModule('mail');
}
/**
* Declares the validation rules.
*/
public function rules()
{
return [
[['showInTopNav', 'newUserRestrictionEnabled'], 'boolean'],
[['newUserConversationRestriction',
'newUserMessageRestriction',
'userConversationRestriction',
'userMessageRestriction'], 'integer', 'min' => 0],
['newUserSinceDays', 'integer', 'min' => 1],
];
}
/**
* Declares customized attribute labels.
* If not declared here, an attribute would have a label that is
* the same as its name with the first letter in upper case.
*/
public function attributeLabels()
{
return [
'showInTopNav' => Yii::t('MailModule.base', 'Show menu item in top Navigation'),
'newUserRestrictionEnabled' => Yii::t('MailModule.base', 'Seperate restrictions for new users'),
'newUserSinceDays' => Yii::t('MailModule.base', 'Until a user is member since (days)'),
'newUserConversationRestriction' => Yii::t('MailModule.base', 'Max number of new conversations allowed for a new user per day'),
'newUserMessageRestriction' => Yii::t('MailModule.base', 'Max number of messages allowed for a new user per day'),
'userConversationRestriction' => Yii::t('MailModule.base', 'Max number of new conversations allowed for a user per day'),
'userMessageRestriction' => Yii::t('MailModule.base', 'Max messages allowed per day'),
];
}
public function save()
{
if (!$this->validate()) {
return false;
}
$module = static::getModule();
$module->settings->set('showInTopNav', $this->showInTopNav);
$module->settings->set('newUserRestrictionEnabled', $this->newUserRestrictionEnabled);
$module->settings->set('newUserSinceDays', $this->newUserSinceDays);
$module->settings->set('newUserConversationRestriction', $this->newUserConversationRestriction);
$module->settings->set('newUserMessageRestriction', $this->newUserMessageRestriction);
$module->settings->set('userConversationRestriction', $this->userConversationRestriction);
$module->settings->set('userMessageRestriction', $this->userMessageRestriction);
return true;
}
public function canCreateConversation(User $originator)
{
if ($originator->isSystemAdmin()) {
return true;
}
$maxConversations = $this->isNewUser($originator)
? $this->newUserConversationRestriction
: $this->userConversationRestriction;
return empty($maxConversations) || $this->getConversationCount($originator) < $maxConversations;
}
public function isNewUser(User $originator)
{
if (empty($this->newUserRestrictionEnabled)) {
return false;
}
return (new DateTime($originator->created_at))->diff(new DateTime())->days < $this->newUserSinceDays;
}
public function getConversationCount($originator)
{
$module = static::getModule();
$lastTS = $module->settings->contentContainer($originator)->get('conversationCountTime');
if (!$lastTS) {
$module->settings->contentContainer($originator)->set('conversationCountTime', time());
$module->settings->contentContainer($originator)->set('conversationCount', 0);
return 0;
}
$lastDate = (new \DateTime())->setTimestamp($lastTS);
$today = (new \DateTime())->setTime(0, 0, 0);
if ($today > $lastDate) {
$module->settings->contentContainer($originator)->set('conversationCountTime', time());
$module->settings->contentContainer($originator)->set('conversationCount', 0);
return 0;
}
return (int) static::getModule()->settings->contentContainer($originator)->get('conversationCount', 0);
}
public function reset($originator)
{
$module = static::getModule();
$module->settings->contentContainer($originator)->set('conversationCountTime', null);
$module->settings->contentContainer($originator)->set('conversationCount', null);
}
public function incrementConversationCount($originator)
{
static::getModule()->settings->contentContainer($originator)->set('conversationCount', ($this->getConversationCount($originator) + 1));
}
}