Skip to content

Commit

Permalink
Изменение и удаление задач
Browse files Browse the repository at this point in the history
  • Loading branch information
sabian committed Jan 10, 2016
1 parent 189ca50 commit 2157ef2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
61 changes: 61 additions & 0 deletions controllers/TodoBackendController.php
Expand Up @@ -7,6 +7,16 @@
*/
class TodoBackendController extends BackController
{
public function filters()
{
return CMap::mergeArray(
parent::filters(),
[
'postOnly + delete',
]
);
}

public function actionIndex()
{
$model = new Todo('search');
Expand Down Expand Up @@ -38,4 +48,55 @@ public function actionCreate()

$this->render('create', ['model' => $model]);
}

public function actionUpdate($id)
{
$model = $this->loadModel($id);

if ($data = Yii::app()->getRequest()->getPost('Todo')) {

$model->setAttributes($data);

if ($model->update()) {
Yii::app()->user->setFlash(YFlashMessages::SUCCESS_MESSAGE, 'Задача успешно обновлена');

$submitType = Yii::app()->getRequest()->getPost('submit-type');

if (isset($submitType)) {
$this->redirect([$submitType]);
} else {
$this->redirect(['update', 'id' => $model->id]);
}
}
}

$this->render('update', ['model' => $model]);
}

public function actionDelete($id)
{
if ($this->loadModel($id)->delete()) {
Yii::app()->user->setFlash(YFlashMessages::SUCCESS_MESSAGE, 'Задача успешно удалена');

if (!Yii::app()->getRequest()->getParam('ajax')) {
$this->redirect((array)Yii::app()->getRequest()->getPost('returnUrl', 'index'));
}
}
}

/**
* @param $id
* @return Todo
* @throws CHttpException
*/
private function loadModel($id)
{
$model = Todo::model()->findByPk($id);

if ($model === null) {
throw new CHttpException(404, 'Запрошенная страница не найдена.');
}

return $model;
}
}
24 changes: 24 additions & 0 deletions views/todoBackend/update.php
@@ -0,0 +1,24 @@
<?php
/**
* @var $model Todo
*/

$this->pageTitle = 'ToDo - Изменение задачи';

$this->breadcrumbs = [
'Список задач' => ['/todo/todoBackend/index'],
$this->pageTitle
];

$this->menu = [
['icon' => 'fa fa-fw fa-list-alt', 'label' => 'Список задач', 'url' => ['/todo/todoBackend/index']],
['icon' => 'fa fa-fw fa-plus-square', 'label' => 'Создать задачу', 'url' => ['/todo/todoBackend/create']],
['icon' => 'fa fa-fw fa-pencil', 'label' => 'Изменить задачу', 'url' => ['/todo/todoBackend/update', 'id' => $model->id]],
];
?>
<div class="page-header">
<h1>Изменение задачи <small><?= $model->id ?></small></h1>
</div>

<?= $this->renderPartial('_form', ['model' => $model]); ?>

0 comments on commit 2157ef2

Please sign in to comment.