Skip to content

Commit 7f6350c

Browse files
committed
save/load/delete works again
1 parent 738d2f7 commit 7f6350c

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

ProfileManager.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class ProfileManager {
66

77
protected $profiles = [];
88

9+
public function __construct() {
10+
$this->load();
11+
}
12+
913
/**
1014
* Return a list of all available profiles
1115
*
@@ -51,25 +55,30 @@ public function getEmptyConfig() {
5155
'user' => '',
5256
'pass' => '',
5357
'timeout' => 15,
58+
'type' => 0,
5459
];
5560
}
5661

5762
/**
5863
* Set the given config for the given profile
5964
*
60-
* When $num is null, the data is added to a new profile
65+
* When $num is false, the data is added to a new profile
6166
*
62-
* @param int|null $num
67+
* @param int|false $num
6368
* @param $data
69+
* @return int the index of the saved profile
6470
*/
6571
public function setProfileConfig($num, $data) {
66-
if($num !== null && isset($this->profiles[$num])) {
72+
if($num === false || !isset($this->profiles[$num])) {
73+
$num = count($this->profiles);
74+
}
75+
if(isset($this->profiles[$num])) {
6776
$this->profiles[$num] = array_merge($this->profiles[$num], $data);
6877
} else {
6978
$this->profiles[$num] = $data;
7079
}
71-
7280
$this->save();
81+
return $num;
7382
}
7483

7584
/**
@@ -81,6 +90,8 @@ public function getProfileLabels() {
8190
$labels = [];
8291
foreach($this->profiles as $idx => $profile) {
8392
$label = parse_url($profile['server'], PHP_URL_HOST);
93+
if($label === null) $label = $profile['server'];
94+
$label = ($idx + 1) . '. ' . $label;
8495
if($profile['user'] !== '') $label = $profile['user'] . '@' . $label;
8596
if($profile['ns'] !== '') $label .= ':' . $profile['ns'];
8697
$labels[$idx] = $label;

admin.php

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,27 @@ function _connect() {
8585
* handle profile saving/deleting
8686
*/
8787
function handle() {
88-
if(isset($_REQUEST['prf']) && is_array($_REQUEST['prf'])) {
89-
if(isset($_REQUEST['sync__delete']) && $this->profno !== false) {
90-
// delete profile
91-
unset($this->profiles[$this->profno]);
92-
$this->profiles = array_values($this->profiles); //reindex
93-
$this->profno = '';
88+
global $INPUT;
89+
90+
$profile = $INPUT->arr('prf');
91+
if(!$profile) return;
92+
93+
if(!checkSecurityToken()) return;
94+
95+
try {
96+
if($INPUT->has('sync__delete') && $this->profno !== false) {
97+
// profile deletion
98+
$this->profileManager->deleteProfileConfig($this->profno);
99+
$this->profno = false;
100+
$INPUT->remove('prf');
101+
msg('profile deleted', 1);
94102
} else {
95-
// add/edit profile
96-
if($this->profno === '') $this->profno = count($this->profiles);
97-
if(!isset($_REQUEST['prf']['timeout']) || !is_numeric($_REQUEST['prf']['timeout'])) {
98-
$_REQUEST['prf']['timeout'] = $this->defaultTimeout;
99-
}
100-
$this->profiles[$this->profno] = $_REQUEST['prf'];
103+
// profile add/edit
104+
$this->profno = $this->profileManager->setProfileConfig($this->profno, $profile);
105+
msg('profile saved', 1);
101106
}
102-
$this->_profileSave();
103-
104-
// reset the client
105-
$this->client = null;
107+
} catch(\dokuwiki\plugin\sync\SyncException $e) {
108+
msg(hsc($e->getMessage()), -1);
106109
}
107110
}
108111

@@ -211,7 +214,7 @@ protected function profileDropdown() {
211214
$profiles[''] = $this->getLang('newprofile');
212215

213216
$form->addFieldsetOpen($this->getLang('profile'));
214-
$form->addDropdown('no', $profiles);
217+
$form->addDropdown('no', $profiles)->val($this->profno);
215218
$form->addButton('', $this->getLang('select'));
216219
$form->addFieldsetClose();
217220

@@ -226,6 +229,7 @@ protected function profileForm() {
226229
[
227230
'action' => wl('', ['do' => 'admin', 'page' => 'sync'], false, '&'),
228231
'method' => 'POST',
232+
'class' => 'sync_profile',
229233
]
230234
);
231235

@@ -238,15 +242,15 @@ protected function profileForm() {
238242
}
239243

240244
$depths = [
241-
0 => $this->getLang('level0'),
242-
1 => $this->getLang('level1'),
243-
2 => $this->getLang('level2'),
244-
3 => $this->getLang('level3'),
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']],
245249
];
246250
$types = [
247-
0 => $this->getLang('type0'),
248-
1 => $this->getLang('type1'),
249-
2 => $this->getLang('type2'),
251+
['label' => $this->getLang('type0'), 'attr' => ['value' => '0']],
252+
['label' => $this->getLang('type1'), 'attr' => ['value' => '1']],
253+
['label' => $this->getLang('type2'), 'attr' => ['value' => '2']],
250254
];
251255

252256
$form->addFieldsetOpen($legend);
@@ -261,9 +265,9 @@ protected function profileForm() {
261265
$form->addDropdown('prf[type]', $types, $this->getLang('type'))->val($profile['type']);
262266
$form->addButton('', $this->getLang('save'))->attr('type', 'submit');
263267

264-
// if($no !== '' && $this->profiles[$no]['ltime']) {
265-
// echo '<small>' . $this->getLang('changewarn') . '</small>';
266-
// }
268+
if($this->profno !== false && !empty($profile['ltime'])) {
269+
echo '<small>' . $this->getLang('changewarn') . '</small>';
270+
}
267271

268272
$form->addFieldsetClose();
269273

0 commit comments

Comments
 (0)