1<?php
2/**
3 * Plugin : autolink3
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Arthur Lobert <arthur.lobert@gmail.com>
7 */
8
9if(!defined('DOKU_TINC')) define('DOKU_TINC',realpath(dirname(__FILE__).'/../../../').'/');
10if(!defined('DOKU_PAGE')) define('DOKU_PAGE', DOKU_TINC.'data/pages/');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_TINC.'lib/plugins/');
12if (!defined('DOKU_REG')) define ('DOKU_REG', DOKU_PLUGIN.'autolink3/register/');
13require_once(DOKU_PLUGIN.'admin.php');
14
15/**
16 * All DokuWiki plugins to extend the admin function
17 * need to inherit from this class
18 */
19class admin_plugin_autolink3 extends DokuWiki_Admin_Plugin
20{
21	function getInfo()
22	{
23		return array(
24            'author' => 'Arthur Lobert',
25            'email'  => 'arthur.lobert@gmail.com',
26            'date'   => @file_get_contents(DOKU_PLUGIN.'autolink3/VERSION'),
27            'name'   => 'autolink3',
28            'desc'   => 'Replace key words by appropriates links',
29            'url'    => 'http://www.dokuwiki.org/plugin:autolink3',
30		);
31	}
32
33	function handle(){
34	$this->error = 0;
35		if (isset($_REQUEST['word'])) $word = $_REQUEST['word'];
36		if (isset($_REQUEST['link'])) $page = $_REQUEST['link'];
37		if (isset($_REQUEST['local'])) $local = $_REQUEST['local'];
38		if (isset($_REQUEST['supr'])) mod_link($_REQUEST['supr']);
39		if (isset($_REQUEST['complete']) && isset($_REQUEST['new_link']) && $_REQUEST['new_link'] != '')
40		{
41			$new_ligne = sprintf("%s	%s	%s\r\n", $_REQUEST['new_link'],$_REQUEST['new_page'],$_REQUEST['new_locate']);
42			mod_link($_REQUEST['ligne'], $new_ligne);
43		}
44		$rd = fopen (DOKU_REG."register.txt", "a");
45		$global = sprintf("%s	%s	%s\r\n", $word, $page, $local);
46		if ($local && $word && $local && !is_link_exist($page, $local, $word)) fwrite ($rd, $global);
47			elseif($_REQUEST['add']) $this->error = 1;
48		fclose($rd);
49	}
50
51
52/**
53 * create the link tab of the plugin interface
54 * @return $ret which is the appropriate html code
55 */
56
57	private function _get_link_tab()
58	{
59		if (isset($_REQUEST['mod'])){
60			$ligne = str_replace(';', '	',$_REQUEST['mod']);
61		}
62		$reg = read_file();
63		isset ($_REQUEST['arrow']) ? $reg = sort_tab($reg, compare_alpha,$_REQUEST['arrow']) : $reg = sort_tab($reg, compare_alpha,$_REQUEST['up']);
64		$pages = get_dokupage_tree(DOKU_PAGE, $pages, '', 0);
65			$local[] = ':';
66		$local = get_dokupage_tree(DOKU_PAGE, $local, '', 1);
67
68		foreach($pages as $file):
69		{
70			$page_tree .= "<option value='".$file."'>".$file."</option>";
71		}
72		endforeach;
73		foreach($local as $file):
74		{
75			$data_tree .= "<option value='".$file."'>".$file."</option>";
76		}
77		endforeach ;
78		if (isset($reg))
79		foreach ($reg as $lign):
80		{
81			if ($lign != '\r\n')
82			{
83				$ret .= '<tr onmouseover="this.bgColor=\'#CEF6CE\'" onmouseout="this.bgColor =\'#FFFFFF\'">';
84				if (strcmp($ligne,rtrim($lign[0].'	'.$lign[1].'	'.$lign[2]))){
85					$global = $lign[0].';'.$lign[1].';'.$lign[2];
86					$ret .="<td>".$lign[0]."</td><td>".$lign[1]."</td><td>".$lign[2]."</td>
87								<td>
88
89			 						<input type='image' src='".DOKU_BASE."/lib/plugins/autolink3/ressources/delete.gif' name='supr' value='".$global."' alt='del' />
90			 						<input type='image' src='".DOKU_BASE."/lib/plugins/autolink3/ressources/edit.gif' name='mod' value='".$global."' alt ='edit' />
91			 					</td>
92			 				</tr>";
93				}
94				else {print_r($lign);
95
96							$ret .="<td><input type = 'text' name='new_link' value='".$lign[0]."'/></td>
97							<td><select name = 'new_page'>".'<option value='.$lign[1].'>'.$lign[1].'</option>'.$page_tree."</select></td>
98							<td><select name = 'new_locate'>".'<option value='.$lign[2].'>'.$lign[2].'</option>'.$data_tree."</select></td>
99							<td>
100			 					<input type='image' src='".DOKU_BASE."/lib/plugins/autolink3/ressources/add.gif' name='complete' value='sd".$global."' alt='add' />
101			 				</td></tr>";
102				}
103			}
104		}
105		endforeach;
106		return ($ret);
107	}
108
109	/**
110	 * output appropriate html
111	 */
112
113	function html()
114	{
115		$word = $this->_get_link_tab();
116		ptln('<!-- Pagemove Plugin start -->');
117		$tab = get_dokupage_tree(DOKU_PAGE, $tab, '', 0);
118			$local[] = ':';
119		$local = get_dokupage_tree(DOKU_PAGE, $local, '', 1);
120		foreach($tab as $file):
121		{
122			$page_tree .= "<option value='".$file."'>".$file."</option>";
123		}
124		endforeach;
125		foreach($local as $file):
126		{
127			$data_tree .= "<option value='".$file."'>".$file."</option>";
128		}
129		endforeach ;
130		//initialisation des fleches de tri
131		$tab = array(0,0,0);
132		isset ($_REQUEST['arrow']) ? $mem = $_REQUEST['arrow'] : $mem = $_REQUEST['up'];
133		$tab[$mem] = 1;
134		if ($this->error == 1)
135			ptln ("<div class='error'>".$this->getLang('error')."</div>");
136		echo "
137		<div id='autolink3'>
138			<h1 align='center'>
139				Auto Link
140			</h1>
141			".$this->getLang('description')."
142			<form action='".wl()."' method='post' accept-chartset='utf-8'>
143				<input type='hidden' name='do' value='admin' />
144				<input type='hidden' name='add' value=''/>
145				<input type='hidden' name='page' value='autolink3' />
146				<input type='hidden' name='ligne' value='".$_REQUEST['mod']."' />
147				<input type='hidden' name='up' value='".$mem."' />
148				<table>
149					<tr>
150						<th><div style='width:80%;float:left;'>".$this->getLang('link')."</div><div align='right' style='width:16px;float:left;'><input type='image' src='".DOKU_BASE."/lib/plugins/autolink3/ressources/arrow_".($tab[0] ? "up" : "down").".gif' alt='sort' name='arrow' value ='0'/></div></th>
151						<th><div style='width:80%;float:left;'>".$this->getLang('to')."</div><div align='right' style='width:16px;float:left;'><input type='image' src='".DOKU_BASE."/lib/plugins/autolink3/ressources/arrow_".($tab[1] ? "up" : "down").".gif' alt='sort' name='arrow' value ='1'/></div></th>
152						<th><div style='width:80%;float:left;'>".$this->getLang('in')."</div><div align='right' style='width:16px;float:left;'><input type='image' src='".DOKU_BASE."/lib/plugins/autolink3/ressources/arrow_".($tab[2] ? "up" : "down").".gif' alt='sort' name='arrow' value ='2'/></div></th>
153						<th>".$this->getLang('action')." </th>
154					</tr>
155					".$word."
156					<tr>
157	        			<td>
158	        				<input type = 'text' name='word'/>
159	        			</td>
160	        			<td>
161	        				<select name = 'link'>
162							".$page_tree."
163							</select>
164						</td>
165						<td>
166							<select name = 'local'>
167							".$data_tree."
168							</select>
169						</td>
170						<td>
171							<input type='image' src='".DOKU_BASE."/lib/plugins/autolink3/ressources/add.gif' alt='add' name='add' value = 1/>
172						</td>
173					</tr>
174				</table>
175			</form>
176		<script type='text/javascript' src='".DOKU_BASE."/lib/plugins/autolink3/script.js'>
177		</script>
178		<fieldset class='pl_si_out'>
179			<button class='button' id='pl_si_gobtn' onclick='plugin_tagindex_go()'>
180				Rebuild Linkindex
181			</button>
182			<div id='pl_si_out'>
183        		<img src='".DOKU_BASE."/lib/images/loading.gif' id='pl_si_throbber' style='visibility: hidden' alt='load'/>
184			</div>
185		</fieldset>
186	</div>";
187	}
188}
189