Skip to content

Commit 2157ef2

Browse files
committed
Изменение и удаление задач
1 parent 189ca50 commit 2157ef2

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

controllers/TodoBackendController.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
*/
88
class TodoBackendController extends BackController
99
{
10+
public function filters()
11+
{
12+
return CMap::mergeArray(
13+
parent::filters(),
14+
[
15+
'postOnly + delete',
16+
]
17+
);
18+
}
19+
1020
public function actionIndex()
1121
{
1222
$model = new Todo('search');
@@ -38,4 +48,55 @@ public function actionCreate()
3848

3949
$this->render('create', ['model' => $model]);
4050
}
51+
52+
public function actionUpdate($id)
53+
{
54+
$model = $this->loadModel($id);
55+
56+
if ($data = Yii::app()->getRequest()->getPost('Todo')) {
57+
58+
$model->setAttributes($data);
59+
60+
if ($model->update()) {
61+
Yii::app()->user->setFlash(YFlashMessages::SUCCESS_MESSAGE, 'Задача успешно обновлена');
62+
63+
$submitType = Yii::app()->getRequest()->getPost('submit-type');
64+
65+
if (isset($submitType)) {
66+
$this->redirect([$submitType]);
67+
} else {
68+
$this->redirect(['update', 'id' => $model->id]);
69+
}
70+
}
71+
}
72+
73+
$this->render('update', ['model' => $model]);
74+
}
75+
76+
public function actionDelete($id)
77+
{
78+
if ($this->loadModel($id)->delete()) {
79+
Yii::app()->user->setFlash(YFlashMessages::SUCCESS_MESSAGE, 'Задача успешно удалена');
80+
81+
if (!Yii::app()->getRequest()->getParam('ajax')) {
82+
$this->redirect((array)Yii::app()->getRequest()->getPost('returnUrl', 'index'));
83+
}
84+
}
85+
}
86+
87+
/**
88+
* @param $id
89+
* @return Todo
90+
* @throws CHttpException
91+
*/
92+
private function loadModel($id)
93+
{
94+
$model = Todo::model()->findByPk($id);
95+
96+
if ($model === null) {
97+
throw new CHttpException(404, 'Запрошенная страница не найдена.');
98+
}
99+
100+
return $model;
101+
}
41102
}

views/todoBackend/update.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* @var $model Todo
4+
*/
5+
6+
$this->pageTitle = 'ToDo - Изменение задачи';
7+
8+
$this->breadcrumbs = [
9+
'Список задач' => ['/todo/todoBackend/index'],
10+
$this->pageTitle
11+
];
12+
13+
$this->menu = [
14+
['icon' => 'fa fa-fw fa-list-alt', 'label' => 'Список задач', 'url' => ['/todo/todoBackend/index']],
15+
['icon' => 'fa fa-fw fa-plus-square', 'label' => 'Создать задачу', 'url' => ['/todo/todoBackend/create']],
16+
['icon' => 'fa fa-fw fa-pencil', 'label' => 'Изменить задачу', 'url' => ['/todo/todoBackend/update', 'id' => $model->id]],
17+
];
18+
?>
19+
<div class="page-header">
20+
<h1>Изменение задачи <small>№<?= $model->id ?></small></h1>
21+
</div>
22+
23+
<?= $this->renderPartial('_form', ['model' => $model]); ?>
24+

0 commit comments

Comments
 (0)