plugins.php 10.6 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage admin
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
Penny Leach's avatar
Penny Leach committed
9
10
11
12
13
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
14
define('MENUITEM', 'configextensions/pluginadmin');
Penny Leach's avatar
Penny Leach committed
15
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
Martyn Smith's avatar
Martyn Smith committed
16
define('TITLE', get_string('pluginadmin', 'admin'));
Penny Leach's avatar
Penny Leach committed
17
18
19
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'plugins');
20
require('upgrade.php');
Penny Leach's avatar
Penny Leach committed
21

22
23
24
// @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
25
$plugins = array();
26
$plugins['blocktype'] = array();
Penny Leach's avatar
Penny Leach committed
27
28

foreach (plugin_types()  as $plugin) {
29
    // this has to happen first because of broken artefact/blocktype ordering
Penny Leach's avatar
Penny Leach committed
30
31
32
    $plugins[$plugin] = array();
    $plugins[$plugin]['installed'] = array();
    $plugins[$plugin]['notinstalled'] = array();
33
}
34
foreach (array_keys($plugins) as $plugin) {
35
    if (table_exists(new XMLDBTable($plugin . '_installed'))) {
36
        if ($installed = plugins_installed($plugin, true)) {
37
38
39
40
41
            foreach ($installed as $i) {
                $key = $i->name;
                if ($plugin == 'blocktype') {
                    $key = blocktype_single_to_namespaced($i->name, $i->artefactplugin);
                }
42
43
                if (!safe_require_plugin($plugin, $key)) {
                    continue;
44
                }
45

46
                $classname = generate_class_name($plugin, $key);
47
48
                $plugins[$plugin]['installed'][$key] = array(
                    'active' => $i->active,
49
                    'disableable' => call_static_method($classname, 'can_be_disabled'),
50
51
                    'deprecated' => call_static_method($classname, 'is_deprecated'),
                    'name' => call_static_method($classname, 'get_plugin_display_name'),
52
                    'dependencies' => call_static_method($classname, 'has_plugin_dependencies'),
53
                    'enableable' => call_static_method($classname, 'is_usable')
54
                );
55
56
57
58
                if (
                    ($i->active && $plugins[$plugin]['installed'][$key]['disableable'])
                    || (!$i->active && $plugins[$plugin]['installed'][$key]['enableable'])
                ){
59
60
                    $plugins[$plugin]['installed'][$key]['activateform'] = activate_plugin_form($plugin, $i);
                }
61
62
63
64
65
66
67
                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')) {
68
                                $plugins[$plugin]['installed'][$key]['types'][$collapseto]['config'] = true;
69
70
                            }
                            else {
71
                                $plugins[$plugin]['installed'][$key]['types'][$t]['config'] =
72
73
                                    call_static_method($classname, 'has_config');
                            }
74
75
76
77
78
79
80
                            if ($collapseto = call_static_method($classname, 'collapse_config_info')) {
                                $plugins[$plugin]['installed'][$key]['types'][$collapseto]['info'] = true;
                            }
                            else {
                                $plugins[$plugin]['installed'][$key]['types'][$t]['info'] =
                                    call_static_method($classname, 'has_config_info');
                            }
81
                        }
Penny Leach's avatar
Penny Leach committed
82
                    }
Aaron Wells's avatar
Aaron Wells committed
83
                }
84
85
                else {
                    $classname = generate_class_name($plugin, $i->name);
86
                    safe_require($plugin, $key);
87
88
89
                    if (call_static_method($classname, 'has_config')) {
                        $plugins[$plugin]['installed'][$key]['config'] = true;
                    }
90
91
92
                    if (call_static_method($classname, 'has_config_info')) {
                        $plugins[$plugin]['installed'][$key]['info'] = true;
                    }
Penny Leach's avatar
Penny Leach committed
93
94
95
                }
            }
        }
Aaron Wells's avatar
Aaron Wells committed
96

97
98
99
100
101
        $dirhandle = opendir(get_config('docroot') . $plugin);
        while (false !== ($dir = readdir($dirhandle))) {
            $installed = false; // reinitialise
            if (strpos($dir, '.') === 0) {
                continue;
102
            }
103
104
            if (!is_dir(get_config('docroot') . $plugin . '/' . $dir)) {
                continue;
105
            }
106
107
108
109
110
111
            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)) {
112
113
                continue;
            }
114
115
116
117
            if (empty($installed)) {
                $plugins[$plugin]['notinstalled'][$dir] = array();
                try {
                    validate_plugin($plugin, $dir);
118
119
                    $classname = generate_class_name($plugin, $dir);
                    $classname::sanity_check();
120
121
                    $plugins[$plugin]['notinstalled'][$dir]['name'] = call_static_method($classname, 'get_plugin_display_name');
                    $plugins[$plugin]['notinstalled'][$dir]['dependencies'] = call_static_method($classname, 'has_plugin_dependencies');
122
123
124
                }
                catch (InstallationException $e) {
                    $plugins[$plugin]['notinstalled'][$dir]['notinstallable'] = $e->GetMessage();
125
                }
126
127
128
129
130
131
132
133
134
                // If there are 'required' dependencies then we mark the plugin notinstallable
                if (isset($plugins[$plugin]['notinstalled'][$dir]['dependencies']['requires'])) {
                    if (isset($plugins[$plugin]['notinstalled'][$dir]['notinstallable'])) {
                        $plugins[$plugin]['notinstalled'][$dir]['notinstallable'] .= $plugins[$plugin]['notinstalled'][$dir]['dependencies']['requires'];
                    }
                    else {
                        $plugins[$plugin]['notinstalled'][$dir]['notinstallable'] = $plugins[$plugin]['notinstalled'][$dir]['dependencies']['requires'];
                    }
                }
135
136
137
138
            }
            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)) {
139
140
                    continue;
                }
141
142
143
144
145
146
147
148

                $btdirhandle = opendir($btlocation);
                while (false !== ($btdir = readdir($btdirhandle))) {
                    if (strpos($btdir, '.') === 0) {
                        continue;
                    }
                    if (!is_dir(get_config('docroot') . $plugin . '/' . $dir . '/blocktype/' . $btdir)) {
                        continue;
149
                    }
150
151
152
153
154
                    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));
                            }
Aaron Wells's avatar
Aaron Wells committed
155
                            validate_plugin('blocktype', $dir . '/' . $btdir,
156
157
158
159
160
161
                                get_config('docroot') . 'artefact/' . $dir . '/blocktype/' . $btdir);
                            $plugins['blocktype']['notinstalled'][$dir . '/' . $btdir] = array();
                        }
                        catch (InstallationException $_e) {
                            $plugins['blocktype']['notinstalled'][$dir . '/' . $btdir]['notinstallable'] = $_e->getMessage();
                        }
162
163
164
165
                    }
                }
            }
        }
Penny Leach's avatar
Penny Leach committed
166
167
168
    }
}

169
global $THEME;
170

171
172
173
$loadingicon = 'icon icon-spinner icon-pulse';
$successicon = 'icon icon-check text-success';
$failureicon = 'icon icon-exclaimation-triangle';
174

175
176
177
$loadingstring = json_encode(get_string('upgradeloading', 'admin'));
$successstring = json_encode(get_string('upgradesuccesstoversion', 'admin'));
$failurestring = json_encode(get_string('upgradefailure', 'admin'));
178
179
180

$javascript = <<<JAVASCRIPT

181
182
183
184
185
186
187
188
189
190
191
192
193
var installplugin = (function($) {
  return function (name) {
      $('[id="' + name + '.message"]').html('<span class="{$loadingicon}" title=' + {$loadingstring} + '" role="presentation" aria-hidden="true"></span>');

      sendjsonrequest('../upgrade.json.php', { 'name': name }, 'GET', function (data) {
          if (!data.error) {
              var message = {$successstring} + data.newversion;
              $('[id="' + name + '.message"]').html('<span class="{$successicon}" title=":)" role="presentation" aria-hidden="true"></span>' + message);
              $('[id="' + name + '.install"]').html('');
              $('[id="' + name + '"]').removeClass('list-group-item-danger').addClass('list-group-item-success');
              // move the whole thing into the list of installed plugins
              // new parent node
              var bits = name.split('\.');
194
              $("ul[id='" + bits[0] + ".installed'] li").eq(0).after($('[id="' + name + '"]'));
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
              var oldlist = $("ul[id='" + bits[0] + ".notinstalled']").find('li:not(:has(h3))');
              if (oldlist.length == 0) {
                  $("ul[id='" + bits[0] + ".notinstalled']").hide();
              }
          }
          else {
              var message = '';
              if (data.errormessage) {
                  message = data.errormessage;
              }
              else {
                  message = {$failurestring};
              }
              $('[id="' + name + '"]').html('<span class="{$failureicon}" title=":(" role="presentation" aria-hidden="true"></span>' + message);
          }
      },
      function () {
          message = {$failurestring};
          $('[id="' + name + '"]').html(message);
      },
      true);
  }
}(jQuery));
218
219
JAVASCRIPT;

220
$plugins['blocktype']['configure'] = true;
Penny Leach's avatar
Penny Leach committed
221
$smarty = smarty();
222
setpageicon($smarty, 'icon-plug');
223

224
$smarty->assign('INLINEJAVASCRIPT', $javascript);
Penny Leach's avatar
Penny Leach committed
225
$smarty->assign('plugins', $plugins);
226
$smarty->assign('installlink', 'installplugin');
227
$smarty->display('admin/extensions/plugins.tpl');