-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.php
110 lines (101 loc) · 3.44 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
<?php
/**
* Handles inteactions between REDCap plugin space (e.g. index.php) and plugin
* framework components (i.e. controllers, renders, routers, etc.).
*/
class Plugin {
protected $CONN;
protected $USER;
protected $CONFIG;
protected $RENDERER;
/**
* Given the REDCap MySQL database connection, the current user's username,
* and the path to a plugin config file, create a plugin object.
*
* Expects a config.ini file in the same directory (unless specified).
*/
public function __construct(
$conn, $username, $config_file_path='config.ini'
) {
$this->CONN = $conn;
$this->USER = $username;
// Create plugin configuration object.
require_once(FRAMEWORK_ROOT.'PluginConfig.php');
$this->CONFIG = new PluginConfig($config_file_path);
$this->set_default_renderer();
}
/**
* Sets the default renderer to Twig.
*/
protected function set_default_renderer() {
// Include and configure Twig template engine
// (http://twig.sensiolabs.org/)
require_once(
FRAMEWORK_ROOT.'lib/twig/'.$this->CONFIG['versions']['twig']
.'/lib/Twig/Autoloader.php'
);
Twig_Autoloader::register();
$twig_loader = new Twig_Loader_Filesystem(array(
'./templates',
FRAMEWORK_ROOT.'templates'
));
$twig = new Twig_Environment($twig_loader, array());
$this->set_renderer($twig);
}
/**
* Allows for Twig to be replaced with another renderer.
*/
public function set_renderer($renderer) {
$this->RENDERER = $renderer;
}
/**
* An overridable method for handling plugin authorization. This has two
* use cases:
*
* 1. To call the REDcap access control helper functions (commented out
* below) which halt execution, and called in the index.php.
* 2. To return a boolean result which will be used in the
* request_to_response method.
*/
public function authorize() {
// Limit access to plugin using REDCap helper functions
//REDCap::allowProjects(...);
//REDCap::allowUsers(REDCap::getUsers());
return True;
}
/**
* Given the PHP HTTP request "super globals" and the path to a routes file,
* handle the routing, controller instantiation, and return the response
* HTML.
*
* Expects a routes.php file in the same directory (unless specified).
*/
public function request_to_response(
$_GET, $_POST, $_REQUEST, $router_file_path='routes.php'
) {
if(!$this->authorize()) {
$ControllerClass = 'NotFoundController';
} else {
// Query router for relavent controller based on $_REQUEST vars
require_once($router_file_path);
$ControllerClass = route($_REQUEST);
}
// Include and instantiate controller class
if(file_exists('controllers/'.$ControllerClass.'.php')) {
require_once('controllers/'.$ControllerClass.'.php');
} else {
require_once(FRAMEWORK_ROOT.'controllers/'.$ControllerClass.'.php');
}
$controller = new $ControllerClass(
$_GET,
$_POST,
$this->RENDERER,
$this->CONN,
$this->USER,
$this->CONFIG
);
// Generate response HTML
return $controller->process_request();
}
}
?>