1<?php
2/**
3 * @author Andrés Seré <andres@sere.uy>
4 *
5 */
6
7namespace dokuwiki\plugin\structgeohash\types;
8
9use dokuwiki\plugin\struct\types\AbstractBaseType;
10//use dokuwiki\plugin\struct\types\TraitFilterPrefix;
11//use dokuwiki\plugin\struct\meta\QueryBuilder;
12//use dokuwiki\plugin\struct\meta\QueryBuilderWhere;
13
14class GeoHash extends AbstractBaseType {
15
16	protected $config = array (
17		'default' => '69zguzduymzu',
18		'urlTMP' => 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw',
19		'attribution' => 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
20    		'mapId' => 'mapbox/streets-v11',
21    		'maxZoom' => 18,
22	);
23
24	/**
25	 * @inheritDoc
26	 */
27	public function validate($rawvalue) {
28		// todo: rise an exception if given an error.
29		if(!preg_match('/^[a-z0-9]{12}$/', $rawvalue)) {
30			$rawvalue = $this->config['default'];
31		}
32		return $rawvalue;
33	}
34
35	/**
36	 * @inheritDoc
37	 */
38	public function renderValue($value, \Doku_Renderer $R, $mode) {
39		if( $mode == 'xhtml' ) {
40			$R->doc .= $this->valueEditor('dummy', $value, 0);
41		}
42		else {
43			$R->cdata($value);
44		}
45		return true;
46	}
47
48	/**
49	 * @inheritDoc
50	 */
51	public function valueEditor($name, $rawvalue, $htmlID) {
52		if(!preg_match('/^[a-z0-9]{12}$/', $rawvalue)) {
53			$rawvalue = $this->config['default'];
54		}
55
56		$class = 'struct_' . strtolower($this->getClass());
57
58		$params = array(
59			'name' => $name,
60			'value' => $rawvalue,
61			'class' => $class,
62			//'type' => 'geohash',
63			'id' => $htmlID,
64		);
65
66		$params_div = array(
67			'class' => $class.'_map',
68			//'type' => 'geohash_map',
69			'id' => $htmlID.'_map',
70			'data-urlTMP' => $this->config['urlTMP'],
71			'data-attribution' => $this->config['attribution'],
72    			'data-maxZoom' => $this->config['maxZoom'],
73			'data-mapId' => $this->config['mapId'],
74		);
75
76		$attributes = buildAttributes($params, true);
77		$attributes_div = buildAttributes($params_div, true);
78		// When is called from renderValue function, name='dummy';
79		// the input field must be disabled.
80		if ( $name == 'dummy' )
81			return "<input $attributes disabled><div $attributes_div></div>";
82		else
83			return "<input $attributes><div $attributes_div></div>";
84	}
85
86}
87
88
89
90
91
92
93
94
95
96