Jump to content

Extending the PW-backend: e.g. CRUD system


dragan
 Share

Recommended Posts

Hello everyone,

I'm a PW newbie. Overwhelmed from all I've seen and tried out the last few days.

My CMS of choice the last couple of years has been MODX. Other than that, I am working as a frontend and backend developer since over 10 years. Having so much freedom to work directly with PHP inside a CMS / CMF is a relief. No new pseudo scripting language one has to learn (as in Typo3), no unnecessary restrictions or bloat.

So, my first general question:

If I have an existing dataset in mySQL, that I would like to manage with PW (i.e. inside the admin area of PW) - how would I most likely do that?

I know I could export the database tables to PW pages. But what if I wanted a database CRUD system inside the PW area? In MODX "Evo" you could build a custom module for such things. You know - phpMyAdmin is over the top for most clients, and an admin section with a different login and URL is clutter.

How can I extend the PW backend? Is it possible to include an off-the shelf CRUD script somewhere? Is that not possible with PW? Or frowned upon? I'm just trying to see what the "best practises" are for these kinds of scenarios.

Thanks for explanations, tips and pointers.

Link to comment
Share on other sites

Dragan,

Welcome to PW!

PW is very flexible....your decisions will not be frowned upon but of course advice will be offered, especially when it comes to security :). So, as for best practices, my experience is that PW is very flexible and in most cases there is not one way of doing anything. I know, it sounds like a cliche...but seriously, you'll notice this very quickly once you start getting your hands dirty. One thing you will notice is that with PW, you will end up writing less code :) It's simply that good. 

Yes; extending PW is in most cases quite easy....

Have a look at this module (WIP). You will get ideas how you can create your CRUD script....It's using DataTables to display PW pages. You can create your own to do something different, e.g. your CRUD stuff using either native SQL or PW's db class. Having worked in the industry that long I don't need to tell you care needs to be exercised if handing a client that much fire power ;). There is also this new kid on the block...a new take at making custom admin pages...

Oh, btw, am ex-MODx as well. There's more in here :)

Enjoy!

  • Like 1
Link to comment
Share on other sites

  • 7 months later...

Hmm ... I was wondering the same, but rather about how to create a CRUD system accessible from the front end (after login) ... much like how you can create custom content types and views in Drupal, and let various user roles (even non-logged in users) add content through the ".../node/add" interface.

Should I start a new thread for this, or could someone elaborate a little on what it would take to get something like this working?

Link to comment
Share on other sites

Since ProcessWire doesn't generate markup for you, you'd have to do this from the API side. Here's a simple example where you have an email subscription form and you accept an email address and store it in the 'title' field of a new page. 

<?php

$showForm = true; 
$email = $sanitizer->email($input->post->email); 

if($email) {

  // parent where we will store our new page
  $parent = $pages->get("/subscriptions/");

  if($parent->child("title=" . $sanitizer->selectorValue($email)) {
    // this checks if we already have this email 
    echo "<p>You are already subscribed!</p>";

  } else {
    // create a new 'subscription' page
    $subscription = new Page();
    $subscription->parent = $parent;
    $subscription->template = 'subscription';
    $subscription->title = $email; 
    $subscription->save();
    $showForm = false;
    echo "<p>Thank you! You have been subscribed.</p>"; 
  }
} 

if($showForm) echo "
  <form action='./' method='post'>
    <label for='email'>Enter your email address</label>
    <input type='email' name='email' id='email' />
    <input type='submit' name='subscribe' value='Subscribe' />
  </form>
  ";
 


There are also some tools that can automate this process to some extend, like FormBuilder, and the FormTemplateProcessor (proof of concept), among others. But the best way to get exactly what you want is to use the API. 

  • Like 2
Link to comment
Share on other sites

  • 3 months later...

Since ProcessWire doesn't generate markup for you, you'd have to do this from the API side. Here's a simple example where you have an email subscription form and you accept an email address and store it in the 'title' field of a new page. 

<?php

$showForm = true; 
$email = $sanitizer->email($input->post->email); 

if($email) {

  // parent where we will store our new page
  $parent = $pages->get("/subscriptions/");

  if($parent->child("title=" . $sanitizer->selectorValue($email)) {
    // this checks if we already have this email 
    echo "<p>You are already subscribed!</p>";

  } else {
    // create a new 'subscription' page
    $subscription = new Page();
    $subscription->parent = $parent;
    $subscription->template = 'subscription';
    $subscription->title = $email; 
    $subscription->save();
    $showForm = false;
    echo "<p>Thank you! You have been subscribed.</p>"; 
  }
} 

if($showForm) echo "
  <form action='./' method='post'>
    <label for='email'>Enter your email address</label>
    <input type='email' name='email' id='email' />
    <input type='submit' name='subscribe' value='Subscribe' />
  </form>
  ";
 

There are also some tools that can automate this process to some extend, like FormBuilder, and the FormTemplateProcessor (proof of concept), among others. But the best way to get exactly what you want is to use the API. 

Thanks for the snippet. Just tried it out for learning purposes but I can´t get it working.

First in the if($parent->child line is a closing bracket missing.

As I´m not really into php it wasn´t to easy to find^^

So the form is showing up but whatever email I try it´s saying "You are already subscribed!"

and I have no idea what to do.

Ah, important to mention that I included head.inc and foot.inc and created the /subscriptions/ page. Is there anything else to do?

Would nice to a have a little hint :-)

Cheers

Can

Link to comment
Share on other sites

I'm a little surprised as this example comes from Ryan. :)

The closing ) is something that happens all the time even to those into programming, but usually an error indicates that there's something missing. 

Well there seems to be a problem with the if($parent->child(....)) { ... }

It will always resolve to true, even if no page with that title found. This is because such a search will return a NullPage object if none found. In a if() this will resolve to true.

You'd have to add a little more to check if a page was found, like test for the id.

if( $parent->child("title=" . $sanitizer->selectorValue($email))->id ) {
   ...
}

Now this works.

  • Like 2
Link to comment
Share on other sites

wow that was fast and sounds logic

will try it in a couple of minutes have to make some shopping first :D

thank you soma!

UPDATE:

took a bit longer, because we just met a guy who will probably sail us to latin america  ^-^

It works like a charm..thank you Soma!!

Edited by Can
  • Like 1
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

×
×
  • Create New...