Completed
Push — master ( 097360...16d695 )
by Mark
02:46
created

connectTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * Copyright (c) 2014-2016 Mark C. Prins <[email protected]>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
if (!defined('DOKU_INC')) {
18
	die ();
19
}
20
if (!defined('DOKU_PLUGIN')) {
21
	define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
22
}
23
require_once (DOKU_PLUGIN . 'syntax.php');
24
25
/**
26
 * DokuWiki Plugin dokuwikispatial (findnearby Syntax Component).
27
 *
28
 * @license BSD license
29
 * @author Mark C. Prins <[email protected]>
30
 */
31
class syntax_plugin_spatialhelper_findnearby extends DokuWiki_Syntax_Plugin {
32
	/**
33
	 *
34
	 * @see DokuWiki_Syntax_Plugin::getType()
35
	 */
36
	public function getType() {
37
		return 'substition';
38
	}
39
40
	/**
41
	 * Return 'normal' so this syntax can be rendered inline.
42
	 *
43
	 * @see DokuWiki_Syntax_Plugin::getPType()
44
	 */
45
	public function getPType() {
46
		return 'normal';
47
	}
48
49
	/**
50
	 *
51
	 * @see Doku_Parser_Mode::getSort()
52
	 */
53
	public function getSort() {
54
		return 307;
55
	}
56
57
	/**
58
	 * define our special pattern: {{findnearby>Some linkt text or nothing}}.
59
	 *
60
	 * @see Doku_Parser_Mode::connectTo()
61
	 */
62
	public function connectTo($mode) {
63
		$this->Lexer->addSpecialPattern('\{\{findnearby>.*?\}\}', $mode, 'plugin_spatialhelper_findnearby');
64
	}
65
66
	/**
67
	 * look up the page's geo metadata and pass that on to render.
68
	 *
69
	 * @see DokuWiki_Syntax_Plugin::handle()
70
	 */
71
	public function handle($match, $state, $pos, Doku_Handler $handler) {
72
		$data = array();
73
		$data [0] = trim(substr($match, strlen('{{findnearby>'), - 2));
74
		if (strlen($data [0]) < 1) {
75
			$data [0] = $this->getLang('search_findnearby');
76
		}
77
		$meta = p_get_metadata(getID(), 'geo');
78
		if ($meta) {
79
			if ($meta ['lat'] && $meta ['lon']) {
80
				$data [1] = array(
81
						'do' => 'findnearby',
82
						'lat' => $meta ['lat'],
83
						'lon' => $meta ['lon']
84
				);
85
			} elseif ($meta ['geohash']) {
86
				$data [1] = array(
87
						'do' => 'findnearby',
88
						'geohash' => $meta ['geohash']
89
				);
90
			}
91
			return $data;
92
		}
93
		return false;
94
	}
95
96
	/**
97
	 * Render a link to a search page.
98
	 *
99
	 * @see DokuWiki_Syntax_Plugin::render()
100
	 */
101
	public function render($mode, Doku_Renderer $renderer, $data) {
102
		if ($data === false) {
103
					return false;
104
		}
105
106
		if ($mode == 'xhtml') {
107
			$renderer->doc .= '<a href="' . wl(getID(), $data [1]) . '" class="findnearby">' . hsc($data [0]) . '</a>';
108
			return true;
109
		} elseif ($mode == 'metadata') {
110
			return false;
111
		} elseif ($mode == 'odt') {
112
			// don't render anything in ODT
113
			return false;
114
		}
115
		return false;
116
	}
117
}
118