66use yii \filters \VerbFilter ;
77use yii \web \Controller ;
88use yii \web \NotFoundHttpException ;
9+ use yii \web \UploadedFile ;
910use yii2mod \cms \models \CmsModel ;
1011use yii2mod \editable \EditableAction ;
1112use yii2mod \rbac \filters \AccessControl ;
@@ -42,6 +43,11 @@ class ManageController extends Controller
4243 */
4344 public $ modelClass = 'yii2mod\cms\models\CmsModel ' ;
4445
46+ /**
47+ * @var string model class name for attachment model
48+ */
49+ public $ attachmentModelClass = 'yii2mod\cms\models\AttachmentModel ' ;
50+
4551 /**
4652 * @inheritdoc
4753 */
@@ -55,6 +61,7 @@ public function behaviors()
5561 'create ' => ['get ' , 'post ' ],
5662 'update ' => ['get ' , 'post ' ],
5763 'delete ' => ['post ' ],
64+ 'image-upload ' => ['post ' ],
5865 ],
5966 ],
6067 'access ' => [
@@ -161,6 +168,28 @@ public function actionDelete($id)
161168 return $ this ->redirect (['index ' ]);
162169 }
163170
171+ /**
172+ * @return \yii\web\Response
173+ */
174+ public function actionFileUpload ()
175+ {
176+ $ model = Yii::createObject ($ this ->attachmentModelClass );
177+ $ model ->file = UploadedFile::getInstanceByName ('file ' );
178+
179+ if ($ model ->save ()) {
180+ $ result = [
181+ 'filelink ' => $ model ->getFileUrl (),
182+ 'filename ' => $ model ->getFileSelfName (),
183+ ];
184+ } else {
185+ $ result = [
186+ 'error ' => $ model ->getFirstError ('file ' ),
187+ ];
188+ }
189+
190+ return $ this ->asJson ($ result );
191+ }
192+
164193 /**
165194 * Finds the CmsModel model based on its primary key value.
166195 * If the model is not found, a 404 HTTP exception will be thrown.
0 commit comments