1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace backend\widgets\map\jvector; |
4
|
|
|
|
5
|
|
|
use yii\base\InvalidArgumentException; |
6
|
|
|
use yii\base\Widget; |
7
|
|
|
use yii\helpers\Html; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
use backend\widgets\map\jvector\assets\MapAsset; |
10
|
|
|
use yii\helpers\Json; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Map |
14
|
|
|
* |
15
|
|
|
* @package backend\widgets\map\jvector |
16
|
|
|
*/ |
17
|
|
|
class Map extends Widget |
18
|
|
|
{ |
19
|
|
|
/** @var bool */ |
20
|
|
|
public $status = true; |
21
|
|
|
/** @var array */ |
22
|
|
|
public $containerOptions = []; |
23
|
|
|
/** @var array */ |
24
|
|
|
public $clientOptions = []; |
25
|
|
|
/** @var array */ |
26
|
|
|
public $maps = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritDoc |
30
|
|
|
*/ |
31
|
|
|
public function init() |
32
|
|
|
{ |
33
|
|
|
parent::init(); |
34
|
|
|
if ($this->status === true) { |
35
|
|
|
$this->registerAsset(); |
36
|
|
|
} |
37
|
|
|
$this->id = $this->id ?: $this->getId(); |
38
|
|
|
ArrayHelper::setValue($this->containerOptions, 'id', $this->id); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Run |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function run() |
47
|
|
|
{ |
48
|
|
|
return Html::tag('div', '', $this->containerOptions); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Client Options |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function getClientOptions() |
57
|
|
|
{ |
58
|
|
|
$mapsArray = $this->getMapsArray(); |
59
|
|
|
$value = str_replace('-', '_', $mapsArray[key($mapsArray)]); |
60
|
|
|
$clientOptions = ['map' => $value,]; |
61
|
|
|
$clientOptions = ArrayHelper::merge($clientOptions, $this->clientOptions); |
62
|
|
|
if (!empty($mapsArray[$clientOptions['map']])) { |
63
|
|
|
ArrayHelper::setValue( |
64
|
|
|
$clientOptions, |
65
|
|
|
'map', |
66
|
|
|
str_replace('-', '_', $mapsArray[$clientOptions['map']]) |
67
|
|
|
); |
68
|
|
|
return $clientOptions; |
69
|
|
|
} |
70
|
|
|
throw new InvalidArgumentException( |
71
|
|
|
'Map "' . $clientOptions['map'] . |
72
|
|
|
'" is missing from the array: \'maps\' => [\'' . $clientOptions['map'] . '\' => \'' . |
73
|
|
|
str_replace('_', '-', $clientOptions['map']) . '\']' |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getMapsArray() |
81
|
|
|
{ |
82
|
|
|
$maps = ['world_mill_en' => 'world-mill-en']; |
83
|
|
|
return ArrayHelper::merge($maps, $this->maps); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Register Asset |
88
|
|
|
*/ |
89
|
|
|
public function registerAsset() |
90
|
|
|
{ |
91
|
|
|
$id = $this->id; |
92
|
|
|
$view = $this->getView(); |
93
|
|
|
$clientOptions = $this->getClientOptions(); |
94
|
|
|
MapAsset::$maps = $this->getMapsArray(); |
95
|
|
|
MapAsset::$mapName = $clientOptions['map']; |
96
|
|
|
MapAsset::register($view); |
97
|
|
|
|
98
|
|
|
$clientOptionsJson = Json::encode($clientOptions); |
99
|
|
|
$script = " |
100
|
|
|
let map_{$id} = $('#{$id}').vectorMap({$clientOptionsJson}); |
101
|
|
|
"; |
102
|
|
|
$view->registerJs($script); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|