1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hauntd\vote\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use yii\helpers\ArrayHelper; |
8
|
|
|
use hauntd\vote\traits\ModuleTrait; |
9
|
|
|
use hauntd\vote\Module; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Alexander Kononenko <[email protected]> |
13
|
|
|
* @package hauntd\vote\models |
14
|
|
|
*/ |
15
|
|
|
class VoteForm extends Model |
16
|
|
|
{ |
17
|
|
|
use ModuleTrait; |
18
|
|
|
|
19
|
|
|
const ACTION_POSITIVE = 'positive'; |
20
|
|
|
const ACTION_NEGATIVE = 'negative'; |
21
|
|
|
const ACTION_TOGGLE = 'toggle'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string entity (e.g. "user.like" or "page.voting") |
25
|
|
|
*/ |
26
|
|
|
public $entity; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var integer target model id |
30
|
|
|
*/ |
31
|
|
|
public $targetId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string +/-? |
35
|
|
|
*/ |
36
|
|
|
public $action; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return array |
40
|
|
|
* @throws \yii\base\InvalidConfigException |
41
|
|
|
*/ |
42
|
|
|
public function rules() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
[['entity', 'targetId', 'action'], 'required'], |
46
|
|
|
['targetId', 'integer'], |
47
|
|
|
['action', 'in', 'range' => [self::ACTION_NEGATIVE, self::ACTION_POSITIVE, self::ACTION_TOGGLE]], |
48
|
|
|
['entity', 'checkModel'], |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
public function getValue() |
56
|
|
|
{ |
57
|
|
|
return $this->action == self::ACTION_NEGATIVE ? Vote::VOTE_NEGATIVE : Vote::VOTE_POSITIVE; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return bool |
62
|
|
|
* @throws \yii\base\InvalidConfigException |
63
|
|
|
*/ |
64
|
|
|
public function checkModel() |
65
|
|
|
{ |
66
|
|
|
$module = $this->getModule(); |
67
|
|
|
$settings = $module->getSettingsForEntity($this->entity); |
68
|
|
|
$allowGuests = ArrayHelper::getValue($settings, 'allowGuests', false); |
|
|
|
|
69
|
|
|
|
70
|
|
|
if (!$settings) { |
71
|
|
|
$this->addError('entity', Yii::t('vote', 'This entity is not supported.')); |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
if (Yii::$app->user->isGuest && ($settings['type'] == Module::TYPE_TOGGLE || !$allowGuests)) { |
75
|
|
|
$this->addError('entity', Yii::t('vote', 'Guests are not allowed for this voting.')); |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
$targetModel = Yii::createObject($settings['modelName']); |
79
|
|
|
if ($targetModel->findOne(['id' => $this->targetId]) == null) { |
80
|
|
|
$this->addError('targetId', Yii::t('vote', 'Target model not found.')); |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.