Jump to content

Creating Pages under the Admin Section


Jon
 Share

Recommended Posts

Hello, 

I am just after a bit advice as to if this is the correct/best away of going about this. Iam looking to store booking information (no card info just names and booking information) with each booking being a new page. This is how I was thinking of doing it. Front end from which sends the relative information then I would great a page(s) in the admin tree Admin>Bookings>Booking

To create this page I would be using the following code along with the information ill be storing

$p = new Page(); // create new page object
$p->template = 'booking'; // set template
$p->parent = wire('pages')->get('/site-manager/bookings/');
......

Is this the best practice to do this and secure?

Cheers

Jon

Link to comment
Share on other sites

Looks good, only thing I would check is:

You probaply will give a name and / or title to your new page. I would suggest to create it first and check if a page with that given name or title already exists:

$parent = wire('pages')->get('/site-manager/bookings/');
$genericTitle = $somehowCreatedFromYourDataOrTimestampOrWhatever;
$p = $pages->get("title={$genericTitle}, template=booking, parent.id=" . $parent->id);
if(0 == $p->id) { // page does not exist, we can create a new one
    $p = new Page(); // create new page object
    $p->template = 'booking'; // set template
    $p->parent = $parent;
    $p->title = $genericTitle;
    ......
} else {
    // UUUPs! A Page with that title exists. 
    // This case must be handled different! 
    ......
}
  • Like 1
Link to comment
Share on other sites

Hello Horst,

Thats great thank :) I was going to use a timestamp it would be very unlikely we got a booking at the exact same time, but adding a check just in case would be a good idea!

Thanks for that. 

Jon

Link to comment
Share on other sites

You could also get ProcessWire to handle duplicate cases for you.

$p = new Page();
$p->name = uniqid();
...
$p->save(array('adjustName' => true));

From the API function:

adjustName (boolean) - Adjust page name to ensure it is unique within its parent (default=false)
  • 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...