Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntd committed Jul 9, 2016
1 parent 90d4a65 commit 56ba7b3
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion behaviors/VoteBehavior.php
Expand Up @@ -83,7 +83,7 @@ public function __set($name, $value)
*/
protected function checkAttribute($name)
{
foreach ($this->getModule()->entities as $entity => $options) {
foreach (array_keys($this->getModule()->entities) as $entity) {
if ($name == "{$entity}Positive" || $name == "{$entity}Negative" || $name == "{$entity}Rating" ||
$name == "{$entity}UserValue") {
return true;
Expand Down
5 changes: 2 additions & 3 deletions models/VoteForm.php
Expand Up @@ -65,12 +65,11 @@ public function checkModel()
{
$module = $this->getModule();
$settings = $module->getSettingsForEntity($this->entity);
$allowGuests = ArrayHelper::getValue($settings, 'allowGuests', false);

if (!$settings) {
if ($settings === null) {
$this->addError('entity', Yii::t('vote', 'This entity is not supported.'));
return false;
}
$allowGuests = ArrayHelper::getValue($settings, 'allowGuests', false);
if (Yii::$app->user->isGuest && ($settings['type'] == Module::TYPE_TOGGLE || !$allowGuests)) {
$this->addError('entity', Yii::t('vote', 'Guests are not allowed for this voting.'));
return false;
Expand Down
5 changes: 2 additions & 3 deletions widgets/BaseWidget.php
Expand Up @@ -90,12 +90,11 @@ abstract class BaseWidget extends Widget
public $viewFile = 'vote';

/**
* @param $classes
* @return string
*/
public function getSelector($classes)
public function getSelector()
{
$classes = str_replace(' ', '.', $classes);
$classes = str_replace(' ', '.', $this->options['class']);
return ".{$classes}[data-entity=\"' + entity + '\"][data-target-id=\"' + target + '\"]";
}

Expand Down
7 changes: 4 additions & 3 deletions widgets/Favorite.php
Expand Up @@ -46,11 +46,12 @@ public function getDefaultButtonOptions()

/**
* Initialize with default events.
*
* @param string $selector
*/
public function initJsEvents()
public function initJsEvents($selector)
{
parent::initJsEvents();
$selector = $this->getSelector($this->options['class']);
parent::initJsEvents($selector);
$this->jsChangeCounters = "
if (data.success) {
$('$selector .vote-count').text(data.aggregate.positive);
Expand Down
7 changes: 4 additions & 3 deletions widgets/Vote.php
Expand Up @@ -32,7 +32,7 @@ public function init()
{
parent::init();
$this->options = array_merge($this->getDefaultOptions(), $this->options);
$this->initJsEvents();
$this->initJsEvents($this->getSelector($this->options['class']));
$this->registerJs();
}

Expand All @@ -57,10 +57,11 @@ public function run()

/**
* Initialize with default events.
*
* @param string $selector
*/
public function initJsEvents()
public function initJsEvents($selector)
{
$selector = $this->getSelector($this->options['class']);
if (!isset($this->jsBeforeVote)) {
$this->jsBeforeVote = "
$('$selector .vote-btn').prop('disabled', 'disabled').addClass('vote-loading');
Expand Down
7 changes: 4 additions & 3 deletions widgets/VoteToggle.php
Expand Up @@ -53,7 +53,7 @@ public function init()
parent::init();
$this->options = array_merge($this->getDefaultOptions(), $this->options);
$this->buttonOptions = array_merge($this->getDefaultButtonOptions(), $this->buttonOptions);
$this->initJsEvents();
$this->initJsEvents($this->getSelector($this->options['class']));
$this->registerJs();
}

Expand All @@ -77,10 +77,11 @@ public function run()

/**
* Initialize with default events.
*
* @param string $selector
*/
public function initJsEvents()
public function initJsEvents($selector)
{
$selector = $this->getSelector($this->options['class']);
if (!isset($this->jsBeforeVote)) {
$this->jsBeforeVote = "
$('$selector .vote-btn').prop('disabled', 'disabled').addClass('vote-loading');
Expand Down

0 comments on commit 56ba7b3

Please sign in to comment.