1<?php
2if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4require_once(DOKU_PLUGIN.'admin.php');
5
6/**
7 * All DokuWiki plugins to extend the admin function
8 * need to inherit from this class
9 */
10class admin_plugin_freesync extends DokuWiki_Admin_Plugin {
11
12	var $fshlp = null;
13
14	var $_sync_list_pages = null;
15	var $_sync_list_files = null;
16	var $_sum = '';
17	var $_showSync = 0;
18	var $_showDoSync = 0;
19
20	function admin_plugin_freesync () {
21		$this->fshlp =& plugin_load('helper', 'freesync');
22		if(!$this->fshlp) msg('Loading the freesync helper failed. Make sure that the tags plugin is installed.', -1);
23	}
24
25	/**
26	 * return some info
27	 */
28	function getInfo(){
29		return array(
30        'author' => 'Mikhail I. Izmestev',
31        'email'  => 'izmmishao5@gmail.com',
32        'date'   => '2009-03-09',
33        'name'   => 'freesync dokuwiki plugin',
34        'desc'   => 'Free sync plugin',
35        'url'    => '',
36		);
37	}
38
39	/**
40	 * return sort order for position in admin menu
41	 */
42	function getMenuSort() {
43		return 999;
44	}
45
46	/**
47	 * handle user request
48	 */
49	function handle() {
50
51		$fn   = $_REQUEST['fn'];
52
53		if (is_array($fn)) {
54			$cmd = key($fn);
55			$param = is_array($fn[$cmd]) ? $fn[$cmd] : null;
56		} else {
57			$cmd = $fn;
58			$param = null;
59		}
60
61		$this->fshlp->loadProfile($_REQUEST['profile']);
62
63		switch($cmd) {
64			case "save_profile":	$this->fshlp->saveProfile($_REQUEST['oldname'], $param); break;
65			case "sync":		$this->_sync(); break;
66			case "dosync":		$this->_dosync($_REQUEST['pages'], $_REQUEST['files'], $_REQUEST['sum']); break;
67		}
68
69	}
70
71	function _dosync($pages, $files, $summary) {
72		$this->_sum = $summary;
73		$this->_showDoSync = 1;
74		function needmerge($var) {
75			return $var != 'nothing';
76		}
77
78		if(!is_array($pages))
79			$pages = array();
80		if(!is_array($files))
81			$files = array();
82
83		$this->_sync_list_pages = array_filter($pages, "needmerge");
84		$this->_sync_list_files = array_filter($files, "needmerge");
85	}
86
87	function _sync() {
88		$this->_sync_list = $this->fshlp->getPagesDiff();
89		if(empty($this->_sync_list))
90		msg("All pages are synchronized");
91		else
92		$this->_showSync = 1;
93	}
94
95	function _html_profileSelect() {
96		ptln('<div class="half"><fieldset>');
97		ptln('<legend>'.$this->getLang('sync_profile').'</legend>');
98		ptln('<form method="post">');
99		ptln('<select name="profile" class="edit">');
100		ptln('<option value="new">'.$this->getLang('new_profile').'</option>');
101
102		$profiles = $this->fshlp->getProfiles();
103		reset($profiles);
104		while($profile = each($profiles)) {
105			ptln('<option value="'.$profile['value'].'"'.
106			(($profile['value'] == $this->fshlp->getProfileName())?'selected="selected"':'').
107	    '>'.$profile['value'].'</option>');
108		}
109		ptln('</select>');
110		ptln('<input class="button" type="submit" value="'.$this->getLang('select').'"/>');
111		ptln('</form>');
112		ptln('</fieldset>');
113
114		if($this->fshlp->getProfileName() != "") {
115			$this->_html_startSync();
116		}
117
118		ptln('</div>');
119	}
120
121	function _html_profileEdit() {
122		$profile = $this->fshlp->getProfile();
123		ptln('<div class="half"><fieldset>');
124		ptln('<legend>Edit '.$this->fshlp->getProfileName().' profile</legend>');
125		ptln('<table class="inline"><form method="post">');
126		ptln('<tr><td><label for="xmlrpcurl">'.$this->getLang('xmlrpcurl').'</label></td>');
127		ptln('<td><input type="text" class="edit" name="fn[save_profile][xmlrpcurl]" id="xmlrpcurl" value="'.$profile['xmlrpcurl'].'"/></td></tr>');
128		ptln('<tr><td><label for="namespace">'.$this->getLang('namespace').'</label></td>');
129		ptln('<td><input type="text" class="edit" name="fn[save_profile][namespace]" id="namespace" value="'.$profile['namespace'].'"/></td></tr>');
130		ptln('<tr><td><label for="user">'.$this->getLang('user').'</label></td>');
131		ptln('<td><input type="text" class="edit" name="fn[save_profile][user]" id="user" value="'.$profile['user'].'"/></td></tr>');
132		ptln('<tr><td><label for="pass">'.$this->getLang('pass').'</label></td>');
133		ptln('<td><input type="text" class="edit" name="fn[save_profile][pass]" id="pass" value="'.$profile['pass'].'"/></td></tr>');
134
135		ptln('<tr><td><label for="pages">'.$this->getLang('pages').'</label></td>');
136		ptln('<td><input type="checkbox" class="edit" name="fn[save_profile][pages]" id="pages" '.($profile['pages']?'checked="checked"':'').'"/></td></tr>');
137		ptln('<tr><td><label for="files">'.$this->getLang('files').'</label></td>');
138		ptln('<td><input type="checkbox" class="edit" name="fn[save_profile][files]" id="files" '.($profile['files']?'checked="checked"':'').'"/></td></tr>');
139
140		ptln('<tr><td colspan="2"><input type="hidden" name="oldname" value="'.$this->fshlp->getProfileName().'"/>');
141		ptln('<input type="submit" class="button" name="fn[save_profile][save]" value="'.$this->getLang('save').'"/></td>');
142		ptln('</form>');
143		ptln('</table></fieldset></div>');
144	}
145
146	function _html_startSync() {
147		ptln('<div><fieldset>');
148		ptln('<legend>'.$this->getLang('start_sync').'</legend>');
149		ptln($this->fshlp->getProfileName().'<br/>');
150		ptln('<form method="post">');
151		ptln('<input type="hidden" name="profile" value="'.$this->fshlp->getProfileName().'"/>');
152		ptln('<input type="submit" name="fn[sync][sync]" class="button" value="'.$this->getLang('sync').'"/>');
153		ptln('</form>');
154		ptln('</fieldset></div>');
155	}
156
157	function _html_showSync() {
158		ptln('<div>');
159		ptln('<form method="post">');
160		ptln('<table class="inline">');
161		ptln('<thead>');
162		ptln('<tr><th>'.$this->getLang('page').'</th><th>'.$this->getLang('local').'</th><th>&gt;</th><th>=</th><th>&lt;</th><th>'.$this->getLang('remote').'</th><th>'.$this->getLang('diff').'</th></tr>');
163		ptln('</thead>');
164		ptln('<tbody>');
165
166		while(list($p, $page) = each($this->_sync_list)) {
167			ptln('<tr><td>'.$p.'</td>');
168			ptln('<td>'.(($page['llastModified'])?date("Y/m/d H:i (", $page['llastModified']).$page['lsize'].' bytes)':'-').'</td>');
169			ptln('<td><input class="edit" type="radio" name="'.($page["file"]?'files':'pages').'['.$p.']" value="l2r" '.(($page['llastModified']>$page['rlastModified'])?'checked="checked"':'').'/></td>');
170			ptln('<td><input class="edit" type="radio" name="'.($page["file"]?'files':'pages').'['.$p.']" value="nothing"/></td>');
171			ptln('<td><input class="edit" type="radio" name="'.($page["file"]?'files':'pages').'['.$p.']" value="r2l" '.(($page['llastModified']<$page['rlastModified'])?'checked="checked"':'').'/></td>');
172			ptln('<td>'.(($page['rlastModified'])?date("Y/m/d H:i (", $page['rlastModified']).$page['rsize'].' bytes)':'-').'</td>');
173			ptln('<td>'.((!$page["file"] && $page['llastModified'] && $page['rlastModified'])?'<a href="javascript:plugin_freesync_diff(\''.$p.'\', \''.$this->fshlp->getProfileName().'\')">Diff</a>':'').'</td>');
174			ptln('</tr>');
175		}
176		ptln('</tbody>');
177		ptln('</table>');
178		ptln('<input type="hidden" name="profile" value="'.$this->fshlp->getProfileName().'"/>');
179		ptln('<label>'.$this->getLang('sum').'<input class="edit" name="sum" type="text"/></label>');
180		ptln('<input type="submit" class="button" name="fn[dosync]" value="'.$this->getLang('dosync').'"/>');
181		ptln('</form>');
182		ptln('</div>');
183	}
184
185	function _html_doSync() {
186		ptln('<ul>');
187		if(!empty($this->_sync_list_pages)) {
188			foreach($this->_sync_list_pages as $page => $direction) {
189				ptln('<li>');
190				ptln($this->getLang('pull').' '.$page.'...');
191				ptln('<span id="'.$page.'">');
192				ptln('<script type="text/javascript" language="javascript">');
193				ptln('_sync_queue.push(Array("'.$page.'", "call=_freesync_page_'.$direction.'&profile='.$this->fshlp->getProfileName().'&id='.$page.'&sum='.$this->_sum.'"));');
194				ptln('</script>');
195				ptln('</span>');
196				ptln('</li>');
197			}
198		}
199		if(!empty($this->_sync_list_files)) {
200			foreach($this->_sync_list_files as $file => $direction) {
201				ptln('<li>');
202				ptln($this->getLang('pull').' '.$file.'...');
203				ptln('<span id="'.$file.'">');
204				ptln('<script type="text/javascript" language="javascript">');
205				ptln('_sync_queue.push(Array("'.$file.'", "call=_freesync_file_'.$direction.'&profile='.$this->fshlp->getProfileName().'&id='.$file.'&sum='.$this->_sum.'"));');
206				ptln('</script>');
207				ptln('</span>');
208				ptln('</li>');
209			}
210		}
211		ptln('</ul>');
212		ptln('<script type="text/javascript" language="javascript">');
213		ptln("addEvent(window,'load', plugin_freesync_sync);");
214		ptln('</script>');
215
216	}
217
218	/**
219	 * output appropriate html
220	 */
221	function html() {
222		print $this->locale_xhtml('intro');
223
224		ptln('<div id="free__sync">');
225
226		if($this->_showSync) {
227			$this->_html_showSync();
228		}elseif($this->_showDoSync) {
229			$this->_html_doSync();
230		}else {
231			$this->_html_profileSelect();
232			$this->_html_profileEdit();
233		}
234
235		ptln('</div>');
236	}
237
238}
239