pmtpenza

Untitled

Jun 29th, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. <?php
  2.  
  3. namespace backend\controllers;
  4.  
  5. use Yii;
  6. use backend\models\Zayvki;
  7. use backend\models\ZayvkiSearch;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. use backend\models\Order;
  12. use backend\models\OrderForm;
  13. use backend\models\Category;
  14. use backend\models\Uslugi;
  15.  
  16. /**
  17. * ZayvkiController implements the CRUD actions for Zayvki model.
  18. */
  19. class ZayvkiController extends Controller
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function behaviors()
  25. {
  26. return [
  27. 'verbs' => [
  28. 'class' => VerbFilter::className(),
  29. 'actions' => [
  30. 'delete' => ['POST'],
  31. ],
  32. ],
  33. ];
  34. }
  35.  
  36. /**
  37. * Lists all Zayvki models.
  38. * @return mixed
  39. */
  40. public function actionIndex()
  41. {
  42. $searchModel = new ZayvkiSearch();
  43. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  44.  
  45. return $this->render('index', [
  46. 'searchModel' => $searchModel,
  47. 'dataProvider' => $dataProvider,
  48. ]);
  49. }
  50.  
  51. /**
  52. * Displays a single Zayvki model.
  53. * @param integer $id
  54. * @return mixed
  55. * @throws NotFoundHttpException if the model cannot be found
  56. */
  57. public function actionView($id)
  58. {
  59. return $this->render('view', [
  60. 'model' => $this->findModel($id),
  61. ]);
  62. }
  63.  
  64. /**
  65. * Creates a new Zayvki model.
  66. * If creation is successful, the browser will be redirected to the 'view' page.
  67. * @return mixed
  68. */
  69. public function actionCreate()
  70. {
  71. $model = new Zayvki();
  72.  
  73. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  74. $order = new Order();
  75. $order->load(Yii::$app->request->post());
  76. $order->uslugi_id = Yii::$app->request->post('Zayvki')['uslugi_id'];
  77. $order->hours = Yii::$app->request->post('Zayvki')['hours'];
  78. $order->date = Yii::$app->request->post('Zayvki')['date'];
  79. $order->category_id = 3;
  80. $order->link('zayvki', $model);
  81. return $this->redirect(['view', 'id' => $model->id]);
  82. }
  83.  
  84.  
  85. return $this->render('create', [
  86. 'model' => $model,
  87. ]);
  88. }
  89.  
  90. public function actionCreateiko()
  91. {
  92. $model = new Zayvki();
  93. $orders = [new OrderForm()];
  94. if ($model->load(Yii::$app->request->post()) && $model->save())
  95. {
  96.  
  97. foreach(Yii::$app->request->post('OrderForm', []) as $form) {
  98. foreach ($form['uslugi_id'] as $service) {
  99. $order = new Order();
  100. $order->category_id =$form['category_id'];
  101. $order->uslugi_id = $service;
  102. $order->link('zayvki', $model);
  103. }
  104. }
  105.  
  106. return $this->redirect(['view', 'id' => $model->id]);
  107. }
  108. return $this->render('createiko', [
  109. 'model' => $model,
  110. 'orders' => $orders,
  111. ]);
  112. }
  113.  
  114.  
  115. /**
  116. * Updates an existing Zayvki model.
  117. * If update is successful, the browser will be redirected to the 'view' page.
  118. * @param integer $id
  119. * @return mixed
  120. * @throws NotFoundHttpException if the model cannot be found
  121. */
  122. public function actionUpdate($id)
  123. {
  124. $model = $this->findModel($id);
  125.  
  126. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  127. return $this->redirect(['view', 'id' => $model->id]);
  128. }
  129.  
  130. return $this->render('update', [
  131. 'model' => $model,
  132. ]);
  133. }
  134.  
  135. /**
  136. * Deletes an existing Zayvki model.
  137. * If deletion is successful, the browser will be redirected to the 'index' page.
  138. * @param integer $id
  139. * @return mixed
  140. * @throws NotFoundHttpException if the model cannot be found
  141. */
  142. public function actionDelete($id)
  143. {
  144. $this->findModel($id)->delete();
  145.  
  146. return $this->redirect(['index']);
  147. }
  148.  
  149. /**
  150. * Finds the Zayvki model based on its primary key value.
  151. * If the model is not found, a 404 HTTP exception will be thrown.
  152. * @param integer $id
  153. * @return Zayvki the loaded model
  154. * @throws NotFoundHttpException if the model cannot be found
  155. */
  156. protected function findModel($id)
  157. {
  158. if (($model = Zayvki::findOne($id)) !== null) {
  159. return $model;
  160. }
  161.  
  162. throw new NotFoundHttpException('The requested page does not exist.');
  163. }
  164. public function actionGetCategory($q = null, $uslugi_id = null)
  165. {
  166. $response = Yii::$app->response;
  167. $response->format = \yii\web\Response::FORMAT_JSON;
  168.  
  169. $query = Category::find()
  170. ->asArray()
  171. ->select(['category.id', 'category.name AS text']);
  172. if ($uslugi_id) {
  173. $query->join('join', 'usligi rs', 'category.id = rs.category_id')
  174. ->andWhere(['rs.id' => $uslugi_id]);
  175. }
  176.  
  177.  
  178. $query->andFilterWhere(['like', 'category.name', $q]);
  179.  
  180. $data = $query->orderBy('category.name ASC')->limit(20)->all();
  181.  
  182. $out['results'] = array_values($data);
  183.  
  184. return $out;
  185. }
  186. public function actionGetService($q = null, $category_id = null)
  187. {
  188. $response = Yii::$app->response;
  189. $response->format = \yii\web\Response::FORMAT_JSON;
  190. $query =Uslugi::find()
  191. ->asArray()
  192. ->select(['uslugi.id', 'uslugi.usluginame AS text']);
  193. if (null !== $category_id ) {
  194. $query->join('join', 'category cat', 'uslugi.category_id = cat.id')
  195. ->andWhere(['cat.id' => $category_id]);
  196. }
  197. $data = $query->orderBy('uslugi.usluginame ASC')->limit(20)->all();
  198.  
  199. $out['results'] = array_values($data);
  200.  
  201. return $out;
  202. }
  203. }
Add Comment
Please, Sign In to add comment