Advertisement
pmtpenza22

Untitled

Feb 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. <?php
  2.  
  3. namespace backend\controllers;
  4.  
  5. use Yii;
  6. use backend\models\Product;
  7. use backend\models\ProductSearch;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. use kartik\widgets\FileInput;
  12. use backend\models\ImagesManager;
  13. use yii\web\MethodNotAllowedHttpException;
  14.  
  15. /**
  16. * ProductController implements the CRUD actions for Product model.
  17. */
  18. class ProductController extends Controller
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function behaviors()
  24. {
  25. return [
  26. 'verbs' => [
  27. 'class' => VerbFilter::className(),
  28. 'actions' => [
  29. 'delete' => ['POST'],
  30. 'delete-image' => ['POST'],
  31. 'sort-image' => ['POST'],
  32. ],
  33. ],
  34. ];
  35. }
  36.  
  37. /**
  38. * Lists all Product models.
  39. * @return mixed
  40. */
  41. public function actionIndex()
  42. {
  43. $searchModel = new ProductSearch();
  44. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  45.  
  46. return $this->render('index', [
  47. 'searchModel' => $searchModel,
  48. 'dataProvider' => $dataProvider,
  49. ]);
  50. }
  51.  
  52. /**
  53. * Displays a single Product model.
  54. * @param integer $id
  55. * @return mixed
  56. * @throws NotFoundHttpException if the model cannot be found
  57. */
  58. public function actionView($id)
  59. {
  60. return $this->render('view', [
  61. 'model' => $this->findModel($id),
  62. ]);
  63. }
  64.  
  65. /**
  66. * Creates a new Product model.
  67. * If creation is successful, the browser will be redirected to the 'view' page.
  68. * @return mixed
  69. */
  70. public function actionCreate()
  71. {
  72.  
  73. $model = new Product();
  74.  
  75. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  76. return $this->redirect(['update', 'id' => $model->id]);
  77. }
  78. return $this->render('create', [
  79. 'model' => $model,
  80. 'images' => [],
  81. ]);
  82. }
  83.  
  84. /**
  85. * Updates an existing Product model.
  86. * If update is successful, the browser will be redirected to the 'view' page.
  87. * @param integer $id
  88. * @return mixed
  89. * @throws NotFoundHttpException if the model cannot be found
  90. */
  91. public function actionUpdate($id)
  92. {
  93. $model = $this->findModel($id);
  94.  
  95. /* $images = ImagesManager::find()->where(['product_id' => $id])->all();
  96. \yii\helpers\VarDumper::dump($images,10,true);exit;*/
  97.  
  98.  
  99. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  100. return $this->redirect(['view', 'id' => $model->id]);
  101. }
  102. return $this->render('update', [
  103. 'model' => $model,
  104. 'images' => ImagesManager::find()->where(['product_id' => $id])->all(),
  105. ]);
  106. }
  107.  
  108. /**
  109. * Deletes an existing Product model.
  110. * If deletion is successful, the browser will be redirected to the 'index' page.
  111. * @param integer $id
  112. * @return mixed
  113. * @throws NotFoundHttpException if the model cannot be found
  114. */
  115. public function actionDelete($id)
  116. {
  117. $this->findModel($id)->delete();
  118.  
  119. return $this->redirect(['index']);
  120. }
  121.  
  122. /**
  123. * Finds the Product model based on its primary key value.
  124. * If the model is not found, a 404 HTTP exception will be thrown.
  125. * @param integer $id
  126. * @return Product the loaded model
  127. * @throws NotFoundHttpException if the model cannot be found
  128. */
  129. protected function findModel($id)
  130. {
  131. if (($model = Product::findOne($id)) !== null) {
  132. return $model;
  133. }
  134.  
  135. throw new NotFoundHttpException('The requested page does not exist.');
  136. }
  137.  
  138. public function actionDeleteImage()
  139. {
  140. if(($model = ImagesManager::findOne(Yii::$app->request->post('key'))
  141. ) and $model->delete()){
  142. return true;
  143. }else{
  144. throw new NotFoundHttpException('Ошибка');
  145. }
  146. }
  147.  
  148. public function actionSortImage($id){
  149.  
  150. if(Yii::$app->request->isAjax) {
  151. $post = Yii::$app->request->post('sort');
  152. if($post['oldIndex'] > $post['newIndex']){
  153. $param = ['and',['>=','sort',$post['newIndex']],['<','sort',$post['oldIndex']]];
  154. $counter = 1;
  155. }else{
  156. $param = ['and',['<=','sort',$post['newIndex']],['>','sort',$post['oldIndex']]];
  157. $counter = -1;
  158. }
  159. ImagesManager::updateAllCounters(['sort'=>$counter], ['and',['class'=>'product','product_id'=>$id],$param]);
  160. ImagesManager::updateAll(['sort' => $post['newIndex']], ['id' => $post['stack'][$post['newIndex']]['key']]);
  161. return true;
  162. }
  163. throw new MethodNotAllowedHttpException();
  164. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement