| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- class CrmProcesMap {
- public $initData = array();
- public $initProcesWithGroups = array();
- public $initProcesIds = array();
- public $stepToRecDeepLvl = array();// Recurce Deep Level => ID_PROCES
- public $stepToGroup = array();// recurse deep => [ID_PROCES => ?]
- public $usedGroups = array();// ID_GROUPS => group name
- public $gotoIds = array();
- public $procesData = array();
- public function __construct($idProcesInit) {
- $this->initData = $this->fetchProcesIdxForInit($idProcesInit);
- $gotoIdsTodo = array();
- foreach ($this->initData as $r) {
- $this->procesData[$r->ID_PROCES] = $r;
- $this->stepToRecDeepLvl[$r->ID_PROCES] = 0;
- $this->initProcesIds[$r->ID_PROCES] = true;
- $this->initProcesWithGroups[$r->idx_PROCES_WITH_GROUPS_ID] = true;
- if ($r->ID_GOTO_AND_RETURN > 0) {
- $gotoIdsTodo[$r->ID_GOTO_AND_RETURN][] = $r->ID_PROCES;
- }
- }
- 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>';}
- $loopLimit = 10;
- for ($i = 1; $i <= $loopLimit && !empty($gotoIdsTodo); $i++) {
- foreach ($gotoIdsTodo as $gotoId => $fromIds) {
- foreach ($fromIds as $fromId) {
- $this->gotoIds[$gotoId][] = $fromId;
- }
- }
- $gotoData = $this->fetchProcesIdxForGotoIds($gotoIdsTodo);
- $gotoIdsTodo = array();
- 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>';}
- foreach ($gotoData as $r) {
- $this->procesData[$r->ID_PROCES] = $r;
- if (!array_key_exists($r->ID_PROCES, $this->stepToRecDeepLvl)) {
- $this->stepToRecDeepLvl[$r->ID_PROCES] = $i;
- } else {
- continue;// ? throw new Exception("Loop detected");
- }
- if ($r->ID_GOTO_AND_RETURN > 0) {
- $gotoIdsTodo[$r->ID_GOTO_AND_RETURN][] = $r->ID_PROCES;
- }
- }
- }
- $this->stepToGroup = $this->fetchGroupsByProcesIds(array_keys($this->stepToRecDeepLvl));
- foreach ($this->stepToGroup as $idProces => $idGroups) {
- foreach ($idGroups as $idGroup) {
- $this->usedGroups[$idGroup] = true;
- }
- }
- ?>
- <table class="table table-bordered">
- <thead>
- <tr>
- <th>Lp.</th>
- <th>idGroup</th>
- <?php foreach ($this->stepToRecDeepLvl as $idProces => $deepLvl) : ?>
- <th>
- <?php echo $idProces; ?>
- <br><i title="Proces init">{<?php echo $this->procesData[$idProces]->idx_PROCES_INIT_ID; ?>}</i>
- </th>
- <?php endforeach; ?>
- </tr>
- </thead>
- <tbody>
- <?php $i = 0; foreach ($this->usedGroups as $idGroup => $groupName) : ?>
- <tr>
- <td><?php echo $i; ?></td>
- <td><?php echo $idGroup; ?></td>
- <?php foreach ($this->stepToRecDeepLvl as $idProces => $deepLvl) : ?>
- <td><?php
- if (array_key_exists($idProces, $this->stepToGroup)) {
- if (in_array($idGroup, $this->stepToGroup[$idProces])) {
- echo '#';
- } else {
- echo '.';
- }
- } else {
- echo 'x';
- }
- ?></td>
- <?php endforeach; ?>
- </tr>
- <?php $i++; endforeach; ?>
- </tbody>
- </table>
- <?php
- if(V::get('DBG', '', $_REQUEST)){
- echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->initData = ';print_r($this->initData);echo'</pre>';
- echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->stepToGroup = ';print_r($this->stepToGroup);echo'</pre>';
- echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->stepToRecDeepLvl = ';print_r($this->stepToRecDeepLvl);echo'</pre>';
- echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->gotoIds = ';print_r($this->gotoIds);echo'</pre>';
- echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->initProcesIds = ';print_r($this->initProcesIds);echo'</pre>';
- echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this->initProcesWithGroups = ';print_r($this->initProcesWithGroups);echo'</pre>';
- echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$this = ';print_r($this);echo'</pre>';
- }
- $usedGroups = $this->getGroupIdsForProcesInit($idProcesInit);
- $usedGoto = $this->getGotoAnReturnIdsForProcesInit($idProcesInit);
- if(V::get('DBG', '', $_REQUEST)){
- echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$usedGroups = ';print_r($usedGroups);echo'</pre>';
- echo '<pre style="width:600px;border:1px solid red;max-height:300px;overflow:auto">$usedGoto = ';print_r($usedGoto);echo'</pre>';
- }
- }
- public function fetchProcesIdxForInit($idProcesInit) {
- $db = DB::getDB();
- $data = array();
- $sql = "select i.*
- , IF(p.`IF_TRUE_GOTO_FLAG`='GOTO_AND_RETURN' and p.`IF_TRUE_GOTO`>0
- , p.`IF_TRUE_GOTO`
- , 0) as `ID_GOTO_AND_RETURN`
- from `CRM_PROCES_idx` i
- join `CRM_PROCES` p on(p.`ID`=i.`ID_PROCES`)
- where (i.`idx_PROCES_INIT_ID`='{$idProcesInit}'
- or i.`idx_PROCES_WITH_GROUPS_ID`='{$idProcesInit}')
- ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $data[] = $r;
- }
- return $data;
- }
- public function fetchProcesIdxForGotoIds($gotoIdsTodo) {
- $db = DB::getDB();
- if (empty($gotoIdsTodo)) return;
- $sqlIds = array_keys($gotoIdsTodo);
- if (empty($sqlIds)) return;
- $sqlIds = implode(",", $sqlIds);
- $data = array();
- $sql = "select i.*
- , IF(p.`IF_TRUE_GOTO_FLAG`='GOTO_AND_RETURN' and p.`IF_TRUE_GOTO`>0
- , p.`IF_TRUE_GOTO`
- , 0) as `ID_GOTO_AND_RETURN`
- from `CRM_PROCES_idx` i
- join `CRM_PROCES` p on(p.`ID`=i.`ID_PROCES`)
- where (i.`idx_PROCES_INIT_ID` in({$sqlIds})
- or i.`idx_PROCES_WITH_GROUPS_ID` in({$sqlIds}))
- ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $data[] = $r;
- }
- return $data;
- }
- public function fetchGroupsByProcesIds($procesIds) {
- $db = DB::getDB();
- if (empty($procesIds)) return;
- $sqlIds = implode(",", $procesIds);
- $data = array();
- $sql = "select gp.`ID_PROCES`, gp.`ID_GROUP`
- from `CRM_PROCES_idx_GROUP_to_PROCES` gp
- where gp.`ID_PROCES` in({$sqlIds})
- ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $data[$r->ID_PROCES][] = $r->ID_GROUP;
- }
- return $data;
- }
- public function getGroupIdsForProcesInit($idProcesInit) {
- $usedGroups = array();
- $db = DB::getDB();
- $sql = "
- select gp.`ID_PROCES`, gp.`ID_GROUP`
- from (
- select i.`idx_PROCES_WITH_GROUPS_ID` as `ID_PROCES`
- from `CRM_PROCES_idx` i
- where i.`idx_PROCES_INIT_ID`='{$idProcesInit}'
- group by i.`idx_PROCES_WITH_GROUPS_ID`
- ) gids
- join `CRM_PROCES_idx_GROUP_to_PROCES` gp on (gp.`ID_PROCES`=gids.`ID_PROCES`)
- ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $usedGroups[$r->ID_PROCES][] = $r->ID_GROUP;
- }
- return $usedGroups;
- }
- public function getGotoAnReturnIdsForProcesInit($idProcesInit) {
- $usedGoto = array();
- $db = DB::getDB();
- $sql = "
- select p.`ID`, p.`IF_TRUE_GOTO`
- from (
- select i.`ID_PROCES` as `ID_PROCES`
- from `CRM_PROCES_idx` i
- where i.`idx_PROCES_INIT_ID`='{$idProcesInit}'
- group by i.`ID_PROCES`
- ) pids
- join `CRM_PROCES` p on (p.`ID`=pids.`ID_PROCES`
- and p.`IF_TRUE_GOTO_FLAG`='GOTO_AND_RETURN'
- and p.`IF_TRUE_GOTO`>0
- )
- ";
- $res = $db->query($sql);
- while ($r = $db->fetch($res)) {
- $usedGoto[$r->IF_TRUE_GOTO][] = $r->ID;
- }
- return $usedGoto;
- }
- }
|