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

Export a list of users in OctoberCMS to CSV

我已阅读有关在 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 控制器时,出现错误:

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

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

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

所以我实际上用不同于控制器的方式解决了类似的问题。我制作了一个分离的前端管理系统。因此,网站所有者永远不必登录后端。相信我,已经避免了用户乱搞他们不该搞的事情的无数麻烦和错误。所以这就是我解决这个问题的方法:

  1. 安装插件 Content Type 作者:Sozonov Alexey
  2. 创建一个 CMS 页面并确保它在 url 的末尾有 .csv。你 应该看到内容类型插件在中添加了一个内容类型选项卡 页面设置。您可以单独保留 html 选择,但添加 text/csv 右边是你自己的。

  3. 这里是页面模板的示例。

    {% spaceless %}
    NAME,EMAIL
    {% for row in csv %}
    {{ row.name }},{{ row.email }}
    {% endfor %}
    {% endspaceless %}
    
  4. CMS 页面 PHP 代码部分的外观如下。这可以让您根据需要对列表进行查询和过滤。然后您可以检查客户端是否登录到后端或以用户身份登录,可能是管理员或版主。当然你可以做一个插件和组件,然后把它附加到这个页面。

    use Rainlab\User\Models\User;
    
    function onStart() {
        $users = User::all();
        $this['csv'] = $users;
    }
    

旁注我已经使用相同的技术来创建动态 css、javascript 或 rss 提要。我也用这个制作站点地图。

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

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

End result

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 :) .

如有疑问请评论。