Jump to content


Photo

Module: ProcessPreview


  • Please log in to reply
52 replies to this topic

#1 Nico Knoll

Nico Knoll

    The Boss.

  • Members
  • PipPipPipPipPip
  • 789 posts
  • 393

  • LocationBerlin, Germany

Posted 31 December 2011 - 07:39 AM

Hi,
here's a really simple preview module. It adds a "preview" button next to "save".

To use it you have to copy "preview.php" to your root folder. (If the module has write permission it should do it automatically).

Please let me now if it work and what do you think about it.

You can download it here: https://github.com/N...ProcessPreview/

Happy new year(!!!),
Nico

#2 nikola

nikola

    Sr. Member

  • Members
  • PipPipPipPip
  • 233 posts
  • 137

  • LocationZagreb, Croatia

Posted 31 December 2011 - 08:36 AM

Works great Nico.

How about cloning it to upper right area (like the save button and other buttons are cloned)?

I would also add "float: left" to #wrap_preview div in ProcessPreview.css so it would show before "Save page" button, it makes sense...
Check out my ProcessWire admin themes: Futura Remixed Admin Theme / Moderna Admin Theme / Futura Admin Theme

#3 Nico Knoll

Nico Knoll

    The Boss.

  • Members
  • PipPipPipPipPip
  • 789 posts
  • 393

  • LocationBerlin, Germany

Posted 31 December 2011 - 08:40 AM

To the first: Tested it - doesn't look good...

To the second: I guess it's better like this, because "Save"/"Publish" is the main button and "Preview" is just a second, optional.

#4 ryan

ryan

    Reiska

  • Administrators
  • 7,787 posts
  • 6546

  • LocationAtlanta, GA

Posted 31 December 2011 - 10:45 AM

Great module Nico! This is a way of going about it that I hadn't considered before. It seems to work quite well on the pages I tested it with. A few comments/suggestions:

1. This is another module that shouldn't be a 'Process' module the way it's currently implemented. But I think you may want to make this a real Process module for another reason (below). But if you stick with the preview.php method, then you likely want to make this a regular non-Process module and make it extend WireData rather than Process.

2. I'm concerned about the safety of preview.php. This gives any logged-in user access to view any page they want to. If you had to stick with this method, I would change it as follows:

<?php

include './index.php'; // add "./" before index.php to ensure it's not picking up another from PHP's path

if(!wire('user')->isLoggedin()) throw new Wire404Exception(); 

$page = wire('pages')->get((int) $_GET['id']); // typecast to (int)
 
if(!$page || !$page->id) throw new Wire404Exception(); // check that it exists
if(!$page->editable()) throw new WirePermissionException("You don't have access to edit that page"); // check for edit permission 

foreach(wire('input')->get as $key => $value) { // change from $_GET to wire('input')->get
    $key = wire('sanitizer')->name($key); 
    if($key != $_GET['key']) continue; // skip over invalid field name
    if(!$page->fields->getField($key)) continue; // skip over field that this page doesn't have
    $return->setFieldValue($key, $value, false); // add the extra 'false' for optimization: prevents PW from loading previous value
}

echo $return->setOutputFormatting(true)->render();

3. What may be a preferable way to implement this is to move everything from preview.php into an execute() function of your ProcessPreview.module. Then have ProcessPreview's install method add a page using the 'admin' template to /processwire/page/preview/ and assign ProcessPreview to the 'process' property of that page. See the install() method of this module for an example of how to do that: https://github.com/r...PagesCSV.module

The reason may be be a preferable way to do it is because it ensures that PW admin is still the gatekeeper to an admin activity. And it also removes the necessity for someone to have an extra php file that they might or might not have to copy to your site root. However, if you still prefer preview.php, I don't necessarily see a problem with using the preview.php if the security of it is updated like in the example above.

---

Edit: added 2 more lines in the foreach

#5 ryan

ryan

    Reiska

  • Administrators
  • 7,787 posts
  • 6546

  • LocationAtlanta, GA

Posted 05 January 2012 - 06:37 PM

Nico are you still working on this? I'm intrigued by the possibilities with this module–keep up the great work here.

#6 Nico Knoll

Nico Knoll

    The Boss.

  • Members
  • PipPipPipPipPip
  • 789 posts
  • 393

  • LocationBerlin, Germany

Posted 05 January 2012 - 06:40 PM

Well yeah I'm working on it but I hadn't much time this week because of my school life. So at the weekend I probably improve this module :D

#7 Nico Knoll

Nico Knoll

    The Boss.

  • Members
  • PipPipPipPipPip
  • 789 posts
  • 393

  • LocationBerlin, Germany

Posted 06 January 2012 - 10:38 AM

OK, I updated preview.php with something much simular to Ryans posted code. So it should be much safer while I'm working on the backed method. Same download link like in the first post.

#8 ryan

ryan

    Reiska

  • Administrators
  • 7,787 posts
  • 6546

  • LocationAtlanta, GA

Posted 06 January 2012 - 11:45 AM

Thanks for the update Nico!

#9 Soma

Soma

    Hero Member

  • Moderators
  • 5,043 posts
  • 3801

  • LocationSH, Switzerland

Posted 11 January 2012 - 04:20 AM

Just wanted to hook me in. Great idea Nico. Thanks for the module! I will check it out soon, as it's a often requested feature.

@somartist | modules created | support me, flattr my work flattr.com


#10 carlos

carlos

    Jr. Member

  • Members
  • PipPip
  • 13 posts
  • 0

Posted 01 February 2012 - 10:32 AM

I think this forum must read minds. I just had some users ask for this exact feature. Thanks Nico, extremely useful.

#11 Pete

Pete

    Forum Admin

  • Administrators
  • 2,393 posts
  • 1441

  • LocationChester, England

Posted 04 February 2012 - 05:38 AM

Hi Nico

I get the following error:

Request-URI Too Large


Is it trying to pass the entire page body in a $_GET var? That's what it appears to be doing on my installation anyway :(

#12 Nico Knoll

Nico Knoll

    The Boss.

  • Members
  • PipPipPipPipPip
  • 789 posts
  • 393

  • LocationBerlin, Germany

Posted 04 February 2012 - 07:41 AM

Yes, this is how it works. But I'm working on an alternative way. So I wil release a better version within the next week.

/ Nico

#13 Pete

Pete

    Forum Admin

  • Administrators
  • 2,393 posts
  • 1441

  • LocationChester, England

Posted 04 February 2012 - 09:39 AM

Ah right, I was trying to preview a particularly lengthy article ;)

#14 ryan

ryan

    Reiska

  • Administrators
  • 7,787 posts
  • 6546

  • LocationAtlanta, GA

Posted 04 February 2012 - 09:49 AM

I'm guessing this one can be solved just by using POST rather than GET?

#15 Nico Knoll

Nico Knoll

    The Boss.

  • Members
  • PipPipPipPipPip
  • 789 posts
  • 393

  • LocationBerlin, Germany

Posted 07 April 2012 - 03:38 PM

Howdy cowboys!

Please let me introduce ProcessPreview 2.0.

The new version doesn't need the preview.php anymore because it works completely different. Just have a look in the code (only 89 lines!).

Also it now uses AJAX POST requests instead of the old GET way.

You can download it on GitHub: https://github.com/N...ProcessPreview/

Greets,
Nico

#16 Pete

Pete

    Forum Admin

  • Administrators
  • 2,393 posts
  • 1441

  • LocationChester, England

Posted 07 April 2012 - 05:43 PM

Nice - I see precisely what's happening and that's a clever way of doing it indeed :)

For anyone confused as to what the new module does, it basically allows you to view the page as if it were published. In fact, for the current page it sets the status of the page as though it were published (I think) but only for the page launched by the preview button, and doesn't save that state, so you're viewing it as if it were published even though it isn't.

I think that's right anyway.

#17 Soma

Soma

    Hero Member

  • Moderators
  • 5,043 posts
  • 3801

  • LocationSH, Switzerland

Posted 07 April 2012 - 05:49 PM

Nico thanks for the update! I just gave it a try. But...

Do you know that the "view" button of PW does actually same as this module? Any logged in user that can edit/view a page can also "view" it even if the page is unpublished. I didn't knew that for some time.

So this module could just add a target='_blank' attribute to the "view" tab and do the exactly same.

The other thing is: This module doesn't do what I expect from a "preview" module. I would assume that when I want to "preview" a page I'm editing, that it works without saving the page actually. I guess there would be the need to have a more complicated process going to get this work. I can't think of any atm. But I'm sure there is one.

After testing you module, there was an issue when I click preview, it opens new window. I close the window, and when I click "save" it open 1-2 Windows again with the preview. I close them and still the same. I need to reload the page to get saving to work.

@somartist | modules created | support me, flattr my work flattr.com


#18 Soma

Soma

    Hero Member

  • Moderators
  • 5,043 posts
  • 3801

  • LocationSH, Switzerland

Posted 07 April 2012 - 06:15 PM

Ahh sorry now I see it does some saving of the page (post). If I change the title it does show on preview without saving.. but it doesn't seem to work always only every 2th time? There's something strange that's why I assumed it doesn't save.

Also will this approach work with when there's some logic happen in the template view to show something based on the pages id or childs?

@somartist | modules created | support me, flattr my work flattr.com


#19 Nico Knoll

Nico Knoll

    The Boss.

  • Members
  • PipPipPipPipPip
  • 789 posts
  • 393

  • LocationBerlin, Germany

Posted 08 April 2012 - 03:52 AM

Well, the main point is that you don't have to save or publish the page before you see a preview of it. And it should work after the first click on the button you just have to wait some seconds because of the ajax request.

#20 Soma

Soma

    Hero Member

  • Moderators
  • 5,043 posts
  • 3801

  • LocationSH, Switzerland

Posted 08 April 2012 - 03:56 AM

Well, it doesn't. Save button is broken after using "preview" ones. And the preview only shows the changed text every 2th time I click "preview".

@somartist | modules created | support me, flattr my work flattr.com





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users