-
Notifications
You must be signed in to change notification settings - Fork 6
/
Flags.php
105 lines (87 loc) · 2.92 KB
/
Flags.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace yii\flags;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
class Flags extends \yii\base\Widget
{
const FLAT_16 = 'flat/16';
const FLAT_24 = 'flat/24';
const FLAT_32 = 'flat/32';
const FLAT_48 = 'flat/48';
const FLAT_64 = 'flat/64';
const SHINY_16 = 'shiny/16';
const SHINY_24 = 'shiny/24';
const SHINY_32 = 'shiny/32';
const SHINY_48 = 'shiny/48';
const SHINY_64 = 'shiny/64';
public $options = [];
public $label;
public $tagName = 'span';
public $encodeLabel = true;
public $useSprite = false;
public $flag;
public $type = self::FLAT_16;
public function init()
{
parent::init();
if(!isset($this->flag)) {
throw new InvalidConfigException('Flag element is required.');
}
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
$this->flag = strtoupper($this->flag);
Html::addCssClass($this->options, 'flags');
Html::addCssClass($this->options, $this->getCssClass());
}
public function run()
{
$this->registerCss();
echo Html::tag($this->tagName, $this->encodeLabel ? Html::encode($this->label) : $this->label, $this->options);
}
protected function registerCss()
{
$view = $this->getView();
$bundle = new FlagsAsset();
$bundle->publish($view->getAssetManager());
$view->assetBundles[get_called_class()] = $bundle;
if($this->useSprite) {
switch($this->type) {
case self::FLAT_16:
case self::FLAT_24:
case self::FLAT_32:
case self::FLAT_48:
case self::FLAT_64:
$bundle->css[] = $this->type . '/style.css';
break;
case self::SHINY_16:
case self::SHINY_24:
case self::SHINY_32:
case self::SHINY_48:
case self::SHINY_64:
$bundle->css[] = $this->type . '/style.css';
break;
default:
throw new InvalidConfigException('Unknown flags type "' . $type . '".');
}
} else {
$img = $bundle->baseUrl . '/' . $this->type . '/' . $this->flag . '.png';
$size = $this->getSize() . 'px';
$css = [
'background-image: url("'.$img.'")',
'display: inline-block',
'width:' . $size,
'height:' . $size
];
$view->registerCss('.' . $this->getCssClass() . '{' . implode(';', $css) . ';}');
}
}
protected function getCssClass()
{
return 'flag-' . $this->flag . '-' . $this->getSize();
}
protected function getSize()
{
return explode('/', $this->type)[1];
}
}