123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace manager\controllers;
- use app\models\base\Image;
- use app\models\base\News;
- use app\models\base\TopSlider;
- use yii\data\ActiveDataProvider;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- /**
- * TopSliderController implements the CRUD actions for TopSlider model.
- */
- class TopSliderController extends BaseController
- {
- /**
- * @inheritDoc
- */
- public function behaviors()
- {
- return array_merge(
- parent::behaviors(),
- [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'delete' => ['POST'],
- ],
- ],
- ]
- );
- }
- /**
- * Lists all TopSlider models.
- *
- * @return string
- */
- public function actionIndex()
- {
- $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');
- // print_a($query->createCommand()->queryAll());exit;
- $query = $squery->createCommand()->queryAll();
- //$query = "SELECT * FROM `news`, `top_slider` WHERE `news`.`id` = `top_slider`.`post_id`";
- $dataProvider = new ActiveDataProvider([
- 'query' => $squery,
- /*
- 'pagination' => [
- 'pageSize' => 50
- ],
- 'sort' => [
- 'defaultOrder' => [
- 'id' => SORT_DESC,
- ]
- ],
- */
- ]);
- $provider = new \yii\data\ArrayDataProvider([
- 'allModels' => $query,
- 'pagination' => [
- 'pageSize' => 50,
- ],
- ]);
- return $this->render('index', [
- 'dataProvider' => $provider,
- ]);
- }
- /**
- * Displays a single TopSlider model.
- * @param int $id ID
- * @return string
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionView($id)
- {
- return $this->render('view', [
- 'model' => $this->findModel($id),
- ]);
- }
- /**
- * Creates a new TopSlider model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return string|\yii\web\Response
- */
- public function actionCreate()
- {
- $model = new TopSlider();
- if ($this->request->isPost) {
- if ($model->load($this->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id]);
- }
- } else {
- $model->loadDefaultValues();
- }
- return $this->render('create', [
- 'model' => $model,
- ]);
- }
- /**
- * Updates an existing TopSlider model.
- * If update is successful, the browser will be redirected to the 'view' page.
- * @param int $id ID
- * @return string|\yii\web\Response
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
- return $this->redirect(['view', 'id' => $model->id]);
- }
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- public function actionAjaxsave()
- {
- $model = new TopSlider();
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- if ($this->request->isGet) {
- $g = $this->request->get('TopSlider');
- if( !isset($g['published_from']) || !isset($g['published_to']) || $g['published_from'] == '' || $g['published_to'] == '' ){
- return ['success' => 'err', 'err'=>'Не установлена дата'];
- }
- if ($model->load($this->request->get()) && $model->save()) {
- return ['success' => 'ok'];
- }
- }
- return ['success' => 'err', 'err'=>'Ошибка сохранения'];
- }
- /**
- * Deletes an existing TopSlider model.
- * If deletion is successful, the browser will be redirected to the 'index' page.
- * @param int $id ID
- * @return \yii\web\Response
- * @throws NotFoundHttpException if the model cannot be found
- */
- public function actionDelete($id)
- {
- $this->findModel($id)->delete();
- return $this->redirect(['index']);
- }
- /**
- * Finds the TopSlider model based on its primary key value.
- * If the model is not found, a 404 HTTP exception will be thrown.
- * @param int $id ID
- * @return TopSlider the loaded model
- * @throws NotFoundHttpException if the model cannot be found
- */
- protected function findModel($id)
- {
- if (($model = TopSlider::findOne(['id' => $id])) !== null) {
- return $model;
- }
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- public function actionFindNews($q = "")
- {
- $data = [
- "items"=>[],
- "total_count" => 0
- ];
- $news = \app\models\News::find()->from(['n use index (calendar)'=>News::tableName(),])
- ->andWhere(['OR',[
- 'id'=>$q
- ],
- ['like','LOWER(title)',mb_strtolower($q)]
- ])
- ->orderBy(['dt_pub'=>SORT_DESC])
- ->limit(20)->all();
- foreach ($news as $post)
- {
- $data['items'][] = [
- "id"=>$post->id,
- "title"=>$post->title,
- "picture"=>$post->image->getUrl(Image::SIZE_320x180,"webp")
- ];
- $data['total_count']++;
- }
- return $this->asJson($data);
- }
- }
|