Jump to content

Form to pages: Functions or Classes


louisstephens
 Share

Recommended Posts

So I have a form, once completed, will create new pages. All in all, this is eazy-peezy for me now. I guess I need a bit of guidance on how to actually structure the rest of my code.  I thought I could just write a function (_func.php) and pass the fields to the function and let it do its' thing. However, I am kinda hitting a road block when I do it this way. 

I currently am passing first name, last name, city, state (options field), and making pages based on the first/last names. I guess where I run into some issues is I am trying to check to see if the "page" already exists, and if it does, throw out an error:

In the home template:

if(isset( $_POST['submit'])) {
	$firstName =Trim (stripslashes($_POST['firstname']));
	$lastName = Trim(stripslashes($_POST['lastname']));
	$fullName =  $firstName . $lastName;
	$city = Trim(stripslashes($_POST['city']));
	$state = Trim(stripslashes($_POST['state']));
	$lowerCaseName = strtolower($fullName);
	$people = $pages->find("template=person");
	foreach ($people as $person) {
		$checkFirstName = $person->first_name;
		$checkLastName = $person->last_name;
		$checkFullName = $checkFirstName . $checkLastName;
		if ($checkFullName === $lowerCaseName) {
				echo "<p>" . "This person has already created a page. Please choose a different name." . "</p>";
			}
			else {
				echo "hey";
				processNewPerson(need_to_pass_person_details_to_function);

			}
		} // end foreach

In _func.php:

function processNewPerson($list) {
	$u = new Page();
	$u->template = "person";
	$u->parent = wire('pages')->get("/people/");
	$u->title = ;
	$u->first_name = ;
	$u->last_name = ;
	$u->state = ;
	$u->city = ;
	$u->save();
	$u->setOutputFormatting(false);

}

I am a little unsure of how to actually pass all the information to the template, as well as if this is even the best approach to do this. Would it make more sense to do this in a class, or keep it the way it is?

Link to comment
Share on other sites

Just now, breezer said:

You should download the LoginRegister module, it has all the code you need.

I also just found these members gist which has a huge amount of samples you might find useful.

https://gist.github.com/kongondo

https://gist.github.com/somatonic

Thanks @breezer, I will definitly look into that. I do need to clarify that I am not really making a "login/register" site or really updating actual registered users on the site. Basically, as it stand right now, a registered user will fill out the form with a persons information that they will be able to keep track of certain information. I guess think of this as a glorified rollodex of contacts. I will be adding more fields to the form down the road as this evolves.

Link to comment
Share on other sites

Pass it to an associative array (not tested):

<?php namespace ProcessWire;

function processNewPerson(WireArray $data) {
    $u = new Page();    
    $u->template = "person";
    $u->parent = $pages->get("/people/");
  	$u->of(false);
    $u->first_name = $data["first_name"];
    $u->last_name  = $data["last_name"];
    $u->full_name = $data["first_name"] . " " . $data["last_name"];
    $u->city  = $data["city"];
    $u->name = $sanitizer->pageName($u->full_name, true);
    $u->save();
}

if($input->post["submit"]) {
    $data = WireArray::new([
        "first_name" => $sanitizer->text($input->post['first_name']),
        "last_name" => $sanitizer->text($input->post['last_name']),
        "city" => $sanitizer->text($input->post['city'])
    ]);
    $people = $pages->find("template=person, first_name=$data['first_name'], last_name=$data['last_name']");
    if($people->count === 0) processNewPerson($data);
}

 

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