CrmProcesMap.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. class CrmProcesMap {
  3. public $initData = array();
  4. public $initProcesWithGroups = array();
  5. public $initProcesIds = array();
  6. public $stepToRecDeepLvl = array();// Recurce Deep Level => ID_PROCES
  7. public $stepToGroup = array();// recurse deep => [ID_PROCES => ?]
  8. public $usedGroups = array();// ID_GROUPS => group name
  9. public $gotoIds = array();
  10. public $procesData = array();
  11. public function __construct($idProcesInit) {
  12. $this->initData = $this->fetchProcesIdxForInit($idProcesInit);
  13. $gotoIdsTodo = array();
  14. foreach ($this->initData as $r) {
  15. $this->procesData[$r->ID_PROCES] = $r;
  16. $this->stepToRecDeepLvl[$r->ID_PROCES] = 0;
  17. $this->initProcesIds[$r->ID_PROCES] = true;
  18. $this->initProcesWithGroups[$r->idx_PROCES_WITH_GROUPS_ID] = true;
  19. if ($r->ID_GOTO_AND_RETURN > 0) {
  20. $gotoIdsTodo[$r->ID_GOTO_AND_RETURN][] = $r->ID_PROCES;
  21. }
  22. }
  23. if(V::get('DBG', '', $_REQUEST)){echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">TODO: $gotoIdsTodo = ';print_r($gotoIdsTodo);echo'</pre>';}
  24. $loopLimit = 10;
  25. for ($i = 1; $i <= $loopLimit && !empty($gotoIdsTodo); $i++) {
  26. foreach ($gotoIdsTodo as $gotoId => $fromIds) {
  27. foreach ($fromIds as $fromId) {
  28. $this->gotoIds[$gotoId][] = $fromId;
  29. }
  30. }
  31. $gotoData = $this->fetchProcesIdxForGotoIds($gotoIdsTodo);
  32. $gotoIdsTodo = array();
  33. if(V::get('DBG', '', $_REQUEST)){echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">TODO:loop('.$i.'): $gotoData = ';print_r($gotoData);echo'</pre>';}
  34. foreach ($gotoData as $r) {
  35. $this->procesData[$r->ID_PROCES] = $r;
  36. if (!array_key_exists($r->ID_PROCES, $this->stepToRecDeepLvl)) {
  37. $this->stepToRecDeepLvl[$r->ID_PROCES] = $i;
  38. } else {
  39. continue;// ? throw new Exception("Loop detected");
  40. }
  41. if ($r->ID_GOTO_AND_RETURN > 0) {
  42. $gotoIdsTodo[$r->ID_GOTO_AND_RETURN][] = $r->ID_PROCES;
  43. }
  44. }
  45. }
  46. $this->stepToGroup = $this->fetchGroupsByProcesIds(array_keys($this->stepToRecDeepLvl));
  47. foreach ($this->stepToGroup as $idProces => $idGroups) {
  48. foreach ($idGroups as $idGroup) {
  49. $this->usedGroups[$idGroup] = true;
  50. }
  51. }
  52. ?>
  53. <table class="table table-bordered">
  54. <thead>
  55. <tr>
  56. <th>Lp.</th>
  57. <th>idGroup</th>
  58. <?php foreach ($this->stepToRecDeepLvl as $idProces => $deepLvl) : ?>
  59. <th>
  60. <?php echo $idProces; ?>
  61. <br><i title="Proces init">{<?php echo $this->procesData[$idProces]->idx_PROCES_INIT_ID; ?>}</i>
  62. </th>
  63. <?php endforeach; ?>
  64. </tr>
  65. </thead>
  66. <tbody>
  67. <?php $i = 0; foreach ($this->usedGroups as $idGroup => $groupName) : ?>
  68. <tr>
  69. <td><?php echo $i; ?></td>
  70. <td><?php echo $idGroup; ?></td>
  71. <?php foreach ($this->stepToRecDeepLvl as $idProces => $deepLvl) : ?>
  72. <td><?php
  73. if (array_key_exists($idProces, $this->stepToGroup)) {
  74. if (in_array($idGroup, $this->stepToGroup[$idProces])) {
  75. echo '#';
  76. } else {
  77. echo '.';
  78. }
  79. } else {
  80. echo 'x';
  81. }
  82. ?></td>
  83. <?php endforeach; ?>
  84. </tr>
  85. <?php $i++; endforeach; ?>
  86. </tbody>
  87. </table>
  88. <?php
  89. if(V::get('DBG', '', $_REQUEST)){
  90. echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->initData = ';print_r($this->initData);echo'</pre>';
  91. echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->stepToGroup = ';print_r($this->stepToGroup);echo'</pre>';
  92. echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->stepToRecDeepLvl = ';print_r($this->stepToRecDeepLvl);echo'</pre>';
  93. echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->gotoIds = ';print_r($this->gotoIds);echo'</pre>';
  94. echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->initProcesIds = ';print_r($this->initProcesIds);echo'</pre>';
  95. echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->initProcesWithGroups = ';print_r($this->initProcesWithGroups);echo'</pre>';
  96. echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this = ';print_r($this);echo'</pre>';
  97. }
  98. $usedGroups = $this->getGroupIdsForProcesInit($idProcesInit);
  99. $usedGoto = $this->getGotoAnReturnIdsForProcesInit($idProcesInit);
  100. if(V::get('DBG', '', $_REQUEST)){
  101. echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$usedGroups = ';print_r($usedGroups);echo'</pre>';
  102. echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$usedGoto = ';print_r($usedGoto);echo'</pre>';
  103. }
  104. }
  105. public function fetchProcesIdxForInit($idProcesInit) {
  106. $db = DB::getDB();
  107. $data = array();
  108. $sql = "select i.*
  109. , IF(p.`IF_TRUE_GOTO_FLAG`='GOTO_AND_RETURN' and p.`IF_TRUE_GOTO`>0
  110. , p.`IF_TRUE_GOTO`
  111. , 0) as `ID_GOTO_AND_RETURN`
  112. from `CRM_PROCES_idx` i
  113. join `CRM_PROCES` p on(p.`ID`=i.`ID_PROCES`)
  114. where (i.`idx_PROCES_INIT_ID`='{$idProcesInit}'
  115. or i.`idx_PROCES_WITH_GROUPS_ID`='{$idProcesInit}')
  116. ";
  117. $res = $db->query($sql);
  118. while ($r = $db->fetch($res)) {
  119. $data[] = $r;
  120. }
  121. return $data;
  122. }
  123. public function fetchProcesIdxForGotoIds($gotoIdsTodo) {
  124. $db = DB::getDB();
  125. if (empty($gotoIdsTodo)) return;
  126. $sqlIds = array_keys($gotoIdsTodo);
  127. if (empty($sqlIds)) return;
  128. $sqlIds = implode(",", $sqlIds);
  129. $data = array();
  130. $sql = "select i.*
  131. , IF(p.`IF_TRUE_GOTO_FLAG`='GOTO_AND_RETURN' and p.`IF_TRUE_GOTO`>0
  132. , p.`IF_TRUE_GOTO`
  133. , 0) as `ID_GOTO_AND_RETURN`
  134. from `CRM_PROCES_idx` i
  135. join `CRM_PROCES` p on(p.`ID`=i.`ID_PROCES`)
  136. where (i.`idx_PROCES_INIT_ID` in({$sqlIds})
  137. or i.`idx_PROCES_WITH_GROUPS_ID` in({$sqlIds}))
  138. ";
  139. $res = $db->query($sql);
  140. while ($r = $db->fetch($res)) {
  141. $data[] = $r;
  142. }
  143. return $data;
  144. }
  145. public function fetchGroupsByProcesIds($procesIds) {
  146. $db = DB::getDB();
  147. if (empty($procesIds)) return;
  148. $sqlIds = implode(",", $procesIds);
  149. $data = array();
  150. $sql = "select gp.`ID_PROCES`, gp.`ID_GROUP`
  151. from `CRM_PROCES_idx_GROUP_to_PROCES` gp
  152. where gp.`ID_PROCES` in({$sqlIds})
  153. ";
  154. $res = $db->query($sql);
  155. while ($r = $db->fetch($res)) {
  156. $data[$r->ID_PROCES][] = $r->ID_GROUP;
  157. }
  158. return $data;
  159. }
  160. public function getGroupIdsForProcesInit($idProcesInit) {
  161. $usedGroups = array();
  162. $db = DB::getDB();
  163. $sql = "
  164. select gp.`ID_PROCES`, gp.`ID_GROUP`
  165. from (
  166. select i.`idx_PROCES_WITH_GROUPS_ID` as `ID_PROCES`
  167. from `CRM_PROCES_idx` i
  168. where i.`idx_PROCES_INIT_ID`='{$idProcesInit}'
  169. group by i.`idx_PROCES_WITH_GROUPS_ID`
  170. ) gids
  171. join `CRM_PROCES_idx_GROUP_to_PROCES` gp on (gp.`ID_PROCES`=gids.`ID_PROCES`)
  172. ";
  173. $res = $db->query($sql);
  174. while ($r = $db->fetch($res)) {
  175. $usedGroups[$r->ID_PROCES][] = $r->ID_GROUP;
  176. }
  177. return $usedGroups;
  178. }
  179. public function getGotoAnReturnIdsForProcesInit($idProcesInit) {
  180. $usedGoto = array();
  181. $db = DB::getDB();
  182. $sql = "
  183. select p.`ID`, p.`IF_TRUE_GOTO`
  184. from (
  185. select i.`ID_PROCES` as `ID_PROCES`
  186. from `CRM_PROCES_idx` i
  187. where i.`idx_PROCES_INIT_ID`='{$idProcesInit}'
  188. group by i.`ID_PROCES`
  189. ) pids
  190. join `CRM_PROCES` p on (p.`ID`=pids.`ID_PROCES`
  191. and p.`IF_TRUE_GOTO_FLAG`='GOTO_AND_RETURN'
  192. and p.`IF_TRUE_GOTO`>0
  193. )
  194. ";
  195. $res = $db->query($sql);
  196. while ($r = $db->fetch($res)) {
  197. $usedGoto[$r->IF_TRUE_GOTO][] = $r->ID;
  198. }
  199. return $usedGoto;
  200. }
  201. }