Skip to content

Commit

Permalink
Add the 'add' method in PostController.
Browse files Browse the repository at this point in the history
Create the add view and store the codes in app/view/posts/add.ctp file
Create the add_or_edit.ctp file in app/view/elements/
  • Loading branch information
stinkinkevin committed Oct 25, 2015
1 parent a9df142 commit 69bbaeb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
33 changes: 33 additions & 0 deletions app/Controller/PostController.php
Expand Up @@ -20,5 +20,38 @@ function index() {
// from the $posts variable
$this->set(compact('posts'));
}

// This function handles adding post data
function add() {
// Heading and slogan for the add view page
// This is necesaary because we are going to use
// a single element view to displaythe forms to add and edit posts
// elements in Cake enables you to reuse views
$actionHeading = 'Add a Post!';
$actionSLogan = 'Please fill in all fields. Feel free to add your post and express your opinion.';

$this->set(compact('actionHeading','actionSlogan'));

// Next, we check if the add post form has been submitted.
// If the form has not been submitted, the add view is displayed.
// If the submitted data ($this->data) is not empty,
// using the 'save' method of the Post model object,
// the application will attempt to create a new post record.
// The 'save' method automatically uses the validation rules defined
// in the Post Model to check the integrity of the sunmitted text.
// If the post does not pass the validation rules,
// the error message is set, using the 'setFlash' method of the 'Session' object.
// Otherwise, the post is saved to the database table,
// and the success message is set for display in the view.
if (!empty($this->data)){
$this->Post->create();
if ($this->Post->save($this->data)){
$this->Session->setFlash(_('The Post has been saved',true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(_('The Post could not be saved. Please try again.', true));
}
}
}
}
?>
26 changes: 26 additions & 0 deletions app/View/Elements/add_or_edit.ctp
@@ -0,0 +1,26 @@
<fieldset>
<legend>
<?php
__('Add a Post!');
?>
</legend>
Please fill in all fields.
<?php
echo $this->Form->create('Post');
echo $this->Form->error('Post.title');
echo $this->Form->input('Post.title',
array('id'=>'postitle',
'label'=>'Title:',
'size'=> 50,
'maxlength'=>255,
'error'=>false));
echo $this->Form->error('Post.content');
echo $this->Form->input('Post.content',
array('id'=>'postcontent',
'type'=>'textarea',
'label'=>'Content',
'rows'=>10,
'error'=>false));
echo $this->Form->end(array('label'=>'Submit Post'));
?>
</fieldset>
7 changes: 7 additions & 0 deletions app/View/Posts/add.ctp
@@ -0,0 +1,7 @@
<?php
// The $this->element accepts the name of a file stored
// in the view/elements folder (add_or_edit in this case),
// without the file extension (without .ctp).
// It simply transfers the content of add_or_edit.ctp into the add.ctp.
echo $this->element('add_or_edit');
?>
9 changes: 8 additions & 1 deletion app/View/Posts/index.ctp
Expand Up @@ -31,6 +31,7 @@
<td><?php echo $post['Post']['modified']; ?></td>
<td>
<?php if($post[ 'Post' ][ 'published' ] == 1) {
// This link will show: kevlog/post/disable/'id'
echo $this->Html->link('Publish', array('action'=>'disable', $post[ 'Post' ][ 'id' ]));
}else{
echo $this->Html->link('Unpublish', array('action'=>'enable', $post[ 'Post' ][ 'id' ]));
Expand All @@ -39,11 +40,17 @@
</td>
<td>
<?php
// This link will show: kevlog/post/edit/'id'
echo $this->Html->link('Edit', array('action'=>'edit', $post[ 'Post' ][ 'id' ]), null);
?>
</td>
<td>
<?php echo $this->Html->link(__('Delete', true), array('action'=>'delete', $post[ 'Post' ][ 'id' ]), null, sprintf(__('Are you sure you want to delete Post # %s?', true), $post[ 'Post' ][ 'id' ]));?>
<?php
// This link will show: kevlog/post/deletes/'id'
// And a dialog will pop up and ask "'"Are you sure ... Post 'id'?"
// with 2 options: OK and Cancel
echo $this->Html->link(__('Delete', true), array('action'=>'delete', $post[ 'Post' ][ 'id' ]), null, sprintf(__('Are you sure you want to delete Post # %s?', true), $post[ 'Post' ][ 'id' ]));
?>
</td>
</tr>
<?php endforeach;?>
Expand Down

0 comments on commit 69bbaeb

Please sign in to comment.