public function ajaxProcessUpdatePositions()
{
$id_shop = Shop::getContextShopID();
// Build an array of order IDs (could be a page!)
$submittedIds = array();
$rows = (array) Tools::getValue($this->table);
foreach ($rows as $row) {
$ids = explode('_', $row);
$submittedIds[] = (int) $ids[2];
}
// Get all IDs from database
$sql = new DbQuery();
$sql->select('id_ct_top_menu_item');
$sql->from('ct_top_menu_item_shop');
$sql->where('id_shop = ' . (int) $id_shop);
$sql->orderBy('position ASC');
$rows = (array) Db::getInstance()->executeS($sql);
$allIds = array();
foreach ($rows as $row) {
$allIds[] = (int) $row['id_ct_top_menu_item'];
}
// Go through all IDs, if the ID exists in the sorted ID list (could be fragment (page) or sorted IDs)
// then pick an ID from sorted ID list and overwrite the value.
$i = 0;
foreach ($allIds as $key1 => $id) {
$key2 = array_search($id, $submittedIds);
if ($key2 !== false) {
$allIds[$key1] = $submittedIds[$i++];
}
}
// Update positions of all values the way the are ordered in the array
$position = 0;
$isSuccess = true;
$shopIDs = Shop::getContextListShopID();
foreach ($allIds as $id_ct_top_menu_item) {
$isSuccess &= Db::getInstance()->update($this->table . '_shop', array('position' => $position++), 'id_ct_top_menu_item = ' . (int) $id_ct_top_menu_item . ' AND id_shop IN (' . implode(', ', $shopIDs) . ')');
if (!$isSuccess) {
break;
}
}
Hook::exec('actionCTTopMenuCompositionChanged');
if ($isSuccess) {
die(true);
} else {
header('Content-Type: application/json');
die(Tools::jsonEncode(array('hasError' => true, 'errors' => $this->l('Could not update positions in the database table.'))));
}
}