plugins.php 8.06 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
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
Penny Leach's avatar
Penny Leach committed
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * 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
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * 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
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * 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
18
19
20
 *
 * @package    mahara
 * @subpackage admin
21
 * @author     Catalyst IT Ltd
Penny Leach's avatar
Penny Leach committed
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
Penny Leach's avatar
Penny Leach committed
24
25
26
27
28
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
29
define('MENUITEM', 'configextensions/pluginadmin');
Penny Leach's avatar
Penny Leach committed
30
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
31
ini_set('memory_limit', '32M');
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    if (table_exists(new XMLDBTable($plugin . '_installed'))) {
        if ($installed = get_records_array($plugin . '_installed')) {
            foreach ($installed as $i) {
                $key = $i->name;
                if ($plugin == 'blocktype') {
                    $key = blocktype_single_to_namespaced($i->name, $i->artefactplugin);
                }
                $plugins[$plugin]['installed'][$key] = array();
                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');
                            }
72
                        }
Penny Leach's avatar
Penny Leach committed
73
                    }
74
75
76
77
78
79
80
                } 
                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
81
82
83
84
                }
            }
        }
    
85
86
87
88
89
        $dirhandle = opendir(get_config('docroot') . $plugin);
        while (false !== ($dir = readdir($dirhandle))) {
            $installed = false; // reinitialise
            if (strpos($dir, '.') === 0) {
                continue;
90
            }
91
92
            if (!is_dir(get_config('docroot') . $plugin . '/' . $dir)) {
                continue;
93
            }
94
95
96
97
98
99
            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)) {
100
101
                continue;
            }
102
103
104
105
106
107
108
            if (empty($installed)) {
                $plugins[$plugin]['notinstalled'][$dir] = array();
                try {
                    validate_plugin($plugin, $dir);
                }
                catch (InstallationException $e) {
                    $plugins[$plugin]['notinstalled'][$dir]['notinstallable'] = $e->GetMessage();
109
                }
110
111
112
113
            }
            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)) {
114
115
                    continue;
                }
116
117
118
119
120
121
122
123

                $btdirhandle = opendir($btlocation);
                while (false !== ($btdir = readdir($btdirhandle))) {
                    if (strpos($btdir, '.') === 0) {
                        continue;
                    }
                    if (!is_dir(get_config('docroot') . $plugin . '/' . $dir . '/blocktype/' . $btdir)) {
                        continue;
124
                    }
125
126
127
128
129
130
131
132
133
134
135
136
                    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();
                        }
137
138
139
140
                    }
                }
            }
        }
Penny Leach's avatar
Penny Leach committed
141
142
143
    }
}

144
145
146
$loadingicon = theme_get_url('images/loading.gif');
$successicon = theme_get_url('images/success.gif');
$failureicon = theme_get_url('images/failure.gif');
147

148
149
150
$loadingstring = json_encode(get_string('upgradeloading', 'admin'));
$successstring = json_encode(get_string('upgradesuccesstoversion', 'admin'));
$failurestring = json_encode(get_string('upgradefailure', 'admin'));
151
152
153
154

$javascript = <<<JAVASCRIPT

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

157
    sendjsonrequest('../upgrade.json.php', { 'name': name }, 'GET', function (data) {
Penny Leach's avatar
Penny Leach committed
158
        if (!data.error) {
159
            var message = {$successstring} + data.newversion;
Penny Leach's avatar
Penny Leach committed
160
            $(name + '.message').innerHTML = '<img src="{$successicon}" alt=":)" />  ' + message;
161
            $(name + '.install').innerHTML = '';
Penny Leach's avatar
Penny Leach committed
162
163
164
165
166
            // 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));
167
        }
Penny Leach's avatar
Penny Leach committed
168
        else {
169
            var message = '';
170
171
            if (data.errormessage) {
                message = data.errormessage;
172
173
            } 
            else {
174
                message = {$failurestring};
175
176
177
178
            }
            $(name).innerHTML = '<img src="{$failureicon}" alt=":(" /> ' + message;
        }
    },
179
    function () {
180
        message = {$failurestring};
181
182
183
        $(name).innerHTML = message;
    },
    true);
184
185
186
187
}
JAVASCRIPT;


Penny Leach's avatar
Penny Leach committed
188
$smarty = smarty();
189
$smarty->assign('INLINEJAVASCRIPT', $javascript);
Penny Leach's avatar
Penny Leach committed
190
$smarty->assign('plugins', $plugins);
191
$smarty->assign('installlink', 'installplugin');
192
$smarty->assign('heading', get_string('pluginadmin', 'admin'));
193
$smarty->display('admin/extensions/plugins.tpl');
Penny Leach's avatar
Penny Leach committed
194
195

?>