Jump to content

Admin Module - But Without Admin Template?


pokmot
 Share

Recommended Posts

Hi,

Without going into the why's, I would like to generate output in a module used in ADMIN, WITHOUT the admin template.

At the moment I am faking an Ajax request like this:

$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; // Fake an Ajax request

This is because the code in admin.php on line 79 checks for whether it is an Ajax request, then outputs the contents as they are if it is, or will otherwise use the admin template.

I guess I could create a custom admin template which checks if a global variable is set, but it occurs to me it would be much easier if we could switch off the admin template from within the module.

Is this possible, or could it be added as a configuration variable, for instance through something like:

$config->disable_admin_template = true;

?

Link to comment
Share on other sites

Another route is that you can grab the process module that you want and just execute it on your own without PW admin. Depending on which one you are using, you may want to use ProcessController to execute it for you. But either way, it's easy to grab the output that way, whether from a site template or anywhere else. I can post an example when I'm at a computer again if you'd like.

Link to comment
Share on other sites

Hi Ryan,

That would be interesting, thanks! I don't know the internals of Processwire well enough yet to figure out how to do it, but if you can either point me to a module that already does this, or paste an example in here, I would be very grateful!

Regards,

Steve

Link to comment
Share on other sites

Here's a couple examples. This first one grabs 'ProcessLogin' and places the output in $content:

<?php
$process = wire('modules')->get("ProcessLogin");
$content = $process->execute();

Here's another example. This one uses ProcessController, which will take care of access control and matching URL segments to functions in the Process module. This is the preferred way to do it in most cases (specific to Process modules only):

<?php
$controller = new ProcessController();
$controller->setProcessName('ProcessLogin');
$content = $controller->execute();
Link to comment
Share on other sites

Hi Ryan,

Thanks for the example, and I see where you are coming from. However, I believe it might be "the wrong way around", I think. If I have understood your example correctly, your code can be used from a template to pull the content of an admin module? This is very useful for me too, by the way, but doesn't seem to resolve my issue.

Let me be a bit more specific what I am looking for. I am writing a module which is editing a database table - this table is used to lookup information which in turn is called within the template.

The module I am writing is for the administrator only, and is accessed through the "Setup" option in the admin interface.

One of the things I want the module to do is to create an Excel export file by using the code at http://phpexcel.codeplex.com/.

This export requires output which cannot contain any of the admin template, hence I use my "cheat" to disable the template output by tricking the admin system to believe it is an Ajax request.

The alternative is create a specific page on the front end that calls my module, then outputs the content only - and I presume I can use your code to achieve this.

However, I would like the module to be as easy as possible, and having extra pages on the front end to cope with this is messy, and will also be very confusing for the users of the system.

I would therefore like to have a button within my custom admin module that says "Export", it then calls ___executeExport() within the module, and the Excel spreadsheet magically appears.

At the moment, the only way to achieve that is the very inelegant faking of an Ajax request.

So, my questions is: from within an admin module is it possible to disable the admin template without having to resort to a crude workaround?

Thanks for any help, or clarification if I have misunderstood your example!

Regards,

Steve

Link to comment
Share on other sites

Steve, I think I understand now. The solution is easier than you might have guessed. ProcessWire doesn't include any templates until after all the output is generated. So if you want to handle your own output generation, then you should be able to just send it out, and then exit. For instance, try placing this in your executeExport() function of your Process module:

die("Happy New Year!"); 

So I'm thinking you'd do something like this in your executeExport function:

<?php
header('Content-type: application/force-download');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename=spreadsheet.xls');
$fp = fopen('php://output', 'w');
fwrite($fp, $spreadsheet_output);
fclose($fp); 
exit(); 
Link to comment
Share on other sites

Yup, Ryan, you are right - much easier than I guessed!

I am used to letting the framework take control to do house cleaning etc, but with Processwire I assume from your example it's not necessary at all.

I simply used exit();, and problem solved.

Thanks! The more I use Processwire, the more I love it!

Link to comment
Share on other sites

Not sure if I understand your need correctly, but it might be that all you need is modal=1 on your url. That way admin templates strip everything.

Thanks apeisa, that's a very useful trick to know as well although the Admin template still outputs some content. Very useful for loading into a modal dialog box though, and I have in fact a use for it right away!

Link to comment
Share on other sites

I am used to letting the framework take control to do house cleaning etc, but with Processwire I assume from your example it's not necessary at all.

Steve, glad that worked for you. Regarding housecleaning, ProcessWire is still doing it. When you exit() the objects still have their destructors called and ProcessWire's shutdown function is still executed. It should be just fine to use exit() in this case.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...