diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2010-01-21 12:57:45 -0800 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2010-01-21 12:57:45 -0800 |
commit | d59c6ed4f149c201542c8b38f9ad9d61b4daabf4 (patch) | |
tree | ec977402f5a2a83990efb5159654d8083be22b0f /modules | |
parent | 6dd92cfa1cbdade77721f153aa1b6aab965cff82 (diff) |
The admin module controller allows modules to provide a check_environment method
which is called prior to installation. The method allows the module to provide
an error message or warnings if the module can not be installed or activated
without issues. The admin module controller also will fire a pre_deactivate
event, which allows modules to indicate issues that may arise be deactivating the
specified module.
These messages are displayed in a dialog box prior to installation in order to
allow the gallery administrator to determine the appropriate action before proceeding.
Lays the foundation for implementing a fix for ticket #937
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gallery/controllers/admin_modules.php | 41 | ||||
-rw-r--r-- | modules/gallery/helpers/module.php | 65 | ||||
-rw-r--r-- | modules/gallery/views/admin_modules.html.php | 40 | ||||
-rw-r--r-- | modules/gallery/views/admin_modules_confirm.html.php | 22 |
4 files changed, 154 insertions, 14 deletions
diff --git a/modules/gallery/controllers/admin_modules.php b/modules/gallery/controllers/admin_modules.php index 549718e7..46defbef 100644 --- a/modules/gallery/controllers/admin_modules.php +++ b/modules/gallery/controllers/admin_modules.php @@ -25,9 +25,48 @@ class Admin_Modules_Controller extends Admin_Controller { print $view; } + + public function confirm() { + access::verify_csrf(); + + $messages = array("error" => array(), "warn" => array()); + $desired_list = array(); + foreach (module::available() as $module_name => $info) { + if ($info->locked) { + continue; + } + + if ($desired = Input::instance()->post($module_name) == 1) { + $desired_list[] = $module_name; + } + if ($info->active && !$desired && module::is_active($module_name)) { + $messages = array_merge($messages, module::can_deactivate($module_name)); + } else if (!$info->active && $desired && !module::is_active($module_name)) { + $messages = array_merge($messages, module::check_environment($module_name)); + } + } + + if (empty($messages["error"]) && empty($messages["warn"])) { + $this->_do_save(); + $result["reload"] = 1; + } else { + $v = new View("admin_modules_confirm.html"); + $v->messages = $messages; + $v->modules = $desired_list; + $result["dialog"] = (string)$v; + $result["allow_continue"] = empty($messages["error"]); + } + print json_encode($result); + } + public function save() { access::verify_csrf(); + $this->_do_save(); + url::redirect("admin/modules"); + } + + private function _do_save() { $changes->activate = array(); $changes->deactivate = array(); $activated_names = array(); @@ -45,6 +84,7 @@ class Admin_Modules_Controller extends Admin_Controller { } else if (!$info->active && $desired && !module::is_active($module_name)) { $changes->activate[] = $module_name; $activated_names[] = t($info->name); + if (module::is_installed($module_name)) { module::upgrade($module_name); } else { @@ -63,7 +103,6 @@ class Admin_Modules_Controller extends Admin_Controller { if ($deactivated_names) { message::success(t("Deactivated: %names", array("names" => join(", ", $deactivated_names)))); } - url::redirect("admin/modules"); } } diff --git a/modules/gallery/helpers/module.php b/modules/gallery/helpers/module.php index 6c7078a3..2ae83f0d 100644 --- a/modules/gallery/helpers/module.php +++ b/modules/gallery/helpers/module.php @@ -120,19 +120,44 @@ class module_Core { } /** + * Check that the module can be installed. (i.e. all the prerequistes exist) + * @param string $module_name + */ + static function check_environment($module_name) { + module::_add_to_path($module_name); + $messages = array(); + + $installer_class = "{$module_name}_installer"; + if (method_exists($installer_class, "check_environment")) { + $messages = call_user_func(array($installer_class, "check_environment")); + } + + // Now the module is installed but inactive, so don't leave it in the active path + module::_remove_from_path($module_name); + return $messages; + } + + /** + * Check that the module can be installed. (i.e. all the prerequistes exist) + * @param string $module_name + */ + static function can_deactivate($module_name) { + $data = (object)array("module" => $module_name, "messages" => array()); + + module::event("pre_deactivate", $data); + + return $data->messages; + } + + /** * Install a module. This will call <module>_installer::install(), which is responsible for * creating database tables, setting module variables and calling module::set_version(). * Note that after installing, the module must be activated before it is available for use. * @param string $module_name */ static function install($module_name) { - $config = Kohana_Config::instance(); - $kohana_modules = $config->get("core.modules"); - array_unshift($kohana_modules, MODPATH . $module_name); - $config->set("core.modules", $kohana_modules); + module::_add_to_path($module_name); - // Rebuild the include path so the module installer can benefit from auto loading - Kohana::include_paths(true); $installer_class = "{$module_name}_installer"; if (method_exists($installer_class, "install")) { call_user_func_array(array($installer_class, "install"), array()); @@ -142,13 +167,32 @@ class module_Core { module::load_modules(); // Now the module is installed but inactive, so don't leave it in the active path - array_shift($kohana_modules); - $config->set("core.modules", $kohana_modules); + module::_remove_from_path($module_name); log::success( "module", t("Installed module %module_name", array("module_name" => $module_name))); } + private static function _add_to_path($module_name) { + $config = Kohana_Config::instance(); + $kohana_modules = $config->get("core.modules"); + array_unshift($kohana_modules, MODPATH . $module_name); + $config->set("core.modules", $kohana_modules); + // Rebuild the include path so the module installer can benefit from auto loading + Kohana::include_paths(true); + } + + private static function _remove_from_path($module_name) { + $config = Kohana_Config::instance(); + $kohana_modules = $config->get("core.modules"); + if (($key = array_search(MODPATH . $module_name, $kohana_modules)) !== false) { + unset($kohana_modules[$key]); + $kohana_modules = array_values($kohana_modules); // reindex + } + $config->set("core.modules", $kohana_modules); + Kohana::include_paths(true); + } + /** * Upgrade a module. This will call <module>_installer::upgrade(), which is responsible for * modifying database tables, changing module variables and calling module::set_version(). @@ -194,10 +238,7 @@ class module_Core { * @param string $module_name */ static function activate($module_name) { - $config = Kohana_Config::instance(); - $kohana_modules = $config->get("core.modules"); - array_unshift($kohana_modules, MODPATH . $module_name); - $config->set("core.modules", $kohana_modules); + module::_add_to_path($module_name); $installer_class = "{$module_name}_installer"; if (method_exists($installer_class, "activate")) { diff --git a/modules/gallery/views/admin_modules.html.php b/modules/gallery/views/admin_modules.html.php index aebedf09..7f572411 100644 --- a/modules/gallery/views/admin_modules.html.php +++ b/modules/gallery/views/admin_modules.html.php @@ -1,12 +1,50 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> <div class="g-block ui-helper-clearfix"> + <script type="text/javascript"> + $("#g-module-update-form").ready(function() { + $("#g-module-update-form").ajaxForm({ + dataType: "json", + success: function(data) { + if (data.reload) { + window.location.reload(); + } else { + $("body").append('<div id="g-dialog">' + data.dialog + '</div>'); + $("#g-dialog").dialog({ + bgiframe: true, + autoOpen: true, + autoResize: true, + modal: true, + resizable: false, + height: 400, + width: 500, + position: "center", + title: "Confirm Module Activation", + buttons: { + "Continue": function() { + $("form", this).submit(); + }, + Cancel: function() { + $(this).dialog("destroy").remove(); + } + } + }); + if (!data.allow_continue) { + $(".ui-dialog-buttonpane button:contains(Continue)") + .attr("disabled", "disabled") + .addClass("ui-state-disabled"); + } + } + } + }); + }); + </script> <h1> <?= t("Gallery Modules") ?> </h1> <p> <?= t("Power up your Gallery by adding more modules! Each module provides new cool features.") ?> </p> <div class="g-block-content"> - <form method="post" action="<?= url::site("admin/modules/save") ?>"> + <form id="g-module-update-form" method="post" action="<?= url::site("admin/modules/confirm") ?>"> <?= access::csrf_form_field() ?> <table> <tr> diff --git a/modules/gallery/views/admin_modules_confirm.html.php b/modules/gallery/views/admin_modules_confirm.html.php new file mode 100644 index 00000000..59592505 --- /dev/null +++ b/modules/gallery/views/admin_modules_confirm.html.php @@ -0,0 +1,22 @@ +<?php defined("SYSPATH") or die("No direct script access.") ?> +<div class="ui-helper-clearfix"> + <p> + <?= t("The following issue(s) have been identified:") ?> + </p> + + <div id="g-admin-modules-messages" class="g-block-content"> + <ul> + <? foreach (array("error" => "g-error", "warn" => "g-warning") as $type => $class): ?> + <? foreach ($messages[$type] as $message): ?> + <li class="<?= $class ?>" style="padding-bottom: 0"><?= $message ?></li> + <? endforeach ?> + <? endforeach ?> + </ul> + <form method="post" action="<?= url::site("admin/modules/save") ?>"> + <?= access::csrf_form_field() ?> + <? foreach ($modules as $module): ?> + <?= form::hidden($module, 1) ?> + <? endforeach ?> + </form> + </div> +</div> |