3

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()

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
Strij
  • 29
  • 4

2 Answers2

3

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.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
0

So I actually solved something like this in a different way than in the controller. I make make a decoupled front end admin system. So website owners never have to log into the backend. Which trust me has saved numerous headaches and mistakes by users that mess with things they shouldn't. So here is how I have solved this:

  1. Install plugin Content Type by Sozonov Alexey
  2. Make a CMS Page and make sure it has .csv at the end of the url. You should see that the content type plugin added a content type tab in the page settings. You can leave the html selection alone but add text/csv as your own to the right of it.

    enter image description here

  3. Here is an example on how the template of the page looks like.

    {% spaceless %}
    NAME,EMAIL
    {% for row in csv %}
    {{ row.name }},{{ row.email }}
    {% endfor %}
    {% endspaceless %}
    
  4. Here is how the CMS Page PHP Code section would look. This can allow you to do queries on the list and filter as desired. You could then check to see if the client is logged into the backend or is logged in as a user and maybe is an admin or moderator. Of course you can make a plugin and component then attach it to this page.

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

Side note I have used this same technique to create dynamic css, javascript, or rss feeds. I make the site map using this as well.

Pettis Brandon
  • 875
  • 1
  • 6
  • 8
  • `@Pettis` may be you misunderstand, he wanted `filtered list of users to be exported` not all of them. as he already mentioned `he can export all the user`. Also seems its not for `Frontend` as `he used plugin and plugin is for back-end` - `so he is talking about backed I guess` – Hardik Satasiya Oct 09 '19 at 09:35
  • @HardikSatasiya I was actually hoping you would answer his question directly about the back end. I will say though I didn't misunderstand. I said "different way than in the controller" and I also said "This can allow you to do queries on the list and filter as desired". This is a working solution and I stated it is reached on the front end but oh well? – Pettis Brandon Oct 09 '19 at 15:36
  • Yes I am little busy but once I get time I will surely provide some answer :) , good to know you did not get wrong idea. I thought you posted this as answer so was just pointing out :) – Hardik Satasiya Oct 09 '19 at 16:32