forked from bojovyletoun/GmapFormControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GmapFormControl.php
129 lines (110 loc) · 3.52 KB
/
GmapFormControl.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
// uncomment 'use' in order to run it under PHP5.2 non-prefixed
use Nette\Utils\Html,
Nette\Utils\Strings,
\Nette\Templating\FileTemplate,
Nette\Forms\Controls\BaseControl,
\Nette\Forms\Container,
Nette\Latte\Engine;
/**
* GmapFormControl
* @author Jakub Jarabica (http://www.jam3son.sk)
* @license MIT
*
* @property-write string $template
*
*/
final class GmapFormControl extends BaseControl {
const LATITUDE = 'latitude';
const LONGITUDE = 'longitude';
/** @var array default map options */
private $options = array(
'width' => 300,
'height' => 300,
'center' => array(
self::LATITUDE => 0,
self::LONGITUDE => 0,
),
'zoom' => 2,
);
/** @var FileTemplate */
private $template;
/**
* Form container extension method. Do not call directly.
*
* @param Container $form
* @param string $name
* @param string $label
* @param array $options
* @return GmapFormControl
*/
public static function addGmapFormControl(Container $form, $name, $label, $options = NULL) {
return $form[$name] = new self($label, $options);
}
/**
* @param string $label
* @param array $options
*/
public function __construct($label, $options = NULL) {
parent::__construct($label);
if ($options !== NULL) {
$this->options = array_merge($this->options, $options);
}
$this->template = dirname(__FILE__) . '/template.latte';
}
/**
*
* @param string $template path to template
* @param bool is provided path full or relative to this script?
*/
public function setTemplate($template, $isPathFull = FALSE) {
$this->template = ($isPathFull) ? $template : dirname(__FILE__).'/'.$template.'.latte';
}
/**
* Generates control's HTML
*
* @return FileTemplate
*/
public function getControl() {
$original = parent::getControl();
$id = $original->id;
/* create latitude input */
$latitude = clone $original;
$latitude->name .= '[' . self::LATITUDE . ']';
$latitude->id = $id . '-' . self::LATITUDE;
$latitude->value = $this->value[self::LATITUDE];
/* create longitude input */
$longitude = clone $original;
$longitude->name .= '[' . self::LONGITUDE . ']';
$longitude->id = $id . '-' . self::LONGITUDE;
$longitude->value = $this->value[self::LONGITUDE];
$marker = ($this->getValue() === NULL) ? FALSE : $this->getValue();
if (!isset($this->options['center'][self::LATITUDE])) { // allows simpler central point array
$center = array(
self::LATITUDE => $this->options['center'][0],
self::LONGITUDE => $this->options['center'][1],
);
} else {
$center = $this->options['center'];
}
$template = new FileTemplate($this->template);
$template->registerFilter(new Engine);
$template->latitude = $latitude;
$template->longitude = $longitude;
$template->marker = $marker;
$template->options = $this->options;
$template->center = $center;
$template->control_id = $id;
return $template;
}
/**
* Generates label's HTML element.
*
* @return Html
*/
public function getLabel($caption = NULL) {
$label = parent::getLabel($caption);
$label->for = NULL;
return $label;
}
}