| Current Path : /var/www/html/learn/backend/controllers/frontend/ |
| Current File : /var/www/html/learn/backend/controllers/frontend/FrontendGalleryItemController.php |
<?php
namespace backend\controllers\frontend;
use Yii;
use backend\models\frontendgalleryitem\FrontendGalleryItem;
use backend\models\frontendgalleryitem\FrontendGalleryItemSearch;
use backend\models\frontendgallery\FrontendGallery;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;
use common\components\AccessRule;
/**
* FrontendGalleryItemController implements the CRUD actions for FrontendGalleryItem model.
*/
class FrontendGalleryItemController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'ruleConfig' => [
'class' => AccessRule::className(),
],
'rules' => [
[
'actions' => ['index', 'create', 'update', 'view', 'delete'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all FrontendGalleryItem models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new FrontendGalleryItemSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single FrontendGalleryItem model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new FrontendGalleryItem model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new FrontendGalleryItem();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->gallery_item_id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing FrontendGalleryItem model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if (Yii::$app->request->isPost) {
$request = Yii::$app->request;
Yii::$app->db->createCommand()
->update('frontend_gallery_item', ['item_order' => $request->post('FrontendGalleryItem')['item_order']], ['gallery_item_id' => $id])
->execute();
$modelGallery = FrontendGallery::find()->where(['gallery_id'=>$model->gallery_id])->asArray()->one();
return $this->redirect(['index', 'FrontendGalleryItemSearch[gallery_id]' => $model->gallery_id, 'gallery_name' => $modelGallery['gallery_name']]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing FrontendGalleryItem model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
$modelGallery = FrontendGallery::find()->where(['gallery_id'=>$model->gallery_id])->asArray()->one();
$filename = $model->item_path . $model->item_name;
if(file_exists($filename)){
unlink($filename);
}
$this->findModel($id)->delete();
return $this->redirect(['index', 'FrontendGalleryItemSearch[gallery_id]'=>$modelGallery['gallery_id'], 'gallery_name'=>$modelGallery['gallery_name']]);
}
public function actionUpload()
{
$model = new FrontendGalleryItem();
if (Yii::$app->request->isPost) {
$model->load(Yii::$app->request->post());
$model->item_name = UploadedFile::getInstance($model, 'item_name');
$result = $model->upload();
if ($result) {
$modelGallery = FrontendGallery::find()->where(['gallery_id'=>$model->gallery_id])->asArray()->one();
return $this->redirect(['index', 'FrontendGalleryItemSearch[gallery_id]' => $model->gallery_id, 'gallery_name' => $modelGallery['gallery_name']]);
}
}
else{
return $this->render('upload', ['model' => $model]);
}
}
/**
* Finds the FrontendGalleryItem model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return FrontendGalleryItem the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = FrontendGalleryItem::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}