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']); } }