Jump to content

Writing from front-end to back-end


Barry
 Share

Recommended Posts

Hi,

I've been approached by a potential client to build a site with the following basic functionality:

- A members only site listing available classes with a variable number of open spots and the lastest news from the admin. I plan on doing this by simply checking if the username, password and unique id match and setting a cookie or session to allow for viewing of the site. This way I can pull up their unique information and display it on a dashboard-ish page. If anyone has a better suggestion, I'll be happy to hear them!

ok, here's where I get a little lost:

- Members can change their passwords after they've logged in (accounts are made for them, there will be no create an account functionality) - How would I build functionality that will change and write this information from the front-end to the back-end?

- Members can register for classes if there is room available. Again, almost about the same issue as above, how can I write from the front-end to the back-end by connecting a member's id/username to a class that will be created via the backend as a class under the page-type classes? How would I be able to get this information into, say, a list via the backend which will display the user's name, id and email?

Basically, I'm wondering how I would go about allowing users logged into the front-end to manipulate their own personal information in the backend and connect themselves to a class if there is room available.

I've seen this post: http://processwire.c...-widget-system/ which seems like it would be perfect for what I'm looking to build but I don't think my time frame allows for the wait.

Many thanks in advance for any advice - PW rocks! :D

Link to comment
Share on other sites

Hiya Barry, without going into much detail as am a bit rushed at the moment, with PW you can easily use the API to add/edit/delete pages. This case sounds like it would be no different.

Create page:

$p = new Page();
$p->template = 'myTemplate';
$p->name = 'page_name';
$p->parent = '/parent_page/';

$p->of(false);
$p>title = 'My New Page';
$p->save();

Edit page

$p = $pages->get("/about/");
$p->of(false); // turn off output formatting
$p->title = "New Title";
$p->my_field = "My Value";
$p->save();

Delete page

$p = $pages->get("/about/");
$p->delete();

If you want to let members edit this, just place it all in an if statement and set up a form to input the data. Then make sure you sanitize the data before adding it to the new page.

This can apply to users as well as normal pages. It is all editable via the API.

There's tons of topics out there already on the forum but check out the API http://processwire.com/api/

And Soma's cheatsheet http://processwire.com/api/cheatsheet/

  • Like 3
Link to comment
Share on other sites

Regarding to the post onjegolders made, I made a little form to create new pages.

I think from this point it would not be hard to add more functionality.

<?php
/**
* Page template
*
*/
include("./head.inc");
echo $page->body;
$success_message = "<h3 class='success'>Saved</h3>";
$success = false; // we assume it and set to true if form sent
$error = false;
// set and sanitize our form field values
$form = array(
'name' => $sanitizer->text($input->post->name),
'title' => $sanitizer->text($input->post->title)
);
$required_fields = array(
'name' => $input->post->name,
'title' => $input->post->title
);
// check if the form was submitted
if($input->post->submit) {
// determine if any fields were ommitted or didn't validate
foreach($required_fields as $key => $value) {
 if( trim($value) == '' ) {
	 $error_message = "<h3 class='error'>Please check that you have completed all the required fields.</h3>";
	 $error = true;
 }
}
// if no errors, create a new page
if(!$error) {
$p = new Page(); // create new page object
$p->template = 'basic-page'; // set template
$p->parent = wire('pages')->get('/about/'); // set the parent
$p->name = $input->post->name; // give it a name used in the url for the page
$p->title = $input->post->title; // set page title (not neccessary but recommended)
$p->save(); //create the page
// populate fields
$p->summary = 'Variable here'; // Populate a field
$p->body = 'Variable here'; //Populate a field
$p->save(); //save the populated fields
echo $success_message;
echo 'id: '.$p->id.'<br/>';
echo 'path: '.$p->path;
}
}
?>
<?php if(!$success) { ?>
<?php
if($error) {
 echo $error_message;
}
?>
<form action="./" method="post" id="submitform">
<fieldset>		
	 <label for="name">Name *</label>
	 <input type="text" name="name" value="" value="<?php echo $user->name; ?>" autofocus required />		
	 <label for="email">Title *</label>
	 <input type="text" name="title" value="" required />
<input type="submit" name="submit" value="submit" />
</fieldset>
</form>

<?php } else {
echo $success_message;
// testing
 echo 'id: '.$p->id.'<br/>';
echo 'path: '.$p->path;
?>
<?php } ?>
<?php include("./foot.inc"); ?>

works on my localhost.

  • Like 5
Link to comment
Share on other sites

  • 1 year later...

I started a simple module / test to create / edit pages.

Create and edit simple pages works fine, but frontend create pages with file / image field failed with an error.

I think it's missing file upload / handling.

I'll do some more tests, but no time at the moment..

Link to comment
Share on other sites

For your password change functionality you could use something like this

<?php
$out = '';

// create a new form field (also field wrapper)
$form = $modules->get("InputfieldForm");
$form->action = "./";
$form->method = "post";
$form->attr("id+name",'password-form');

// create password inputs from the built in PW password field
$field = $modules->get("InputfieldPassword");
$field->label = "My new Password";
$field->attr("id+name","pass");
$form->append($field);

// submit button!
$submit = $modules->get("InputfieldSubmit");
$submit->label = " ";
$submit->attr("value","Save Changes");
$submit->attr("id+name","submit");
$form->append($submit);

// form was submitted so we process the form
if($input->post->submit) {

    // user submitted the form, process it and check for errors
    $form->processInput($input->post);

    if ($form->id == "password-form") { //only process input from password form in case you have more forms in your dashboard
        //here you could do some validation logic but I don't think it is necessary if you only have password fields
        //to do validation and print out errors for the password field you would do something like
        $pass = $form->get("pass");
        $_pass = $form->get("_pass");
        if ($pass != $_pass) {
            $pass->error("passwords do not match");
        }

        if($form->getErrors()) {
            // the form is processed and populated
            // but contains errors
            $out .= $form->render();
        } else {

            //save new password
            $pass = $form->get("pass")->value;
            if ($pass != '') {
            	$user->of(false);
            	$user->pass = $pass;
            	$user->save();
            	$user->of(true);
            }

            $out .= "Your changes have been saved.";
            $out .= $form->render();

        }
    }
} else {
    // render out form without processing
    $out .= $form->render();
}
echo $out;
?>

Note: don't sanitize passwords as that might change them and then user can't login.

Link to comment
Share on other sites

  • 3 months later...

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...