Skip to content

Commit 738d2f7

Browse files
committed
more refactoring on the admin interface
1 parent 1e723ea commit 738d2f7

File tree

2 files changed

+147
-110
lines changed

2 files changed

+147
-110
lines changed

ProfileManager.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ public function getProfileConfig($num) {
3838
throw new SyncException('No such profile');
3939
}
4040

41+
/**
42+
* Return an empty profile config
43+
*
44+
* @return array
45+
*/
46+
public function getEmptyConfig() {
47+
return [
48+
'server' => '',
49+
'ns' => '',
50+
'depth' => 0,
51+
'user' => '',
52+
'pass' => '',
53+
'timeout' => 15,
54+
];
55+
}
56+
4157
/**
4258
* Set the given config for the given profile
4359
*
@@ -56,6 +72,36 @@ public function setProfileConfig($num, $data) {
5672
$this->save();
5773
}
5874

75+
/**
76+
* List all profiles with a nice label
77+
*
78+
* @return array
79+
*/
80+
public function getProfileLabels() {
81+
$labels = [];
82+
foreach($this->profiles as $idx => $profile) {
83+
$label = parse_url($profile['server'], PHP_URL_HOST);
84+
if($profile['user'] !== '') $label = $profile['user'] . '@' . $label;
85+
if($profile['ns'] !== '') $label .= ':' . $profile['ns'];
86+
$labels[$idx] = $label;
87+
}
88+
89+
return $labels;
90+
}
91+
92+
/**
93+
* Delete a profile
94+
*
95+
* @param int $num
96+
* @throws SyncException
97+
*/
98+
public function deleteProfileConfig($num) {
99+
if(!isset($this->profiles[$num])) throw new SyncException('No such profile');
100+
unset($this->profiles[$num]);
101+
$this->profiles = array_values($this->profiles); //reindex
102+
$this->save();
103+
}
104+
59105
/**
60106
* load profile configuration
61107
*/

admin.php

Lines changed: 101 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
<?php
2+
use dokuwiki\Form\Form;
3+
use dokuwiki\plugin\sync\ProfileManager;
4+
25
// must be run within Dokuwiki
36
if(!defined('DOKU_INC')) die();
4-
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
5-
require_once(DOKU_PLUGIN . 'admin.php');
6-
7-
require_once(DOKU_INC . 'inc/IXR_Library.php');
87

98
/**
109
* All DokuWiki plugins to extend the admin function
1110
* need to inherit from this class
1211
*/
1312
class admin_plugin_sync extends DokuWiki_Admin_Plugin {
1413

15-
protected $profiles = array();
16-
protected $profno = '';
1714
/** @var IXR_Client */
1815
protected $client = null;
1916
protected $apiversion = 0;
2017
protected $defaultTimeout = 15;
2118

19+
protected $profileManager;
20+
protected $profno;
21+
2222
/**
2323
* Constructor.
2424
*/
2525
function __construct() {
26-
$this->_profileLoad();
27-
$this->profno = preg_replace('/[^0-9]+/', '', $_REQUEST['no']);
26+
global $INPUT;
27+
28+
$this->profileManager = new ProfileManager();
29+
$this->profno = $INPUT->int('no', false);
30+
}
31+
32+
/**
33+
* return sort order for position in admin menu
34+
*/
35+
function getMenuSort() {
36+
return 1020;
2837
}
2938

3039
function _connect() {
@@ -72,19 +81,12 @@ function _connect() {
7281
return true;
7382
}
7483

75-
/**
76-
* return sort order for position in admin menu
77-
*/
78-
function getMenuSort() {
79-
return 1020;
80-
}
81-
8284
/**
8385
* handle profile saving/deleting
8486
*/
8587
function handle() {
8688
if(isset($_REQUEST['prf']) && is_array($_REQUEST['prf'])) {
87-
if(isset($_REQUEST['sync__delete']) && $this->profno !== '') {
89+
if(isset($_REQUEST['sync__delete']) && $this->profno !== false) {
8890
// delete profile
8991
unset($this->profiles[$this->profno]);
9092
$this->profiles = array_values($this->profiles); //reindex
@@ -182,18 +184,98 @@ function html() {
182184
echo $this->locale_xhtml('intro');
183185

184186
echo '<div class="sync_left">';
185-
$this->_profilelist($this->profno);
186-
if($this->profno !== '') {
187+
$this->profileDropdown();
188+
if($this->profno !== false) {
187189
echo '<br />';
188190
$this->_profileView();
189191
}
190192
echo '</div>';
191193
echo '<div class="sync_right">';
192-
$this->_profileform($this->profno);
194+
$this->profileForm();
193195
echo '</div>';
194196
}
195197
}
196198

199+
/**
200+
* Dropdown list of available sync profiles
201+
*/
202+
protected function profileDropdown() {
203+
$form = new Form(
204+
[
205+
'action' => wl('', ['do' => 'admin', 'page' => 'sync'], false, '&'),
206+
'method' => 'POST',
207+
]
208+
);
209+
210+
$profiles = $this->profileManager->getProfileLabels();
211+
$profiles[''] = $this->getLang('newprofile');
212+
213+
$form->addFieldsetOpen($this->getLang('profile'));
214+
$form->addDropdown('no', $profiles);
215+
$form->addButton('', $this->getLang('select'));
216+
$form->addFieldsetClose();
217+
218+
echo $form->toHTML();
219+
}
220+
221+
/**
222+
* Form to edit or create a sync profile
223+
*/
224+
protected function profileForm() {
225+
$form = new Form(
226+
[
227+
'action' => wl('', ['do' => 'admin', 'page' => 'sync'], false, '&'),
228+
'method' => 'POST',
229+
]
230+
);
231+
232+
if($this->profno === false) {
233+
$legend = $this->getLang('create');
234+
$profile = $this->profileManager->getEmptyConfig();
235+
} else {
236+
$legend = $this->getLang('edit');
237+
$profile = $this->profileManager->getProfileConfig($this->profno);
238+
}
239+
240+
$depths = [
241+
0 => $this->getLang('level0'),
242+
1 => $this->getLang('level1'),
243+
2 => $this->getLang('level2'),
244+
3 => $this->getLang('level3'),
245+
];
246+
$types = [
247+
0 => $this->getLang('type0'),
248+
1 => $this->getLang('type1'),
249+
2 => $this->getLang('type2'),
250+
];
251+
252+
$form->addFieldsetOpen($legend);
253+
$form->setHiddenField('no', $this->profno);
254+
$form->addTextInput('prf[server]', $this->getLang('server'))->val($profile['server']);
255+
$form->addHTML('<samp>http://example.com/dokuwiki/lib/exe/xmlrpc.php</samp>');
256+
$form->addTextInput('prf[ns]', $this->getLang('ns'))->val($profile['ns']);
257+
$form->addDropdown('prf[depth]', $depths, $this->getLang('depth'))->val($profile['depth']);
258+
$form->addTextInput('prf[user]', $this->getLang('user'))->val($profile['user']);
259+
$form->addPasswordInput('prf[pass]', $this->getLang('pass'))->val($profile['pass']);
260+
$form->addTextInput('prf[timeout]', $this->getLang('timeout'))->val($profile['timeout']);
261+
$form->addDropdown('prf[type]', $types, $this->getLang('type'))->val($profile['type']);
262+
$form->addButton('', $this->getLang('save'))->attr('type', 'submit');
263+
264+
// if($no !== '' && $this->profiles[$no]['ltime']) {
265+
// echo '<small>' . $this->getLang('changewarn') . '</small>';
266+
// }
267+
268+
$form->addFieldsetClose();
269+
270+
if($this->profno !== false) {
271+
$form->addFieldsetOpen($this->getLang('delete'));
272+
$form->addButton('sync__delete', $this->getLang('delete'));
273+
$form->addFieldsetClose();
274+
}
275+
276+
echo $form->toHTML();
277+
}
278+
197279
/**
198280
* Load profiles from serialized storage
199281
*/
@@ -245,97 +327,6 @@ function _profileView() {
245327
echo '</form>';
246328
}
247329

248-
/**
249-
* Dropdown list of available sync profiles
250-
*/
251-
function _profilelist($no = '') {
252-
echo '<form action="" method="post">';
253-
echo '<fieldset><legend>' . $this->getLang('profile') . '</legend>';
254-
echo '<select name="no" class="edit">';
255-
echo ' <option value="">' . $this->getLang('newprofile') . '</option>';
256-
foreach($this->profiles as $pno => $opts) {
257-
$srv = parse_url($opts['server']);
258-
259-
echo '<option value="' . hsc($pno) . '" ' . (($no !== '' && $pno == $no) ? 'selected="selected"' : '') . '>';
260-
echo ($pno + 1) . '. ';
261-
if($opts['user']) echo hsc($opts['user']) . '@';
262-
echo hsc($srv['host']);
263-
if($opts['ns']) echo ':' . hsc($opts['ns']);
264-
echo '</option>';
265-
}
266-
echo '</select>';
267-
echo '<input type="submit" value="' . $this->getLang('select') . '" class="button" />';
268-
echo '</fieldset>';
269-
echo '</form>';
270-
}
271-
272-
/**
273-
* Form to edit or create a sync profile
274-
*/
275-
function _profileform($no = '') {
276-
echo '<form action="" method="post" class="sync_profile">';
277-
echo '<fieldset><legend>';
278-
if($no !== '') {
279-
echo $this->getLang('edit');
280-
} else {
281-
echo $this->getLang('create');
282-
}
283-
echo '</legend>';
284-
285-
echo '<input type="hidden" name="no" value="' . hsc($no) . '" />';
286-
287-
echo '<label for="sync__server">' . $this->getLang('server') . '</label> ';
288-
echo '<input type="text" name="prf[server]" id="sync__server" class="edit" value="' . hsc($this->profiles[$no]['server']) . '" />';
289-
echo '<samp>http://example.com/dokuwiki/lib/exe/xmlrpc.php</samp>';
290-
291-
echo '<label for="sync__ns">' . $this->getLang('ns') . '</label> ';
292-
echo '<input type="text" name="prf[ns]" id="sync__ns" class="edit" value="' . hsc($this->profiles[$no]['ns']) . '" />';
293-
294-
echo '<label for="sync__depth">' . $this->getLang('depth') . '</label> ';
295-
echo '<select name="prf[depth]" id="sync__depth" class="edit">';
296-
echo '<option value="0" ' . (($this->profiles[$no]['depth'] == 0) ? 'selected="selected"' : '') . '>' . $this->getLang('level0') . '</option>';
297-
echo '<option value="1" ' . (($this->profiles[$no]['depth'] == 1) ? 'selected="selected"' : '') . '>' . $this->getLang('level1') . '</option>';
298-
echo '<option value="2" ' . (($this->profiles[$no]['depth'] == 2) ? 'selected="selected"' : '') . '>' . $this->getLang('level2') . '</option>';
299-
echo '<option value="3" ' . (($this->profiles[$no]['depth'] == 3) ? 'selected="selected"' : '') . '>' . $this->getLang('level3') . '</option>';
300-
echo '</select>';
301-
302-
echo '<label for="sync__user">' . $this->getLang('user') . '</label> ';
303-
echo '<input type="text" name="prf[user]" id="sync__user" class="edit" value="' . hsc($this->profiles[$no]['user']) . '" />';
304-
305-
echo '<label for="sync__pass">' . $this->getLang('pass') . '</label> ';
306-
echo '<input type="password" name="prf[pass]" id="sync__pass" class="edit" value="' . hsc($this->profiles[$no]['pass']) . '" />';
307-
308-
echo '<label for="sync__timeout">' . $this->getLang('timeout') . '</label>';
309-
echo '<input type="number" name="prf[timeout]" id="sync__timeout" class="edit" value="' . hsc($this->profiles[$no]['timeout']) . '" />';
310-
311-
echo '<span>' . $this->getLang('type') . '</span>';
312-
313-
echo '<div class="type">';
314-
echo '<input type="radio" name="prf[type]" id="sync__type0" value="0" ' . (($this->profiles[$no]['type'] == 0) ? 'checked="checked"' : '') . '/>';
315-
echo '<label for="sync__type0">' . $this->getLang('type0') . '</label> ';
316-
317-
echo '<input type="radio" name="prf[type]" id="sync__type1" value="1" ' . (($this->profiles[$no]['type'] == 1) ? 'checked="checked"' : '') . '/>';
318-
echo '<label for="sync__type1">' . $this->getLang('type1') . '</label> ';
319-
320-
echo '<input type="radio" name="prf[type]" id="sync__type2" value="2" ' . (($this->profiles[$no]['type'] == 2) ? 'checked="checked"' : '') . '/>';
321-
echo '<label for="sync__type2">' . $this->getLang('type2') . '</label> ';
322-
echo '</div>';
323-
324-
echo '<div class="submit">';
325-
echo '<input type="submit" value="' . $this->getLang('save') . '" class="button" />';
326-
if($no !== '' && $this->profiles[$no]['ltime']) {
327-
echo '<small>' . $this->getLang('changewarn') . '</small>';
328-
}
329-
echo '</div>';
330-
331-
echo '<div class="submit">';
332-
echo '<input name="sync__delete" type="submit" value="' . $this->getLang('delete') . '" class="button" />';
333-
echo '</div>';
334-
335-
echo '</fieldset>';
336-
echo '</form>';
337-
}
338-
339330
/**
340331
* Lock files that will be modified on either side.
341332
*

0 commit comments

Comments
 (0)