第6回ではタスクの追加機能を実装しました。今回は以下の機能を実装します。
- タスクの完了
- タスクの削除
- タスクの編集
- 完了と未完了のタスクを分離
Tasksコントローラの修正
Tasksコントローラのindexアクションを修正し、
<?php
class TasksController extends AppController {
var $name = 'Tasks';
var $uses = array('Task');
function index() {
$this->set('yet_tasks', $this->Task->findAllByStatus('yet', null, 'Task.created ASC'));
$this->set('done_tasks', $this->Task->findAllByStatus('done', null, 'Task.modified DESC'));
}
function add() {
if (!empty($this->data)) {
if ($this->Task->save($this->data, true, array('content', 'created', 'modified'))) {
$this->flash('タスクが追加されました', '/tasks');
return;
}
}
$this->redirect('/tasks');
}
function done($id) {
if ($this->Task->findById($id)) {
$this->Task->id = $id;
$this->Task->save(array('status' => 'done'));
}
$this->redirect('/tasks');
}
function edit($id) {
$task = $this->Task->findById($id);
if (!$task) {
$this->redirect('/tasks');
return;
}
if (!empty($this->data)) {
$task['Task']['content'] = $this->data['Task']['content'];
$this->Task->save($task);
}
$this->set('task', $task);
}
function del($id) {
$this->Task->del($id);
$this->redirect('/tasks');
}
}
indexビューの修正
indexビューは使用する変数が変わり、
<form action="<?php echo h($html->url('/tasks/add')) ?>" method="post" style="margin-bottom:1em">
<p><?php echo $html->input('Task/content') ?>
<?php echo $html->submit('タスクを追加') ?></p>
</form>
<h2>未完了タスク</h2>
<table>
<tr>
<th>Id</th>
<th>タスク内容</th>
<th>状態</th>
<th>操作</th>
<th>作成日</th>
</tr>
<?php foreach ($yet_tasks as $task) { ?>
<tr>
<td><?php echo h($task['Task']['id']) ?></td>
<td><?php echo h($task['Task']['content']) ?></td>
<td><?php echo h($task['Task']['status']) ?></td>
<td>
<?php echo $html->link('完了', '/tasks/done/' . $task['Task']['id'], null, '完了してもよろしいですか?') ?>
<?php echo $html->link('編集', '/tasks/edit/' . $task['Task']['id']) ?>
<?php echo $html->link('削除', '/tasks/del/' . $task['Task']['id'], null, '削除してもよろしいですか?') ?>
</td>
<td><?php echo h($task['Task']['created']) ?></td>
</tr>
<?php } ?>
</table>
<h2>完了タスク</h2>
<table>
<tr>
<th>Id</th>
<th>タスク内容</th>
<th>状態</th>
<th>操作</th>
<th>作成日</th>
</tr>
<?php foreach ($done_tasks as $task) { ?>
<tr>
<td><?php echo h($task['Task']['id']) ?></td>
<td><?php echo h($task['Task']['content']) ?></td>
<td><?php echo h($task['Task']['status']) ?></td>
<td><?php echo $html->link('削除', '/tasks/del/' . $task['Task']['id'], null, '削除してもよろしいですか?') ?></td>
<td><?php echo h($task['Task']['created']) ?></td>
</tr>
<?php } ?>
</table>
editビューの作成
editアクション用のビューとしてapp/
<h1>タスクの編集</h1>
<p><a href="<?php echo h($html->url('/tasks')) ?>">タスク一覧へ戻る</a></p>
<form action="<?php echo h($html->url('/tasks/edit/' . $task['Task']['id'])) ?>" method="post">
<?php echo $html->hidden('Task/id', $task['Task']['id']) ?>
<h2>内容</h2>
<p><?php echo $html->textarea('Task/content', array('cols' => '60', 'rows' => '3', 'value' => $task['Task']['content'])) ?></p>
<p><input type="submit" value="保存"></p>
修正内容の解説:コントローラ編
以上でタスクの完了、
indexアクション:ステータスを指定してタスクを取得する
function index() {
$this->set('yet_tasks', $this->Task->findAllByStatus('yet', null, 'Task.created ASC'));
$this->set('done_tasks', $this->Task->findAllByStatus('done', null, 'Task.modified DESC'));
}
findAllBy[フィールド名]メソッドによってstatusフィールドの値を指定してレコードを取得しています。
doneアクション:タスクを完了する
タスクの完了は
function done($id) {
アクションの定義に引数を設定すると
if ($this->Task->findById($id)) {
$this->Task->id = $id;
$this->Task->save(array('status' => 'done'));
}
このブロックでは、
$this->redirect('/tasks');
タスクの完了画面は出さずに即座にタスク一覧に戻すため、
editアクション:編集画面の表示と編集内容の保存を行う
editアクションは少々複雑で、
function edit($id) {
$task = $this->Task->findById($id);
if (!$task) {
$this->redirect('/tasks');
return;
}
引数で渡されてきたidでデータ取得を試みて、
if (!empty($this->data)) {
$task['Task']['content'] = $this->data['Task']['content'];
$this->Task->save($task);
}
データの存在チェックが通り、
$this->set('task', $task);
最後にビューへTaskモデルのデータを渡して終了です。
delアクション:タスクを削除する
delアクションはタスクを削除し、
function del($id) {
$this->Task->del($id);
$this->redirect('/tasks');
}
データの削除はモデルのdelメソッドにidを渡すことで行います。
ちなみにここではあえてデータの存在チェックを行っていません。削除行為自体はデータがあってもなくても行えるため、
修正内容の解説:ビュー編
indexビュー:完了と未完了を分け、各機能へのリンクを設置
indexビューでは、
<td>
<?php echo $html->link('完了', '/tasks/done/' . $task['Task']['id'], null, '完了してもよろしいですか?') ?>
<?php echo $html->link('編集', '/tasks/edit/' . $task['Task']['id']) ?>
<?php echo $html->link('削除', '/tasks/del/' . $task['Task']['id'], null, '削除してもよろしいですか?') ?>
</td>
ここではHTMLヘルパーのlinkメソッドを使用してリンクを表示しています。完了と削除のリンクでは、
余談ですがアプリケーション設計の1つの考え方として、
editビュー:タスク内容の修正フォーム
editビューではタスク内容の修正フォームを表示しています。
<p><a href="<?php echo h($html->url('/tasks')) ?>">タスク一覧へ戻る</a></p>
タスク一覧表示へのリンクです。HTMLヘルパーのlinkメソッドを使わず、
<form action="<?php echo h($html->url('/tasks/edit/' . $task['Task']['id'])) ?>" method="post">
formの開始タグです。
<h2>内容</h2>
<p><?php echo $html->textarea('Task/content', array('cols' => '60', 'rows' => '3', 'value' => $task['Task']['content'])) ?></p>
HTMLヘルパーのtextareaメソッドを使用してtextarea要素を表示しています。
<p><input type="submit" value="Save"></p>
Submitボタンの表示です。ここでもHTMLヘルパーのsubmitメソッドを使わずにSubmitボタンを表示していますが、
修正したアプリケーションの動作を確認する
修正したアプリケーションの動作を確認します。図1→3のような画面で遷移できれば修正は完了です。



今回はコードの書き方に統一性がありませんでしたが、
次回予定はタスク追加のAjax化です。