123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace app\controllers;
- use app\models\ReactionButtons;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- class ReactionButtonsController extends \yii\web\Controller
- {
- public function actionIndex()
- {
- return $this->render('index');
- }
- /*
- * test
- *
- */
- public function actionView($id, $type){
- return $this->render("button",["id"=>$id,"type"=>$type]);
- }
- public function actionGet(){
- if(\Yii::$app->request->isPost){
- if(\Yii::$app->request->post('id') && \Yii::$app->request->post('type') ){
- $id = (int) \Yii::$app->request->post('id'); //страница
- $type = \Yii::$app->request->post('type'); //тип страницы
- if( in_array($type, ReactionButtons::types) ){
- $model = ReactionButtons::findOne(['id'=>$id, 'type'=>$type]);
- if( $model ){
- $a = $model->getcounter();
- return $this->asJson(['status'=>'ok', 'ret'=>$a]);
- }
- }
- }
- }
- return $this->asJson(['status'=>'err']);
- }
- public function actionAjax(){
- if(\Yii::$app->request->isPost){
- if(\Yii::$app->request->post('id') && \Yii::$app->request->post('type') ){
- $model = new ReactionButtons();
- $id = (int) \Yii::$app->request->post('id'); //страница
- $type = \Yii::$app->request->post('type'); //тип страницы
- $vote = (int) \Yii::$app->request->post('vote'); //голос
- if( !in_array($type, $model::types) ) return $this->asJson(['status'=>'err']);;
- $modelx = ReactionButtons::findOne(['id'=>$id, 'type'=>$type]);
- if( ( $modelx instanceof ReactionButtons ) ){
- $model = $modelx;
- }
- $ret = $model->testq($id, $type); //голосовал ли этот клиент на странице
- if( !$ret ){
- $model->addlog($id, $type);
- $a = $model->getcounter();
- $a[$vote] ++;
- $model->counter = json_encode($a);
- $model->type = $type;
- if( $model->isNewRecord ){
- $model->id = $id;
- }
- $model->save(false);
- return $this->asJson(['status'=>'ok','ret'=>$a]);
- }
- $a = $model->getcounter();
- $a[$vote] ++;
- return $this->asJson(['status'=>'ok','ret'=>$a]);
- }
- }
- return $this->asJson(['status'=>'err']);
- }
- }
|