button.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. <style>
  14. .reationbutton{
  15. filter: grayscale(1);
  16. transform: scale(1);
  17. display:block;
  18. }
  19. .reationbutton:hover{
  20. filter: grayscale(0);
  21. transform: scale(1.15);
  22. }
  23. .reactionb i{
  24. text-align:center;
  25. font-style: normal;
  26. color:#aaa;
  27. font-weight: 600;
  28. font-size:10px;
  29. margin:5px;
  30. line-height: 14px;
  31. }
  32. .reationbutton.active{
  33. filter: grayscale(0);
  34. }
  35. </style>
  36. <div class="btn-group reactionb">
  37. <?
  38. foreach( $counters as $key=>$val ){
  39. ?>
  40. <i><img src="/img/reactionbutton/<?=$key+1?>.svg" width="32px" heght="32px" class="reationbutton<?=$val?' active':''?>" onclick="reaction(<?=$key?>)" id="rbki_<?=$key?>"><p id="rbk_<?=$key?>"><?=$val?></p></i>
  41. <?
  42. $key++;
  43. }
  44. ?>
  45. </div>
  46. <?
  47. $this->registerJs(
  48. <<<JS
  49. function reaction(vote){
  50. $.post( "$filterurl", { 'id': $id, 'type': '$type', 'vote':vote}, function( data ) {
  51. console.log(data);
  52. if( data.status == 'ok' ){
  53. if( Array.isArray(data.ret) ){
  54. data.ret.forEach((item, key) => {
  55. $('#rbk_'+key).text(item);
  56. if( item > 0 ) $('#rbki_'+key).addClass('active');
  57. });
  58. }
  59. }
  60. });
  61. }
  62. JS
  63. ,
  64. yii\web\View::POS_END,
  65. 'reaction'
  66. );
  67. $this->registerJs(
  68. <<<JS
  69. setTimeout(() => {
  70. $.post( "$getrurl", { 'id': $id, 'type': '$type'}, function( data ) {
  71. if( data.status == 'ok' ){
  72. if( Array.isArray(data.ret) ){
  73. data.ret.forEach((item, key) => {
  74. $('#rbk_'+key).text(item);
  75. if( item > 0 ) $('#rbki_'+key).addClass('active');
  76. });
  77. }
  78. }
  79. });
  80. }, 5000 );
  81. JS
  82. ,
  83. yii\web\View::POS_END,
  84. 'reaction2'
  85. );
  86. }