Jump to content

Trying to implement a frontend batch deleting process for editor


onjegolders
 Share

Recommended Posts

Hi again guys, as part of my plan to offer a fully-fledged front-end student management system for my site, I'm trying to add a page where there is a list of all students and a checkbox next to each name. Whichever student has a checkbox next to their name gets deleted upon form submit.

Have tried the following but think I have lost my way!

<?php
if($input->post->submit) {
$students = $pages->get("template=students")->children();
foreach ($input->post->$student->name as $s) {
 if (isset($s)) {
  $deletable = $users->get($student->name);
  $deletable->delete();
  echo $deletable->name . " has been deleted!";
 }
}
}
?>

<form action="" method="post">
<ul>
<?php foreach ($students as $student) { ?>
<li>
 <?php echo $student->name; ?>
 <input type="checkbox" name="<?php echo $student->name; ?>">
</li>
<?php } ?>
<input type="submit" value="Delete selected users" name="submit">
</ul>
</form>

Am I missing something simple here?

For those who are stuck doing similar things, I have managed to get the adding and modifying students front-end bit pretty sorted I think so more than happy to lend a hand! :)

Link to comment
Share on other sites

First, there is something confusing me on your code. If the variable $students is defined only after submission, how can you use it for building the form? Is it working?? You are also using the variable $student above the place where you defined it. Still, I will try to help from what I understood...

For multiple check boxes you can use this form:

<input type="checkbox" name="group[]" value="um">

<input type="checkbox" name="group[]" value="dois">

where group will be an array holding the values "um" and "dois"

So, using your code:

<li>
   <?php echo $student->name; ?>
   <input type="checkbox" name="delete[]" value="<?php echo $student->name; ?>">
</li>

and after submission:

if($input->post->submit) {
   foreach ($input->post->delete as $s) {
       $users->get($s)->delete();
       echo $s->name . " has been deleted!";
   }
}

Putting it all together and making some improvements:

if($input->post->submit) {
   foreach ($input->post->delete as $s) {
       if($users->get($s)){
           $users->get($s)->delete();
           echo $s->name . " has been deleted!";
       }else{
           echo "Ididn't find" . $s->name;
       }
   }
}
?>

<form action="" method="post">
<ul>
<?php foreach ($pages->get("template=students")->children() as $student) { ?>

<li>
   <?php echo $student->name; ?>
   <input type="checkbox" name="delete[]" value="<?php echo $student->name; ?>">
</li>
<?php } ?>
<input type="submit" value="Delete selected users" name="submit">
</ul>
</form>

written on the browser and not tested!

Link to comment
Share on other sites

Thanks Diogo, you're always so helpful and it's much appreciated. Think I was trying to solve this problem at the end of a long day and you're right it's a bit all over the place.

My idea was just to have a checkbox next to the name of every student and then our editor could select say 5 of them and hit delete at the bottom and those users would be deleted.

Thanks for your code and suggestions, if it doesn't work, is there a simpler, different approach, I was thinking maybe have a form where the editor could add users to a new array which would then get deleted but my brain's a bit fried at the moment!

Link to comment
Share on other sites

Thanks Diogo, looking at your code again this morning with fresh(er) eyes!

If I try it line for line I get an error: "This page may not be deleted"

I'm also trying to figure out in that code how to check if it only deletes the ones that have been checked?

Link to comment
Share on other sites

good morning :)

You can confirm that only the checked students will be deleted by printing the array.

print_r($input->post->delete);

you have to adapt the code to your situation, I may have misinterpreted something. Looking at the code now, I notice that we are using $pages->get("template=students")->children(); to build the list, but $users->get($s)->delete(); to delete the page. This inconsistency came already from your code. If you want to delete users, you have to build the list by iterating through them, as I interpreted, you can do this foreach($users as $student); but it will include all users, including the superuser and guest.

You will have to find some way of leaving out all users that are not students. By having a checkbox on the users template, for instance, and then calling foreach($users->find("is_student=1") as $student);

Link to comment
Share on other sites

I think I have it (largely thanks to you Diogo)

<?php if($input->post->submit) {
    foreach ($input->post->delete as $s) {
		    $dp = $pages->find("template=student, name=$s");
		    $du = $users->find("name=$s");
		    if($dp){
				   foreach ($dp as $p) {
				    $pages->delete($p);
				   }
				    echo $p->name . " has been deleted!";
		    }else{
				    echo "I didn't find" . $p->name;
		    }
		    if($du){
				   foreach ($du as $u) {
				    $users->delete($u);
				   }
				    echo $u->name . " has been deleted!";
		    }else{
				    echo "I didn't find" . $u->name;
		    }
    }
}
?>
<form action="" method="post">
<ul>
<?php foreach ($pages->get("template=students")->children() as $student) { ?>
<li>
    <?php echo $student->name; ?>
    <input type="checkbox" name="delete[]" value="<?php echo $student->name; ?>">
</li>
<?php } ?>
<input type="submit" value="Delete selected users" name="submit">
</ul>
</form>

If anyone has any improvements, or friendly advice, it's always much appreciated. I can get lost for whole days trying to set these sort of things up and people like Diogo (and others) are vital to keep me rolling with PW.

I hope maybe in a year or so, my PHP and PW skills will be such that I'll be able to return the favour. I'm aware that it's pretty one-way at times!

Link to comment
Share on other sites

Your if() statements will always execute since a PageArray (returned by find) would never resolve to false. The find() could be replaced with a get(), assuming there is only one page with template=student per student. Also there are some out of scope variables, and what looks to be unnecessary foreach() loops. Lastly, the 'name' could use sanitization. I rewrite it like this:

if($input->post->submit) foreach($input->post->delete as $s) {
 $name = wire('sanitizer')->pageName($s);
 if(!$name || $name !== $s) continue; 
 $dp = $pages->get("template=student, name=$name"); 
 $du = $users->get($name); 
 foreach(array($dp, $du) as $item) {
   if(!$item->id) continue; 
   $pages->delete($item);
   echo "<p>$item->path has been deleted!</p>";
 }
}
  • Like 1
Link to comment
Share on other sites

Thanks Ryan, the two foreach loops were to have a separate message for the user being deleted and the user page being deleted but I guess that can be done without.

I thought that name would already be sanitized as it's coming from an existing page name?

Thanks for the tips, definitely more concise!

Link to comment
Share on other sites

Ryan is right, you build the html list with the name but that information will be exposed to the user before the post submission. Therefore it can be changed, and should be considered user input.

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