pmtpenza

Untitled

Jul 16th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. <?php
  2.  
  3. namespace backend\controllers;
  4.  
  5. use Yii;
  6. use backend\models\Deals;
  7. use backend\models\DealsSearch;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. use backend\models\Client;
  12. use yii\web\Response;
  13. use backend\models\Order;
  14. use backend\models\OrderForm;
  15. use backend\models\TypeZakaz;
  16.  
  17. /**
  18. * DealsController implements the CRUD actions for Deals model.
  19. */
  20. class DealsController extends Controller
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function behaviors()
  26. {
  27. return [
  28. 'verbs' => [
  29. 'class' => VerbFilter::className(),
  30. 'actions' => [
  31. 'delete' => ['POST'],
  32. ],
  33. ],
  34. ];
  35. }
  36.  
  37. /**
  38. * Lists all Deals models.
  39. * @return mixed
  40. */
  41. public function actionIndex()
  42. {
  43. $searchModel = new DealsSearch();
  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 Deals 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 Deals 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. $model = new Deals();
  73. $orders = [new OrderForm()];
  74. if ($model->load(Yii::$app->request->post()) && $model->save())
  75. {
  76.  
  77. foreach(Yii::$app->request->post('OrderForm', []) as $form) {
  78. foreach ($form['type_zakaz_id'] as $service) {
  79. $order = new Order();
  80. $order->type_zakaz_id =$form['type_zakaz_id'];
  81. $order->link('deals', $model);
  82. }
  83. }
  84.  
  85. return $this->redirect(['view', 'id' => $model->id]);
  86. }
  87. return $this->render('create', [
  88. 'model' => $model,
  89. 'orders' => $orders,
  90. ]);
  91. }
  92.  
  93. /**
  94. * Updates an existing Deals model.
  95. * If update is successful, the browser will be redirected to the 'view' page.
  96. * @param integer $id
  97. * @return mixed
  98. * @throws NotFoundHttpException if the model cannot be found
  99. */
  100. public function actionUpdate($id)
  101. {
  102. $model = $this->findModel($id);
  103.  
  104. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  105. return $this->redirect(['view', 'id' => $model->id]);
  106. }
  107.  
  108. return $this->render('update', [
  109. 'model' => $model,
  110. ]);
  111. }
  112.  
  113. /**
  114. * Deletes an existing Deals model.
  115. * If deletion is successful, the browser will be redirected to the 'index' page.
  116. * @param integer $id
  117. * @return mixed
  118. * @throws NotFoundHttpException if the model cannot be found
  119. */
  120. public function actionDelete($id)
  121. {
  122. $this->findModel($id)->delete();
  123.  
  124. return $this->redirect(['index']);
  125. }
  126.  
  127. /**
  128. * Finds the Deals model based on its primary key value.
  129. * If the model is not found, a 404 HTTP exception will be thrown.
  130. * @param integer $id
  131. * @return Deals the loaded model
  132. * @throws NotFoundHttpException if the model cannot be found
  133. */
  134. protected function findModel($id)
  135. {
  136. if (($model = Deals::findOne($id)) !== null) {
  137. return $model;
  138. }
  139.  
  140. throw new NotFoundHttpException('The requested page does not exist.');
  141. }
  142. public function actionGetClient($q = null)
  143. {
  144. Yii::$app->response->format = Response::FORMAT_JSON;
  145. $query = Client::find()
  146. ->asArray()
  147. ->select(['id', 'CONCAT(client.company, " , ", client.firstname) AS text']);
  148. $query->andFilterWhere(['or',
  149. ['like', 'client.firstname', $q],
  150. ['like', 'client.company', $q],
  151. ]);
  152.  
  153. $query->orderBy('client.company ASC');
  154.  
  155. $data = $query->limit(10)->all();
  156.  
  157. $out['results'] = array_values($data);
  158.  
  159. return $out;
  160. }
  161. public function actionGetType($q = null, $type_zakaz_id = null)
  162. {
  163. $response = Yii::$app->response;
  164. $response->format = \yii\web\Response::FORMAT_JSON;
  165.  
  166. $query = TypeZakaz::find()
  167. ->asArray()
  168. ->select(['type_zakaz.id', 'type_zakaz.type AS text']);
  169. if ($type_zakaz_id) {
  170. $query->join('join', 'type_zakaz rs', 'type_zakaz.id = rs.type_zakaz_id')
  171. ->andWhere(['rs.id' => $type_zakaz_id]);
  172. }
  173.  
  174.  
  175. $query->andFilterWhere(['like', 'type_zakaz.type', $q]);
  176.  
  177. $data = $query->orderBy('type_zakaz.type ASC')->limit(20)->all();
  178.  
  179. $out['results'] = array_values($data);
  180.  
  181. return $out;
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment