Search code examples
phpoctobercmsoctobercms-plugins

Export a list of users in OctoberCMS to CSV


I have read tutorial about using the import and export functionality in OctoberCMS.

But with the rainlab-users plugin, these guidelines don't work. I also tried to install the plugin vojtasvoboda-userimportexport, but it exports all users, not filtered ones.

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

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

When I added this code into Users.php controller, I got an error:

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

When I try export without code above, I got error:

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


Solution

  • I think its little difficult as we are not having access to user plugin toolbar to add button so.

    But YES we can do it, we need to try little harder :) lets start

    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

    Create folder inside your plugin's root directory and name it user_extension_files

    Inside that directory

    Add config_import_export.yaml with content

    export:
      useList: true
    

    Add _new_list_toolbar.htm with content [ It will be just copy of plugins/rainlab/user/controllers/users/_list_toolbar.htm with slight modification]

    With adding Our Brand New Shiny Export button not pasting whole code it will be too long so just pasting fragment of it.

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

    if any doubts please comment.