gpt4 book ai didi

php - 将 OctoberCMS 中的用户列表导出到 CSV

转载 作者:行者123 更新时间:2023-12-05 03:57:59 27 4
gpt4 key购买 nike

我已阅读有关在 OctoberCMS 中使用导入和导出功能的教程。

但是对于 rainlab-users 插件,这些指南不起作用。我还尝试安装插件 vojtasvoboda-userimportexport,但它会导出所有用户,而不是过滤的用户。

class Users extends \RainLab\User\Controllers\Users
{

public $implement = [
'Backend.Behaviors.FormController',
'Backend.Behaviors.ListController',
'Backend.Behaviors.ImportExportController',
];
....
}

当我将此代码添加到 Users.php Controller 时,出现错误:

Class .....\User\Controllers\Users has already been extended with Backend\Behaviors\ImportExportController.

当我不使用上面的代码尝试导出时,出现错误:

Call to undefined method October\Rain\Database\QueryBuilder::export()

最佳答案

我认为这有点困难,因为我们无法访问用户插件工具栏来添加按钮。

但是是的我们可以做到,我们需要再努力一点:) 让我们开始吧

End result

enter image description here

To add export button we need to Extend rainlab.user plugin. So from your own plugin you need to it.


1. Adding Extension code to your plugin's Boot method

class Plugin extends PluginBase
{
use \System\Traits\ConfigMaker; // trait to read config

public function boot() {

\RainLab\Users\Controllers\Users::extend(function($controller) {
// we only extend if its not already extended with ImportExport Behavior
if(!$controller->isClassExtendedWith('Backend.Behaviors.ImportExportController')) {

$controller->implement[] = 'Backend.Behaviors.ImportExportController';
// make sure you replace this path to your plugin directory
$extensionPath = '$/hardiksatasiya/stackdemo/user_extension_files/';
$controller->addDynamicProperty(
'importExportConfig',
$extensionPath . 'config_import_export.yaml'
);
$newListConfig = $this->makeConfig(
'$/rainlab/user/controllers/users/config_list.yaml'
);
$newListConfig->toolbar['buttons'] =
$extensionPath . '_new_list_toolbar.htm';

$controller->listConfig = $newListConfig;
}
});

}

....

2. Creating folder and files

在插件的根目录中创建文件夹并将其命名为 user_extension_files

在那个目录里面

添加config_import_export.yaml有内容

export:
useList: true

添加_new_list_toolbar.htm内容为 [ 它只是 plugins/rainlab/user/controllers/users/_list_toolbar.htm 的副本稍作修改]

加上Our Brand New Shiny Export button不粘贴整个代码会太长,所以只粘贴它的片段。

<div data-control="toolbar">
... copied code ...

<!-- our export button -->
<a
href="<?= Backend::url('rainlab/user/users/export') ?>"
class="btn btn-primary oc-icon-sign-out">
Export
</a>

</div>

Now, when you click on export button it should export records and It will also respect all the applied filters.


@NOTE: we are copying code to _new_list_toolbar.htm, So in future if user plugin is getting updated and they decide to add new buttons in tool-bar then we are not able to have that changes. So in that time we just need to copy & paste code from plugins/rainlab/user/controllers/users/_list_toolbar.htm to our file _new_list_toolbar.htm again. We are back in business again :) .

如有任何疑问,请发表评论。

关于php - 将 OctoberCMS 中的用户列表导出到 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58286673/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com