aboutsummaryrefslogtreecommitdiffhomepage
path: root/index.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-05-31 18:36:35 +0200
committerArthurHoaro <arthur@hoa.ro>2017-05-31 18:36:35 +0200
commitd99aef535fa209c27c46a97dee4187ac21c84d4d (patch)
treeecc72237c481cc8de6d9dc433bec6b2f67f3e3e2 /index.php
parent4c970f099f2210ac91cccdca1d0c3564a8f79c1a (diff)
downloadShaarli-d99aef535fa209c27c46a97dee4187ac21c84d4d.tar.gz
Shaarli-d99aef535fa209c27c46a97dee4187ac21c84d4d.tar.zst
Shaarli-d99aef535fa209c27c46a97dee4187ac21c84d4d.zip
Refactoring of CHANGETAG part to avoid duplicated code
Diffstat (limited to 'index.php')
-rw-r--r--index.php61
1 files changed, 27 insertions, 34 deletions
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)
1176 die('Wrong token.'); 1176 die('Wrong token.');
1177 } 1177 }
1178 1178
1179 $count = 0;
1180 // Delete a tag:
1181 if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) { 1179 if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) {
1182 $needle = trim($_POST['fromtag']); 1180 $delete = true;
1183 // True for case-sensitive tag search. 1181 } else if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) {
1184 $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); 1182 $delete = false;
1185 foreach($linksToAlter as $key=>$value) 1183 } else {
1186 { 1184 $PAGE->renderPage('changetag');
1187 $tags = explode(' ',trim($value['tags']));
1188 if (($pos = array_search($needle,$tags)) !== false) {
1189 unset($tags[$pos]); // Remove tag.
1190 $value['tags']=trim(implode(' ',$tags));
1191 $LINKSDB[$key]=$value;
1192 $history->updateLink($LINKSDB[$key]);
1193 ++$count;
1194 }
1195 }
1196 $LINKSDB->save($conf->get('resource.page_cache'));
1197 echo '<script>alert("Tag was removed from '.$count.' links.");document.location=\'?do=changetag\';</script>';
1198 exit; 1185 exit;
1199 } 1186 }
1200 1187
1201 // Rename a tag: 1188 $count = 0;
1202 if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) { 1189 $needle = trim($_POST['fromtag']);
1203 $needle = trim($_POST['fromtag']); 1190 // True for case-sensitive tag search.
1204 // True for case-sensitive tag search. 1191 $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
1205 $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true); 1192 foreach($linksToAlter as $key => $value)
1206 foreach($linksToAlter as $key=>$value) { 1193 {
1207 $tags = preg_split('/\s+/', trim($value['tags'])); 1194 $tags = explode(' ',trim($value['tags']));
1208 // Replace tags value. 1195 if (($pos = array_search($needle,$tags)) !== false) {
1209 if (($pos = array_search($needle,$tags)) !== false) { 1196 if ($delete) {
1197 unset($tags[$pos]); // Remove tag.
1198 } else {
1210 $tags[$pos] = trim($_POST['totag']); 1199 $tags[$pos] = trim($_POST['totag']);
1211 $value['tags'] = implode(' ', array_unique($tags));
1212 $LINKSDB[$key] = $value;
1213 $history->updateLink($LINKSDB[$key]);
1214 ++$count;
1215 } 1200 }
1201 $value['tags'] = trim(implode(' ', array_unique($tags)));
1202 $LINKSDB[$key]=$value;
1203 $history->updateLink($LINKSDB[$key]);
1204 ++$count;
1216 } 1205 }
1217 $LINKSDB->save($conf->get('resource.page_cache')); // Save to disk.
1218 echo '<script>alert("Tag was renamed in '.$count.' links.");document.location=\'?searchtags='.urlencode(escape($_POST['totag'])).'\';</script>';
1219 exit;
1220 } 1206 }
1207 $LINKSDB->save($conf->get('resource.page_cache'));
1208 $redirect = $delete ? 'do=changetag' : 'searchtags='. urlencode(escape($_POST['totag']));
1209 $alert = $delete
1210 ? sprintf(t('The tag was removed from %d links.'), $count)
1211 : sprintf(t('The tag was renamed in %d links.'), $count);
1212 echo '<script>alert("'. $alert .'");document.location=\'?'. $redirect .'\';</script>';
1213 exit;
1221 } 1214 }
1222 1215
1223 // -------- User wants to add a link without using the bookmarklet: Show form. 1216 // -------- User wants to add a link without using the bookmarklet: Show form.