Jump to content

Create page on-the-fly


yabapolido
 Share

Recommended Posts

Hi yabapolido and welcome to the forums.

This should do it, although you'll need to tweak the preg_match because I wasn't sure whether you wanted exactly 2 numbers and two letters, or just a minimum of each. Currently it is set to just two of each.

 
Also, this currently only works if you are talking about a page added to the root of your site, but this can be easily tweaked.
 
The other thing you'll want to adjust is the template being assigned to the new page - currently I set it to basic-page.
<?php

/**
 * ProcessWire Auto Create Page
 * by Adrian Jones
 *
 * Automatically creates a page if it doesn't exist
 *
 * ProcessWire 2.x
 * Copyright (C) 2011 by Ryan Cramer
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 *
 * http://www.processwire.com
 * http://www.ryancramer.com
 *
 */

class AutoCreatePage extends WireData implements Module {

public static function getModuleInfo() {
return array(
'title' => 'Auto Create Page',
'summary' => 'Automatically creates a page if it does not exist',
'version' => 1,
'autoload' => true,
'singular' => true
);
}



    /**
     * Populate the default config data
     *
     */
    public function __construct() {

// determine the URL that wasn't found
$url = $_SERVER['REQUEST_URI'];

// if installed in a subdirectory, make $url relative to the directory ProcessWire is installed in
if($this->config->urls->root != '/') {
$url = substr($url, strlen($this->config->urls->root)-1);
}
        $last_slash = strrpos('/', $url);
        $this->last = str_replace("/","",substr($url, $last_slash));

    }



public function init() {
        if (preg_match(
            '/^(?=.*[a-zA-Z].*[a-zA-Z])(?=.*\d.*\d).{4}$/',
            $this->last)) {
 $this->addHookAfter('ProcessPageView::pageNotFound', $this, 'createPage');
        }
}

public function createPage($event) {

        if(!$this->pages->get("parent=1,name={$this->last}")->id){
            $np = new Page();
            $np->parent = $this->pages->get("/");
            $np->template = 'basic-page';
            $np->title = $this->last;
            $np->of(false);
            $np->save();

            $this->session->redirect($np->url);

        }

}

}
  • Like 3
Link to comment
Share on other sites

WOW. Adrian you blew my mind.

I've been reading for the past hour about PW and understood that is powerfull, but this is just awesome.

If I can just figure it out how, I'll try it right away, this is a module right?

It's working like a charm.

Thanks!

Would it be very annoying to ask another thing?

Given that new auto-created page, how should I proceed to let users add new comments and images to that page?

I've been looking at Form modules (because Comments module doesn't allow pictures), but don't know if that's the best choice?

  • Like 1
Link to comment
Share on other sites

Yes, what I just wrote is a module that can be installed directly so long as you tweak those few things I mentioned to suit your needs.

Well, you can easily install the comments module (http://processwire.com/api/fieldtypes/comments/) - it's in the core, just not installed by default. Adding the comments field to the template for the newly added pages and including the appropriate code in the template's php file as per the instructions in that link will get your comments set up automatically.

As for images - not sure the best approach here. You might be able to extend the comments fieldtype, but it might be better to go with a completely custom front end form. Here is the seminal thread on the matter of creating front end forms (https://processwire.com/talk/topic/2089-create-simple-forms-using-api/) - it is fairly simple, but you will have to get your hands dirty.

You might be able to use Ryan's pro Form Builder module (http://processwire.com/api/modules/form-builder/) which does include the ability to upload images. I haven't really ever used it though, so not sure exactly what would be involved in submitting the form and having it populate the same page again, especially if multiple users will be contributing content to the same page.

I think a custom front end form is probably the best option in the end and I have used these many time for user submitted images. I am sure others will chime in with additional ideas on this front though.

Edited by adrian
Added link to front end forms thread
Link to comment
Share on other sites

Btw, I just made one small change to the module - I had it creating the page without a title, which is fixed now.

Up to you, but something else to think about here - you are opening up the possibility for someone to write a script to generate potentially millions of pages on your site just by hitting URLs with the two letter / two number path in the url. If you then give them the ability to comment and upload images with any checks that they are a real users, rather than a bot, you could be creating a real mess and security issue for yourself.

Typically I would ensure any page created by a user was set to unpublished until they have somehow verified their status as a human being - an email activation check, etc. I am guessing that won't fit well with how you want things to work, but thought I should mention it so you can consider the possible implications. Obviously this is not a problem specific to PW.

Not sure your best option, and maybe the regex that limits the page to two letters/two numbers is enough to prevent any issues?

Link to comment
Share on other sites

For the intended purpose, there will be a limit, and there's really no problem that pages are created.

Although, you're right on comments, I will activate akismet or some captcha.

Don't know if you checked http://numplate.co.uk/ but I'll do mostly the same.

There's X number of plates for my country, so having the pages created is fine, they won't have any content until someone uploads pictures or comments.

I've already explored a little more about PW, that module to create a webservice based on pages is a life saver too. I was planning to have an Android application to integrate with the website, and this saves a lot of work.

Anyway, my problem now is with the forms.

I've installed the PoC module, not the premium one, I can generate pages based on the form fields, text, email fields work fine, but I can't upload a file or image :(

I should buy the Form Builder right?! (last resort as this project will be non-profit).

Thanks.

Link to comment
Share on other sites

Akismet is fairly useless in my experience and captchas are a horrible user experience and need to die. It's easy to use a honeypot (regular or reverse) with PW. There is even one built in to the comments module (https://processwire.com/talk/topic/960-comment-spam-filtering-alternatives/).

As I said, form builder may suit your needs, but that post on creating a front end form from scratch might be your best option - it was missing from the original version of my post, but has been added now.

Link to comment
Share on other sites

Soma, Andrian after a good night of sleep, I thought on that.

Maybe it doesn't make too much sense to create a real page, I just need that "id" to add the comments.

There's a limited number of valid license plates, anything else will be 404 (Soma, not a million pages ;) ).

So perhaps I can solve it with URL routing right?! Taking the 1st ou 2nd parameter and validate it against the regex that I have.

If valid, will display a page to add comments and pictures to that license plate.

Thanks

  • Like 1
Link to comment
Share on other sites

There's a limited number of valid license plates, anything else will be 404 (Soma, not a million pages  ;) ).

You can work the other way around without a regex. So basicly you have 2 parent folders: Available and In Use.

All possibilities go in Available, then when chosen, change the parent of the code page.

So move the page from Available and In Use. This way you can check if a code is already gone. And see which one is active.

  • Like 2
Link to comment
Share on other sites

Or simple one parent "plates" with all available pages (could be created ready to use) and just with a field to check "in use" ? So no page generation needed just a update on one field? And sorting/displaying would be easy...

Just some mindgames - didn't setup something yet.

  • Like 2
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...