pmtpenza

Untitled

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