From d99aef535fa209c27c46a97dee4187ac21c84d4d Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 31 May 2017 18:36:35 +0200 Subject: Refactoring of CHANGETAG part to avoid duplicated code --- index.php | 61 +++++++++++++++++++++++++++---------------------------------- 1 file changed, 27 insertions(+), 34 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 39230c80..eb6b17d9 100644 --- a/index.php +++ b/index.php @@ -1176,48 +1176,41 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) die('Wrong token.'); } - $count = 0; - // Delete a tag: if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) { - $needle = trim($_POST['fromtag']); - // True for case-sensitive tag search. - $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); - foreach($linksToAlter as $key=>$value) - { - $tags = explode(' ',trim($value['tags'])); - if (($pos = array_search($needle,$tags)) !== false) { - unset($tags[$pos]); // Remove tag. - $value['tags']=trim(implode(' ',$tags)); - $LINKSDB[$key]=$value; - $history->updateLink($LINKSDB[$key]); - ++$count; - } - } - $LINKSDB->save($conf->get('resource.page_cache')); - echo ''; + $delete = true; + } else if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) { + $delete = false; + } else { + $PAGE->renderPage('changetag'); 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) { + $count = 0; + $needle = trim($_POST['fromtag']); + // True for case-sensitive tag search. + $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); + foreach($linksToAlter as $key => $value) + { + $tags = explode(' ',trim($value['tags'])); + if (($pos = array_search($needle,$tags)) !== false) { + if ($delete) { + unset($tags[$pos]); // Remove tag. + } else { $tags[$pos] = trim($_POST['totag']); - $value['tags'] = implode(' ', array_unique($tags)); - $LINKSDB[$key] = $value; - $history->updateLink($LINKSDB[$key]); - ++$count; } + $value['tags'] = trim(implode(' ', array_unique($tags))); + $LINKSDB[$key]=$value; + $history->updateLink($LINKSDB[$key]); + ++$count; } - $LINKSDB->save($conf->get('resource.page_cache')); // Save to disk. - echo ''; - exit; } + $LINKSDB->save($conf->get('resource.page_cache')); + $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 ''; + exit; } // -------- User wants to add a link without using the bookmarklet: Show form. -- cgit v1.2.3