These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace hauntd\vote\widgets; |
||
4 | |||
5 | use yii\web\View; |
||
6 | use yii\web\JsExpression; |
||
7 | use Yii; |
||
8 | |||
9 | /** |
||
10 | * @author Alexander Kononenko <[email protected]> |
||
11 | * @package hauntd\vote\widgets |
||
12 | */ |
||
13 | class Vote extends BaseWidget |
||
14 | { |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | public $jsCodeKey = 'vote'; |
||
19 | |||
20 | /** |
||
21 | * @return array |
||
22 | */ |
||
23 | public function getDefaultOptions() |
||
24 | { |
||
25 | return [ |
||
26 | 'class' => 'vote', |
||
27 | ]; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * @inherit |
||
32 | */ |
||
33 | public function init() |
||
34 | { |
||
35 | parent::init(); |
||
36 | $this->options = array_merge($this->getDefaultOptions(), $this->options); |
||
37 | $this->initJsEvents(); |
||
38 | $this->registerJs(); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @return string |
||
43 | * @throws \yii\base\InvalidConfigException |
||
44 | */ |
||
45 | public function run() |
||
46 | { |
||
47 | return $this->render($this->viewFile, [ |
||
48 | 'jsCodeKey' => $this->jsCodeKey, |
||
49 | 'entity' => $this->entity, |
||
50 | 'model' => $this->model, |
||
51 | 'targetId' => $this->targetId, |
||
52 | 'userValue' => $this->userValue, |
||
53 | 'positive' => isset($this->aggregateModel->positive) ? $this->aggregateModel->positive : 0, |
||
54 | 'negative' => isset($this->aggregateModel->negative) ? $this->aggregateModel->negative : 0, |
||
55 | 'rating' => isset($this->aggregateModel->rating) ? $this->aggregateModel->rating : 0.0, |
||
56 | 'options' => $this->options, |
||
57 | ]); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Initialize with default events. |
||
62 | */ |
||
63 | public function initJsEvents() |
||
64 | { |
||
65 | $selector = $this->getSelector($this->options['class']); |
||
66 | if (!isset($this->jsChangeCounters)) { |
||
67 | $this->jsChangeCounters = " |
||
68 | if (typeof(data.success) !== 'undefined') { |
||
69 | $('$selector .vote-count span').text(data.aggregate.positive - data.aggregate.negative); |
||
70 | vote.find('button').removeClass('vote-active'); |
||
71 | button.addClass('vote-active'); |
||
72 | } |
||
73 | "; |
||
74 | } |
||
75 | if (!isset($this->jsBeforeVote)) { |
||
76 | $this->jsBeforeVote = " |
||
77 | $('$selector .vote-btn').prop('disabled', 'disabled').addClass('vote-loading'); |
||
78 | $('$selector .vote-count') |
||
79 | .addClass('vote-loading') |
||
80 | .append('<div class=\"vote-loader\"><span></span><span></span><span></span></div>'); |
||
81 | "; |
||
82 | } |
||
83 | if (!isset($this->jsAfterVote)) { |
||
84 | $this->jsAfterVote = " |
||
85 | $('$selector .vote-btn').prop('disabled', false).removeClass('vote-loading'); |
||
86 | $('$selector .vote-count').removeClass('vote-loading').find('.vote-loader').remove(); |
||
87 | "; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Registers jQuery handler. |
||
93 | */ |
||
94 | View Code Duplication | protected function registerJs() |
|
0 ignored issues
–
show
|
|||
95 | { |
||
96 | $js = new JsExpression(" |
||
97 | $('body').on('click', '[data-rel=\"{$this->jsCodeKey}\"] button', function(event) { |
||
98 | var vote = $(this).closest('[data-rel=\"{$this->jsCodeKey}\"]'), |
||
99 | button = $(this), |
||
100 | action = button.attr('data-action'), |
||
101 | entity = vote.attr('data-entity'), |
||
102 | target = vote.attr('data-target-id'); |
||
103 | jQuery.ajax({ |
||
104 | url: '$this->voteUrl', type: 'POST', dataType: 'json', cache: false, |
||
105 | data: { 'VoteForm[entity]': entity, 'VoteForm[targetId]': target, 'VoteForm[action]': action }, |
||
106 | beforeSend: function(jqXHR, settings) { $this->jsBeforeVote }, |
||
107 | success: function(data, textStatus, jqXHR) { $this->jsChangeCounters $this->jsShowMessage }, |
||
108 | complete: function(jqXHR, textStatus) { $this->jsAfterVote }, |
||
109 | error: function(jqXHR, textStatus, errorThrown) { $this->jsErrorVote } |
||
110 | }); |
||
111 | }); |
||
112 | "); |
||
113 | $this->view->registerJs($js, View::POS_END, $this->jsCodeKey); |
||
114 | } |
||
115 | } |
||
116 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.