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,50 +1176,43 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history)
die('Wrong token.'); die('Wrong token.');
} }
$count = 0;
// Delete a tag:
if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) { 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']); $needle = trim($_POST['fromtag']);
// True for case-sensitive tag search. // True for case-sensitive tag search.
$linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
foreach($linksToAlter as $key=>$value) foreach($linksToAlter as $key => $value)
{ {
$tags = explode(' ',trim($value['tags'])); $tags = explode(' ',trim($value['tags']));
if (($pos = array_search($needle,$tags)) !== false) { if (($pos = array_search($needle,$tags)) !== false) {
if ($delete) {
unset($tags[$pos]); // Remove tag. 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; $LINKSDB[$key]=$value;
$history->updateLink($LINKSDB[$key]); $history->updateLink($LINKSDB[$key]);
++$count; ++$count;
} }
} }
$LINKSDB->save($conf->get('resource.page_cache')); $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; 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. // -------- User wants to add a link without using the bookmarklet: Show form.
if ($targetPage == Router::$PAGE_ADDLINK) if ($targetPage == Router::$PAGE_ADDLINK)
{ {