dolibarr 21.0.0-beta
repair.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 * or see https://www.gnu.org/
17 */
18
31function checkElementExist($id, $table)
32{
33 global $db;
34
35 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$table;
36 $sql .= " WHERE rowid = ".((int) $id);
37 $resql = $db->query($sql);
38 if ($resql) {
39 $num = $db->num_rows($resql);
40 if ($num > 0) {
41 return true;
42 } else {
43 return false;
44 }
45 } else {
46 return true; // for security
47 }
48}
49
57function checkLinkedElements($sourcetype, $targettype)
58{
59 global $db, $langs;
60
61 $elements = array();
62 $deleted = 0;
63
64 $sourcetable = $sourcetype;
65 $targettable = $targettype;
66
67 if ($sourcetype == 'shipping') {
68 $sourcetable = 'expedition';
69 } elseif ($targettype == 'shipping') {
70 $targettable = 'expedition';
71 }
72 if ($sourcetype == 'delivery') {
73 $sourcetable = 'livraison';
74 } elseif ($targettype == 'delivery') {
75 $targettable = 'livraison';
76 }
77 if ($sourcetype == 'order_supplier') {
78 $sourcetable = 'commande_fournisseur';
79 } elseif ($targettype == 'order_supplier') {
80 $targettable = 'commande_fournisseur';
81 }
82 if ($sourcetype == 'invoice_supplier') {
83 $sourcetable = 'facture_fourn';
84 } elseif ($targettype == 'invoice_supplier') {
85 $targettable = 'facture_fourn';
86 }
87
88 $out = $langs->trans('SourceType').': '.$sourcetype.' => '.$langs->trans('TargetType').': '.$targettype.' ';
89
90 $sql = "SELECT rowid, fk_source, fk_target FROM ".MAIN_DB_PREFIX."element_element";
91 $sql .= " WHERE sourcetype = '".$db->escape($sourcetype)."' AND targettype = '".$db->escape($targettype)."'";
92 $resql = $db->query($sql);
93 if ($resql) {
94 $num = $db->num_rows($resql);
95 if ($num) {
96 $i = 0;
97 while ($i < $num) {
98 $obj = $db->fetch_object($resql);
99 $elements[$obj->rowid] = array($sourcetype => $obj->fk_source, $targettype => $obj->fk_target);
100 $i++;
101 }
102 }
103 }
104
105 if (!empty($elements)) {
106 foreach ($elements as $key => $element) {
107 if (!checkElementExist($element[$sourcetype], $sourcetable) || !checkElementExist($element[$targettype], $targettable)) {
108 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'element_element';
109 $sql .= " WHERE rowid = ".((int) $key);
110 $resql = $db->query($sql);
111 $deleted++;
112 }
113 }
114 }
115
116 if ($deleted) {
117 $out .= '('.$langs->trans('LinkedElementsInvalidDeleted', $deleted).')<br>';
118 } else {
119 $out .= '('.$langs->trans('NothingToDelete').')<br>';
120 }
121
122 return $out;
123}
124
131{
132 global $db, $langs;
133
134 // Clean data from ecm_directories
135 $sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."ecm_directories";
136 $resql = $db->query($sql);
137 if ($resql) {
138 while ($obj = $db->fetch_object($resql)) {
139 $id = $obj->rowid;
140 $label = $obj->label;
141 $newlabel = dol_sanitizeFileName($label);
142 if ($label != $newlabel) {
143 $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set label = '".$db->escape($newlabel)."' WHERE rowid = ".((int) $id);
144 print '<tr><td>'.$sqlupdate."</td></tr>\n";
145 $resqlupdate = $db->query($sqlupdate);
146 if (!$resqlupdate) {
147 dol_print_error($db, 'Failed to update');
148 return -1;
149 }
150 }
151 }
152 } else {
153 dol_print_error($db, 'Failed to run request');
154 return -1;
155 }
156
157 return 1;
158}
$id
Definition account.php:48
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
clean_data_ecm_directories()
Clean data into ecm_directories table.
checkLinkedElements($sourcetype, $targettype)
Check linked elements and delete if invalid.
checkElementExist($id, $table)
Check if an element exist.