Browse Source

Fix plugin loading using the LegacyListener class

jmontoyaa 8 years ago
parent
commit
6b510437cb

+ 79 - 39
main/admin/settings.lib.php

@@ -40,8 +40,12 @@ function handleRegions()
     $plugin_obj = new AppPlugin();
     $possible_plugins  = $plugin_obj->read_plugins_from_path();
     $installed_plugins = $plugin_obj->get_installed_plugins();
+    $name = isset($_GET['name']) ? $_GET['name'] : '';
 
-    echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
+    echo '<form 
+        name="plugins" 
+        method="post" 
+        action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'&name='.Security::remove_XSS($name).'">';
     echo '<table class="data_table">';
     echo '<tr>';
     echo '<th width="400px">';
@@ -69,7 +73,7 @@ function handleRegions()
         if (file_exists($plugin_info_file)) {
             $plugin_info = array();
             require $plugin_info_file;
-            if (isset($_GET['name']) && $_GET['name'] === $pluginName) {
+            if ($name === $pluginName) {
                 echo '<tr class="row_selected">';
             } else {
                 echo '<tr>';
@@ -124,12 +128,22 @@ function handleExtensions()
  * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  * @author Julio Montoya <gugli100@gmail.com> Beeznest 2012
  */
-function handlePlugins()
+function handlePlugins($installed = true)
 {
     $plugin_obj = new AppPlugin();
     $token = Security::get_token();
-    if (isset($_POST['submit_plugins'])) {
-        storePlugins();
+    if (isset($_REQUEST['action'])) {
+        $name = $_REQUEST['name'];
+        $appPlugin = new AppPlugin();
+        switch ($_REQUEST['action']) {
+            case 'install':
+                $appPlugin->install($name);
+                break;
+            case 'uninstall':
+                $appPlugin->uninstall($name);
+                break;
+        }
+
         // Add event to the system log.
         $user_id = api_get_user_id();
         $category = $_GET['category'];
@@ -150,19 +164,11 @@ function handlePlugins()
     echo Display::page_subheader(get_lang('Plugins'));
     echo '<form class="form-horizontal" name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'&sec_token=' . $token . '">';
     echo '<table class="data_table">';
-    echo '<tr>';
-    echo '<th width="20px">';
-    echo get_lang('Action');
-    echo '</th><th>';
+    /*echo '<tr>';
+    echo '<th>';
     echo get_lang('Description');
     echo '</th>';
-    echo '</tr>';
-
-    /*$plugin_list = array();
-    $my_plugin_list = $plugin_obj->get_plugin_regions();
-    foreach($my_plugin_list as $plugin_item) {
-        $plugin_list[$plugin_item] = $plugin_item;
-    }*/
+    echo '</tr>';*/
 
     foreach ($all_plugins as $pluginName) {
         $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/plugin.php';
@@ -170,32 +176,36 @@ function handlePlugins()
             $plugin_info = array();
             require $plugin_info_file;
 
+            echo '<tr>';
             if (in_array($pluginName, $installed_plugins)) {
-                echo '<tr class="row_selected">';
             } else {
-                echo '<tr>';
+                if ($installed) {
+                    continue;
+                }
+            }
+
+            if ($installed == false) {
+                if (in_array($pluginName, $installed_plugins)) {
+                    continue;
+                }
             }
-            echo '<td>';
-            //Checkbox
-            if (in_array($pluginName, $installed_plugins)) {
-                echo '<input type="checkbox" name="plugin_'.$pluginName.'[]" checked="checked">';
 
+            // Checkbox
+            if (in_array($pluginName, $installed_plugins)) {
+                //echo '<input type="checkbox" name="plugin_'.$pluginName.'[]" checked="checked">';
             } else {
-                echo '<input type="checkbox" name="plugin_'.$pluginName.'[]">';
+                //echo '<input type="checkbox" name="plugin_'.$pluginName.'[]">';
             }
-            echo '</td><td>';
+
+            echo '<td>';
             echo '<h4>'.$plugin_info['title'].' <small>v '.$plugin_info['version'].'</small></h4>';
             echo '<p>'.$plugin_info['comment'].'</p>';
             echo '<p>'.get_lang('Author').': '.$plugin_info['author'].'</p>';
-
             echo '<div class="btn-group">';
-            if (in_array($pluginName, $installed_plugins)) {
-                echo Display::url('<em class="fa fa-cogs"></em> '.get_lang('Configure'), 'configure_plugin.php?name='.$pluginName, array('class' => 'btn btn-default'));
-                echo Display::url('<em class="fa fa-th-large"></em> '.get_lang('Regions'), 'settings.php?category=Regions&name='.$pluginName, array('class' => 'btn btn-default'));
-            }
 
+            $readme = '';
             if (file_exists(api_get_path(SYS_PLUGIN_PATH).$pluginName.'/readme.txt')) {
-                echo Display::url(
+                $readme = Display::url(
                     "<em class='fa fa-file-text-o'></em> readme.txt",
                     api_get_path(WEB_PLUGIN_PATH) . $pluginName . "/readme.txt",
                     [
@@ -209,7 +219,7 @@ function handlePlugins()
 
             $readmeFile = api_get_path(SYS_PLUGIN_PATH).$pluginName.'/README.md';
             if (file_exists($readmeFile)) {
-                echo Display::url(
+                $readme .= Display::url(
                     "<em class='fa fa-file-text-o'></em> README.md",
                     api_get_path(WEB_AJAX_PATH).'plugin.ajax.php?a=md_to_html&plugin='.$pluginName,
                     [
@@ -221,17 +231,39 @@ function handlePlugins()
                 );
             }
 
+            if (in_array($pluginName, $installed_plugins)) {
+                echo Display::url(
+                    '<em class="fa fa-cogs"></em> '.get_lang('Configure'), 'configure_plugin.php?name='.$pluginName,
+                    array('class' => 'btn btn-default')
+                );
+                echo Display::url(
+                    '<em class="fa fa-th-large"></em> '.get_lang('Regions'), 'settings.php?category=Regions&name='.$pluginName,
+                    array('class' => 'btn btn-default')
+                );
+
+                echo $readme;
+
+                echo Display::url(
+                    '<em class="fa trash-o fa-trash-o "></em> '.get_lang('Uninstall'), 'settings.php?category=Plugins&action=uninstall&name='.$pluginName,
+                    array('class' => 'btn btn-danger')
+                );
+            } else {
+                echo Display::url(
+                    '<em class="fa fa-th-large"></em> '.get_lang('Install'), 'settings.php?category=Plugins&action=install&name='.$pluginName,
+                    array('class' => 'btn btn-success')
+                );
+                echo $readme;
+            }
             echo '</div>';
             echo '</td></tr>';
         }
-
     }
     echo '</table>';
 
-    echo '<div class="form-actions bottom_actions">';
+    /*echo '<div class="form-actions bottom_actions">';
     echo '<button class="btn btn-success" type="submit" name="submit_plugins">'.
             get_lang('EnablePlugins').'</button>';
-    echo '</div>';
+    echo '</div>';*/
     echo '</form>';
 }
 
@@ -648,6 +680,7 @@ function storePlugins()
     // Get a list of all current 'Plugins' settings
     $plugin_list = $appPlugin->read_plugins_from_path();
 
+    var_dump($plugin_list);exit;
     $installed_plugins = array();
 
     foreach ($plugin_list as $plugin) {
@@ -663,9 +696,9 @@ function storePlugins()
         $remove_plugins = $plugin_list;
     }
 
-    foreach ($remove_plugins as $plugin) {
+    /*foreach ($remove_plugins as $plugin) {
         $appPlugin->uninstall($plugin);
-    }
+    }*/
 }
 
 /**
@@ -1214,7 +1247,9 @@ function select_gradebook_default_grade_model_id()
  */
 function generateSettingsForm($settings, $settings_by_access_list)
 {
+    return;
     global $_configuration, $settings_to_avoid, $convert_byte_to_mega_list;
+    $multipleAccessUrl = api_get_configuration_value('multiple_access_urls');
     $em = Database::getManager();
     $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
 
@@ -1240,17 +1275,22 @@ function generateSettingsForm($settings, $settings_by_access_list)
     $i = 0;
     $addedSettings = [];
     foreach ($settings as $row) {
-        if (in_array($row['variable'], array_keys($settings_to_avoid))) {
+
+        if (!empty($row['variable']) &&
+            in_array($row['variable'], array_keys($settings_to_avoid))
+        ) {
             continue;
         }
 
-        if (in_array($row['variable'], $addedSettings)) {
+        if (!empty($row['variable']) &&
+            in_array($row['variable'], $addedSettings)
+        ) {
             continue;
         }
 
         $addedSettings[] = $row['variable'];
 
-        if (!empty($_configuration['multiple_access_urls'])) {
+        if (!empty($multipleAccessUrl)) {
             if (api_is_global_platform_admin()) {
                 if ($row['access_url_locked'] == 0) {
                     if ($url_id == 1) {

+ 14 - 9
main/admin/settings.php

@@ -152,7 +152,7 @@ if (!empty($_GET['category']) &&
     $settings_by_access_list = $settings_array['settings_by_access_list'];
     $form = generateSettingsForm($settings, $settings_by_access_list);
 
-    if ($form->validate()) {
+    if (false && $form->validate()) {
         $values = $form->exportValues();
 
         $mark_all = false;
@@ -474,9 +474,9 @@ foreach ($resultcategories as $row) {
     $action_array[] = $url;
 }
 
-echo Display::actions($action_array);
-echo '<br />';
-echo $form_search_html;
+//echo Display::actions($action_array);
+//echo '<br />';
+//echo $form_search_html;
 
 if (!empty($_GET['category'])) {
     switch ($_GET['category']) {
@@ -511,20 +511,25 @@ if (!empty($_GET['category'])) {
                 </script>';
             echo '<div id="tabs">';
             echo '<ul>';
-            echo '<li><a href="#tabs-1">'.get_lang('Plugins').'</a></li>';
-            echo '<li><a href="#tabs-2">'.get_lang('DashboardPlugins').'</a></li>';
-            echo '<li><a href="#tabs-3">'.get_lang('ConfigureExtensions').'</a></li>';
+            echo '<li><a href="#tabs-1">'.get_lang('InstalledPlugins').'</a></li>';
+            echo '<li><a href="#tabs-2">'.get_lang('Plugins').'</a></li>';
+            echo '<li><a href="#tabs-3">'.get_lang('DashboardPlugins').'</a></li>';
+            echo '<li><a href="#tabs-4">'.get_lang('ConfigureExtensions').'</a></li>';
             echo '</ul>';
 
             echo '<div id="tabs-1">';
             handlePlugins();
             echo '</div>';
 
-            echo '<div id="tabs-2">';
-            DashboardManager::handle_dashboard_plugins();
+             echo '<div id="tabs-2">';
+            handlePlugins(false);
             echo '</div>';
 
             echo '<div id="tabs-3">';
+            DashboardManager::handle_dashboard_plugins();
+            echo '</div>';
+
+            echo '<div id="tabs-4">';
             handleExtensions();
             echo '</div>';
             echo '</div>';

+ 6 - 7
main/inc/lib/plugin.lib.php

@@ -260,14 +260,14 @@ class AppPlugin
     *
     * @return null|string
     */
-    public function load_region($region, $template, $forced = false)
+    public function load_region($region, $template, $_plugins, $forced = false)
     {
         if ($region == 'course_tool_plugin') {
             return '';
         }
 
         ob_start();
-        $this->get_all_plugin_contents_by_region($region, $template, $forced);
+        $this->get_all_plugin_contents_by_region($region, $template, $_plugins, $forced);
         $content = ob_get_contents();
         ob_end_clean();
 
@@ -284,7 +284,6 @@ class AppPlugin
      */
     public function load_plugin_lang_variables($plugin_name)
     {
-        global $language_interface;
         $root = api_get_path(SYS_PLUGIN_PATH);
         $strings = null;
 
@@ -300,6 +299,7 @@ class AppPlugin
         }
 
         // 2. Loading the system language
+        /*
         if ($language_interface != 'english') {
             $path = $root.$plugin_name."/lang/$language_interface.php";
 
@@ -330,7 +330,7 @@ class AppPlugin
                     }
                 }
             }
-        }
+        }*/
     }
 
     /**
@@ -342,9 +342,8 @@ class AppPlugin
      *
      * @todo improve this function
      */
-    public function get_all_plugin_contents_by_region($region, $template, $forced = false)
+    public function get_all_plugin_contents_by_region($region, $template, $_plugins, $forced = false)
     {
-        global $_plugins;
         if (isset($_plugins[$region]) && is_array($_plugins[$region])) {
             // Load the plugin information
             foreach ($_plugins[$region] as $plugin_name) {
@@ -375,7 +374,7 @@ class AppPlugin
                     }
 
                     // Setting the plugin info available in the template if exists.
-                    $template->assign($plugin_name, $_template);
+                    $template->addGlobal($plugin_name, $_template);
 
                     // Loading the Twig template plugin files if exists
                     $template_list = array();

+ 48 - 0
src/Chamilo/CoreBundle/EventListener/LegacyListener.php

@@ -100,6 +100,54 @@ class LegacyListener
             foreach ($globals as $index => $value) {
                 $container->get('twig')->addGlobal($index, $value);
             }
+
+            //$result = & api_get_settings('Plugins', 'list', $_configuration['access_url']);
+            $result = & api_get_settings('Plugins', 'list', 1);
+
+            $_plugins = array();
+            foreach ($result as & $row) {
+                $key = $row['variable'];
+                $_plugins[$key][] = $row['selected_value'];
+            }
+
+            // Loading Chamilo plugins
+
+            $appPlugin = new \AppPlugin();
+            $pluginRegions = $appPlugin->get_plugin_regions();
+            $force_plugin_load = true;
+            $pluginList = $appPlugin->get_installed_plugins();
+
+            foreach ($pluginRegions as $pluginRegion) {
+                $regionContent = $appPlugin->load_region(
+                    $pluginRegion,
+                    $container->get('twig'),
+                    $_plugins,
+                    $force_plugin_load
+                );
+
+                foreach ($pluginList as $plugin_name) {
+                    // The plugin_info variable is available inside the plugin index
+                    $pluginInfo = $appPlugin->getPluginInfo($plugin_name);
+                    if (isset($pluginInfo['is_course_plugin']) && $pluginInfo['is_course_plugin']) {
+                        $courseInfo = api_get_course_info();
+                        if (!empty($courseInfo)) {
+                            if (isset($pluginInfo['obj']) && $pluginInfo['obj'] instanceof \Plugin) {
+                                /** @var \Plugin $plugin */
+                                $plugin = $pluginInfo['obj'];
+                                $regionContent .= $plugin->renderRegion($pluginRegion);
+                            }
+                        }
+                    } else {
+                        continue;
+                    }
+                }
+
+                if (!empty($regionContent)) {
+                    $container->get('twig')->addGlobal('plugin_'.$pluginRegion, $regionContent);
+                } else {
+                    $container->get('twig')->addGlobal('plugin_'.$pluginRegion, '');
+                }
+            }
         }
 
         $session->set('access_url_id', $urlId);