plugins.php 8.28 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
Penny Leach's avatar
Penny Leach committed
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Penny Leach's avatar
Penny Leach committed
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Penny Leach's avatar
Penny Leach committed
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Penny Leach's avatar
Penny Leach committed
19
20
21
 *
 * @package    mahara
 * @subpackage admin
22
 * @author     Catalyst IT Ltd
Penny Leach's avatar
Penny Leach committed
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
Penny Leach's avatar
Penny Leach committed
25
26
27
28
29
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
30
define('MENUITEM', 'configextensions/pluginadmin');
Penny Leach's avatar
Penny Leach committed
31
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
Martyn Smith's avatar
Martyn Smith committed
32
define('TITLE', get_string('pluginadmin', 'admin'));
Penny Leach's avatar
Penny Leach committed
33
34
35
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'plugins');
36
require('upgrade.php');
Penny Leach's avatar
Penny Leach committed
37

38
39
40
// @TODO when artefact plugins get installed, move the not installed blocktypes
// that get installed into the list of installed blocktype plugins

Penny Leach's avatar
Penny Leach committed
41
$plugins = array();
42
$plugins['blocktype'] = array();
Penny Leach's avatar
Penny Leach committed
43
44

foreach (plugin_types()  as $plugin) {
45
    // this has to happen first because of broken artefact/blocktype ordering
Penny Leach's avatar
Penny Leach committed
46
47
48
    $plugins[$plugin] = array();
    $plugins[$plugin]['installed'] = array();
    $plugins[$plugin]['notinstalled'] = array();
49
}
50
foreach (array_keys($plugins) as $plugin) {
51
    if (table_exists(new XMLDBTable($plugin . '_installed'))) {
52
        if ($installed = plugins_installed($plugin, true)) {
53
54
55
56
57
            foreach ($installed as $i) {
                $key = $i->name;
                if ($plugin == 'blocktype') {
                    $key = blocktype_single_to_namespaced($i->name, $i->artefactplugin);
                }
58
59
60
61
62
                safe_require($plugin, $key);
                $plugins[$plugin]['installed'][$key] = array(
                    'active' => $i->active,
                    'disableable' => call_static_method(generate_class_name($plugin, $key), 'can_be_disabled'),
                );
63
64
65
66
67
68
69
70
71
72
73
74
75
                if ($plugin == 'artefact') {
                    $plugins[$plugin]['installed'][$key]['types'] = array();
                    safe_require('artefact', $key);
                    if ($types = call_static_method(generate_class_name('artefact', $i->name), 'get_artefact_types')) {
                        foreach ($types as $t) {
                            $classname = generate_artefact_class_name($t);
                            if ($collapseto = call_static_method($classname, 'collapse_config')) {
                                $plugins[$plugin]['installed'][$key]['types'][$collapseto] = true;
                            }
                            else {
                                $plugins[$plugin]['installed'][$key]['types'][$t] = 
                                    call_static_method($classname, 'has_config');
                            }
76
                        }
Penny Leach's avatar
Penny Leach committed
77
                    }
78
79
80
81
82
83
84
                } 
                else {
                    $classname = generate_class_name($plugin, $i->name);
                    safe_require($plugin, $i->name);
                    if (call_static_method($classname, 'has_config')) {
                        $plugins[$plugin]['installed'][$key]['config'] = true;
                    }
Penny Leach's avatar
Penny Leach committed
85
86
87
88
                }
            }
        }
    
89
90
91
92
93
        $dirhandle = opendir(get_config('docroot') . $plugin);
        while (false !== ($dir = readdir($dirhandle))) {
            $installed = false; // reinitialise
            if (strpos($dir, '.') === 0) {
                continue;
94
            }
95
96
            if (!is_dir(get_config('docroot') . $plugin . '/' . $dir)) {
                continue;
97
            }
98
99
100
101
102
103
            if (array_key_exists($dir, $plugins[$plugin]['installed'])) {
                $installed = true;
            }
            // if we're already installed keep going
            // if we're an artefact plugin, we have to check for blocktypes.
            if ($plugin != 'artefact' && !empty($installed)) {
104
105
                continue;
            }
106
107
108
109
110
111
112
            if (empty($installed)) {
                $plugins[$plugin]['notinstalled'][$dir] = array();
                try {
                    validate_plugin($plugin, $dir);
                }
                catch (InstallationException $e) {
                    $plugins[$plugin]['notinstalled'][$dir]['notinstallable'] = $e->GetMessage();
113
                }
114
115
116
117
            }
            if ($plugin == 'artefact' && table_exists(new XMLDBTable('blocktype_installed'))) { // go check it for blocks as well
                $btlocation = get_config('docroot') . $plugin . '/' . $dir . '/blocktype';
                if (!is_dir($btlocation)) {
118
119
                    continue;
                }
120
121
122
123
124
125
126
127

                $btdirhandle = opendir($btlocation);
                while (false !== ($btdir = readdir($btdirhandle))) {
                    if (strpos($btdir, '.') === 0) {
                        continue;
                    }
                    if (!is_dir(get_config('docroot') . $plugin . '/' . $dir . '/blocktype/' . $btdir)) {
                        continue;
128
                    }
129
130
131
132
133
134
135
136
137
138
139
140
                    if (!array_key_exists($dir . '/' . $btdir, $plugins['blocktype']['installed'])) {
                        try {
                            if (!array_key_exists($dir, $plugins['artefact']['installed'])) {
                                throw new InstallationException(get_string('blocktypeprovidedbyartefactnotinstallable', 'error', $dir));
                            }
                            validate_plugin('blocktype', $dir . '/' . $btdir, 
                                get_config('docroot') . 'artefact/' . $dir . '/blocktype/' . $btdir);
                            $plugins['blocktype']['notinstalled'][$dir . '/' . $btdir] = array();
                        }
                        catch (InstallationException $_e) {
                            $plugins['blocktype']['notinstalled'][$dir . '/' . $btdir]['notinstallable'] = $_e->getMessage();
                        }
141
142
143
144
                    }
                }
            }
        }
Penny Leach's avatar
Penny Leach committed
145
146
147
    }
}

148
149
150
151
global $THEME;
$loadingicon = $THEME->get_url('images/loading.gif');
$successicon = $THEME->get_url('images/success.gif');
$failureicon = $THEME->get_url('images/failure.gif');
152

153
154
155
$loadingstring = json_encode(get_string('upgradeloading', 'admin'));
$successstring = json_encode(get_string('upgradesuccesstoversion', 'admin'));
$failurestring = json_encode(get_string('upgradefailure', 'admin'));
156
157
158
159

$javascript = <<<JAVASCRIPT

function installplugin(name) {
160
    $(name + '.message').innerHTML = '<img src="{$loadingicon}" alt=' + {$loadingstring} + '" />';
161

162
    sendjsonrequest('../upgrade.json.php', { 'name': name }, 'GET', function (data) {
Penny Leach's avatar
Penny Leach committed
163
        if (!data.error) {
164
            var message = {$successstring} + data.newversion;
Penny Leach's avatar
Penny Leach committed
165
            $(name + '.message').innerHTML = '<img src="{$successicon}" alt=":)" />  ' + message;
166
            $(name + '.install').innerHTML = '';
Penny Leach's avatar
Penny Leach committed
167
168
169
170
171
            // move the whole thing into the list of installed plugins 
            // new parent node
            var bits = name.split('\.');
            var newparent = $(bits[0] + '.installed');
            appendChildNodes(newparent, $(name));
172
        }
Penny Leach's avatar
Penny Leach committed
173
        else {
174
            var message = '';
175
176
            if (data.errormessage) {
                message = data.errormessage;
177
178
            } 
            else {
179
                message = {$failurestring};
180
181
182
183
            }
            $(name).innerHTML = '<img src="{$failureicon}" alt=":(" /> ' + message;
        }
    },
184
    function () {
185
        message = {$failurestring};
186
187
188
        $(name).innerHTML = message;
    },
    true);
189
190
191
192
}
JAVASCRIPT;


Penny Leach's avatar
Penny Leach committed
193
$smarty = smarty();
194
$smarty->assign('INLINEJAVASCRIPT', $javascript);
Penny Leach's avatar
Penny Leach committed
195
$smarty->assign('plugins', $plugins);
196
$smarty->assign('installlink', 'installplugin');
Richard Mansfield's avatar
Richard Mansfield committed
197
$smarty->assign('PAGEHEADING', hsc(TITLE));
198
$smarty->display('admin/extensions/plugins.tpl');
Penny Leach's avatar
Penny Leach committed
199
200

?>