|
|
|
## is52027c lab 18
|
|
|
|
|
|
|
|
## editing & deleting
|
|
|
|
|
|
|
|
today we will add edit & delete to the blog, so we have the full set of CRUD functions implement through our Blog_Entry_Table class.
|
|
|
|
|
|
|
|
# step-6
|
|
|
|
|
|
|
|
## admin links
|
|
|
|
|
|
|
|
you will notice that in my example code for step 5, the list of admin entries are actually links, constructed in the file views/admin/entries-html.php:
|
|
|
|
```
|
|
|
|
$href = "admin.php?page=editor&id=$entry->entry_id";
|
|
|
|
$entriesAsHTML .= "<li><a href='$href'>$entry->title</a></li>";
|
|
|
|
```
|
|
|
|
|
|
|
|
so the link will point back to the admin front controller, telling it to redirect to the editor and also to pass on the id of the entry that was clicked.
|
|
|
|
|
|
|
|
if you aren't already using this same form of linking when listing your entries on the admin side, add it now. we will need the entry id for editing and deleting.
|
|
|
|
|
|
|
|
check that you are getting the right form of url when you click on your links.
|
|
|
|
|
|
|
|
## pre-loading the form
|
|
|
|
|
|
|
|
the first job the id does is to tell us whether or not we need an empty form. if there's no id in the url calling the editor, then you're creating a new entry, as you have been able to do since step 1.
|
|
|
|
|
|
|
|
but if there is an id, you want to load that entry in to the form so you can edit it or delete it.
|
|
|
|
|
|
|
|
we'll re-write the form in views/admin/editor-html.php on the basis that there might be values for the entry, and these are stored in a standard object (that is, an object of the class StdClass()) called $entryData.
|
|
|
|
|
|
|
|
so the lines
|
|
|
|
```
|
|
|
|
<input type='text' name='title' maxlength='150' />
|
|
|
|
<textarea name='entry'></textarea>
|
|
|
|
```
|
|
|
|
become
|
|
|
|
```
|
|
|
|
<input type='text' name='title' maxlength='150' value='$entryData->title' required />
|
|
|
|
<textarea name='entry'>$entryData->entry_text</textarea>
|
|
|
|
```
|
|
|
|
|
|
|
|
we'll also add a hidden input field that passes the entry id back with the form (this will come in handy in a minute):
|
|
|
|
```
|
|
|
|
<input type='hidden' name='id' value='$entryData->entry_id' />
|
|
|
|
```
|
|
|
|
|
|
|
|
if we use this view as it is it will throw an error because our $entryData object is undefined.
|
|
|
|
|
|
|
|
so we want some if/else logic that will load the entry data if there is an id and sets $entryData to null if not.
|
|
|
|
|
|
|
|
We already have the method we need in the Blog_Entry_Table; the getEntry method. if an id has been passed back to the form, we use that to get the data for the entry.
|
|
|
|
|
|
|
|
in the controllers/admin/editor.php, we will check if there is an id:
|
|
|
|
```
|
|
|
|
$entryRequested = isset( $_GET['id'] );
|
|
|
|
```
|
|
|
|
|
|
|
|
if so, we will set the values:
|
|
|
|
```
|
|
|
|
if ( $entryRequested ) {
|
|
|
|
$id = $_GET['id'];
|
|
|
|
$entryData = $entryTable->getEntry( $id );
|
|
|
|
$entryData->entry_id = $id;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
if not we set them to zero/null:
|
|
|
|
```
|
|
|
|
else {
|
|
|
|
$entryData = new StdClass();
|
|
|
|
$entryData->entry_id = 0;
|
|
|
|
$entryData->title = "";
|
|
|
|
$entryData->entry_text = "";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# step-7
|
|
|
|
|
|
|
|
## deleting entries
|
|
|
|
|
|
|
|
now we're able to get hold of the entries, we're going to figure out how to delete them.
|
|
|
|
|
|
|
|
our table data gateway (i.e. the Blog_Entry_Table) handles database interactions. so we need a deleteEntry method.
|
|
|
|
|
|
|
|
you know how to write an SQL statement to delete an entry in a database.
|
|
|
|
|
|
|
|
as before, we will use PDO prepared statements:
|
|
|
|
```
|
|
|
|
public function deleteEntry ( $id ) {
|
|
|
|
$sql = "DELETE FROM blog_entry WHERE entry_id = ?";
|
|
|
|
$data = array( $id );
|
|
|
|
$statement = $this->makeStatement( $sql, $data );
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
we will also need to add a delete button to our edit form, which only saves entries at the moment.
|
|
|
|
|
|
|
|
```
|
|
|
|
<input type='submit' name='action' value='delete' />
|
|
|
|
```
|
|
|
|
|
|
|
|
so now, when the form is submitted, the value of $_POST['action'] will either be 'save' or delete'.
|
|
|
|
we need to update our editor controller to deal with this.
|
|
|
|
|
|
|
|
in controllers/admin/editor.php we will check if the delete button was clicked, and also grab the value of id
|
|
|
|
```
|
|
|
|
$deleteEntry = ($buttonClicked === 'delete');
|
|
|
|
$id = $_POST['id'];
|
|
|
|
```
|
|
|
|
|
|
|
|
now we can use our new Blog_Entry_Class method to delete the entry:
|
|
|
|
so below the $insertNewEntry option, we add
|
|
|
|
```
|
|
|
|
else if ( $deleteEntry ) {
|
|
|
|
$entryTable->deleteEntry( $id );
|
|
|
|
```
|
|
|
|
|
|
|
|
# step-8
|
|
|
|
|
|
|
|
## update method
|
|
|
|
|
|
|
|
OK, we can delete entries. How about updating them?
|
|
|
|
|
|
|
|
Clearly we're a lot of the way there; we can load an entry from the database in to our editing form.
|
|
|
|
|
|
|
|
But updating an entry is a different interaction with the database, so we need a new method in our Blog_Entry_Table.
|
|
|
|
|
|
|
|
After we've loaded the entry data in to the form we will modify it then submit the form.
|
|
|
|
|
|
|
|
Recall the form of SQL used to update a database entry, and write a method using a PDO prepared statement to update the entry with entry_id = $id with the changed data in the form. You'll need to pass it $id, $title & $entry as parameters.
|
|
|
|
|
|
|
|
It should look something like this:
|
|
|
|
```
|
|
|
|
public function updateEntry ( $id, $title, $entry) {
|
|
|
|
$sql = "UPDATE blog_entry
|
|
|
|
SET title = ?,
|
|
|
|
entry_text = ?
|
|
|
|
WHERE entry_id = ?";
|
|
|
|
$data = array( $title, $entry, $id );
|
|
|
|
$statement = $this->makeStatement( $sql, $data) ;
|
|
|
|
return $statement;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## controller: insert or update?
|
|
|
|
|
|
|
|
when it receives the content of the form, the editor controller needs to decide whether to add a new entry or update an existing one.
|
|
|
|
|
|
|
|
This is where our hidden field in the form comes in handy. Remeber that we pass back the entry_id as the hidden field 'id', and this either has an actual id (if the form pulled data from an existing entry) or is set to zero if no prior entry was requested (i.e. if the form was initially blank).
|
|
|
|
|
|
|
|
So the logic in the controller is something like:
|
|
|
|
* if the save button was clicked and id=0, it's a new entry, so INSERT it
|
|
|
|
* if the save button was clicked, but id != 0, it's an edited entry, so UPDATE it
|
|
|
|
* if the delete button was clicked, DELETE it
|
|
|
|
|
|
|
|
we're setting flags for the editor action; in controllers/admin/editor.php add this after the tests for insert and delete:
|
|
|
|
|
|
|
|
```
|
|
|
|
$updateEntry = ( $save and $insertNewEntry === false );
|
|
|
|
```
|
|
|
|
|
|
|
|
Then we can add this as a third option in our action logic; after if ( $insertNewEntry ) we can add
|
|
|
|
```
|
|
|
|
else if ( $updateEntry ){
|
|
|
|
$entryTable->updateEntry( $id, $title, $entry );
|
|
|
|
$savedEntryId = $id;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
You should now have a blog admin that allows you to create, update and delete blog entries. Hooray!
|
|
|
|
|
|
|
|
# step-9
|
|
|
|
|
|
|
|
One obvious problem with the way the blog makes these CRUD changes is the lack of feedback about what happened. The form is submitted and we just have to assume that the desired action has taken place.
|
|
|
|
|
|
|
|
We'll add some messages to make the process more transparent. We'll also reload a saved or edited entry in to the form, to confirm to the user the results of their action.
|
|
|
|
|
|
|
|
This will require small changes to model, controller and view.
|
|
|
|
|
|
|
|
## model
|
|
|
|
|
|
|
|
We already know the id for the edited entry, but reload the saved entry we need to know its id. We can get this from a PDO method that gives us the last inserted id. Add this at the end of the saveEntry method in your Blog_Entry_Class:
|
|
|
|
```
|
|
|
|
return $this->db->lastInsertId();
|
|
|
|
```
|
|
|
|
|
|
|
|
## controller
|
|
|
|
|
|
|
|
In the controller we'll store the id of the saved or updated entry, so we can pull the data back in to the form. Instead of just executing the save method, we store the returned id in a variable called $savedEntryId.
|
|
|
|
So we change
|
|
|
|
```
|
|
|
|
$entryTable->saveEntry( $title, $entry );
|
|
|
|
```
|
|
|
|
to
|
|
|
|
```
|
|
|
|
$savedEntryId = $entryTable->saveEntry( $title, $entry );
|
|
|
|
```
|
|
|
|
If the entry was updated rather than saved, we simply need the current id, so in that case
|
|
|
|
```
|
|
|
|
$savedEntryId = $id;
|
|
|
|
```
|
|
|
|
|
|
|
|
Overall, the controller logic should look like this:
|
|
|
|
```
|
|
|
|
if ( $insertNewEntry ) {
|
|
|
|
$savedEntryId = $entryTable->saveEntry( $title, $entry );
|
|
|
|
} else if ( $updateEntry ){
|
|
|
|
$entryTable->updateEntry( $id, $title, $entry );
|
|
|
|
$savedEntryId = $id;
|
|
|
|
} else if ( $deleteEntry ) {
|
|
|
|
$entryTable->deleteEntry( $id );
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Before calling the view, the controller then reloads the appropriate data in to the $entryData object.
|
|
|
|
We set a flag for the fact that an entry has been saved:
|
|
|
|
```
|
|
|
|
$entrySaved = isset( $savedEntryId );
|
|
|
|
```
|
|
|
|
|
|
|
|
We'll also add a message to say the entry was saved.
|
|
|
|
```
|
|
|
|
if ( $entrySaved ) {
|
|
|
|
$entryData = $entryTable->getEntry( $savedEntryId );
|
|
|
|
$entryData->message = "Entry was saved";
|
|
|
|
```
|
|
|
|
For the other cases, where we show the form for the first time, or where we are loading an entry in to the form for editing, the message should be blank; i.e. $entryData->message = "";
|
|
|
|
|
|
|
|
The last part of controller/admin/editor.php, before including the view, should like like this:
|
|
|
|
```
|
|
|
|
if ( $entryRequested ) {
|
|
|
|
$id = $_GET['id'];
|
|
|
|
$entryData = $entryTable->getEntry( $id );
|
|
|
|
$entryData->entry_id = $id;
|
|
|
|
$entryData->message = "";
|
|
|
|
} else if ( $entrySaved ) {
|
|
|
|
$entryData = $entryTable->getEntry( $savedEntryId );
|
|
|
|
$entryData->message = "Entry was saved";
|
|
|
|
} else {
|
|
|
|
$entryData = new StdClass();
|
|
|
|
$entryData->entry_id = 0;
|
|
|
|
$entryData->title = "";
|
|
|
|
$entryData->entry_text = "";
|
|
|
|
$entryData->message = "";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## view
|
|
|
|
|
|
|
|
All that's left is to show the message, so for this we edit the view.
|
|
|
|
|
|
|
|
Add
|
|
|
|
```
|
|
|
|
<p id='editor-message'>$entryData->message</p>
|
|
|
|
```
|
|
|
|
|
|
|
|
Here endeth the lab. |
|
|
|
\ No newline at end of file |