2
0
forked from Wavyzz/dolibarr

Fix: task #11248 : there is no DB transaction on products->delete

This commit is contained in:
Laurent Destailleur
2011-08-28 11:53:06 +00:00
parent 8e75a21df9
commit 3562f6bba0
2 changed files with 108 additions and 30 deletions

View File

@@ -548,7 +548,7 @@ class Product extends CommonObject
* Delete a product from database (if not used) * Delete a product from database (if not used)
* *
* @param id Product id * @param id Product id
* @return int < 0 if KO, >= 0 if OK * @return int < 0 if KO, 0 = Not possible, > 0 if OK
*/ */
function delete($id) function delete($id)
{ {
@@ -559,54 +559,129 @@ class Product extends CommonObject
if ($user->rights->produit->supprimer) if ($user->rights->produit->supprimer)
{ {
$prod_use = $this->verif_prod_use($id); $prod_use = $this->verif_prod_use($id);
if ($prod_use == 0) if (empty($prod_use))
{ {
// TODO possibility to add external module constraint $this->db->begin();
$elements = array('product_price','product_lang','categorie_product');
foreach($elements as $table) // Delete supplier prices log
{ if (! $error)
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$table; {
$sql.= " WHERE fk_product = ".$id; $sql = 'DELETE pfpl';
$result = $this->db->query($sql); $sql.= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price_log as pfpl, '.MAIN_DB_PREFIX.'product_fournisseur as pf';
} $sql.= ' WHERE pfpl.fk_product_fournisseur = pf.rowid';
$sql.= ' AND pf.fk_product = '.$id;
$result = $this->db->query($sql);
if (! $result)
{
$error++;
$this->error = $this->db->lasterror();
dol_syslog(get_class($this).'::delete error '.$this->error, LOG_ERR);
}
}
$sqlz = "DELETE FROM ".MAIN_DB_PREFIX."product"; // Delete supplier prices
$sqlz.= " WHERE rowid = ".$id; if (! $error)
$resultz = $this->db->query($sqlz); {
$sql = 'DELETE pfp';
$sql.= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price as pfp, '.MAIN_DB_PREFIX.'product_fournisseur as pf';
$sql.= ' WHERE pfp.fk_product_fournisseur = pf.rowid';
$sql.= ' AND pf.fk_product = '.$id;
$result = $this->db->query($sql);
if (! $result)
{
$error++;
$this->error = $this->db->lasterror();
dol_syslog(get_class($this).'::delete error '.$this->error, LOG_ERR);
}
}
if ( ! $resultz ) // Other child tables
{ if (! $error)
dol_syslog('Product::delete error sqlz='.$sqlz, LOG_ERR); {
$error++; $elements = array('product_price','product_lang','categorie_product');
} foreach($elements as $table)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX.$table;
$sql.= " WHERE fk_product = ".$id;
dol_syslog(get_class($this).'::delete sql='.$this->error, LOG_ERR);
$result = $this->db->query($sql);
if (! $result)
{
$error++;
$this->error = $this->db->lasterror();
dol_syslog(get_class($this).'::delete error '.$this->error, LOG_ERR);
}
}
}
// Appel des triggers if (! $error)
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); {
$interface=new Interfaces($this->db); $sqlz = "DELETE FROM ".MAIN_DB_PREFIX."product";
$result=$interface->run_triggers('PRODUCT_DELETE',$this,$user,$langs,$conf); $sqlz.= " WHERE rowid = ".$id;
if ($result < 0) { $error++; $this->errors=$interface->errors; } dol_syslog(get_class($this).'::delete sql='.$sql, LOG_DEBUG);
// Fin appel triggers $resultz = $this->db->query($sqlz);
if ( ! $resultz )
{
$error++;
$this->error = $this->db->lasterror();
dol_syslog(get_class($this).'::delete error '.$this->error, LOG_ERR);
}
}
if (! $error)
{
// Actions on extra fields (by external module or standard code)
include_once(DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php');
$hookmanager=new HookManager($this->db);
$hookmanager->callHooks(array('product_extrafields'));
$parameters=array(); $action='delete';
$reshook=$hookmanager->executeHooks('deleteExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if (! empty($hookmanager->error))
{
$error++;
$this->error=$hookmanager->error;
}
else if (empty($reshook))
{
// TODO
//$result=$this->deleteExtraFields($this);
//if ($result < 0) $error++;
}
}
if (! $error)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('PRODUCT_DELETE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
if ($error) if ($error)
{ {
$this->db->rollback();
return -$error; return -$error;
} }
else else
{ {
return 0; $this->db->commit;
return 1;
} }
} }
else else
{ {
$this->error .= "FailedToDeleteProduct. Already used.\n"; $this->error = "ErrorRecordHasChildren";
return -1; return 0;
} }
} }
} }
/** /**
* Update ou cree les traductions des infos produits * Update ou cree les traductions des infos produits
*
* @param int <0 if KO, >0 if OK
*/ */
function setMultiLangs() function setMultiLangs()
{ {
@@ -672,6 +747,8 @@ class Product extends CommonObject
/** /**
* Load array this->multilangs * Load array this->multilangs
*
* @param int <0 if KO, >0 if OK
*/ */
function getMultiLangs() function getMultiLangs()
{ {
@@ -700,6 +777,7 @@ class Product extends CommonObject
$this->multilangs["$obj->lang"]["description"] = $obj->description; $this->multilangs["$obj->lang"]["description"] = $obj->description;
$this->multilangs["$obj->lang"]["note"] = $obj->note; $this->multilangs["$obj->lang"]["note"] = $obj->note;
} }
return 1;
} }
else else
{ {
@@ -714,7 +792,7 @@ class Product extends CommonObject
* Ajoute un changement de prix en base dans l'historique des prix * Ajoute un changement de prix en base dans l'historique des prix
* *
* @param user Objet utilisateur qui modifie le prix * @param user Objet utilisateur qui modifie le prix
* @return int <0 si KO, >0 si OK * @param int <0 if KO, >0 if OK
*/ */
function _log_price($user,$level=0) function _log_price($user,$level=0)
{ {

View File

@@ -364,9 +364,9 @@ if ($action == 'confirm_delete' && $confirm == 'yes')
$result = $product->delete($id); $result = $product->delete($id);
} }
if ($result == 0) if ($result > 0)
{ {
Header('Location: '.DOL_URL_ROOT.'/product/liste.php?delprod='.$product->ref); Header('Location: '.DOL_URL_ROOT.'/product/liste.php?delprod='.urlencode($product->ref));
exit; exit;
} }
else else