-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEvents.php
363 lines (310 loc) · 14.9 KB
/
Events.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
namespace humhub\modules\xcoin;
use humhub\modules\admin\permissions\SeeAdminInformation;
use humhub\modules\xcoin\assets\Assets;
use humhub\modules\xcoin\helpers\AccountHelper;
use humhub\modules\xcoin\helpers\AssetHelper;
use humhub\modules\xcoin\models\Account;
use humhub\modules\xcoin\models\Challenge;
use humhub\modules\xcoin\models\Funding;
use humhub\modules\xcoin\models\Marketplace;
use humhub\modules\xcoin\models\Product;
use humhub\modules\xcoin\models\Transaction;
use humhub\widgets\TopMenu;
use Yii;
use yii\base\BaseObject;
use yii\base\Event;
use yii\base\Exception;
use yii\helpers\Url;
use yii\web\HttpException;
class Events
{
public static function onTopMenuInit($event)
{
if (Yii::$app->getModule('xcoin')->isCrowdfundingEnabled()) {
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Crowd Funding'),
'url' => Url::to(['/xcoin/funding-overview']),
'icon' => '<i class="fa fa-leaf"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && Yii::$app->controller->id == 'funding-overview'),
'sortOrder' => 900,
]);
}
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Marketplace'),
'url' => Url::to(['/xcoin/marketplace-overview']),
'icon' => '<i class="fa fa-shopping-basket"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && Yii::$app->controller->id == 'marketplace-overview'),
'sortOrder' => 900,
]);
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Search'),
'url' => Url::to(['/search']),
'icon' => '<i class="fa fa-search"></i>',
'isActive' => (Yii::$app->controller->id == 'search'),
'sortOrder' => 900,
]);
$event->sender->addItem([
'label' => Yii::t('DashboardModule.base', 'Home'),
'id' => 'dashboard',
'icon' => '<i class="fa fa-home"></i>',
'url' => Url::toRoute('/home'),
'sortOrder' => 100,
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'dashboard'),
]);
$event->sender->addItem([
'label' => Yii::t('DashboardModule.base', 'People'),
'id' => 'dashboard',
'icon' => '<i class="fa fa-user"></i>',
'url' => Url::toRoute('/directory/members'),
'sortOrder' => 200,
'isActive' => (
Yii::$app->controller->module &&
Yii::$app->controller->module->id == 'directory' &&
Yii::$app->controller->action->id == 'members'
),
]);
$event->sender->addItem([
'label' => Yii::t('DashboardModule.base', 'Spaces'),
'id' => 'dashboard',
'icon' => '<i class="fa fa-connectdevelop"></i>',
'url' => Url::toRoute('/directory/spaces'),
'sortOrder' => 200,
'isActive' => (
Yii::$app->controller->module &&
Yii::$app->controller->module->id == 'directory' &&
Yii::$app->controller->action->id == 'spaces'
),
]);
Event::on(TopMenu::class, TopMenu::EVENT_BEFORE_RUN, function ($event) {
// deactivate directory menu item
$event->sender->deleteItemByUrl(Url::to(['/directory/directory']));
$event->sender->deleteItemByUrl(Url::to(['/dashboard/dashboard']));
});
}
public static function onSpaceMenuInit($event)
{
$space = $event->sender->space;
if ($space->isModuleEnabled('xcoin')) {
// used to include ether-icon since it's not present in fontawesome 4.7.0 icons
Assets::register(Yii::$app->view);
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Accounts'),
'url' => $space->createUrl('/xcoin/overview'),
'icon' => '<i class="fa fa-money"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && !in_array(Yii::$app->controller->id, ['funding', 'product', 'ethereum', 'challenge', 'marketplace'])),
]);
$isChallengesActive = AssetHelper::canManageAssets($space) || Challenge::find()->where(['space_id' => $space->id])->count() > 0;
$isFundingsActive = AssetHelper::canManageAssets($space) || Funding::find()->where(['space_id' => $space->id])->count() > 0;
$isMarketplacesActive = AssetHelper::canManageAssets($space) || Marketplace::find()->where(['space_id' => $space->id])->count() > 0;
$isProductsActive = AssetHelper::canManageAssets($space) || Product::find()->where(['space_id' => $space->id])->count() > 0;
if (Yii::$app->getModule('xcoin')->isCrowdfundingEnabled() && ($isChallengesActive || $isFundingsActive)) {
$event->sender->addItemGroup([
'id' => 'crowdfunding',
'label' => Yii::t('SpaceModule.widgets_SpaceMenuWidget', Yii::t('XcoinModule.base', 'Crowdfunding')),
'sortOrder' => 30000,
]);
if ($isChallengesActive) {
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Space Challenges'),
'group' => 'crowdfunding',
'url' => $space->createUrl('/xcoin/challenge'),
'icon' => '<i class="fa fa-users"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && Yii::$app->controller->id === 'challenge'),
'sortOrder' => 30000,
]);
}
if ($isFundingsActive) {
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Submitted Proposals'),
'group' => 'crowdfunding',
'url' => $space->createUrl('/xcoin/funding'),
'icon' => '<i class="fa fa-leaf"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && Yii::$app->controller->id === 'funding'),
'sortOrder' => 30000,
]);
}
}
if ($isMarketplacesActive || $isProductsActive) {
$event->sender->addItemGroup([
'id' => 'marketplace',
'label' => Yii::t('SpaceModule.widgets_SpaceMenuWidget', Yii::t('XcoinModule.base', 'Marketplace')),
'sortOrder' => 40000,
]);
if ($isMarketplacesActive) {
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Space marketplaces'),
'group' => 'marketplace',
'url' => $space->createUrl('/xcoin/marketplace'),
'icon' => '<i class="fa fa-shopping-basket"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && Yii::$app->controller->id === 'marketplace'),
'sortOrder' => 40000,
]);
}
if ($isProductsActive) {
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Offered products'),
'group' => 'marketplace',
'url' => $space->createUrl('/xcoin/product'),
'icon' => '<i class="fa fa-product-hunt"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && Yii::$app->controller->id === 'product'),
'sortOrder' => 40000,
]);
}
}
}
}
public static function onProfileMenuInit($event)
{
$user = $event->sender->user;
if ($user->isModuleEnabled('xcoin')) {
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Accounts'),
'url' => $user->createUrl('/xcoin/overview'),
'icon' => '<i class="fa fa-money"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && Yii::$app->controller->id === 'overview'),
]);
}
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Products'),
'url' => $user->createUrl('/xcoin/product'),
'icon' => '<i class="fa fa-product-hunt"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && Yii::$app->controller->id == 'product')
]);
}
public static function onAccountTopMenuInit($event)
{
$user = Yii::$app->user->getIdentity();
if ($user->isModuleEnabled('xcoin')) {
$event->sender->addItem(array(
'label' => '---',
'url' => '#',
'sortOrder' => 205,
));
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Accounts'),
'url' => $user->createUrl('/xcoin/overview'),
'icon' => '<i class="fa fa-money"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && Yii::$app->controller->id === 'overview'),
'sortOrder' => 210,
]);
$event->sender->addItem([
'label' => Yii::t('XcoinModule.base', 'Products'),
'url' => $user->createUrl('/xcoin/product'),
'icon' => '<i class="fa fa-product-hunt"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'xcoin' && Yii::$app->controller->id == 'product'),
'sortOrder' => 215,
]);
}
}
/**
* When a members joins a space for the first time an account TYPE_COMMUNITY_INVESTOR
* is created and credited by an amount specified by the admin.
*
* @param Event
* @throws HttpException
* @throws Exception
*/
public static function onSpaceMemberAdd($event)
{
// Get new account transaction parameters
$module = Yii::$app->getModule('xcoin');
//Prepare accounts
$space = $event->space;
$user = $event->user;
if (!$module->settings->space($space)) {
return;
}
$accountTitle = $module->settings->space($space)->get('accountTitle');
$transactionAmount = $module->settings->space($space)->get('transactionAmount');
$transactionComment = $module->settings->space($space)->get('transactionComment');
$spaceIssueAccount = AccountHelper::getIssueAccount($space);
$spaceDefaultAccount = Account::findOne(['space_id' => $space->id, 'account_type' => Account::TYPE_DEFAULT]);
//Exit if module settings are not set or space default account or issue account are not set
if (!$accountTitle || !$transactionAmount || !$transactionComment || !$spaceIssueAccount || !$spaceDefaultAccount) {
return;
}
$memberAccount = Account::findOne([
'investor_id' => $user->id,
'space_id' => $space->id,
'account_type' => Account::TYPE_COMMUNITY_INVESTOR
]);
if ($memberAccount) {
$memberAccount->updateAttributes(['user_id' => $user->id]);
return;
}
$memberAccount = new Account();
$memberAccount->space_id = $space->id;
$memberAccount->user_id = $user->id;
$memberAccount->title = $accountTitle;
$memberAccount->account_type = Account::TYPE_COMMUNITY_INVESTOR;
$memberAccount->investor_id = $user->id;
if (!$memberAccount->save()) {
throw new Exception('Could not create member account!');
}
//Issue transaction amount to default account
$issueTransaction = new Transaction();
$issueTransaction->amount = $transactionAmount;
$issueTransaction->from_account_id = $spaceIssueAccount->id;
$issueTransaction->to_account_id = $spaceDefaultAccount->id;
$issueTransaction->asset_id = AssetHelper::getSpaceAsset($space)->id;
$issueTransaction->transaction_type = Transaction::TRANSACTION_TYPE_ISSUE;
$issueTransaction->comment = "Issue transaction Amount to default Account";
if (!$issueTransaction->save()) {
throw new HttpException(500, "can't issue this Amount !");
}
\humhub\components\Event::trigger(Transaction::class, Transaction::EVENT_TRANSACTION_TYPE_ISSUE, new Event(['sender' => $issueTransaction]));
//New member account transaction
$transferTransaction = new Transaction();
$transferTransaction->amount = $transactionAmount;
$transferTransaction->from_account_id = $spaceDefaultAccount->id;
$transferTransaction->to_account_id = $memberAccount->id;
$transferTransaction->asset_id = AssetHelper::getSpaceAsset($space)->id;
$transferTransaction->transaction_type = Transaction::TRANSACTION_TYPE_TRANSFER;
$transferTransaction->comment = $transactionComment;
if (!$transferTransaction->save()) {
throw new HttpException(500, "Can't transfer transaction amount to member account");
}
}
/**
* When a members leaves leaving an TYPE_COMMUNITY_INVESTOR account , he will be
* removed from managing this account.
*
* @param Event
*/
public static function onSpaceMemberRemove($event)
{
$space = $event->space;
$user = $event->user;
$memberAccount = Account::findOne([
'investor_id' => $user->id,
'space_id' => $space->id,
'account_type' => Account::TYPE_COMMUNITY_INVESTOR
]);
if (!$memberAccount) {
return;
}
$memberAccount->updateAttributes(['user_id' => null]);
}
/**
* Creating account type DEFAULT for new registered user
*
* @param Event
*/
public static function onUserRegistration($event)
{
$user = $event->identity;
AccountHelper::initContentContainer($user);
}
public static function onAdminMenuInit($event)
{
$event->sender->addItem([
'label' => Yii::t('AdminModule.widgets_AdminMenuWidget', 'Statistics'),
'url' => Url::toRoute('/xcoin/dashboard/statistics'),
'icon' => '<i class="fa fa-bar-chart"></i>',
'sortOrder' => 10000,
'newItemCount' => 0,
'isVisible' => Yii::$app->user->can(new SeeAdminInformation())
]);
}
}