Joomla! 3.2新功能——“安装后消息”
在Joomla! 3.2版本中发布的众多新功能中,可能最不被宣传但绝对对管理员非常有用、对开发者也很有趣的,是所谓的“安装后消息”,它会通知管理员有关已安装需要其注意的扩展和功能的信息,下面我们来详细了解一下其功能和用法。
如果您已经安装了Joomla! 3.2,您一定注意到了,在控制面板中会出现一条信息提示,点击相应的按钮,您会收到关于新功能“双因素认证”的通知,该功能需要您的关注,这是第一个具体使用这一新特性的应用。

用途
这是一个消息通知系统,可以在安装或更新扩展时使用。例如,许多扩展在安装后需要一些配置活动,这是为了确保扩展的正确运行,由于其敏感性,不能像已提到的“双因素认证”那样自动启用。使用新的通知系统,您会收到通知,表示功能可用,但需要适当的配置才能激活并正确运行。

如何使用
其工作原理基于新的com_postinstall组件,它通过定义两个函数来管理通知系统和扩展之间的交互,即
- action(要执行的行动)
- condition(何时通知消息)
以及存储在新的表#_postinstall_messages中的信息
基本上,这是一个很好的例子,说明如何使用新的Joomla! 快速应用程序开发框架(FOF)来快速开发Joomla! 扩展。
如何在您的扩展中使用它
使用扩展的安装后通知系统相当简单。假设我们想要使用这个系统来通知系统管理员在安装扩展后需要执行一些配置,在我们的例子中,我们假设目标扩展是一个名为example的系统插件。
与普通扩展相比,我们需要添加两个东西
- 一个包含函数定义(postinstall_action, postinstall_condition)的文件
- 在我们的信息插入到表#_postinstall_messages
为了简单起见,我在我的插件根目录下创建了一个名为postinstall的新文件夹,并向其中添加了一个名为actions.php的文件,它将包含我即将定义的两个函数的代码。同一个文件将在表#_postinstall_messages的action_file和condition_file字段中被引用
动作函数和条件函数
因此,我们详细定义两个函数(postinstall_action, postinstall_condition)
函数postinstall_condition:它将处理通知消息的条件,例如在我们的例子中,当插件安装但未启用时,我们将通过查询表#_extensions来验证我们的插件状态是否为“disabled”
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中,为此,如果我们扩展尚未这样做,我们将使用在安装或更新我们的扩展阶段执行脚本的选项,特别是我们必须在安装阶段采取行动,特别是要配置以下字段:
action和condition_method
使用先前定义的相应函数的名称example_install_action() 和example_install_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();
}
}
您已经注意到在安装和卸载阶段使用了两个函数install和unistall,在安装阶段,我们将向表#_postinstall_messages插入我们的信息,在卸载阶段,我们将从相应的表中删除我们的条目。
字段title_key,descrption_key,action_key用于设置将在通知阶段用于通知用户有关新安装的扩展及其需要您注意的特点的消息。
参考
如果您想深入了解所讨论的主题
在Joomla社区杂志上发表的一些文章代表了作者对特定主题的个人观点或经验,可能不与Joomla项目的官方立场一致
通过接受,您将访问由https://magazine.joomla.net.cn/外部第三方提供的服务
评论