第6回ではタスクの追加機能を実装しました。今回は以下の機能を実装します。
- タスクの完了
- タスクの削除
- タスクの編集
- 完了と未完了のタスクを分離
Tasksコントローラの修正
Tasksコントローラのindexアクションを修正し、 indexビューは使用する変数が変わり、 editアクション用のビューとしてapp/ 以上でタスクの完了、 findAllBy[フィールド名]メソッドによってstatusフィールドの値を指定してレコードを取得しています。 タスクの完了は アクションの定義に引数を設定すると このブロックでは、 タスクの完了画面は出さずに即座にタスク一覧に戻すため、 editアクションは少々複雑で、 引数で渡されてきたidでデータ取得を試みて、 データの存在チェックが通り、 最後にビューへTaskモデルのデータを渡して終了です。 delアクションはタスクを削除し、 データの削除はモデルのdelメソッドにidを渡すことで行います。 ちなみにここではあえてデータの存在チェックを行っていません。削除行為自体はデータがあってもなくても行えるため、 indexビューでは、 ここではHTMLヘルパーのlinkメソッドを使用してリンクを表示しています。完了と削除のリンクでは、 余談ですがアプリケーション設計の1つの考え方として、 editビューではタスク内容の修正フォームを表示しています。 タスク一覧表示へのリンクです。HTMLヘルパーのlinkメソッドを使わず、 formの開始タグです。 HTMLヘルパーのtextareaメソッドを使用してtextarea要素を表示しています。 Submitボタンの表示です。ここでもHTMLヘルパーのsubmitメソッドを使わずにSubmitボタンを表示していますが、 修正したアプリケーションの動作を確認します。図1→3のような画面で遷移できれば修正は完了です。 今回はコードの書き方に統一性がありませんでしたが、 次回予定はタスク追加のAjax化です。<?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ビューの修正
<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ビューの作成
<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'));
}
doneアクション:タスクを完了する
function done($id) {
if ($this->Task->findById($id)) {
$this->Task->id = $id;
$this->Task->save(array('status' => 'done'));
}
$this->redirect('/tasks');
editアクション:編集画面の表示と編集内容の保存を行う
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);
delアクション:タスクを削除する
function del($id) {
$this->Task->del($id);
$this->redirect('/tasks');
}
修正内容の解説:ビュー編
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>
editビュー:タスク内容の修正フォーム
<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">
<h2>内容</h2>
<p><?php echo $html->textarea('Task/content', array('cols' => '60', 'rows' => '3', 'value' => $task['Task']['content'])) ?></p>
<p><input type="submit" value="Save"></p>
修正したアプリケーションの動作を確認する