-
Notifications
You must be signed in to change notification settings - Fork 19
/
Events.php
190 lines (164 loc) · 7.21 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
<?php
namespace humhub\modules\cfiles;
use humhub\modules\cfiles\models\File;
use humhub\modules\cfiles\models\Folder;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\models\ContentContainer;
use humhub\modules\content\models\ContentContainerModuleState;
use humhub\modules\file\actions\DownloadAction;
use humhub\modules\file\models\File as BaseFile;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use Yii;
use yii\base\Event;
/**
* cfiles Events
*
* @author luke
*/
class Events
{
public static function onSpaceMenuInit($event)
{
if ($event->sender->space !== null && $event->sender->space->moduleManager->isEnabled('cfiles')) {
$event->sender->addItem([
'label' => Yii::t('CfilesModule.base', 'Files'),
'group' => 'modules',
'url' => $event->sender->space->createUrl('/cfiles/browse'),
'icon' => '<i class="fa fa-files-o"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'cfiles'),
]);
}
}
/**
* Callback to validate module database records.
*
* @param Event $event
*/
public static function onIntegrityCheck($event)
{
$integrityController = $event->sender;
$integrityController->showTestHeadline("CFile Module (" . File::find()->count() . " entries)");
foreach (File::find()->each() as $file) {
/* @var $file \humhub\modules\cfiles\models\File */
// If parent_folder_id is 0 or null its an old root child which is not merged yet.
if (!empty($file->parent_folder_id) && empty($file->parentFolder)) {
if ($integrityController->showFix("Deleting cfile id " . $file->id . " without existing parent!")) {
$file->hardDelete();
}
}
}
$integrityController->showTestHeadline("CFile Module (" . File::find()->count() . " entries)");
foreach (Folder::find()->each() as $folder) {
/* @var $file \humhub\modules\cfiles\models\File */
// If parent_folder_id is 0 or null its either an old root child which is not merged yet or an root directory.
if (!empty($folder->parent_folder_id) && empty($folder->parentFolder)) {
if ($integrityController->showFix("Deleting cfile folder id " . $folder->id . " without existing parent!")) {
$folder->hardDelete();
}
}
}
}
public static function onProfileMenuInit($event)
{
if ($event->sender->user !== null && $event->sender->user->moduleManager->isEnabled('cfiles')) {
$event->sender->addItem([
'label' => Yii::t('CfilesModule.base', 'Files'),
'url' => $event->sender->user->createUrl('/cfiles/browse'),
'icon' => '<i class="fa fa-files-o"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'cfiles'),
]);
}
}
/**
* Callback on after file controller action
*
* @param Event $event
*/
public static function onAfterFileAction(Event $event)
{
if (isset($event->action) &&
$event->action instanceof DownloadAction &&
($downloadedFile = File::getFileByGuid(Yii::$app->request->get('guid')))
) {
$downloadedFile->updateAttributes(['download_count' => $downloadedFile->download_count + 1]);
}
}
/**
* Callback when user or space is inserted
*
* @param Event $event
*/
public static function onContentContainerActiveRecordInsert($event)
{
/**
* @var ContentContainerActiveRecord|Space|User $container
*/
$container = $event->sender;
if ($container instanceof ContentContainerActiveRecord &&
$container->moduleManager->isEnabled('cfiles')) {
Folder::initRoot($container);
Folder::initPostedFilesFolder($container);
}
}
/**
* Callback when module is enabled first time
*
* @param Event $event
*/
public static function onContentContainerModuleStateInsert($event)
{
/**
* @var ContentContainerModuleState $moduleState
*/
$moduleState = $event->sender;
if (!($moduleState instanceof ContentContainerModuleState &&
$moduleState->module_id == 'cfiles' &&
$moduleState->module_state)) {
return;
}
if (($contentContainer = ContentContainer::findOne(['id' => $moduleState->contentcontainer_id])) &&
($container = $contentContainer->getPolymorphicRelation())) {
Folder::initRoot($container);
Folder::initPostedFilesFolder($container);
}
}
public static function onRestApiAddRules()
{
/* @var \humhub\modules\rest\Module $restModule */
$restModule = Yii::$app->getModule('rest');
$restModule->addRules([
//File
['pattern' => 'cfiles/files/container/<containerId:\d+>', 'route' => 'cfiles/rest/file/find-by-container', 'verb' => 'GET'],
['pattern' => 'cfiles/files/container/<containerId:\d+>', 'route' => 'cfiles/rest/file/upload', 'verb' => 'POST'],
['pattern' => 'cfiles/file/<id:\d+>', 'route' => 'cfiles/rest/file/view', 'verb' => ['GET', 'HEAD']],
['pattern' => 'cfiles/file/<id:\d+>', 'route' => 'cfiles/rest/file/delete', 'verb' => 'DELETE'],
//Folder
['pattern' => 'cfiles/folders/container/<containerId:\d+>', 'route' => 'cfiles/rest/folder/find-by-container', 'verb' => 'GET'],
['pattern' => 'cfiles/folders/container/<containerId:\d+>', 'route' => 'cfiles/rest/folder/create', 'verb' => 'POST'],
['pattern' => 'cfiles/folder/<id:\d+>', 'route' => 'cfiles/rest/folder/view', 'verb' => ['GET', 'HEAD']],
['pattern' => 'cfiles/folder/<id:\d+>', 'route' => 'cfiles/rest/folder/update', 'verb' => 'PUT'],
['pattern' => 'cfiles/folder/<id:\d+>', 'route' => 'cfiles/rest/folder/delete', 'verb' => 'DELETE'],
//Items management
['pattern' => 'cfiles/items/container/<containerId:\d+>/make-public', 'route' => 'cfiles/rest/manage/make-public', 'verb' => 'PATCH'],
['pattern' => 'cfiles/items/container/<containerId:\d+>/make-private', 'route' => 'cfiles/rest/manage/make-private', 'verb' => 'PATCH'],
['pattern' => 'cfiles/items/container/<containerId:\d+>/move', 'route' => 'cfiles/rest/manage/move', 'verb' => 'POST'],
['pattern' => 'cfiles/items/container/<containerId:\d+>/delete', 'route' => 'cfiles/rest/manage/delete', 'verb' => 'DELETE'],
], 'cfiles');
}
public static function onAfterNewStoredFile($event)
{
$baseFile = $event->sender;
if (!($baseFile instanceof BaseFile)) {
return;
}
$file = File::findOne($baseFile->object_id);
if (!$file) {
return;
}
$file->content->updateAttributes([
'updated_at' => $baseFile->updated_at,
'updated_by' => $baseFile->updated_by,
]);
}
}