Skip to content

Commit 3e7646f

Browse files
committed
more refactoring of the edit/save/view stuff
1 parent 7f6350c commit 3e7646f

File tree

4 files changed

+67
-52
lines changed

4 files changed

+67
-52
lines changed

Profile.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ public function __construct($config) {
4242
/**
4343
* Access the config that initialized this profile
4444
*
45-
* @return array
45+
* @param string|null $key when a key is given only that key's config value is returned
46+
* @return mixed
4647
*/
47-
public function getConfig() {
48+
public function getConfig($key=null) {
49+
if($key !== null) {
50+
if(isset($this->config[$key])) {
51+
return $this->config[$key];
52+
} else {
53+
return null;
54+
}
55+
}
4856
return $this->config;
4957
}
5058

ProfileManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ public function getProfileLabels() {
9191
foreach($this->profiles as $idx => $profile) {
9292
$label = parse_url($profile['server'], PHP_URL_HOST);
9393
if($label === null) $label = $profile['server'];
94-
$label = ($idx + 1) . '. ' . $label;
9594
if($profile['user'] !== '') $label = $profile['user'] . '@' . $label;
9695
if($profile['ns'] !== '') $label .= ':' . $profile['ns'];
97-
$labels[$idx] = $label;
96+
$label = ($idx + 1) . '. ' . $label;
97+
$labels["$idx."] = $label;
9898
}
9999

100100
return $labels;

admin.php

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
use dokuwiki\Form\Form;
33
use dokuwiki\plugin\sync\ProfileManager;
4+
use dokuwiki\plugin\sync\SyncException;
45

56
// must be run within Dokuwiki
67
if(!defined('DOKU_INC')) die();
@@ -26,7 +27,11 @@ function __construct() {
2627
global $INPUT;
2728

2829
$this->profileManager = new ProfileManager();
29-
$this->profno = $INPUT->int('no', false);
30+
if($INPUT->str('no') == 'none') {
31+
$this->profno = -1;
32+
} else {
33+
$this->profno = $INPUT->int('no', -1);
34+
}
3035
}
3136

3237
/**
@@ -104,7 +109,7 @@ function handle() {
104109
$this->profno = $this->profileManager->setProfileConfig($this->profno, $profile);
105110
msg('profile saved', 1);
106111
}
107-
} catch(\dokuwiki\plugin\sync\SyncException $e) {
112+
} catch(SyncException $e) {
108113
msg(hsc($e->getMessage()), -1);
109114
}
110115
}
@@ -149,7 +154,7 @@ function html() {
149154
ob_flush();
150155

151156
echo '<p>' . $this->getLang('syncdone') . '</p>';
152-
} elseif($_REQUEST['startsync'] && $this->profno !== '') {
157+
} elseif($_REQUEST['startsync'] && $this->profno !== -1) {
153158
// get sync list
154159
list($lnow, $rnow) = $this->_getTimes();
155160
$pages = array();
@@ -188,9 +193,9 @@ function html() {
188193

189194
echo '<div class="sync_left">';
190195
$this->profileDropdown();
191-
if($this->profno !== false) {
196+
if($this->profno !== -1) {
192197
echo '<br />';
193-
$this->_profileView();
198+
$this->profileInfo();
194199
}
195200
echo '</div>';
196201
echo '<div class="sync_right">';
@@ -199,6 +204,38 @@ function html() {
199204
}
200205
}
201206

207+
/**
208+
* Check connection for choosen profile and display last sync date.
209+
*/
210+
protected function profileInfo() {
211+
try {
212+
$profile = $this->profileManager->getProfile($this->profno);
213+
$version = $profile->getRemotVersion();
214+
$ltime = $profile->getConfig('ltime');
215+
} catch(SyncException $e) {
216+
echo '<div class="error">' . $this->getLang('noconnect') . '<br />' . hsc($e->getMessage()) . '</div>';
217+
return;
218+
}
219+
220+
$form = new Form(
221+
[
222+
'action' => wl('', ['do' => 'admin', 'page' => 'sync'], false, '&'),
223+
'method' => 'POST',
224+
]
225+
);
226+
$form->setHiddenField('no', $this->profno);
227+
$form->addFieldsetOpen($this->getLang('syncstart'));
228+
$form->addHTML('<p>' . $this->getLang('remotever') . ' ' . hsc($version) . '</p>');
229+
if($ltime) {
230+
$form->addHTML('<p>' . $this->getLang('lastsync') . ' ' . dformat($ltime) . '</p>');
231+
} else {
232+
$form->addHTML('<p>' . $this->getLang('neversync') . '</p>');
233+
}
234+
$form->addButton('startsync', $this->getLang('syncstart'))->attr('type', 'submit');
235+
$form->addFieldsetClose();
236+
echo $form->toHTML();
237+
}
238+
202239
/**
203240
* Dropdown list of available sync profiles
204241
*/
@@ -210,8 +247,8 @@ protected function profileDropdown() {
210247
]
211248
);
212249

213-
$profiles = $this->profileManager->getProfileLabels();
214-
$profiles[''] = $this->getLang('newprofile');
250+
$profiles = ['none' => $this->getLang('newprofile')];
251+
$profiles = array_merge($profiles, $this->profileManager->getProfileLabels());
215252

216253
$form->addFieldsetOpen($this->getLang('profile'));
217254
$form->addDropdown('no', $profiles)->val($this->profno);
@@ -233,7 +270,7 @@ protected function profileForm() {
233270
]
234271
);
235272

236-
if($this->profno === false) {
273+
if($this->profno === -1) {
237274
$legend = $this->getLang('create');
238275
$profile = $this->profileManager->getEmptyConfig();
239276
} else {
@@ -242,15 +279,15 @@ protected function profileForm() {
242279
}
243280

244281
$depths = [
245-
['label' => $this->getLang('level0'), 'attr' => ['value' => '0']],
246-
['label' => $this->getLang('level1'), 'attr' => ['value' => '1']],
247-
['label' => $this->getLang('level2'), 'attr' => ['value' => '2']],
248-
['label' => $this->getLang('level3'), 'attr' => ['value' => '3']],
282+
['label' => $this->getLang('level0')],
283+
['label' => $this->getLang('level1')],
284+
['label' => $this->getLang('level2')],
285+
['label' => $this->getLang('level3')],
249286
];
250287
$types = [
251-
['label' => $this->getLang('type0'), 'attr' => ['value' => '0']],
252-
['label' => $this->getLang('type1'), 'attr' => ['value' => '1']],
253-
['label' => $this->getLang('type2'), 'attr' => ['value' => '2']],
288+
['label' => $this->getLang('type0')],
289+
['label' => $this->getLang('type1')],
290+
['label' => $this->getLang('type2')],
254291
];
255292

256293
$form->addFieldsetOpen($legend);
@@ -265,13 +302,13 @@ protected function profileForm() {
265302
$form->addDropdown('prf[type]', $types, $this->getLang('type'))->val($profile['type']);
266303
$form->addButton('', $this->getLang('save'))->attr('type', 'submit');
267304

268-
if($this->profno !== false && !empty($profile['ltime'])) {
305+
if($this->profno !== -1 && !empty($profile['ltime'])) {
269306
echo '<small>' . $this->getLang('changewarn') . '</small>';
270307
}
271308

272309
$form->addFieldsetClose();
273310

274-
if($this->profno !== false) {
311+
if($this->profno !== -1) {
275312
$form->addFieldsetOpen($this->getLang('delete'));
276313
$form->addButton('sync__delete', $this->getLang('delete'));
277314
$form->addFieldsetClose();
@@ -300,37 +337,6 @@ function _profileSave() {
300337
io_saveFile($profiles, serialize($this->profiles));
301338
}
302339

303-
/**
304-
* Check connection for choosen profile and display last sync date.
305-
*/
306-
function _profileView() {
307-
if(!$this->_connect()) return;
308-
309-
global $conf;
310-
$no = $this->profno;
311-
312-
$ok = $this->client->query('dokuwiki.getVersion');
313-
$version = '';
314-
if($ok) $version = $this->client->getResponse();
315-
316-
echo '<form action="" method="post">';
317-
echo '<input type="hidden" name="no" value="' . hsc($no) . '" />';
318-
echo '<fieldset><legend>' . $this->getLang('syncstart') . '</legend>';
319-
if($version) {
320-
echo '<p>' . $this->getLang('remotever') . ' ' . hsc($version) . '</p>';
321-
if($this->profiles[$no]['ltime']) {
322-
echo '<p>' . $this->getLang('lastsync') . ' ' . strftime($conf['dformat'], $this->profiles[$no]['ltime']) . '</p>';
323-
} else {
324-
echo '<p>' . $this->getLang('neversync') . '</p>';
325-
}
326-
echo '<input name="startsync" type="submit" value="' . $this->getLang('syncstart') . '" class="button" />';
327-
} else {
328-
echo '<p class="error">' . $this->getLang('noconnect') . '<br />' . hsc($this->client->getErrorMessage()) . '</p>';
329-
}
330-
echo '</fieldset>';
331-
echo '</form>';
332-
}
333-
334340
/**
335341
* Lock files that will be modified on either side.
336342
*

lang/en/lang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
$lang['save'] = 'Save';
3737
$lang['changewarn'] = 'Resaving this profile will reset the sync times. You will need to manually choose the sync directions for all files on the next sync.';
3838

39+
$lang['noconnect'] = 'Could not connect to remote wiki:';
3940
$lang['lockfail'] = 'couldn\'t lock and will skip:';
4041
$lang['pullfail'] = 'pull failed:';
4142
$lang['pullok'] = 'pull succeded:';

0 commit comments

Comments
 (0)