Fix: Error management if bookmarks added twice

This commit is contained in:
Laurent Destailleur 2008-09-04 17:35:29 +00:00
parent cb1987916b
commit 33ad422c76
2 changed files with 36 additions and 18 deletions

View File

@ -96,6 +96,8 @@ class Bookmark
*/ */
function create() function create()
{ {
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."bookmark (fk_user,dateb,url,target"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."bookmark (fk_user,dateb,url,target";
$sql.= " ,title,favicon"; $sql.= " ,title,favicon";
if ($this->fk_soc) $sql.=",fk_soc"; if ($this->fk_soc) $sql.=",fk_soc";
@ -109,22 +111,26 @@ class Bookmark
if ($resql) if ($resql)
{ {
$id = $this->db->last_insert_id (MAIN_DB_PREFIX."bookmark"); $id = $this->db->last_insert_id(MAIN_DB_PREFIX."bookmark");
if ($id > 0) if ($id > 0)
{ {
$this->id = $id; $this->id = $id;
$this->db->commit();
return $id; return $id;
} }
else else
{ {
$this->error=$this->db->error(); $this->error=$this->db->lasterror();
$this->errno=$this->db->lasterrno();
$this->db->rollback();
return -2; return -2;
} }
} }
else else
{ {
dolibarr_print_error ($this->db); $this->error=$this->db->lasterror();
$this->errno=$this->db->lasterrno();
$this->db->rollback();
return -1; return -1;
} }
} }

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org> /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2005 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2005-2008 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -15,17 +15,14 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*/ */
/** /**
\file htdocs/bookmarks/fiche.php * \file htdocs/bookmarks/fiche.php
\brief Page affichage/creation des bookmarks * \brief Page affichage/creation des bookmarks
\ingroup bookmark * \ingroup bookmark
\version $Revision$ * \version $Id$
*/ */
require("./pre.inc.php"); require("./pre.inc.php");
@ -55,7 +52,7 @@ if ($action == 'add')
$societe=new Societe($db); $societe=new Societe($db);
$societe->fetch($_GET["socid"]); $societe->fetch($_GET["socid"]);
$bookmark->fk_soc=$societe->id; $bookmark->fk_soc=$societe->id;
$bookmark->url=DOL_URL_ROOT.'/comm/fiche.php?socid='.$societe->id; $bookmark->url=DOL_URL_ROOT.'/soc.php?socid='.$societe->id;
$bookmark->target='0'; $bookmark->target='0';
$bookmark->title=$societe->nom; $bookmark->title=$societe->nom;
} }
@ -71,18 +68,29 @@ if ($action == 'add')
if (! $mesg) if (! $mesg)
{ {
$bookmark->favicon='xxx'; $bookmark->favicon='none';
$res=$bookmark->create(); $res=$bookmark->create();
if ($res > 0) if ($res > 0)
{ {
$urlsource=isset($_GET["urlsource"])?$_GET["urlsource"]:DOL_URL_ROOT.'/bookmarks/liste.php'; $urlsource=isset($_GET["urlsource"])?$_GET["urlsource"]:DOL_URL_ROOT.'/bookmarks/liste.php';
header("Location: ".$urlsource); header("Location: ".$urlsource);
exit;
} }
else else
{ {
$mesg='<div class="error">'.$bookmark->error.'</div>'; if ($bookmark->errno == 'DB_ERROR_RECORD_ALREADY_EXISTS')
$action='create'; {
$langs->load("errors");
$mesg='<div class="warning">'.$langs->trans("WarningBookmarkAlreadyExists").'</div>';
}
else
{
$mesg='<div class="error">'.$bookmark->error.'</div>';
}
$action='create';
$title=$bookmark->title;
$url=$bookmark->url;
} }
} }
else else
@ -106,6 +114,7 @@ if ($_GET["action"] == 'delete')
if ($res > 0) if ($res > 0)
{ {
header("Location: ".$_SERVER["PHP_SELF"]); header("Location: ".$_SERVER["PHP_SELF"]);
exit;
} }
else else
{ {
@ -114,6 +123,9 @@ if ($_GET["action"] == 'delete')
} }
/*
* View
*/
llxHeader(); llxHeader();
@ -136,7 +148,7 @@ if ($action == 'create')
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';
print '<tr><td width="20%">'.$langs->trans("BookmarkTitle").'</td><td><input class="flat" name="title" size="30" value="'.$title.'"></td><td>'.$langs->trans("SetHereATitleForLink").'</td></tr>'; print '<tr><td width="20%">'.$langs->trans("BookmarkTitle").'</td><td><input class="flat" name="title" size="30" value="'.$title.'"></td><td>'.$langs->trans("SetHereATitleForLink").'</td></tr>';
print '<tr><td width="20%">'.$langs->trans("UrlOrLink").'</td><td><input class="flat" name="url" size="50" value=""></td><td>'.$langs->trans("UseAnExternalHttpLinkOrRelativeDolibarrLink").'</td></tr>'; print '<tr><td width="20%">'.$langs->trans("UrlOrLink").'</td><td><input class="flat" name="url" size="50" value="'.$url.'"></td><td>'.$langs->trans("UseAnExternalHttpLinkOrRelativeDolibarrLink").'</td></tr>';
print '<tr><td width="20%">'.$langs->trans("BehaviourOnClick").'</td><td>'; print '<tr><td width="20%">'.$langs->trans("BehaviourOnClick").'</td><td>';
$liste=array(1=>$langs->trans("OpenANewWindow"),0=>$langs->trans("ReplaceWindow")); $liste=array(1=>$langs->trans("OpenANewWindow"),0=>$langs->trans("ReplaceWindow"));
$html->select_array('target',$liste,1); $html->select_array('target',$liste,1);