FixZasobBaseAlias.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_FixZasobBaseAlias extends RouteBase {
  4. public function handleAuth() {
  5. if (!User::logged()) {
  6. throw new HttpException('Unauthorized', 401);
  7. }
  8. }
  9. public function defaultAction() {
  10. SE_Layout::gora();
  11. ?>
  12. <div class="jumbotron">
  13. <div class="container">
  14. <form class="form-inline" method="POST">
  15. <input type="hidden" name="_route" value="FixZasobPath" />
  16. <input type="hidden" name="_task" value="run" />
  17. <button type="submit" id="fldExecuteBtn" class="btn btn-primary" autocomplete="off">
  18. Zaktualizuj <code>ID_ZASOB_BASE</code>
  19. </button>
  20. </form>
  21. </div>
  22. </div>
  23. <script type="text/javascript">
  24. jQuery(document).ready(function () {
  25. jQuery('#fldExecuteBtn').on('click', function () {
  26. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  27. jQuery(this).parent().submit();
  28. })
  29. });
  30. </script>
  31. <?php
  32. SE_Layout::dol();
  33. }
  34. public function checkPathsAction() {
  35. $sql = "
  36. ";
  37. $rows = array();
  38. $db = DB::getDB();
  39. if ($db->has_errors()) {
  40. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  41. }
  42. $res = $db->query($sql);
  43. while ($r = $db->fetch($res)) {
  44. $rows[] = $r;
  45. }
  46. echo $sql;
  47. echo'<pre>';print_r($rows);echo'</pre>';
  48. die('OK');
  49. }
  50. public function runAction() {
  51. SE_Layout::gora();
  52. SE_Layout::menu();
  53. $this->_callProcedure();
  54. ?>
  55. <div class="container">
  56. <div class="alert alert-success">
  57. Zaktualizowano <code>`CRM_LISTA_ZASOBOW`.`ID_ZASOB_BASE`</code>
  58. </div>
  59. </div>
  60. <?php
  61. SE_Layout::dol();
  62. }
  63. public function runApiAction() {
  64. $this->_callProcedure();
  65. die('Zaktualizowano `CRM_LISTA_ZASOBOW`.`ID_ZASOB_BASE`');
  66. }
  67. private function _callProcedure() {
  68. $sql = <<<SQL
  69. update `CRM_LISTA_ZASOBOW` SET
  70. `ID_ZASOB_BASE`=IF(`ALIAS_ID`>0, FIND_ID_ZASOB_BASE(`ALIAS_ID`), `ID`);
  71. SQL;
  72. /* update fields:
  73. * `CRM_LISTA_ZASOBOW`.`path`
  74. * `CRM_WSKAZNIK`.`path_CRM_LISTA_ZASOBOW`
  75. */
  76. $db = DB::getDB();
  77. if ($db->has_errors()) {
  78. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  79. }
  80. $res = $db->query($sql);
  81. if ($db->has_errors()) {
  82. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  83. }
  84. }
  85. public function reinstallAction() {
  86. $this->reinstall();
  87. die('OK');
  88. }
  89. public function reinstall() {
  90. $sqlList = array();
  91. $sqlList['RemoveFunction'] = "DROP FUNCTION IF EXISTS `FIND_ID_ZASOB_BASE`";
  92. $sqlList['InstallFunction'] = <<<SQL_FUNCTION
  93. CREATE DEFINER=`root`@`localhost`
  94. FUNCTION `FIND_ID_ZASOB_BASE`(`ID_ZASOB` INT) RETURNS INT
  95. READS SQL DATA
  96. BEGIN
  97. SET @ID_ZASOB_BASE = `ID_ZASOB`;
  98. SET @LOOP_LIMIT = 10;
  99. REPEAT
  100. SELECT `ALIAS_ID`
  101. INTO @ALIAS_ID
  102. FROM `CRM_LISTA_ZASOBOW`
  103. WHERE `ID`=@ID_ZASOB_BASE;
  104. IF @ALIAS_ID IS NOT NULL AND @ALIAS_ID > 0 THEN
  105. SET @ID_ZASOB_BASE = @ALIAS_ID;
  106. END IF;
  107. SET @LOOP_LIMIT = @LOOP_LIMIT - 1;
  108. UNTIL @LOOP_LIMIT <= 0 END REPEAT;
  109. RETURN @ID_ZASOB_BASE;
  110. END
  111. SQL_FUNCTION;
  112. $db = DB::getDB();
  113. if ($db->has_errors()) {
  114. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  115. }
  116. foreach ($sqlList as $sqlName => $sql) {
  117. $res = $db->query($sql);
  118. if ($db->has_errors()) {
  119. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  120. }
  121. }
  122. }
  123. /*
  124. Table 'DB.P5-MSG:Route_FixZasobPath:WARNING: Update all paths' doesn't exist
  125. Table 'DB.P5-MSG:Route_FixZasobPath:ERROR: Loop detected ID=P_ID' doesn't exist
  126. Table 'DB.P5-MSG:Route_FixZasobPath:ERROR: Parent item not exists' doesn't exist
  127. Table 'DB.P5-MSG:Route_FixZasobPath:ERROR: Loop detected in path' doesn't exist
  128. */
  129. public function parseMessageFromStorage($msg) {
  130. switch ($msg) {
  131. case 'WARNING: Update all paths': {
  132. $msg = "Zaktualizuj ścieżki zasobów!";
  133. break;
  134. }
  135. case 'ERROR: Loop detected ID=P_ID': {
  136. $msg = "Nr rekordu nadrzędnego musi różnić się od nr rekordu";
  137. break;
  138. }
  139. case 'ERROR: Parent item not exists': {
  140. $msg = "Nie istnieje rekord o numerze podanym jako nr nadrzędny";
  141. break;
  142. }
  143. case 'ERROR: Loop detected in path': {
  144. $msg = "Nieprawidłowy nr nadrzędny";
  145. break;
  146. }
  147. }
  148. return $msg;
  149. }
  150. public function parseMessageFromMsgsSystem($msg) {
  151. switch ($msg) {
  152. case 'Update all paths': {
  153. $msg = "Zaktualizuj ścieżki zasobów";
  154. break;
  155. }
  156. }
  157. return $msg;
  158. }
  159. public function runByMessageFromMsgsSystem($msg, &$execNotes) {
  160. switch ($msg) {
  161. case 'Update all paths': {
  162. $execNotes .= 'call procedure... ';
  163. $this->_callProcedure();
  164. $execNotes .= ' done';
  165. break;
  166. }
  167. }
  168. }
  169. }