pmtpenza22

Untitled

Feb 18th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 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. 'images' => [],
  50. ]);
  51. }
  52.  
  53. /**
  54. * Displays a single Product model.
  55. * @param integer $id
  56. * @return mixed
  57. * @throws NotFoundHttpException if the model cannot be found
  58. */
  59. public function actionView($id)
  60. {
  61. return $this->render('view', [
  62. 'model' => $this->findModel($id),
  63. 'images' => [],
  64. ]);
  65. }
  66.  
  67. /**
  68. * Creates a new Product model.
  69. * If creation is successful, the browser will be redirected to the 'view' page.
  70. * @return mixed
  71. */
  72. public function actionCreate()
  73. {
  74.  
  75. $model = new Product();
  76.  
  77. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  78. return $this->redirect(['update', 'id' => $model->id]);
  79. }
  80. return $this->render('create', [
  81. 'model' => $model,
  82. 'images' => [],
  83. ]);
  84. }
  85.  
  86. /**
  87. * Updates an existing Product model.
  88. * If update is successful, the browser will be redirected to the 'view' page.
  89. * @param integer $id
  90. * @return mixed
  91. * @throws NotFoundHttpException if the model cannot be found
  92. */
  93. public function actionUpdate($id)
  94. {
  95. $model = $this->findModel($id);
  96.  
  97. /* $images = ImagesManager::find()->where(['product_id' => $id])->all();
  98. \yii\helpers\VarDumper::dump($images,10,true);exit;*/
  99.  
  100.  
  101. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  102. return $this->redirect(['view', 'id' => $model->id]);
  103. }
  104. return $this->render('update', [
  105. 'model' => $model,
  106. 'images' => ImagesManager::find()->where(['product_id' => $id])->all(),
  107. ]);
  108. }
  109.  
  110. /**
  111. * Deletes an existing Product model.
  112. * If deletion is successful, the browser will be redirected to the 'index' page.
  113. * @param integer $id
  114. * @return mixed
  115. * @throws NotFoundHttpException if the model cannot be found
  116. */
  117. public function actionDelete($id)
  118. {
  119. $this->findModel($id)->delete();
  120.  
  121. return $this->redirect(['index']);
  122. }
  123.  
  124. /**
  125. * Finds the Product model based on its primary key value.
  126. * If the model is not found, a 404 HTTP exception will be thrown.
  127. * @param integer $id
  128. * @return Product the loaded model
  129. * @throws NotFoundHttpException if the model cannot be found
  130. */
  131. protected function findModel($id)
  132. {
  133. if (($model = Product::findOne($id)) !== null) {
  134. return $model;
  135. }
  136.  
  137. throw new NotFoundHttpException('The requested page does not exist.');
  138. }
  139.  
  140. public function actionDeleteImage()
  141. {
  142. if(($model = ImagesManager::findOne(Yii::$app->request->post('key'))
  143. ) and $model->delete()){
  144. return true;
  145. }else{
  146. throw new NotFoundHttpException('Ошибка');
  147. }
  148. }
  149.  
  150. public function actionSortImage($id){
  151.  
  152. if(Yii::$app->request->isAjax) {
  153. $post = Yii::$app->request->post('sort');
  154. if($post['oldIndex'] > $post['newIndex']){
  155. $param = ['and',['>=','sort',$post['newIndex']],['<','sort',$post['oldIndex']]];
  156. $counter = 1;
  157. }else{
  158. $param = ['and',['<=','sort',$post['newIndex']],['>','sort',$post['oldIndex']]];
  159. $counter = -1;
  160. }
  161. ImagesManager::updateAllCounters(['sort'=>$counter], ['and',['class'=>'product','product_id'=>$id],$param]);
  162. ImagesManager::updateAll(['sort' => $post['newIndex']], ['id' => $post['stack'][$post['newIndex']]['key']]);
  163. return true;
  164. }
  165. throw new MethodNotAllowedHttpException();
  166. }}
Advertisement
Add Comment
Please, Sign In to add comment