Accueil/ajax/update-links-order.php
2010-09-21 14:00:04 +00:00

36 lines
966 B
PHP

<?
$label = $_POST['label'];
$order = $_POST['order'];
$itemMoved = $_POST['itemMoved'];
$orderArray = explode(',', $order);
$itemIndex = 0;
foreach($orderArray as $index=>$value){
if($value == $itemMoved)
$itemIndex = (int)$index;
}
$itemAfter = '';
if(isset($orderArray[$itemIndex+1]))
$itemAfter = $orderArray[$itemIndex+1];
$doc = new DOMDocument;
$doc->load('../db/links.xml');
$xpath = new DOMXpath($doc);
// Buffering and deleting moved element
foreach($xpath->query('label/link[title = "'.$itemMoved.'"]') as $node) {
$buffer = $node;
$node->parentNode->removeChild($node);
}
if($itemAfter != ''){
$searchLabel = $xpath->query('label[@id="'.$label.'"]/link[title = "'.$itemAfter.'"]')->item(0);
$searchLabel->parentNode->insertBefore($buffer, $searchLabel);
}else{
$searchLabel = $xpath->query('label[@id="'.$label.'"]');
$searchLabel->item(0)->appendChild($buffer);
}
$doc->save('../db/links.xml');