1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hauntd\vote; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\InvalidConfigException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Alexander Kononenko <[email protected]> |
10
|
|
|
* @package hauntd\vote |
11
|
|
|
*/ |
12
|
|
|
class Module extends \yii\base\Module |
13
|
|
|
{ |
14
|
|
|
const TYPE_VOTING = 'voting'; |
15
|
|
|
const TYPE_TOGGLE = 'toggle'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool Apply default styles by default |
19
|
|
|
*/ |
20
|
|
|
public $registerAsset = true; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public $controllerNamespace = 'hauntd\vote\controllers'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array Entities that will be used by vote widgets. |
29
|
|
|
* - `$modelName`: model class name |
30
|
|
|
* - `$allowGuests`: allow users to vote |
31
|
|
|
* - `$type`: vote type (Module::TYPE_VOTING or Module::TYPE_TOGGLE) |
32
|
|
|
*/ |
33
|
|
|
public $entities; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
public $guestTimeLimit = 3600; // 1 hour per vote for guests |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $redirectUrl = '/site/login'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $entity |
47
|
|
|
* @return int |
48
|
|
|
*/ |
49
|
|
|
public function encodeEntity($entity) |
50
|
|
|
{ |
51
|
|
|
return sprintf("%u", crc32($entity)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $entity |
56
|
|
|
* @return array|null |
57
|
|
|
* @throws InvalidConfigException |
58
|
|
|
*/ |
59
|
|
|
public function getSettingsForEntity($entity) |
60
|
|
|
{ |
61
|
|
|
if (!isset($this->entities[$entity])) { |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
$settings = $this->entities[$entity]; |
65
|
|
|
if (!is_array($settings)) { |
66
|
|
|
$settings = ['modelName' => $settings]; |
67
|
|
|
} |
68
|
|
|
$settings = array_merge($this->getDefaultSettings(), $settings); |
69
|
|
|
if (!in_array($settings['type'], [self::TYPE_TOGGLE, self::TYPE_VOTING])) { |
70
|
|
|
throw new InvalidConfigException('Unsupported voting type.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $settings; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
protected function getDefaultSettings() |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
'type' => self::TYPE_VOTING, |
83
|
|
|
'allowGuests' => false, |
84
|
|
|
'allowSelfVote' => true, |
85
|
|
|
'entityAuthorAttribute' => 'user_id', |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|