dolibarr/htdocs/install/lib/repair.lib.php

159 lines
4.2 KiB
PHP
Raw Permalink Normal View History

2012-06-29 09:30:24 +02:00
<?php
2018-10-27 14:43:12 +02:00
/* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
2012-06-29 09:30:24 +02:00
*
* 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
* the Free Software Foundation; either version 3 of the License, or
2012-06-29 09:30:24 +02:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* or see https://www.gnu.org/
2012-06-29 09:30:24 +02:00
*/
/**
* \file htdocs/install/lib/repair.lib.php
* \brief Library of repair functions
*/
/**
* Check if an element exist
*
* @param int $id Element id
* @param string $table Table of Element
2012-07-25 11:40:23 +02:00
* @return boolean True if child exists
2012-06-29 09:30:24 +02:00
*/
2012-07-02 19:30:37 +02:00
function checkElementExist($id, $table)
{
global $db;
2021-09-30 15:59:47 +02:00
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$table;
$sql .= " WHERE rowid = ".((int) $id);
$resql = $db->query($sql);
2021-02-22 15:18:01 +01:00
if ($resql) {
2012-07-02 19:30:37 +02:00
$num = $db->num_rows($resql);
2021-02-22 15:18:01 +01:00
if ($num > 0) {
return true;
} else {
return false;
}
} else {
return true; // for security
}
2012-06-29 09:30:24 +02:00
}
/**
* Check linked elements and delete if invalid
*
* @param string $sourcetype Source element type
* @param string $targettype Target element type
* @return string
*/
2012-07-02 19:30:37 +02:00
function checkLinkedElements($sourcetype, $targettype)
{
global $db, $langs;
$elements = array();
$deleted = 0;
2012-06-29 09:30:24 +02:00
$sourcetable = $sourcetype;
$targettable = $targettype;
2012-06-29 10:07:11 +02:00
2021-02-22 15:18:01 +01:00
if ($sourcetype == 'shipping') {
$sourcetable = 'expedition';
} elseif ($targettype == 'shipping') {
$targettable = 'expedition';
}
if ($sourcetype == 'delivery') {
$sourcetable = 'livraison';
} elseif ($targettype == 'delivery') {
$targettable = 'livraison';
}
if ($sourcetype == 'order_supplier') {
$sourcetable = 'commande_fournisseur';
} elseif ($targettype == 'order_supplier') {
$targettable = 'commande_fournisseur';
}
if ($sourcetype == 'invoice_supplier') {
$sourcetable = 'facture_fourn';
} elseif ($targettype == 'invoice_supplier') {
$targettable = 'facture_fourn';
}
2012-06-29 09:30:24 +02:00
2012-07-02 19:30:37 +02:00
$out = $langs->trans('SourceType').': '.$sourcetype.' => '.$langs->trans('TargetType').': '.$targettype.' ';
2021-09-30 15:59:47 +02:00
$sql = "SELECT rowid, fk_source, fk_target FROM ".MAIN_DB_PREFIX."element_element";
$sql .= " WHERE sourcetype = '".$db->escape($sourcetype)."' AND targettype = '".$db->escape($targettype)."'";
$resql = $db->query($sql);
2021-02-22 15:18:01 +01:00
if ($resql) {
2012-07-02 19:30:37 +02:00
$num = $db->num_rows($resql);
2021-02-22 15:18:01 +01:00
if ($num) {
2012-07-02 19:30:37 +02:00
$i = 0;
2021-02-22 15:18:01 +01:00
while ($i < $num) {
2012-07-02 19:30:37 +02:00
$obj = $db->fetch_object($resql);
$elements[$obj->rowid] = array($sourcetype => $obj->fk_source, $targettype => $obj->fk_target);
2012-07-02 19:30:37 +02:00
$i++;
}
}
2012-06-29 09:30:24 +02:00
}
2012-07-02 19:30:37 +02:00
2021-02-22 15:18:01 +01:00
if (!empty($elements)) {
foreach ($elements as $key => $element) {
if (!checkElementExist($element[$sourcetype], $sourcetable) || !checkElementExist($element[$targettype], $targettable)) {
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.'element_element';
2021-09-30 15:59:47 +02:00
$sql .= " WHERE rowid = ".((int) $key);
$resql = $db->query($sql);
2012-07-02 19:30:37 +02:00
$deleted++;
}
}
2012-06-29 09:30:24 +02:00
}
2021-02-22 15:18:01 +01:00
if ($deleted) {
$out .= '('.$langs->trans('LinkedElementsInvalidDeleted', $deleted).')<br>';
} else {
$out .= '('.$langs->trans('NothingToDelete').')<br>';
}
2012-07-02 19:30:37 +02:00
return $out;
2012-06-29 09:30:24 +02:00
}
/**
* Clean data into ecm_directories table
2012-07-25 11:40:23 +02:00
*
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, >0 if OK
*/
function clean_data_ecm_directories()
{
global $db, $langs;
2012-07-30 18:43:49 +02:00
// Clean data from ecm_directories
$sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."ecm_directories";
$resql = $db->query($sql);
2021-02-22 15:18:01 +01:00
if ($resql) {
while ($obj = $db->fetch_object($resql)) {
$id = $obj->rowid;
$label = $obj->label;
$newlabel = dol_sanitizeFileName($label);
2021-02-22 15:18:01 +01:00
if ($label != $newlabel) {
2021-04-25 15:55:36 +02:00
$sqlupdate = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set label = '".$db->escape($newlabel)."' WHERE rowid = ".((int) $id);
2012-07-30 18:43:49 +02:00
print '<tr><td>'.$sqlupdate."</td></tr>\n";
$resqlupdate = $db->query($sqlupdate);
2021-02-22 15:18:01 +01:00
if (!$resqlupdate) {
dol_print_error($db, 'Failed to update');
2023-01-06 19:34:45 +01:00
return -1;
2021-02-22 15:18:01 +01:00
}
2012-07-30 18:43:49 +02:00
}
}
2021-02-22 15:18:01 +01:00
} else {
dol_print_error($db, 'Failed to run request');
2023-01-06 19:34:45 +01:00
return -1;
2021-02-22 15:18:01 +01:00
}
2023-01-06 19:34:45 +01:00
return 1;
}