1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: class Ansel_Widget_Geotag extends Ansel_Widget_Base
18: {
19: 20: 21: 22: 23:
24: protected $_supported_views = array(
25: 'Image',
26: 'Gallery');
27:
28: 29: 30: 31: 32:
33: protected $_params = array(
34: 'default_zoom' => 15,
35: 'max_auto_zoom' => 15);
36:
37: 38: 39: 40: 41: 42: 43:
44: public function __construct($params)
45: {
46: parent::__construct($params);
47: $this->_title = _("Location");
48: }
49:
50: 51: 52: 53: 54: 55: 56:
57: public function attach(Ansel_View_Base $view)
58: {
59: if (empty($GLOBALS['conf']['maps']['driver'])) {
60: return false;
61: }
62: parent::attach($view);
63: return true;
64: }
65:
66: 67: 68: 69: 70:
71: public function html()
72: {
73: $ansel_storage = $GLOBALS['injector']->getInstance('Ansel_Storage');
74: $geodata = $ansel_storage->getImagesGeodata($this->_params['images']);
75: $viewType = $this->_view->viewType();
76:
77:
78: if (count($geodata) == 0 && $viewType != 'Image') {
79: return '';
80: } elseif (count($geodata) == 0) {
81: $noGeotag = true;
82: }
83:
84:
85: Ansel::initHordeMap($GLOBALS['conf']['maps']);
86: Horde::addScriptFile('popup.js', 'horde');
87: Horde::addScriptFile('widgets/geotag.js');
88:
89:
90: $url = Horde::url('map_edit.php', true);
91: $rtext = _("Relocate this image");
92: $dtext = _("Delete geotag");
93: $thisTitleText = _("This image");
94: $otherTitleText = _("Other images in this gallery");
95: $imple = $GLOBALS['injector']
96: ->getInstance('Horde_Core_Factory_Imple')
97: ->create(array('ansel', 'ImageSaveGeotag'));
98: $impleUrl = $imple->getUrl();
99:
100: $permsEdit = (integer)$this->_view->gallery->hasPermission(
101: $GLOBALS['registry']->getAuth(),
102: Horde_Perms::EDIT);
103:
104:
105: $imple = $GLOBALS['injector']
106: ->getInstance('Horde_Core_Factory_Imple')
107: ->create(array('ansel', 'MapLayerSelect'));
108: $layerImpleUrl = $imple->getUrl();
109:
110:
111: $defaultLayer = $GLOBALS['prefs']->getValue('current_maplayer');
112:
113:
114: foreach ($geodata as $id => $data) {
115: $geodata[$id]['icon'] = (string)Ansel::getImageUrl(
116: $geodata[$id]['image_id'],
117: 'mini',
118: true);
119: $geodata[$id]['markerOnly'] = ($viewType == 'Image');
120: $geodata[$id]['link'] = (string)Ansel::getUrlFor(
121: 'view',
122: array(
123: 'view' => 'Image',
124: 'gallery' => $this->_view->gallery->id,
125: 'image' => $geodata[$id]['image_id']),
126: true);
127: }
128:
129:
130: $html = $this->_htmlBegin() . "\n";
131: $content = '<div id="ansel_geo_widget">';
132:
133:
134: if ($viewType == 'Image') {
135: $image_id = $this->_view->resource->id;
136: $others = $this->_getGalleryImagesWithGeodata();
137: foreach ($others as $id => $data) {
138: if ($id != $image_id) {
139: $others[$id]['icon'] = (string)Ansel::getImageUrl(
140: $others[$id]['image_id'],
141: 'mini',
142: true);
143: $others[$id]['link'] = (string)Ansel::getUrlFor(
144: 'view',
145: array(
146: 'view' => 'Image',
147: 'gallery' => $this->_view->gallery->id,
148: 'image' => $others[$id]['image_id']),
149: true);
150: } else {
151: unset($others[$id]);
152: }
153: }
154: $geodata = array_values(array_merge($geodata, $others));
155:
156:
157:
158: if (empty($noGeotag)) {
159: $content .= '<div id="ansel_map"></div>';
160: $content .= '<div class="ansel_geolocation">';
161: $content .= '<div id="ansel_locationtext"></div>';
162: $content .= '<div id="ansel_latlng"></div>';
163: $content .= '<div id="ansel_relocate"></div><div id="ansel_deleteGeotag"></div></div>';
164: $content .= '<div id="ansel_map_small"></div>';
165: } elseif ($permsEdit) {
166:
167: $addurl = Horde::url('map_edit.php')->add(
168: 'image',
169: $this->_params['images'][0]);
170: $addLink = $addurl->link(
171: array('onclick' => Horde::popupJs(
172: Horde::url('map_edit.php'),
173: array('params' => array('image' => $this->_params['images'][0]), 'urlencode' => true, 'width' => '750', 'height' => '600'))
174: . 'return false;'));
175:
176: $imgs = $ansel_storage
177: ->getRecentImagesGeodata($GLOBALS['registry']->getAuth());
178: if (count($imgs) > 0) {
179: $imgsrc = '<div class="ansel_location_sameas">';
180: foreach ($imgs as $id => $data) {
181: if (!empty($data['image_location'])) {
182: $title = $data['image_location'];
183: } else {
184: $title = $this->_point2Deg($data['image_latitude'], true)
185: . ' '
186: . $this->_point2Deg($data['image_longitude']);
187: }
188: $imgsrc .= $addurl->link(
189: array('
190: title' => $title,
191: 'onclick' => "Ansel.widgets.geotag.setLocation(" . $image_id . ",'" . $data['image_latitude'] . "', '" . $data['image_longitude'] . "'); return false"
192: ))
193: . '<img src="' . Ansel::getImageUrl($id, 'mini', true) . '" alt="[image]" /></a>';
194: }
195: $imgsrc .= '</div>';
196: $content .= sprintf(_("No location data present. Place using %s map %s or click on image to place at the same location."), $addLink, '</a>') . $imgsrc;
197: } else {
198: $content .= sprintf(_("No location data present. You may add some %s."), $addLink . _("here") . '</a>');
199: }
200: } else {
201: $content .= _("No location data present.");
202: }
203:
204: } else {
205:
206: $image_id = 0;
207: $content .= '<div id="ansel_map"></div>'
208: . '<div id="ansel_locationtext" style="min-height: 20px;"></div>'
209: . '<div id="ansel_map_small"></div>';
210: }
211: $content .= '</div>';
212:
213:
214: $json = Horde_Serialize::serialize(array_values($geodata), Horde_Serialize::JSON);
215: $html .= <<<EOT
216: <script type="text/javascript">
217: Ansel.widgets = Ansel.widgets || {};
218: Ansel.widgets.geotag = new AnselGeoTagWidget(
219: {$json},
220: {
221: smallMap: 'ansel_map_small',
222: mainMap: 'ansel_map',
223: viewType: '{$viewType}',
224: relocateUrl: '{$url}',
225: relocateText: '{$rtext}',
226: markerLayerTitle: '{$thisTitleText}',
227: imageLayerTitle: '{$otherTitleText}',
228: defaultBaseLayer: '{$defaultLayer}',
229: deleteGeotagText: '{$dtext}',
230: hasEdit: {$permsEdit},
231: updateEndpoint: '{$impleUrl}',
232: layerUpdateEndpoint: '{$layerImpleUrl}',
233: geocoder: "{$GLOBALS['conf']['maps']['geocoder']}"
234: }
235: );
236: EOT;
237: if (empty($noGeotag)) {
238: $html .= "\n" . 'Event.observe(window, "load", function() { Ansel.widgets.geotag.doMap(); });' . "\n";
239: }
240: $html .= '</script>' . "\n";
241: $html .= $content. $this->_htmlEnd();
242:
243: return $html;
244: }
245:
246: 247: 248: 249:
250: protected function _getGalleryImagesWithGeodata()
251: {
252: return $GLOBALS['injector']
253: ->getInstance('Ansel_Storage')
254: ->getImagesGeodata(array(), $this->_view->gallery->id);
255: }
256:
257: 258: 259: 260: 261: 262: 263: 264:
265: protected function _point2Deg($value, $lat = false)
266: {
267: $letter = $lat ? ($value > 0 ? "N" : "S") : ($value > 0 ? "E" : "W");
268: $value = abs($value);
269: $deg = floor($value);
270: $min = floor(($value - $deg) * 60);
271: $sec = ($value - $deg - $min / 60) * 3600;
272: return $deg . "° " . $min . '\' ' . round($sec, 2) . '" ' . $letter;
273: }
274:
275: }
276: