TopSliderController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace manager\controllers;
  3. use app\models\base\Image;
  4. use app\models\base\News;
  5. use app\models\base\TopSlider;
  6. use yii\data\ActiveDataProvider;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. use yii\filters\VerbFilter;
  10. /**
  11. * TopSliderController implements the CRUD actions for TopSlider model.
  12. */
  13. class TopSliderController extends BaseController
  14. {
  15. /**
  16. * @inheritDoc
  17. */
  18. public function behaviors()
  19. {
  20. return array_merge(
  21. parent::behaviors(),
  22. [
  23. 'verbs' => [
  24. 'class' => VerbFilter::className(),
  25. 'actions' => [
  26. 'delete' => ['POST'],
  27. ],
  28. ],
  29. ]
  30. );
  31. }
  32. /**
  33. * Lists all TopSlider models.
  34. *
  35. * @return string
  36. */
  37. public function actionIndex()
  38. {
  39. $squery = \app\models\News::find()->select("news.id,title,post_id,published_from,published_to,".TopSlider::tableName().".id as ts_id")->rightjoin(TopSlider::tableName(), 'news.id = '.TopSlider::tableName().'.post_id');
  40. // print_a($query->createCommand()->queryAll());exit;
  41. $query = $squery->createCommand()->queryAll();
  42. //$query = "SELECT * FROM `news`, `top_slider` WHERE `news`.`id` = `top_slider`.`post_id`";
  43. $dataProvider = new ActiveDataProvider([
  44. 'query' => $squery,
  45. /*
  46. 'pagination' => [
  47. 'pageSize' => 50
  48. ],
  49. 'sort' => [
  50. 'defaultOrder' => [
  51. 'id' => SORT_DESC,
  52. ]
  53. ],
  54. */
  55. ]);
  56. $provider = new \yii\data\ArrayDataProvider([
  57. 'allModels' => $query,
  58. 'pagination' => [
  59. 'pageSize' => 50,
  60. ],
  61. ]);
  62. return $this->render('index', [
  63. 'dataProvider' => $provider,
  64. ]);
  65. }
  66. /**
  67. * Displays a single TopSlider model.
  68. * @param int $id ID
  69. * @return string
  70. * @throws NotFoundHttpException if the model cannot be found
  71. */
  72. public function actionView($id)
  73. {
  74. return $this->render('view', [
  75. 'model' => $this->findModel($id),
  76. ]);
  77. }
  78. /**
  79. * Creates a new TopSlider model.
  80. * If creation is successful, the browser will be redirected to the 'view' page.
  81. * @return string|\yii\web\Response
  82. */
  83. public function actionCreate()
  84. {
  85. $model = new TopSlider();
  86. if ($this->request->isPost) {
  87. if ($model->load($this->request->post()) && $model->save()) {
  88. return $this->redirect(['view', 'id' => $model->id]);
  89. }
  90. } else {
  91. $model->loadDefaultValues();
  92. }
  93. return $this->render('create', [
  94. 'model' => $model,
  95. ]);
  96. }
  97. /**
  98. * Updates an existing TopSlider model.
  99. * If update is successful, the browser will be redirected to the 'view' page.
  100. * @param int $id ID
  101. * @return string|\yii\web\Response
  102. * @throws NotFoundHttpException if the model cannot be found
  103. */
  104. public function actionUpdate($id)
  105. {
  106. $model = $this->findModel($id);
  107. if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
  108. return $this->redirect(['view', 'id' => $model->id]);
  109. }
  110. return $this->render('update', [
  111. 'model' => $model,
  112. ]);
  113. }
  114. public function actionAjaxsave()
  115. {
  116. $model = new TopSlider();
  117. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  118. if ($this->request->isGet) {
  119. $g = $this->request->get('TopSlider');
  120. if( !isset($g['published_from']) || !isset($g['published_to']) || $g['published_from'] == '' || $g['published_to'] == '' ){
  121. return ['success' => 'err', 'err'=>'Не установлена дата'];
  122. }
  123. if ($model->load($this->request->get()) && $model->save()) {
  124. return ['success' => 'ok'];
  125. }
  126. }
  127. return ['success' => 'err', 'err'=>'Ошибка сохранения'];
  128. }
  129. /**
  130. * Deletes an existing TopSlider model.
  131. * If deletion is successful, the browser will be redirected to the 'index' page.
  132. * @param int $id ID
  133. * @return \yii\web\Response
  134. * @throws NotFoundHttpException if the model cannot be found
  135. */
  136. public function actionDelete($id)
  137. {
  138. $this->findModel($id)->delete();
  139. return $this->redirect(['index']);
  140. }
  141. /**
  142. * Finds the TopSlider model based on its primary key value.
  143. * If the model is not found, a 404 HTTP exception will be thrown.
  144. * @param int $id ID
  145. * @return TopSlider the loaded model
  146. * @throws NotFoundHttpException if the model cannot be found
  147. */
  148. protected function findModel($id)
  149. {
  150. if (($model = TopSlider::findOne(['id' => $id])) !== null) {
  151. return $model;
  152. }
  153. throw new NotFoundHttpException('The requested page does not exist.');
  154. }
  155. public function actionFindNews($q = "")
  156. {
  157. $data = [
  158. "items"=>[],
  159. "total_count" => 0
  160. ];
  161. $news = \app\models\News::find()->from(['n use index (calendar)'=>News::tableName(),])
  162. ->andWhere(['OR',[
  163. 'id'=>$q
  164. ],
  165. ['like','LOWER(title)',mb_strtolower($q)]
  166. ])
  167. ->orderBy(['dt_pub'=>SORT_DESC])
  168. ->limit(20)->all();
  169. foreach ($news as $post)
  170. {
  171. $data['items'][] = [
  172. "id"=>$post->id,
  173. "title"=>$post->title,
  174. "picture"=>$post->image->getUrl(Image::SIZE_320x180,"webp")
  175. ];
  176. $data['total_count']++;
  177. }
  178. return $this->asJson($data);
  179. }
  180. }