/
PluginHelper.php
128 lines (113 loc) · 4.38 KB
/
PluginHelper.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
<?php
/**
* @todo Better name?
*/
class PluginHelper extends Survey_Common_Action
{
/** @inheritdoc */
public $defaultAction = 'sidebody';
/**
* Helper function to let a plugin put content
* into the side-body easily.
*
* @param int $surveyid
* @param string $plugin Name of the plugin class
* @param string $method Name of the plugin method
* @return void
*/
public function sidebody($surveyid = 0, $plugin = '', $method = '')
{
$aData = array();
$aData['title_bar']['title'] = "";
$aData['surveybar']['buttons']['view'] = false;
$aData['activated'] = null;
if ($surveyid) {
$surveyid = sanitize_int($surveyid);
$oSurvey = Survey::model()->findByPk($surveyid);
if ($oSurvey) {
$aData['surveybar']['buttons']['view'] = true;
$aData['title_bar']['title'] = viewHelper::flatEllipsizeText($oSurvey->defaultlanguage->surveyls_title) . " (" . gT("ID") . ":" . $surveyid . ")";
$aData['activated'] = $oSurvey->active;
}
// Plugin must test to send 404 if needed, maybe want to “create” a new survey, who know …
}
$aData['surveyid'] = $surveyid;
$content = $this->getContent($surveyid, $plugin, $method);
$aData['sidemenu'] = array();
$aData['sidemenu']['state'] = false;
$aData['sideMenuBehaviour'] = getGlobalSetting('sideMenuBehaviour');
$aData['content'] = $content;
$aData['sideMenuOpen'] = false; // TODO: Assume this for all plugins?
$this->_renderWrappedTemplate(null, array('super/sidebody'), $aData);
}
/**
* Helper function to let a plugin put content
* into the full page wrapper easily.
* @param string $plugin
* @param string $method
* @return void
*/
public function fullpagewrapper($plugin, $method)
{
$aData = array();
$content = $this->getContent(null, $plugin, $method);
$aData['content'] = $content;
$this->_renderWrappedTemplate(null, 'super/dummy', $aData, 'layout_main.php');
}
/**
* At ajax, just echo content
* @param string $plugin
* @param string $method
*/
public function ajax($plugin, $method)
{
list($pluginInstance, $refMethod) = $this->getPluginInstanceAndMethod($plugin, $method);
$request = Yii::app()->request;
echo $refMethod->invoke($pluginInstance, $request);
}
/**
*
* @param string $pluginName
* @param string $methodName
* @return array
*/
protected function getPluginInstanceAndMethod($pluginName, $methodName)
{
// Get plugin class, abort if not found
try {
$refClass = new ReflectionClass($pluginName);
} catch (ReflectionException $ex) {
throw new \CException("Can't find a plugin with class name $pluginName");
}
$record = Plugin::model()->findByAttributes(array('name' => $pluginName, 'active' => 1));
if (empty($record)) {
throw new Exception('Plugin with name ' . $pluginName . ' is not active or can\' be found');
}
$pluginManager = App()->getPluginManager();
$pluginInstance = $refClass->newInstance($pluginManager, $record->id);
/* Check if method is in allowed list */
if (is_array($pluginInstance->allowedPublicMethods) && !in_array($methodName, $pluginInstance->allowedPublicMethods)) {
throw new \CHttpException(400, sprintf(gT("Forbidden call of method %s for plugin %s"), $methodName, $pluginName));
}
Yii::app()->setPlugin($pluginInstance);
// Get plugin method, abort if not found
try {
$refMethod = $refClass->getMethod($methodName);
} catch (ReflectionException $ex) {
throw new \CException('Plugin ' . $pluginName . ' has no method ' . $methodName);
}
return array($pluginInstance, $refMethod);
}
/**
* Get HTML content for side-body
*
* @param string $plugin Name of the plugin class
* @param string $method Name of the plugin method
* @return string
*/
protected function getContent($surveyId, $plugin, $method)
{
list($pluginInstance, $refMethod) = $this->getPluginInstanceAndMethod($plugin, $method);
return $refMethod->invoke($pluginInstance, $surveyId);
}
}