1<?php
2
3/**
4 * Plugin bahnde: Search-Form for Bahn.de
5 *
6 * @license    GNU
7 * @author     Juergen A.Lamers <jaloma.ac@googlemail.com>
8 */
9
10if (!defined('DOKU_INC'))
11	define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
12if (!defined('DOKU_PLUGIN'))
13	define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
14require_once (DOKU_PLUGIN . 'syntax.php');
15
16/**
17 * All DokuWiki plugins to extend the parser/rendering mechanism
18 * need to inherit from this class
19 */
20
21class syntax_plugin_bahnde extends DokuWiki_Syntax_Plugin {
22
23  var $dflt = array(
24  'start_station' => '',
25  'end_station' => '',
26  'start_date' => '',
27  'start_time' =>'',
28  'allow_toggle_station' => false,
29  'departure' => '',
30);
31
32	/**
33	 * return some info
34	 */
35	function getInfo() {
36		return array (
37			'author' => 'Juergen A.Lamers',
38			'email' => 'jaloma.ac@googlemail.com',
39			'date' => '2008-12-01',
40			'name' => 'bahnde -- www.reiseauskunft.de Plugin',
41			'desc' => 'Bahn.de Formular for your Site',
42			'url' => 'http://wiki.dokuwiki.org/plugin:bahnde',
43		);
44	}
45
46	/**
47	 * What kind of syntax are we?
48	 */
49	function getType() {
50		return 'substition';
51	}
52
53	/**
54	 * What about paragraphs? (optional)
55	 */
56	function getPType() {
57		return 'block';
58	}
59
60	/**
61	 * Where to sort in?
62	 */
63	function getSort() {
64		return 999;
65	}
66
67	/**
68	 * Connect pattern to lexer
69	 */
70	function connectTo($mode) {
71		$this->Lexer->addSpecialPattern('<bahnde.*?/>', $mode, 'plugin_bahnde');
72	}
73
74	function matchLength() {
75		return strlen("<bahnde");
76	}
77
78	/**
79	 * Handle the match
80	 */
81	function handle($match, $state, $pos, & $handler) {
82    	$match = html_entity_decode(substr($match,$this->matchLength(),-2));
83    	$gmap = $this->_extract_params($match);
84    	return $gmap;
85	}
86
87  /**
88   * extract parameters for the googlemap from the parameter string
89   *
90   * @param   string    $str_params   string of key="value" pairs
91   * @return  array                   associative array of parameters key=>value
92   */
93  function _extract_params($str_params) {
94    $param = array();
95    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
96    if (sizeof($param) == 0) {
97      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
98    }
99    // parse match for instructions, break into key value pairs
100    $gmap = $this->dflt;
101    foreach($param as $kvpair) {
102      list($match,$key,$val) = $kvpair;
103//      $key = strtolower($key);
104      if (isset($gmap[$key])) $gmap[$key] = $val;
105    }
106
107    return $gmap;
108  }
109
110	/**
111	 * Create output
112	 */
113	function render($mode, & $renderer, $data) {
114		if ($mode == 'xhtml') {
115		  $start_station = $data['start_station'];
116		  $end_station = $data['end_station'];
117		  $start_date = $data['start_date'];
118		  $start_time = $data['start_time'];
119		  $departure = $data['departure'];
120		  $allow_toggle_station = $data['allow_toggle_station'];
121		  $txt = $this->bahnDeDiv($end_station, $start_date, $start_time, $departure, $allow_toggle_station, $start_station);
122		  $renderer->doc .= $txt;
123		  return true;
124		}
125		return false;
126	}
127
128	function bahnDeDiv($end_station, $start_date, $start_time, $departure, $allow_toggle_station, $start_station) {
129//		$txt = '++'.$start_station.'--';
130		$txt = '';
131		$txt .= '<div style="background-image:url('."'http://www.db.de/site/bahn/layout,property=LOGO__PROVIDER__IMG.gif'".');background-repeat:no-repeat;background-position:bottom left;width=200px">';
132$txt .= '		<form accept-charset="ISO-8859-1" action="http://reiseauskunft.bahn.de/bin/query.exe/dn" name="auskunft" method="post" id="auskunft" target="_blank">';
133
134$txt .= '			<input type="hidden" name="start" value="1" />';
135if ($start_date != null) {
136$txt .= '<input type="hidden" name="returnTime" value="'.$start_date.'" />';
137}
138$txt .= '			<input type="hidden" name="returnTimesel" value="depart" />';
139$txt .= '			<input type="hidden" name="REQ0Tariff_TravellerType.1" value="E"  />';
140$txt .= '			<input type="hidden" name="REQ0Tariff_TravellerReductionClass.1" value="0"  />';
141$txt .= '			<input type="hidden" name="REQ0Tariff_Class" value="2"  />';
142$txt .= '			<input type="hidden" name="REQ0JourneyProduct_prod_list" id="REQ0JourneyProduct_prod_list" value="1:1111111111000000"  />';
143
144$txt .= '			<table cellpadding="0" cellspacing="0" border="0">';
145$txt .= '			<tr><td>';
146
147$txt .= '				<table cellspacing="0" cellpadding="1" style="margin-top:3px;margin-bottom:3px;">';
148$txt .= '					<tr>';
149$txt .= '			            <td>';
150$txt .= '							<a href="#" id="bf" title="Geben Sie eine Station ein, um hier einen Abfahrtsplan zu &ouml;ffnen"><label for="S">Von:</label><br />';
151$txt .= '						</td>';
152
153$txt .= '						<td colspan="2">';
154if ($start_station != null) {
155$txt .= '							<input id="S" name="S" type="hidden" size="18" value="'.$start_station.'"/>'.$start_station.'<br />';
156} else {
157$txt .= '							<input id="S" name="S" type="text" onKeyUp="updateurl();" size="18" /><br />';
158}
159$txt .= '						</td>';
160
161$txt .= '					</tr>';
162$txt .= '					<tr>';
163$txt .= '						<td>';
164$txt .= '							<label for="Z">Nach:</label><br />';
165$txt .= '						</td>';
166$txt .= '						<td colspan="2">';
167if ($end_station != null ) {
168$txt .= '							<input id="Z" name="Z" type="hidden" size="18" value="'.$end_station.'"/>'.$end_station.'<br />';
169
170} else {
171$txt .= '							<input id="Z" name="Z" type="text" size="18" />&nbsp;<br />';
172}
173$txt .= '						</td>';
174$txt .= '					</tr>';
175$txt .= '					<tr>';
176$txt .= '						<td>';
177$txt .= '							<label for="date">Datum:</label>';
178$txt .= '						</td>';
179$txt .= '						<td>';
180if ($start_date != null) {
181$txt .= '							<input id="date" name="date" type="hidden" maxlength="10" size="6" value="'.$start_date.'"/>'.$start_date;
182} else {
183$txt .= '							<input id="date" name="date" type="text" maxlength="10" size="6" />';
184}
185$txt .= '						</td>';
186$txt .= '						<td rowspan="2">';
187if ($departure != '') {
188	if (($departure == true || $departure == "true")) {
189		$time_prefix = "Abfahrtszeit";
190		$txt .= '							<input type="hidden" checked="1" name="timesel" value="depart" id="T1" />';
191	}
192	if (($departure == false || $departure=="false")) {
193		$time_prefix = "Ankunftszeit";
194		$txt .= '							<input type="hidden" checked="1" name="timesel" value="arrive" id="T2" />';
195	}
196} else {
197  	$txt .= '							<label for="T1">Abfahrt</label><br />';
198  	$txt .= '							<input type="radio" name="timesel" value="depart" id="T1" checked="1"/>';
199	$txt .= '							<label for="T2">Ankunft</label>';
200	$txt .= '							<input type="radio" name="timesel" value="arrive" id="T2" />';
201}
202$txt .= '						</td>';
203$txt .= '					</tr>';
204$txt .= '					<tr>';
205$txt .= '						<td>';
206if ($time_prefix != null) {
207$txt .= '							<label for="time">'.$time_prefix.':&nbsp;</label><br />';
208} else {
209$txt .= '							<label for="time">Uhrzeit:&nbsp;</label><br />';
210}
211$txt .= '						</td>';
212$txt .= '						<td>';
213if ($start_time != null) {
214$txt .= '							<input id="time" name="time" type="hidden" maxlength="5" size="6" value="'.$start_time.'"/>'.$start_time;
215
216} else {
217$txt .= '							<input id="time" name="time" type="text" maxlength="5" size="6" />';
218}
219$txt .= '						</td>';
220$txt .= '					</tr>';
221$txt .= '					<tr>';
222$txt .= '						<td colspan="3">';
223$txt .= '							<input name="submitButton" value="Verbindung suchen" class="submit" alt="Submit" type="submit" />';
224$txt .= '						</td>';
225$txt .= '					</tr>';
226$txt .= '				</table>';
227
228$txt .= '			</td>';
229if ($allow_toggle_station == 'true') {
230$txt.='<td valign="top">';
231$txt .= '				<table class="tables" cellpadding="0" cellspacing="0" border="0">';
232$txt .= '					<tr>';
233$txt .= '						<td>';
234$txt .= '							<input type="button" class="switch" onClick="flipStations();" value=" " title="Stationen tauschen" />';
235$txt .= '							<img onClick="flipStations();" src="http://projects.ff22.de/gadgets/flip.gif" title="Stationen tauschen" class="imgs" />';
236$txt .= '						</td>';
237$txt .= '					</tr>';
238$txt .= '				</table>';
239$txt .= '			</td>';
240}
241$txt .= '			</tr>';
242$txt .= '			</table>';
243
244$txt .= '		</form>';
245$txt .= '	</div>';
246		return $txt;
247	}
248} // Class
249
250
251//Setup VIM: ex: et ts=4 :