FixProjectPath.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_FixProjectPath extends RouteBase {
  4. public function handleAuth() {
  5. if (!User::logged()) {
  6. throw new HttpException('Unauthorized', 401);
  7. }
  8. }
  9. public function defaultAction() {
  10. //$sqlList['Check'] = "SHOW PROCEDURE STATUS where `Name`='update_project_path_idx_rec'";
  11. SE_Layout::gora();
  12. //echo '<a href="/index.php?_route=FixProjectPath&_task=run">Zaktualizuj ścieżki projektów</a>';
  13. ?>
  14. <div class="jumbotron">
  15. <div class="container">
  16. <form class="form-inline" method="POST">
  17. <input type="hidden" name="_route" value="FixProjectPath" />
  18. <input type="hidden" name="_task" value="run" />
  19. <button type="submit" id="fldExecuteBtn" class="btn btn-primary" autocomplete="off">
  20. Zaktualizuj ścieżki projektów
  21. </button>
  22. </form>
  23. </div>
  24. </div>
  25. <script type="text/javascript">
  26. jQuery(document).ready(function () {
  27. jQuery('#fldExecuteBtn').on('click', function () {
  28. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  29. jQuery(this).parent().submit();
  30. })
  31. });
  32. </script>
  33. <?php
  34. SE_Layout::dol();
  35. }
  36. public function checkPathsAction() {
  37. $sql = "
  38. select p.`ID`
  39. , p.`path` as proj_path
  40. , k.`path` as koresp_path
  41. , i.`idx_PATH` as idx_path
  42. from `IN7_MK_BAZA_DYSTRYBUCJI` p
  43. join `_project_path_idx` i on(i.`ID`=p.`ID`)
  44. left join `IN7_DZIENNIK_KORESP` k on(k.`ID_PROJECT`=p.`ID`)
  45. where (p.`path`!=i.`idx_PATH` or k.`path`!=i.`idx_PATH`)
  46. ";
  47. $rows = array();
  48. $db = DB::getDB();
  49. if ($db->has_errors()) {
  50. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  51. }
  52. $res = $db->query($sql);
  53. while ($r = $db->fetch($res)) {
  54. $rows[] = $r;
  55. }
  56. echo $sql;
  57. echo'<pre>';print_r($rows);echo'</pre>';
  58. die('OK');
  59. }
  60. public function runAction() {
  61. SE_Layout::gora();
  62. SE_Layout::menu();
  63. $this->_callProcedure();
  64. ?>
  65. <div class="container">
  66. <div class="alert alert-success">
  67. Zaktualizowano ścieżki projektów
  68. </div>
  69. </div>
  70. <?php
  71. SE_Layout::dol();
  72. }
  73. public function runApiAction() {
  74. $this->_callProcedure();
  75. die('Zaktualizowano ścieżki projektów');
  76. }
  77. private function _callProcedure() {
  78. $sql = "call `update_project_path_idx_rec`();";
  79. /* update fields:
  80. * `IN7_MK_BAZA_DYSTRYBUCJI`.`path`
  81. * `IN7_DZIENNIK_KORESP`.`path`
  82. * `PROBLEMS`.`ID_PROJECT_path`
  83. */
  84. $db = DB::getDB();
  85. if ($db->has_errors()) {
  86. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  87. }
  88. $res = $db->query($sql);
  89. if ($db->has_errors()) {
  90. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  91. }
  92. }
  93. public function cleanAllPathsAction() {
  94. $sqlList = array();
  95. $sqlList['CleanPath_Projekty'] = "update `IN7_MK_BAZA_DYSTRYBUCJI` p set p.`path`='' ";
  96. $sqlList['CleanPath_Koresp'] = "update `IN7_DZIENNIK_KORESP` k set k.`path`='' ";
  97. $sqlList['CleanPath_Problems'] = "update `PROBLEMS` pr set pr.`path`='' ";
  98. $db = DB::getDB();
  99. if ($db->has_errors()) {
  100. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  101. }
  102. foreach ($sqlList as $sqlName => $sql) {
  103. $res = $db->query($sql);
  104. if ($db->has_errors()) {
  105. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  106. }
  107. }
  108. die('OK');
  109. }
  110. public function reinstallAction() {
  111. $this->reinstall();
  112. die('OK');
  113. }
  114. public function reinstall() {
  115. $sqlList = array();
  116. $sqlList['RemoveTable'] = "DROP TABLE IF EXISTS `_project_path_idx`";
  117. $sqlList['InstallTable'] = "
  118. CREATE TABLE IF NOT EXISTS `_project_path_idx` (
  119. `ID` int(11) NOT NULL
  120. , `P_ID` int(11) NOT NULL DEFAULT '0'
  121. , `idx_PATH` varchar(255) NOT NULL DEFAULT ''
  122. , KEY `ID` (`ID`)
  123. , KEY `P_ID` (`P_ID`)
  124. ) ENGINE=MyISAM DEFAULT CHARSET=latin2
  125. ";
  126. $sqlList['RemoveProcedure'] = "DROP PROCEDURE if exists `update_project_path_idx_rec`";
  127. $sqlList['CreateProcedure'] = "
  128. CREATE PROCEDURE `update_project_path_idx_rec`()
  129. BEGIN
  130. SET @conf_last_exec_key = 'tbl_indexer_project_last_exec';
  131. replace into `CRM_CONFIG` (`conf_key`, `conf_val`) values (@conf_last_exec_key, NOW());
  132. truncate table `_project_path_idx`;
  133. -- delete from `_project_path_idx`;
  134. insert into `_project_path_idx` (`ID`,`P_ID`)
  135. select p.`ID`, p.`P_ID`
  136. from `IN7_MK_BAZA_DYSTRYBUCJI` p
  137. where 1=1
  138. ;
  139. update `_project_path_idx` as p set p.`idx_PATH`=concat('0-', p.`ID`) where p.`P_ID` is null or p.`P_ID`=0;
  140. update `_project_path_idx` as p set p.`idx_PATH`=concat('-1-', p.`ID`) where p.`P_ID`=-1;
  141. SET @i = 0;
  142. SET @loopLomit = 100;
  143. SET @pinitCnt = 1;
  144. WHILE @i < @loopLomit and @pinitCnt > 0 DO
  145. update `_project_path_idx` p join `_project_path_idx` pp on(pp.`ID`=p.`P_ID`)
  146. set p.`idx_PATH`=concat(pp.`idx_PATH`, '-', p.`ID`)
  147. where p.`idx_PATH`='' and pp.`idx_PATH`!='';
  148. SET @pinitCnt = ROW_COUNT();
  149. SET @i = @i + 1;
  150. END WHILE;
  151. update `IN7_MK_BAZA_DYSTRYBUCJI` p join `_project_path_idx` i on(i.`ID`=p.`ID`)
  152. set p.`path`=i.`idx_PATH`;
  153. update `IN7_DZIENNIK_KORESP` k left join `_project_path_idx` i on(i.`ID`=k.`ID_PROJECT`)
  154. set k.`path`=coalesce(i.`idx_PATH`, '?');
  155. update `PROBLEMS` pr left join `_project_path_idx` i on(i.`ID`=pr.`ID_PROJECT`)
  156. set pr.`ID_PROJECT_path`=coalesce(i.`idx_PATH`, '?');
  157. update `CRM_UI_MSGS`
  158. set `A_STATUS`='OFF_HARD'
  159. , `actionExecutedTime`=NOW()
  160. , `A_RECORD_UPDATE_DATE`=NOW()
  161. , `A_RECORD_UPDATE_AUTHOR`='update_zasob_path_idx_rec'
  162. where `A_STATUS`='WAITING'
  163. and `app_className`='FixProjectPath'
  164. and `msg`='Update all paths'
  165. and `uiTargetType`='default_db_table'
  166. and `uiTargetName`='IN7_MK_BAZA_DYSTRYBUCJI';
  167. END
  168. ";
  169. $sqlList['RemoveTrigger_BeforeInsertProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_INSERT`";
  170. $sqlList['CreateTrigger_BeforeInsertProject'] = "
  171. CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_INSERT` BEFORE INSERT ON `IN7_MK_BAZA_DYSTRYBUCJI`
  172. FOR EACH ROW BEGIN
  173. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  174. SET NEW.path = (select CONCAT(
  175. (select IF (NEW.`P_ID`>0,
  176. coalesce(
  177. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`P_ID` limit 1)
  178. , '?')
  179. , NEW.`P_ID`
  180. ))
  181. , '-'
  182. , (select AUTO_INCREMENT from information_schema.TABLES where TABLE_SCHEMA=DATABASE() AND TABLE_NAME='IN7_MK_BAZA_DYSTRYBUCJI')
  183. ));
  184. END
  185. ";
  186. $sqlList['RemoveTrigger_BeforeUpdateProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE`";
  187. // throws errors:
  188. // #1146 - Table '{DATABASE_NAME}.ERROR: Loop detected ID=P_ID' doesn't exist
  189. // #1146 - Table '{DATABASE_NAME}.ERROR: Parent item not exists' doesn't exist
  190. // #1146 - Table '{DATABASE_NAME}.ERROR: Loop detected in path' doesn't exist
  191. $sqlList['CreateTrigger_BeforeUpdateProject'] = "
  192. CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE` BEFORE UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
  193. FOR EACH ROW BEGIN
  194. -- IF NEW.P_ID<>OLD.P_ID THEN
  195. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  196. -- update IN7_DZIENNIK_KORESP ik set ik.path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID) where path like concat('%',NEW.ID,'%');
  197. -- -- update PROBLEMS ik set ik.ID_PROJET_path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID) where ID_PROJET_path like concat('%',NEW.ID,'%');
  198. -- END IF;
  199. IF NEW.`P_ID`!=OLD.`P_ID` THEN
  200. -- send error if loop
  201. -- OLD.path like concat('%-', NEW.P_ID, '-%')
  202. IF OLD.ID = NEW.P_ID THEN
  203. UPDATE `P5-MSG:Route_FixProjectPath:ERROR: Loop detected ID=P_ID` SET x=1;
  204. -- #1146 - Table 'biall.ERROR: Loop detected ID=P_ID' doesn't exist
  205. END IF;
  206. IF NEW.P_ID > 0 THEN
  207. -- check if project exists
  208. IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.P_ID) = 0 THEN
  209. UPDATE `P5-MSG:Route_FixProjectPath:ERROR: Parent item not exists` SET x=1;
  210. -- #1146 - Table 'biall.ERROR: Parent item not exists' doesn't exist
  211. END IF;
  212. -- check loop error
  213. -- state: 1276.path='0-868-1218-1276', 1218.path='0-868-1218'
  214. -- update P_ID=1276 where ID=1218 - should throw error
  215. IF (select IF(
  216. p.`path` like concat('%-',OLD.ID,'-%')
  217. or p.`path` like concat(OLD.ID,'-%')
  218. , 1
  219. , 0) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.P_ID) > 0 THEN
  220. UPDATE `P5-MSG:Route_FixProjectPath:ERROR: Loop detected in path` SET x=1;
  221. -- #1146 - Table 'biall.ERROR: Loop detected in path' doesn't exist
  222. END IF;
  223. END IF;
  224. SET NEW.`path` = (select CONCAT(
  225. (select IF (NEW.`P_ID`>0,
  226. coalesce(
  227. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`P_ID` limit 1)
  228. , '?')
  229. , NEW.`P_ID`
  230. ))
  231. , '-'
  232. , NEW.ID
  233. ));
  234. -- DONT: throw warning to update all pathes in `IN7_MK_BAZA_DYSTRYBUCJI` with path under current?
  235. -- NOTE: throw error like that prevent update fields
  236. -- IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`P_ID`=NEW.ID) > 0 THEN
  237. -- UPDATE `P5-MSG:Route_FixProjectPath:WARNING: Update all paths` SET x=1;
  238. -- END IF;
  239. -- DONT: update `IN7_DZIENNIK_KORESP`.`path` - rows under NEW.ID has wrong path
  240. -- update `IN7_DZIENNIK_KORESP` k
  241. -- set k.`path`=(select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=k.`ID_PROJECT`)
  242. -- where k.`path` like concat(OLD.`path`, '-%');
  243. -- TODO: update `PROBLEMS`.`ID_PROJECT_path` if table exists
  244. -- update `PROBLEMS` k
  245. -- set k.`ID_PROJECT_path`=(select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=k.`ID_PROJECT`)
  246. -- where (k.`ID_PROJECT_path` like concat(OLD.`path`, '-%')
  247. -- or k.`ID_PROJECT_path`=OLD.`path`
  248. -- );
  249. END IF;
  250. END
  251. ";
  252. $sqlList['RemoveTrigger_AfterUpdateProject'] = "DROP TRIGGER IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI_tree_AFTER_UPDATE`";
  253. // throws errors:
  254. // #1146 - Table '{DATABASE_NAME}.WARNING: Update all paths' doesn't exist
  255. $sqlList['CreateTrigger_AfterUpdateProject'] = "
  256. CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_AFTER_UPDATE` AFTER UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
  257. FOR EACH ROW BEGIN
  258. IF NEW.`P_ID`!=OLD.`P_ID` THEN
  259. -- throw warning to update all pathes in `IN7_MK_BAZA_DYSTRYBUCJI` with path under current?
  260. IF (select count(1) from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`P_ID`=NEW.ID) > 0 THEN
  261. IF (select count(1) from `CRM_UI_MSGS` where `app_className`='FixProjectPath'
  262. and `msg`='Update all paths'
  263. and `uiTargetType`='default_db_table'
  264. and `uiTargetName`='IN7_MK_BAZA_DYSTRYBUCJI'
  265. and `A_STATUS`='WAITING'
  266. ) = 0 THEN
  267. INSERT INTO `CRM_UI_MSGS` (`ID`
  268. , `app_className`, `msg`, `msgType`
  269. , `uiTargetType`, `uiTargetName`
  270. , `userTargetType`
  271. , `A_RECORD_CREATE_DATE`, `A_RECORD_CREATE_AUTHOR`)
  272. VALUES (NULL
  273. , 'FixProjectPath', 'Update all paths', 'danger'
  274. , 'default_db_table', 'IN7_MK_BAZA_DYSTRYBUCJI'
  275. , 'everyone'
  276. , NOW(), NEW.A_RECORD_UPDATE_AUTHOR
  277. );
  278. END IF;
  279. UPDATE `P5-MSG:Route_FixProjectPath:WARNING: Update all paths` SET x=1;
  280. END IF;
  281. END IF;
  282. END
  283. ";
  284. $sqlList['RemoveTrigger_BeforeInsertKoresp'] = "DROP TRIGGER IF EXISTS `_IN7_DZIENNIK_KORESP_tree_INSERT`";
  285. $sqlList['CreateTrigger_BeforeInsertKoresp'] = "
  286. CREATE TRIGGER `_IN7_DZIENNIK_KORESP_tree_INSERT` BEFORE INSERT ON `IN7_DZIENNIK_KORESP`
  287. FOR EACH ROW BEGIN
  288. IF NEW.ID_PROJECT IS NOT NULL and NEW.ID_PROJECT>0 THEN
  289. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  290. SET NEW.path = (select coalesce(
  291. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  292. , '?'));
  293. END IF;
  294. END
  295. ";
  296. $sqlList['RemoveTrigger_BeforeUpdateKoresp'] = "DROP TRIGGER IF EXISTS `_IN7_DZIENNIK_KORESP_tree_UPDATE`";
  297. $sqlList['CreateTrigger_BeforeUpdateKoresp'] = "
  298. CREATE TRIGGER `_IN7_DZIENNIK_KORESP_tree_UPDATE` BEFORE UPDATE ON `IN7_DZIENNIK_KORESP`
  299. FOR EACH ROW BEGIN
  300. IF NEW.ID_PROJECT IS NULL THEN
  301. SET NEW.path = '';
  302. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  303. -- SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  304. IF NEW.ID_PROJECT>0 THEN
  305. SET NEW.path = (select coalesce(
  306. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  307. , '?'));
  308. ELSE
  309. SET NEW.path = '';
  310. END IF;
  311. END IF;
  312. END
  313. ";
  314. $sqlList['RemoveTrigger_BeforeInsertProblems'] = "DROP TRIGGER IF EXISTS `_PROBLEMS_tree_INSERT`";
  315. $sqlList['CreateTrigger_BeforeInsertProblems'] = "
  316. CREATE TRIGGER `_PROBLEMS_tree_INSERT` BEFORE INSERT ON `PROBLEMS`
  317. FOR EACH ROW BEGIN
  318. IF NEW.ID_PROJECT IS NOT NULL and NEW.ID_PROJECT>0 THEN
  319. -- SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  320. SET NEW.ID_PROJECT_path = (select coalesce(
  321. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  322. , '?'));
  323. END IF;
  324. END
  325. ";
  326. $sqlList['RemoveTrigger_BeforeUpdateProblems'] = "DROP TRIGGER IF EXISTS `_PROBLEMS_tree_UPDATE`";
  327. $sqlList['CreateTrigger_BeforeUpdateProblems'] = "
  328. CREATE TRIGGER `_PROBLEMS_tree_UPDATE` BEFORE UPDATE ON `PROBLEMS`
  329. FOR EACH ROW BEGIN
  330. IF NEW.ID_PROJECT IS NULL THEN
  331. SET NEW.ID_PROJECT_path = '';
  332. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  333. -- SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  334. IF NEW.ID_PROJECT>0 THEN
  335. SET NEW.ID_PROJECT_path = (select coalesce(
  336. (select p.`path` from `IN7_MK_BAZA_DYSTRYBUCJI` p where p.`ID`=NEW.`ID_PROJECT` limit 1)
  337. , '?'));
  338. ELSE
  339. SET NEW.ID_PROJECT_path = '';
  340. END IF;
  341. END IF;
  342. END
  343. ";
  344. $sqlList['RemoveEvent_everyDay'] = "DROP EVENT IF EXISTS `_IN7_MK_BAZA_DYSTRYBUCJI__IN7_DZIENNIK_KORESP_tree_event`";
  345. $sqlList['CreateEvent_everyDay'] = "
  346. CREATE EVENT `_IN7_MK_BAZA_DYSTRYBUCJI__IN7_DZIENNIK_KORESP_tree_event` ON SCHEDULE EVERY 1 DAY STARTS '2015-05-15 05:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
  347. call `update_project_path_idx_rec`();
  348. END
  349. ";
  350. $db = DB::getDB();
  351. if ($db->has_errors()) {
  352. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  353. }
  354. foreach ($sqlList as $sqlName => $sql) {
  355. $res = $db->query($sql);
  356. if ($db->has_errors()) {
  357. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  358. }
  359. }
  360. }
  361. /*
  362. Table 'DB.P5-MSG:Route_FixProjectPath:WARNING: Update all paths' doesn't exist
  363. Table 'DB.P5-MSG:Route_FixProjectPath:ERROR: Loop detected ID=P_ID' doesn't exist
  364. Table 'DB.P5-MSG:Route_FixProjectPath:ERROR: Parent item not exists' doesn't exist
  365. Table 'DB.P5-MSG:Route_FixProjectPath:ERROR: Loop detected in path' doesn't exist
  366. */
  367. public function parseMessageFromStorage($msg) {
  368. switch ($msg) {
  369. case 'WARNING: Update all paths': {
  370. $msg = "Zaktualizuj ścieżki projektów!";
  371. break;
  372. }
  373. case 'ERROR: Loop detected ID=P_ID': {
  374. $msg = "Nr rekordu nadrzędnego musi różnić się od nr rekordu";
  375. break;
  376. }
  377. case 'ERROR: Parent item not exists': {
  378. $msg = "Nie istnieje rekord o numerze podanym jako nr nadrzędny";
  379. break;
  380. }
  381. case 'ERROR: Loop detected in path': {
  382. $msg = "Nieprawidłowy nr nadrzędny";
  383. break;
  384. }
  385. }
  386. return $msg;
  387. }
  388. public function parseMessageFromMsgsSystem($msg) {
  389. switch ($msg) {
  390. case 'Update all paths': {
  391. $msg = "Zaktualizuj ścieżki projektów";
  392. break;
  393. }
  394. }
  395. return $msg;
  396. }
  397. public function runByMessageFromMsgsSystem($msg, &$execNotes) {
  398. switch ($msg) {
  399. case 'Update all paths': {
  400. $execNotes .= 'call procedure... ';
  401. $this->_callProcedure();
  402. $execNotes .= ' done';
  403. break;
  404. }
  405. }
  406. }
  407. }
  408. /**
  409. * Old triggers before 2015-06-10:
  410. CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_INSERT` BEFORE INSERT ON `IN7_MK_BAZA_DYSTRYBUCJI`
  411. FOR EACH ROW BEGIN
  412. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  413. END
  414. CREATE TRIGGER `_IN7_MK_BAZA_DYSTRYBUCJI_tree_UPDATE` BEFORE UPDATE ON `IN7_MK_BAZA_DYSTRYBUCJI`
  415. FOR EACH ROW BEGIN
  416. IF NEW.P_ID<>OLD.P_ID THEN
  417. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.P_ID);
  418. update IN7_DZIENNIK_KORESP ik set ik.path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID) where path like concat('%',NEW.ID,'%');
  419. -- update PROBLEMS ik set ik.ID_PROJET_path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID) where ID_PROJET_path like concat('%',NEW.ID,'%');
  420. END IF;
  421. END
  422. CREATE TRIGGER `_IN7_DZIENNIK_KORESP_tree_INSERT` BEFORE INSERT ON `IN7_DZIENNIK_KORESP`
  423. FOR EACH ROW BEGIN
  424. IF NEW.ID_PROJECT IS NOT NULL THEN
  425. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  426. END IF;
  427. END
  428. CREATE TRIGGER `_IN7_DZIENNIK_KORESP_tree_UPDATE` BEFORE UPDATE ON `IN7_DZIENNIK_KORESP`
  429. FOR EACH ROW BEGIN
  430. IF NEW.ID_PROJECT is null THEN
  431. SET NEW.path = '';
  432. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  433. SET NEW.path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  434. END IF;
  435. END
  436. CREATE TRIGGER `_PROBLEMS_tree_INSERT` BEFORE INSERT ON `PROBLEMS`
  437. FOR EACH ROW BEGIN
  438. IF NEW.ID_PROJECT IS NOT NULL THEN
  439. SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  440. END IF;
  441. END
  442. CREATE TRIGGER `_PROBLEMS_tree_UPDATE` BEFORE UPDATE ON `PROBLEMS`
  443. FOR EACH ROW BEGIN
  444. IF NEW.ID_PROJECT is null THEN
  445. SET NEW.ID_PROJECT_path = '';
  446. ELSEIF OLD.ID_PROJECT IS NULL or NEW.ID_PROJECT<>OLD.ID_PROJECT THEN
  447. SET NEW.ID_PROJECT_path = IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-', NEW.ID_PROJECT);
  448. END IF;
  449. END
  450. CREATE FUNCTION `IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path`(`delimiter` TEXT, `node` INT) RETURNS text CHARSET latin2
  451. READS SQL DATA
  452. BEGIN
  453. DECLARE _path TEXT;
  454. DECLARE _type CHAR(255);
  455. DECLARE _lvl INT;
  456. DECLARE _cpath TEXT;
  457. DECLARE _id INT;
  458. DECLARE _id_cur INT;
  459. DECLARE EXIT HANDLER FOR NOT FOUND RETURN _path;
  460. SET _id = COALESCE(node, @id);
  461. SET _path = _id;
  462. SET _lvl=1;
  463. the_loop: LOOP
  464. SET _lvl =_lvl+1;
  465. IF _lvl>100 THEN
  466. RETURN concat('ERROR',_path);
  467. LEAVE the_loop;
  468. END IF;
  469. SELECT P_ID,M_DIST_TYPE,ID
  470. INTO _id,_type,_id_cur
  471. FROM IN7_MK_BAZA_DYSTRYBUCJI
  472. WHERE id = _id
  473. AND COALESCE(id <> @start_with, TRUE);
  474. SET _path = CONCAT(_id, delimiter, _path);
  475. END LOOP the_loop;
  476. END
  477. CREATE EVENT `_IN7_MK_BAZA_DYSTRYBUCJI__IN7_DZIENNIK_KORESP_tree_event` ON SCHEDULE EVERY 1 DAY STARTS '2015-05-15 05:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
  478. update IN7_MK_BAZA_DYSTRYBUCJI set path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-',ID);
  479. update IN7_DZIENNIK_KORESP set path=IN7_MK_BAZA_DYSTRYBUCJI_hierarchy_sys_connect_by_path('-',ID_PROJECT);
  480. END
  481. */