|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hauntd\vote\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use hauntd\vote\Module; |
|
7
|
|
|
use hauntd\vote\traits\ModuleTrait; |
|
8
|
|
|
use yii\base\Model; |
|
9
|
|
|
use yii\helpers\ArrayHelper; |
|
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. "userLike" or "pageVoting") |
|
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
|
|
|
if ($settings === null) { |
|
69
|
|
|
$this->addError('entity', Yii::t('vote', 'This entity is not supported.')); |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
$allowGuests = ArrayHelper::getValue($settings, 'allowGuests', false); |
|
73
|
|
|
if (Yii::$app->user->isGuest && ($settings['type'] == Module::TYPE_TOGGLE || !$allowGuests)) { |
|
74
|
|
|
$this->addError('entity', Yii::t('vote', 'Guests are not allowed for this voting.')); |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
$targetModel = Yii::createObject($settings['modelName']); |
|
78
|
|
|
$entityModel = $targetModel->findOne(['id' => $this->targetId]); |
|
79
|
|
|
if ($entityModel == null) { |
|
80
|
|
|
$this->addError('targetId', Yii::t('vote', 'Target model not found.')); |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
$allowSelfVote = ArrayHelper::getValue($settings, 'allowSelfVote', false); |
|
84
|
|
|
if (!$allowSelfVote) { |
|
85
|
|
|
$entityAuthorAttribute = ArrayHelper::getValue($settings, 'entityAuthorAttribute', 'user_id'); |
|
86
|
|
|
if(!Yii::$app->user->isGuest && Yii::$app->user->id == $entityModel->{$entityAuthorAttribute}) { |
|
87
|
|
|
$this->addError('entity', Yii::t('vote', 'Self-voting are not allowed.')); |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|