/
PluginBase.php
220 lines (198 loc) · 6.73 KB
/
PluginBase.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
<?php
/**
* Base class for plugins.
*/
abstract class PluginBase implements iPlugin {
/**
*
* @var LimesurveyApi
*/
protected $api = null;
protected $id = null;
protected $storage = 'DummyStorage';
static protected $description = 'Base plugin object';
static protected $name = 'PluginBase';
private $store = null;
protected $settings = array();
/**
* This holds the pluginmanager that instantiated the plugin
*
* @var PluginManager
*/
protected $pluginManager;
/**
* Constructor for the plugin
*
* @param PluginManager $manager The plugin manager instantiating the object
* @param int $id The id for storage
*/
public function __construct(PluginManager $manager, $id)
{
$this->pluginManager = $manager;
$this->id = $id;
$this->api = $manager->getAPI();
}
/**
* This function retrieves plugin data. Do not cache this data; the plugin storage
* engine will handling caching. After the first call to this function, subsequent
* calls will only consist of a few function calls and array lookups.
*
* @param string $key
* @param string $model
* @param int $id
* @return boolean
*/
protected function get($key = null, $model = null, $id = null, $default = null)
{
return $this->getStore()->get($this, $key, $model, $id, $default);
}
/**
* Return the description for this plugin
*/
public static function getDescription()
{
return static::$description;
}
/**
* Returns the id of the plugin
*
* Used by storage model to find settings specific to this plugin
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Provides meta data on the plugin settings that are available for this plugin.
* This does not include enable / disable; a disabled plugin is never loaded.
*
*/
public function getPluginSettings($getValues = true)
{
$settings = $this->settings;
foreach ($settings as $name => &$setting)
{
if ($getValues)
{
$setting['current'] = $this->get($name);
}
if ($setting['type'] == 'logo')
{
$setting['path'] = $this->publish($setting['path']);
}
}
return $settings;
}
public static function getName()
{
return static::$name;
}
/**
* Returns the plugin storage and takes care of
* instantiating it
*
* @return iPluginStorage
*/
public function getStore()
{
if (is_null($this->store)) {
$this->store = $this->pluginManager->getStore($this->storage);
}
return $this->store;
}
/**
* Publishes plugin assets.
*/
public function publish($fileName)
{
// Check if filename is relative.
if (strpos('//', $fileName) === false)
{
// This is a limesurvey relative path.
if (strpos('/', $fileName) === 0)
{
$url = Yii::getPathOfAlias('webroot') . $fileName;
}
else // This is a plugin relative path.
{
$path = Yii::getPathOfAlias('webroot.plugins.' . get_class($this)) . DIRECTORY_SEPARATOR . $fileName;
/*
* By using the asset manager the assets are moved to a publicly accessible path.
* This approach allows a locked down plugin directory that is not publicly accessible.
*/
$url = App()->assetManager->publish($path);
}
}
else
{
$url = $fileName;
}
return $url;
}
/**
*
* @param string $name Name of the setting.
* The type of the setting is either a basic type or choice.
* The choice type is either a single or a multiple choice setting.
* @param array $options
* Contains parameters for the setting. The 'type' key contains the parameter type.
* The type is one of: string, int, float, choice.
* Supported keys per type:
* String: max-length(int), min-length(int), regex(string).
* Int: max(int), min(int).
* Float: max(float), min(float).
* Choice: choices(array containing values as keys and names as values), multiple(bool)
* Note that the values for choice will be translated.
*/
protected function registerSetting($name, $options = array('type' => 'string'))
{
$this->settings[$name] = $options;
}
/**
*
* @param type $settings
*/
public function saveSettings($settings)
{
foreach ($settings as $name => $setting)
{
$this->set($name, $setting);
}
}
/**
* This function stores plugin data.
*
* @param string $key
* @param mixed $data
* @param string $model
* @param int $id
* @return boolean
*/
protected function set($key, $data, $model = null, $id = null)
{
return $this->getStore()->set($this, $key, $data, $model, $id);
}
/**
* Here you should handle subscribing to the events your plugin will handle
*/
//abstract public function registerEvents();
/**
* This function subscribes the plugin to receive an event.
*
* @param string $event
*/
protected function subscribe($event, $function = null)
{
return $this->pluginManager->subscribe($this, $event, $function);
}
/**
* This function unsubscribes the plugin from an event.
* @param string $event
*/
protected function unsubscribe($event)
{
return $this->pluginManager->unsubscribe($this, $event);
}
}