forked from DenverArtMuseum/ActivityFields
/
Plugin.php
189 lines (168 loc) · 6.27 KB
/
Plugin.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
<?php namespace DenverArt\ActivityFields;
use Backend;
use Auth;
use Illuminate\Support\Facades\Event;
use DMA\Friends\Models\Activity as Activity;
use Rainlab\User\Models\User as User;
use DenverArt\ActivityFields\Models\ExtraFields as Fields;
use DMA\Friends\Components\ActivityCatalog as ActivityCatalog;
use Illuminate\Foundation\AliasLoader;
use DenverArt\ActivityFields\Classes\ExtendedActivity as ExtendedActivity;
/**
* Activity Fileds plugin information file
*
* @package DenverArt\ActivityFields
* @author Matt Popke
*/
class Plugin extends \System\Classes\PluginBase
{
/**
* @var array plugin dependencies
*/
public $require = ['DMA.Friends'];
/**
* Return information about this plugin
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'Friends Activities Extensions',
'description' => 'Adds additional fields to friends activities.',
'author' => 'Denver Art Museum',
'icon' => 'icon-database'
];
}
/**
* {@inheritDoc}
*/
public function boot()
{
// Extend Activity model to support extra fields & ratings
Activity::extend(function($model) {
$model->hasOne['activity_fields'] = ['DenverArt\ActivityFields\Models\ExtraFields'];
$model->hasMany['ratings'] = ['DenverArt\ActivityFields\Models\Rating'];
$model->addDynamicMethod('scopeNotIgnored', function($query, $user) {
$query = $query->whereHas('ratings', function ($q) use ($user) {
$q->where('user_id', $user->getKey())
->where('rating', 0);
}, '<', 1);
});
$model->addDynamicMethod('scopeIgnored', function($query, $user) {
$query = $query->whereHas('ratings', function ($q) use ($user) {
$q->where('user_id', $user->getKey())
->where('rating', 0);
});
});
$model->addDynamicMethod('scopeNotComplete', function($query, $user) {
$query = $query->whereHas('users', function ($q) use ($user) {
$q->where('user_id', $user->getKey());
}, '<', 1);
});
$model->addDynamicMethod('scopeComplete', function($query, $user) {
$query = $query->whereHas('users', function ($q) use ($user) {
$q->where('user_id', $user->getKey());
});
});
});
// Extend User model to support ratings
User::extend(function ($model) {
$model->hasMany['ratings'] = ['DenverArt\ActivityFields\Models\Rating'];
});
// Extend Activity fields
$context = $this;
Event::listen('backend.form.extendFields', function($widget) use ($context) {
$context->extendedActivityFields($widget);
});
// Extend Activity admin listing table columns
Event::listen('backend.list.extendColumns', function($widget) {
if (!$widget->getController() instanceof \DMA\Friends\Controllers\Activities) return;
$widget->addColumns([
'duration' => [
'label' => 'Duration',
'relation' => 'activity_fields',
'sortable' => true,
'select' => '@duration',
'searchable' => true
],
'location' => [
'label' => 'Location',
'relation' => 'activity_fields',
'sortable' => true,
'select' => '@location',
'searchable' => true
],
]);
});
}
/**
* {@inheritDoc}
*/
public function registerComponents()
{
return [
'DenverArt\ActivityFields\Components\ActivityInteractions' => 'ActivityInteractions',
'DenverArt\ActivityFields\Components\Explore' => 'Explore',
'DenverArt\ActivityFields\Components\Ignored' => 'Ignored',
'DenverArt\ActivityFields\Components\PopupHelp' => 'PopupHelp',
];
}
/**
* {@inheritDoc}
*/
public function registerFormWidgets()
{
return [
'DenverArt\ActivityFields\FormWidgets\TimePicker' => [
'label' => 'TimePicker',
'code' => 'timepicker',
],
];
}
/**
* Extend Activity fields when editing Friends Activities
* @param [type] $widget [description]
*/
private function extendedActivityFields($widget)
{
if (!$widget->getController() instanceof \DMA\Friends\Controllers\Activities) return;
if ($widget->getContext() != 'update') return;
// Make sure the extended fields exist for this Activity
if (!Fields::getFromActivity($widget->model)) return;
$option_list = Fields::getOptions();
$widget->addFields([
'activity_fields[duration]' => [
'label' => 'Estimated Duration',
'tab' => 'Configuration',
'span' => 'left',
'type' => 'dropdown',
'options' => $option_list['duration'],
],
'activity_fields[engagement]' => [
'label' => 'Engagement Level',
'tab' => 'Configuration',
'span' => 'left',
'type' => 'dropdown',
'options' => $option_list['engagement'],
],
'activity_fields[location]' => [
'label' => 'Location',
'tab' => 'Configuration',
'span' => 'left',
],
'activity_fields[start_time]' => [
'label' => 'Start Time',
'tab' => 'Configuration',
'span' => 'left',
'type' => 'timepicker',
],
'activity_fields[ticketed]' => [
'label' => 'Not included in General Admission',
'tab' => 'Configuration',
'span' => 'left',
'type' => 'checkbox',
],
], 'secondary');
}
}