Refactoring of CHANGETAG part to avoid duplicated code

This commit is contained in:
ArthurHoaro 2017-05-31 18:36:35 +02:00
parent 4c970f099f
commit d99aef535f

View file

@ -1176,9 +1176,16 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history)
die('Wrong token.');
}
$count = 0;
// Delete a tag:
if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) {
$delete = true;
} else if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) {
$delete = false;
} else {
$PAGE->renderPage('changetag');
exit;
}
$count = 0;
$needle = trim($_POST['fromtag']);
// True for case-sensitive tag search.
$linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
@ -1186,40 +1193,26 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history)
{
$tags = explode(' ',trim($value['tags']));
if (($pos = array_search($needle,$tags)) !== false) {
if ($delete) {
unset($tags[$pos]); // Remove tag.
$value['tags']=trim(implode(' ',$tags));
} else {
$tags[$pos] = trim($_POST['totag']);
}
$value['tags'] = trim(implode(' ', array_unique($tags)));
$LINKSDB[$key]=$value;
$history->updateLink($LINKSDB[$key]);
++$count;
}
}
$LINKSDB->save($conf->get('resource.page_cache'));
echo '<script>alert("Tag was removed from '.$count.' links.");document.location=\'?do=changetag\';</script>';
$redirect = $delete ? 'do=changetag' : 'searchtags='. urlencode(escape($_POST['totag']));
$alert = $delete
? sprintf(t('The tag was removed from %d links.'), $count)
: sprintf(t('The tag was renamed in %d links.'), $count);
echo '<script>alert("'. $alert .'");document.location=\'?'. $redirect .'\';</script>';
exit;
}
// Rename a tag:
if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) {
$needle = trim($_POST['fromtag']);
// True for case-sensitive tag search.
$linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
foreach($linksToAlter as $key=>$value) {
$tags = preg_split('/\s+/', trim($value['tags']));
// Replace tags value.
if (($pos = array_search($needle,$tags)) !== false) {
$tags[$pos] = trim($_POST['totag']);
$value['tags'] = implode(' ', array_unique($tags));
$LINKSDB[$key] = $value;
$history->updateLink($LINKSDB[$key]);
++$count;
}
}
$LINKSDB->save($conf->get('resource.page_cache')); // Save to disk.
echo '<script>alert("Tag was renamed in '.$count.' links.");document.location=\'?searchtags='.urlencode(escape($_POST['totag'])).'\';</script>';
exit;
}
}
// -------- User wants to add a link without using the bookmarklet: Show form.
if ($targetPage == Router::$PAGE_ADDLINK)
{