fixation of sale position

This commit is contained in:
Marion Muszynski 2017-07-20 16:51:19 +02:00
parent 4e0938b32e
commit e1663b365b
4 changed files with 69 additions and 56 deletions

View File

@ -125,6 +125,11 @@ if (isset($_GET['getSales'])){
<i class="glyphicon glyphicon-eye-open"></i> Voir Catégorie BO
</a>
</li>
<li>
<a style="cursor: pointer;" onclick="'.($sale->lock_position?'itemUnLockPosition('.$sale->id.')':'itemLockPosition('.$sale->id.')').'">
<span class="anticon anticon-lock"></span> '.($sale->lock_position?'Défixer':'Fixer').' position
</a>
</li>
<li role="separator" class="divider"></li>
<li>
<a style="cursor: pointer;" onclick="itemDeletion('.$sale->id.')">
@ -227,76 +232,53 @@ if (isset($_GET['updatePositionDrag'])){
die();
}
$updated_sales = array();
$lock_positions = array();
foreach(Db::getInstance()->executeS('SELECT position FROM '._DB_PREFIX_.'privatesale WHERE lock_position = 1') as $lock){
$lock_positions[] = (int)$lock['position'];
}
$_positions = array();
if($position > 0){
$sales_sql = "SELECT id_sale, position, lock_position FROM "._DB_PREFIX_."privatesale WHERE position BETWEEN ". ($position_initial +1) .' AND ' .$new_position.' ORDER BY position ASC';
$sales_sql = "SELECT id_sale, position FROM "._DB_PREFIX_."privatesale WHERE position BETWEEN ". ($position_initial +1) .' AND ' .$new_position.' AND lock_position = 0 ORDER BY position DESC';
$sales = Db::getInstance()->ExecuteS($sales_sql);
foreach ($sales as $key => $sale){
if(in_array($sale['id_sale'], $updated_sales)){
continue;
}
if((int)$sale['lock_position'] == 1){
$i = 1;
if(isset($sales[$key+1])){
$new_sale = $sales[$key+$i];
$updated_sales[] = (int)$new_sale['id_sale'];
}
while ($new_sale) {
$i++;
if((int)$new_sale['lock_position'] == 1){
if(isset($sales[$key+$i])) {
$new_sale = $sales[$key+$i];
$updated_sales[] = (int)$new_sale['id_sale'];
} else {
$new_sale = false;
}
} else {
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = (position -'.$i.') WHERE id_sale = '. $new_sale['id_sale']);
$new_sale = false;
}
}
} else {
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = (position -1) WHERE id_sale = '. $sale['id_sale']);
$updated_sales[] = (int)$sale['id_sale'];
$pos = (int)$sale['position']-1;
while(in_array($pos, $lock_positions)){
$pos = $pos - 1;
}
$_positions[(int)$sale['id_sale']] = array(
'initial' => (int)$sale['position'],
'new' => $pos,
);
//Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = '.$pos.' WHERE id_sale = '. $sale['id_sale']);
}
} else {
$sales_sql = "SELECT id_sale, position, lock_position FROM "._DB_PREFIX_."privatesale WHERE position BETWEEN ". $new_position . ' AND ' . ($position_initial - 1).' ORDER BY position ASC';
$sales_sql = "SELECT id_sale, position FROM "._DB_PREFIX_."privatesale WHERE position BETWEEN ". $new_position . ' AND ' . ($position_initial - 1).' AND lock_position = 0 ORDER BY position DESC';
$sales = Db::getInstance()->ExecuteS($sales_sql);
foreach ($sales as $key => $sale){
if(in_array($sale['id_sale'], $updated_sales)){
continue;
$pos = (int)$sale['position']+1;
while(in_array($pos, $lock_positions)){
$pos = $pos + 1;
}
$_positions[(int)$sale['id_sale']] = array(
'initial' => (int)$sale['position'],
'new' => $pos,
);
if((int)$sale['lock_position'] == 1){
$i = 1;
if(isset($sales[$key+1])){
$new_sale = $sales[$key+$i];
$updated_sales[] = (int)$new_sale['id_sale'];
}
while ($new_sale) {
$i++;
if((int)$new_sale['lock_position'] == 1){
if(isset($sales[$key+$i])) {
$new_sale = $sales[$key+$i];
$updated_sales[] = (int)$new_sale['id_sale'];
} else {
$new_sale = false;
}
} else {
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = (position +'.$i.') WHERE id_sale = '. $new_sale['id_sale']);
$new_sale = false;
}
}
} else {
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = (position + 1) WHERE id_sale = '. $sale['id_sale']);
$updated_sales[] = (int)$sale['id_sale'];
}
//Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = '.$position.' WHERE id_sale = '. $sale['id_sale']);
}
}
foreach ($_positions as $id => $value) {
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = '. (int)$value['new'].' WHERE id_sale = '. $id);
}
// die(json_encode(array(
// "position" => $position,
// "position_initial" => $position_initial,
// "new_position" => $new_position,
// "sale_positions" => $_positions,
// )));
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET position = '. (int)$new_position.' WHERE id_sale = '. $id_sale);
die();

View File

@ -930,6 +930,7 @@ class AdminPrivateSalesSales extends AdminTab {
success: function(html) {
var loader = "<p style=\"text-align:center\"><img style=\"display:inline-block;\" src=\"../img/loader_payment.gif\" /></p>";
$(".list_vente").html(loader);
ajax_query();
}
});
@ -1055,6 +1056,11 @@ class AdminPrivateSalesSales extends AdminTab {
<i class="glyphicon glyphicon-eye-open"></i> Voir Catégorie BO
</a>
</li>
<li>
<a style="cursor: pointer;" onclick="'.($sale->lock_position?'itemUnLockPosition('.$sale->id.')':'itemLockPosition('.$sale->id.')').'">
<span class="anticon anticon-lock"></span> '.($sale->lock_position?'Défixer':'Fixer').' position
</a>
</li>
<li role="separator" class="divider"></li>
<li>
<a style="cursor: pointer;" onclick="itemDeletion('.$sale->id.')">
@ -1273,6 +1279,12 @@ class AdminPrivateSalesSales extends AdminTab {
} elseif(isset($_GET['delete']) && $id_sale = Tools::getValue('id')) {
Sale::deleteSale($id_sale);
Tools::redirectAdmin($currentIndex.'&token='.Tools::getAdminTokenLite('AdminPrivateSalesSales'));
} elseif(isset($_GET['lock_position']) && $id_sale = Tools::getValue('id')) {
Sale::lockPosition($id_sale);
Tools::redirectAdmin($currentIndex.'&token='.Tools::getAdminTokenLite('AdminPrivateSalesSales'));
} elseif(isset($_GET['unlock_position']) && $id_sale = Tools::getValue('id')) {
Sale::unlockPosition($id_sale);
Tools::redirectAdmin($currentIndex.'&token='.Tools::getAdminTokenLite('AdminPrivateSalesSales'));
}
}
}

View File

@ -100,6 +100,7 @@ class Sale {
`id_employee` = '.(int) $this->id_employee.',
`shipping_fr` = '.(int) $this->shipping_fr.',
`delivery_delay` = '.(int) $this->delivery_delay.',
`lock_position` = '.(int) $this->lock_position.',
`date_upd` = NOW()
WHERE `id_sale` = '.(int) $this->id.'
');
@ -284,6 +285,18 @@ class Sale {
}
}
public static function lockPosition($id) {
if($sale = Sale::getSale($id)) {
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET lock_position = 1 WHERE id_sale = '. $id);
}
}
public static function unlockPosition($id) {
if($sale = Sale::getSale($id)) {
Db::getInstance()->ExecuteS( 'UPDATE '._DB_PREFIX_.'privatesale SET lock_position = 0 WHERE id_sale = '. $id);
}
}
public static function rrmdir($dir) {
if(is_dir($dir)) {
$objects = scandir($dir);

View File

@ -8,3 +8,9 @@ function itemDeletion(linkId) {
document.location.href = current_location + "&id=" + linkId + "&delete=1";
}
}
function itemLockPosition(linkId) {
document.location.href = current_location + "&id=" + linkId + "&lock_position=1";
}
function itemUnLockPosition(linkId) {
document.location.href = current_location + "&id=" + linkId + "&unlock_position=1";
}