77 */
88class 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}
0 commit comments