Joomla! 3.2 新特性:安装后消息
在 Joomla! 3.2 版本中发布的许多新特性中,最引起我兴趣的,也许是不太为人所知的但肯定对管理员非常有用、对开发者也很有趣的特性之一,就是所谓的“安装后消息”。这些消息会告知网站管理员在扩展成功安装后需要关注的特性。
让我们更详细地考察一下其特性和它们如何被扩展开发者所使用。如果您已经安装了 Joomla! 3.2,您可能已经在控制面板中注意到类似以下图示的信息性消息。通过点击“查看消息”按钮,您将被告知新“双因素认证”的可用性,这需要您的关注。这是使用此新特性的具体应用的第一个例子。
这是什么?
这是一个管理安装后和升级消息的系统,它可以在安装或升级扩展时使用。例如,在安装后,许多扩展需要配置任务,这对于扩展本身的正常工作至关重要,但由于其复杂性,不能自动启用。上述“双因素认证”,例如,使用新的通知系统,会通知您该功能可用,但为了正确执行任务,需要您进行一些配置设置。

它是如何工作的?
这项工作由新的组件 com_postinstall 完成,它通过定义两个函数管理通知系统与扩展之间的交互
- action(执行的操作,例如重定向到配置页面)
- condition(何时显示消息,例如安装后)
数据存储在新表 # _postinstall_messages 中。
基本上,这是一个如何使用新的 Joomla! 快速开发框架应用别名 FOF(3.2 引入的许多新特性之一)来快速开发 Joomla! 扩展的极好例子。
如何在您的扩展中使用它
扩展开发者使用后安装消息通知系统的过程相对简单。假设我们想使用这个系统来通知网站管理员,在扩展安装后(条件),需要执行一些配置任务。在我们的案例中,扩展应是一个名为“example”的插件类型系统,我们希望在点击通知消息按钮后,将管理员重定向到插件配置页面(操作)。
与普通扩展相比,我们需要添加两个东西
- 创建一个包含两个函数定义的文件(postinstall_action,postinstall_condition)
- 在表# _postinstall_messages中插入扩展信息
为了简单起见,我在插件根目录下创建了一个名为“postinstall”的新文件夹,并创建了 文件 actions.php,该文件将包含我将定义的两个函数的代码。这个文件将在表#_postinstall_messages中的action_file和condition_file字段中引用。
让我们详细定义两个函数(postinstall_action,postinstall_condition)。
postinstall_condition 函数:这个函数将处理消息的通知条件。在我们的案例中,条件是当插件已安装但未启用时
function example_postinstall_condition()
{
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from($db->qn('#__extensions'))
->where($db->qn('type') . ' = ' . $db->q('plugin'))
->where($db->qn('enabled') . ' = ' . $db->q('1'))
->where($db->qn('folder') . ' = ' . $db->q('system'))
->where($db->qn('element') . ' = ' . $db->q('example'));
$db->setQuery($query);
$enabled_plugins = $db->loadObjectList();
return count($enabled_plugins) == 0;
}
postinstall_action 函数:当管理员点击通知消息中显示的按钮时,将激活此函数。在我们的案例中,操作是:将插件设置为启用,并将管理员重定向到插件配置页面
function example_postinstall_action(){
// Enable the plugin
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from($db->qn('#__extensions'))
->where($db->qn('type') . ' = ' . $db->q('plugin'))
->where($db->qn('enabled') . ' = ' . $db->q('0'))
->where($db->qn('folder') . ' = ' . $db->q('system'))
->where($db->qn('element') . ' = ' . $db->q('example'));
$db->setQuery($query);
$enabled_plugins = $db->loadObjectList();
$query = $db->getQuery(true)
->update($db->qn('#__extensions'))
->set($db->qn('enabled') . ' = ' . $db->q(1))
->where($db->qn('type') . ' = ' . $db->q('plugin'))
->where($db->qn('folder') . ' = ' . $db->q('system'))
->where($db->qn('element') . ' = ' . $db->q('example'));
$db->setQuery($query);
$db->execute();
//Redirect the user to the plugin configuration page
$url = 'index.php?option=com_plugins&task=plugin.edit&extension_id='
.$enabled_plugins[0]->extension_id ;
JFactory::getApplication()->redirect($url);
}
完成我们的工作的最后一件事是在表#_postinstall_messages中插入配置数据。为此,我们使用manifest文件中的scriptfile标签:
<scriptfile>example.script.php</scriptfile>
为了能够在安装或升级我们的扩展期间运行脚本,特别是我们需要更新以下字段:
- action和condition_method
分别使用之前定义的函数的名称,例如example_postinstall_action ()和example_postinstall_condition ()
- action_file和condition_file
分别使用包含相应函数的文件名(在我们的案例中是actions.php)。
代码可能看起来像这样
class plgSystemexampleInstallerScript
{
/*
* $parent is the class calling this method.
* install runs after the database scripts are executed.
* If the extension is new, the install method is run.
* If install returns false, Joomla will abort the install and undo everything already done.
*/
function install( $parent ) {
$db = JFactory::getDbo();
$query = 'INSERT INTO '. $db->quoteName('#__postinstall_messages') .
' ( `extension_id`,
`title_key`,
`description_key`,
`action_key`,
`language_extension`,
`language_client_id`,
`type`,
`action_file`,
`action`,
`condition_file`,
`condition_method`,
`version_introduced`,
`enabled`) VALUES '
.'( 700,
"PLG_SYSTEM_EXAMPLE_POSTINSTALL_TITLE",
"PLG_SYSTEM_EXAMPLE_POSTINSTALL_BODY",
"PLG_SYSTEM_EXAMPLE_POSTINSTALL_ACTION",
"plg_system_example",
1,
"action",
"site://plugins/system/example/postinstall/actions.php",
"example_postinstall_action",
"site://plugins/system/example/postinstall/actions.php",
"example_postinstall_condition",
"3.2.0",
1)';
$db->setQuery($query);
$db->execute();
}
/*
* $parent is the class calling this method
* uninstall runs before any other action is taken (file removal or database processing).
*/
function uninstall( $parent ) {
$db = JFactory::getDbo();
$query = 'DELETE FROM '.$db->quoteName('#__postinstall_messages').
' WHERE '. $db->quoteName('language_extension').' = '.$db->quote('plg_system_example');
$db->setQuery($query);
$db->execute();
}
}
如果您想深入了解这里讨论的主题,请阅读
在Joomla社区杂志上发表的一些文章代表了作者对特定主题的个人观点或经验,可能不代表Joomla项目的官方立场
通过接受,您将访问https://magazine.joomla.net.cn/外部提供的第三方服务
评论