ReactionButtonsController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\controllers;
  3. use app\models\ReactionButtons;
  4. use yii\web\Controller;
  5. use yii\web\NotFoundHttpException;
  6. class ReactionButtonsController extends \yii\web\Controller
  7. {
  8. public function actionIndex()
  9. {
  10. return $this->render('index');
  11. }
  12. /*
  13. * test
  14. *
  15. */
  16. public function actionView($id, $type){
  17. return $this->render("button",["id"=>$id,"type"=>$type]);
  18. }
  19. public function actionGet(){
  20. if(\Yii::$app->request->isPost){
  21. if(\Yii::$app->request->post('id') && \Yii::$app->request->post('type') ){
  22. $id = (int) \Yii::$app->request->post('id'); //страница
  23. $type = \Yii::$app->request->post('type'); //тип страницы
  24. if( in_array($type, ReactionButtons::types) ){
  25. $model = ReactionButtons::findOne(['id'=>$id, 'type'=>$type]);
  26. if( $model ){
  27. $a = $model->getcounter();
  28. return $this->asJson(['status'=>'ok', 'ret'=>$a]);
  29. }
  30. }
  31. }
  32. }
  33. return $this->asJson(['status'=>'err']);
  34. }
  35. public function actionAjax(){
  36. if(\Yii::$app->request->isPost){
  37. if(\Yii::$app->request->post('id') && \Yii::$app->request->post('type') ){
  38. $model = new ReactionButtons();
  39. $id = (int) \Yii::$app->request->post('id'); //страница
  40. $type = \Yii::$app->request->post('type'); //тип страницы
  41. $vote = (int) \Yii::$app->request->post('vote'); //голос
  42. if( !in_array($type, $model::types) ) return $this->asJson(['status'=>'err']);;
  43. $modelx = ReactionButtons::findOne(['id'=>$id, 'type'=>$type]);
  44. if( ( $modelx instanceof ReactionButtons ) ){
  45. $model = $modelx;
  46. }
  47. $ret = $model->testq($id, $type); //голосовал ли этот клиент на странице
  48. if( !$ret ){
  49. $model->addlog($id, $type);
  50. $a = $model->getcounter();
  51. $a[$vote] ++;
  52. $model->counter = json_encode($a);
  53. $model->type = $type;
  54. if( $model->isNewRecord ){
  55. $model->id = $id;
  56. }
  57. $model->save(false);
  58. return $this->asJson(['status'=>'ok','ret'=>$a]);
  59. }
  60. $a = $model->getcounter();
  61. $a[$vote] ++;
  62. return $this->asJson(['status'=>'ok','ret'=>$a]);
  63. }
  64. }
  65. return $this->asJson(['status'=>'err']);
  66. }
  67. }