repair.lib.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  19. * \file htdocs/install/lib/repair.lib.php
  20. * \brief Library of repair functions
  21. */
  22. /**
  23. * Check if an element exist
  24. *
  25. * @param int $id Element id
  26. * @param string $table Table of Element
  27. * @return boolean True if child exists
  28. */
  29. function checkElementExist($id, $table)
  30. {
  31. global $db;
  32. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$table;
  33. $sql .= " WHERE rowid = ".((int) $id);
  34. $resql = $db->query($sql);
  35. if ($resql) {
  36. $num = $db->num_rows($resql);
  37. if ($num > 0) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. } else {
  43. return true; // for security
  44. }
  45. }
  46. /**
  47. * Check linked elements and delete if invalid
  48. *
  49. * @param string $sourcetype Source element type
  50. * @param string $targettype Target element type
  51. * @return string
  52. */
  53. function checkLinkedElements($sourcetype, $targettype)
  54. {
  55. global $db, $langs;
  56. $elements = array();
  57. $deleted = 0;
  58. $sourcetable = $sourcetype;
  59. $targettable = $targettype;
  60. if ($sourcetype == 'shipping') {
  61. $sourcetable = 'expedition';
  62. } elseif ($targettype == 'shipping') {
  63. $targettable = 'expedition';
  64. }
  65. if ($sourcetype == 'delivery') {
  66. $sourcetable = 'livraison';
  67. } elseif ($targettype == 'delivery') {
  68. $targettable = 'livraison';
  69. }
  70. if ($sourcetype == 'order_supplier') {
  71. $sourcetable = 'commande_fournisseur';
  72. } elseif ($targettype == 'order_supplier') {
  73. $targettable = 'commande_fournisseur';
  74. }
  75. if ($sourcetype == 'invoice_supplier') {
  76. $sourcetable = 'facture_fourn';
  77. } elseif ($targettype == 'invoice_supplier') {
  78. $targettable = 'facture_fourn';
  79. }
  80. $out = $langs->trans('SourceType').': '.$sourcetype.' => '.$langs->trans('TargetType').': '.$targettype.' ';
  81. $sql = "SELECT rowid, fk_source, fk_target FROM ".MAIN_DB_PREFIX."element_element";
  82. $sql .= " WHERE sourcetype = '".$db->escape($sourcetype)."' AND targettype = '".$db->escape($targettype)."'";
  83. $resql = $db->query($sql);
  84. if ($resql) {
  85. $num = $db->num_rows($resql);
  86. if ($num) {
  87. $i = 0;
  88. while ($i < $num) {
  89. $obj = $db->fetch_object($resql);
  90. $elements[$obj->rowid] = array($sourcetype => $obj->fk_source, $targettype => $obj->fk_target);
  91. $i++;
  92. }
  93. }
  94. }
  95. if (!empty($elements)) {
  96. foreach ($elements as $key => $element) {
  97. if (!checkElementExist($element[$sourcetype], $sourcetable) || !checkElementExist($element[$targettype], $targettable)) {
  98. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'element_element';
  99. $sql .= " WHERE rowid = ".((int) $key);
  100. $resql = $db->query($sql);
  101. $deleted++;
  102. }
  103. }
  104. }
  105. if ($deleted) {
  106. $out .= '('.$langs->trans('LinkedElementsInvalidDeleted', $deleted).')<br>';
  107. } else {
  108. $out .= '('.$langs->trans('NothingToDelete').')<br>';
  109. }
  110. return $out;
  111. }
  112. /**
  113. * Clean data into ecm_directories table
  114. *
  115. * @return void
  116. */
  117. function clean_data_ecm_directories()
  118. {
  119. global $db, $langs;
  120. // Clean data from ecm_directories
  121. $sql = "SELECT rowid, label FROM ".MAIN_DB_PREFIX."ecm_directories";
  122. $resql = $db->query($sql);
  123. if ($resql) {
  124. while ($obj = $db->fetch_object($resql)) {
  125. $id = $obj->rowid;
  126. $label = $obj->label;
  127. $newlabel = dol_sanitizeFileName($label);
  128. if ($label != $newlabel) {
  129. $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set label = '".$db->escape($newlabel)."' WHERE rowid = ".((int) $id);
  130. print '<tr><td>'.$sqlupdate."</td></tr>\n";
  131. $resqlupdate = $db->query($sqlupdate);
  132. if (!$resqlupdate) {
  133. dol_print_error($db, 'Failed to update');
  134. }
  135. }
  136. }
  137. } else {
  138. dol_print_error($db, 'Failed to run request');
  139. }
  140. return;
  141. }