CrmProcesMap.php 7.1 KB

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