dolibarr  13.0.2
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 
31 function checkElementExist($id, $table)
32 {
33  global $db;
34 
35  $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.$table;
36  $sql .= ' WHERE rowid = '.$id;
37  $resql = $db->query($sql);
38  if ($resql)
39  {
40  $num = $db->num_rows($resql);
41  if ($num > 0) return true;
42  else return false;
43  } else return true; // for security
44 }
45 
53 function checkLinkedElements($sourcetype, $targettype)
54 {
55  global $db, $langs;
56 
57  $elements = array();
58  $deleted = 0;
59 
60  $sourcetable = $sourcetype;
61  $targettable = $targettype;
62 
63  if ($sourcetype == 'shipping') $sourcetable = 'expedition';
64  elseif ($targettype == 'shipping') $targettable = 'expedition';
65  if ($sourcetype == 'delivery') $sourcetable = 'livraison';
66  elseif ($targettype == 'delivery') $targettable = 'livraison';
67  if ($sourcetype == 'order_supplier') $sourcetable = 'commande_fournisseur';
68  elseif ($targettype == 'order_supplier') $targettable = 'commande_fournisseur';
69  if ($sourcetype == 'invoice_supplier') $sourcetable = 'facture_fourn';
70  elseif ($targettype == 'invoice_supplier') $targettable = 'facture_fourn';
71 
72  $out = $langs->trans('SourceType').': '.$sourcetype.' => '.$langs->trans('TargetType').': '.$targettype.' ';
73 
74  $sql = 'SELECT rowid, fk_source, fk_target FROM '.MAIN_DB_PREFIX.'element_element';
75  $sql .= ' WHERE sourcetype="'.$sourcetype.'" AND targettype="'.$targettype.'"';
76  $resql = $db->query($sql);
77  if ($resql)
78  {
79  $num = $db->num_rows($resql);
80  if ($num)
81  {
82  $i = 0;
83  while ($i < $num)
84  {
85  $obj = $db->fetch_object($resql);
86  $elements[$obj->rowid] = array($sourcetype => $obj->fk_source, $targettype => $obj->fk_target);
87  $i++;
88  }
89  }
90  }
91 
92  if (!empty($elements))
93  {
94  foreach ($elements as $key => $element)
95  {
96  if (!checkElementExist($element[$sourcetype], $sourcetable) || !checkElementExist($element[$targettype], $targettable))
97  {
98  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'element_element';
99  $sql .= ' WHERE rowid = '.$key;
100  $resql = $db->query($sql);
101  $deleted++;
102  }
103  }
104  }
105 
106  if ($deleted) $out .= '('.$langs->trans('LinkedElementsInvalidDeleted', $deleted).')<br>';
107  else $out .= '('.$langs->trans('NothingToDelete').')<br>';
108 
109  return $out;
110 }
111 
118 {
119  global $db, $langs;
120 
121  // Clean data from ecm_directories
122  $sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."ecm_directories";
123  $resql = $db->query($sql);
124  if ($resql)
125  {
126  while ($obj = $db->fetch_object($resql))
127  {
128  $id = $obj->rowid;
129  $label = $obj->label;
130  $newlabel = dol_sanitizeFileName($label);
131  if ($label != $newlabel)
132  {
133  $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set label='".$db->escape($newlabel)."' WHERE rowid=".$id;
134  print '<tr><td>'.$sqlupdate."</td></tr>\n";
135  $resqlupdate = $db->query($sqlupdate);
136  if (!$resqlupdate) dol_print_error($db, 'Failed to update');
137  }
138  }
139  } else dol_print_error($db, 'Failed to run request');
140 
141  return;
142 }
checkLinkedElements($sourcetype, $targettype)
Check linked elements and delete if invalid.
Definition: repair.lib.php:53
clean_data_ecm_directories()
Clean data into ecm_directories table.
Definition: repair.lib.php:117
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
print
Draft customers invoices.
Definition: index.php:89
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
checkElementExist($id, $table)
Check if an element exist.
Definition: repair.lib.php:31