button.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. use app\models\ReactionButtons;
  3. if( isset($id) && isset($type) ){
  4. $filterurl = \yii\helpers\Url::base('https')."/reactionbuttons/ajax";
  5. $getrurl = \yii\helpers\Url::base('https')."/reactionbuttons/get";
  6. $model = ReactionButtons::findOne(['id'=>$id, 'type'=>$type]);
  7. if( ( !$model instanceof ReactionButtons ) ){
  8. $counters=[0,0,0,0,0,0];
  9. }else{
  10. $counters =json_decode( $model->counter, true );
  11. }
  12. ?>
  13. <div class="btn-group reactionb">
  14. <?
  15. foreach( $counters as $key=>$val ){
  16. ?>
  17. <i><img src="/img/reactionbutton/<?=$key+1?>.svg" width="32" height="32" class="reationbutton<?=$val?' active':''?>" onclick="reaction(<?=$key?>)" id="rbki_<?=$key?>"><p id="rbk_<?=$key?>"><?=$val?></p></i>
  18. <?
  19. $key++;
  20. }
  21. ?>
  22. </div>
  23. <?
  24. $this->registerJs(
  25. <<<JS
  26. function reaction(vote){
  27. $.post( "$filterurl", { 'id': $id, 'type': '$type', 'vote':vote}, function( data ) {
  28. console.log(data);
  29. if( data.status == 'ok' ){
  30. if( Array.isArray(data.ret) ){
  31. data.ret.forEach((item, key) => {
  32. $('#rbk_'+key).text(item);
  33. if( item > 0 ) $('#rbki_'+key).addClass('active');
  34. });
  35. }
  36. }
  37. });
  38. }
  39. JS
  40. ,
  41. yii\web\View::POS_END,
  42. 'reaction'
  43. );
  44. $this->registerJs(
  45. <<<JS
  46. setTimeout(() => {
  47. $.post( "$getrurl", { 'id': $id, 'type': '$type'}, function( data ) {
  48. if( data.status == 'ok' ){
  49. if( Array.isArray(data.ret) ){
  50. data.ret.forEach((item, key) => {
  51. $('#rbk_'+key).text(item);
  52. if( item > 0 ) $('#rbki_'+key).addClass('active');
  53. });
  54. }
  55. }
  56. });
  57. }, 5000 );
  58. JS
  59. ,
  60. yii\web\View::POS_END,
  61. 'reaction2'
  62. );
  63. }