TreeList.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. *
  4. * DBG: url arg: '&DBG_TF=1'
  5. */
  6. class TreeList {
  7. private $_tbl;
  8. private $_tblParentField;
  9. private $_id;
  10. private $_listIds = array();
  11. private $_treeFlat = array();
  12. private $_listFlat = array();
  13. private $_data = array();
  14. public function __construct($tbl, $id, $parentField = 'P_ID') {
  15. $this->_tbl = $tbl;
  16. $this->_id = $id;
  17. $this->_tblParentField = $parentField;
  18. }
  19. public function fetchTreeFlat() {
  20. if (!empty($this->_treeFlat)) {
  21. return;
  22. }
  23. $this->_treeFlat = array();
  24. $db = DB::getDB();
  25. $sql = "select t.`ID`, t.`{$this->_tblParentField}` as PARENT_ID , t.`TYPE`
  26. from `{$this->_tbl}` as t
  27. where t.`A_STATUS` in('WAITING','NORMAL')
  28. order by t.`{$this->_tblParentField}`, t.`SORT_PRIO`, t.`ID`
  29. ";
  30. if(V::get('DBG_TF', '', $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  31. $res = $db->query($sql);
  32. while ($r = $db->fetch($res)) {
  33. $this->_treeFlat[$r->PARENT_ID] []= $r->ID;
  34. $this->_treeType[$r->ID]= $r->TYPE;
  35. }
  36. if(V::get('DBG_TF', '', $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">treeFlatAll (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_treeFlat);echo'</pre>';}
  37. }
  38. public function generateListFlat() {
  39. if (!empty($this->_listFlat)) {
  40. return $this->_listFlat;
  41. }
  42. /**
  43. 1 2768 Założenie konta SIP z numerem na platformie AMS [proces urucham
  44. 2 2770 Zaloguj się na platformę AMS Jeżeli posiadasz konto na platformie
  45. 2.1 2771 Dodanie nowego klienta Proces mówiący o tym jak dodać nowego k
  46. 2.1.1 2772 Wprowadź dane klienta Wprowadź wszystkie dane klienta: imię, nazwi
  47. 2.2 2774 Dodanie numeru telefonu dla istniejącego klienta Proces dodawania nume
  48. 2.2.1 2773 Wprowadź dane dotyczące nowego numeru telefonu Wypełnij dane z tabeli:
  49. 2.2.2 2828 Edytowanie konta klienta Dodawanie dostępu do panelu klienckiego (miejsca
  50. *
  51. * 1 [2768] deep:1
  52. * 2 [2770] deep:1
  53. * 3 [2771] deep:2
  54. *
  55. */
  56. $this->_listFlat = array();// array('ID'=>$id_proces, 'LIST_NRS' => array(1));
  57. $lastUserIds = array();
  58. $lastUserIds[] = (object)array('ID'=>$this->_id, 'LIST_NRS' => array(1), 'deep'=>1,'LIST_NRS_NUM' => array(0));
  59. for ($i = 0; $i < 1000; $i++) {
  60. if (empty($lastUserIds)) {
  61. break;
  62. }
  63. $vItem = array_shift($lastUserIds);
  64. $this->_listFlat[$vItem->ID] = $vItem;
  65. // next steps
  66. if (array_key_exists($vItem->ID, $this->_treeFlat)) {
  67. if (count($this->_treeFlat[$vItem->ID]) > 1) {
  68. $subNr = 1;
  69. $subNr_NUM = 1;
  70. foreach ($this->_treeFlat[$vItem->ID] as $vSubProcesId) {
  71. $vSubItem = (object)array('ID'=>$vSubProcesId, 'deep'=>$vItem->deep + 1);
  72. $vSubItem->LIST_NRS = $vItem->LIST_NRS;
  73. $vSubItem->LIST_NRS_NUM = $vItem->LIST_NRS_NUM;
  74. if(in_array($this->_treeType[$vSubProcesId],array('TEXT_BREAK','PROCES_INIT')) ) { //@2014-01 added by sqix : TEXT_BREAK ma nie dawac osobnego punktu
  75. // $vSubItem->LIST_NRS_NUM[] = $subNr_NUM;
  76. // $vSubItem->LIST_NRS_NUM[] = '';
  77. } else {
  78. $vSubItem->LIST_NRS_NUM[] = $subNr_NUM++;
  79. $vSubItem->LIST_NRS_NUM[] = '';
  80. }
  81. $vSubItem->LIST_NRS[] = $subNr++;
  82. $vSubItem->LIST_NRS[] = '';
  83. $vSubItem->p_ID = $vItem->ID;
  84. $lastUserIds[] = $vSubItem;
  85. }
  86. }
  87. else {
  88. foreach ($this->_treeFlat[$vItem->ID] as $vSubProcesId) {
  89. $vSubItem = (object)array('ID'=>$vSubProcesId, 'deep'=>$vItem->deep);
  90. $vSubItem->LIST_NRS = $vItem->LIST_NRS;
  91. $vSubItem->LIST_NRS_NUM = $vItem->LIST_NRS_NUM;
  92. $subNr = array_pop($vSubItem->LIST_NRS);
  93. $subNr_NUM = array_pop($vSubItem->LIST_NRS_NUM);
  94. $vSubItem->LIST_NRS[] = ++$subNr;
  95. if(in_array($this->_treeType[$vSubProcesId],array('TEXT_BREAK','PROCES_INIT')) ) { //@2014-01 added by sqix : TEXT_BREAK ma nie dawac osobnego punktu
  96. $vSubItem->LIST_NRS_NUM[] = $subNr_NUM++;
  97. }
  98. $vSubItem->LIST_NRS_NUM[] = ++$subNr_NUM;
  99. $lastUserIds[] = $vSubItem;
  100. }
  101. }
  102. }
  103. }
  104. if(V::get('DBG_TF', '', $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">listFlat (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_listFlat);echo'</pre>';}
  105. foreach ($this->_listFlat as $kID => $vItem) {
  106. $listNrOut = array();
  107. foreach ($vItem->LIST_NRS as $nr) {
  108. if ($nr) {
  109. $listNrOut[] = $nr;
  110. }
  111. }
  112. $this->_listFlat[$kID]->listNr = implode('.', $listNrOut);
  113. }
  114. foreach ($this->_listFlat as $kID => $vItem) {
  115. $listNrOut = array();
  116. foreach ($vItem->LIST_NRS_NUM as $nr) {
  117. if ($nr) {
  118. $listNrOut[] = $nr;
  119. }
  120. }
  121. $this->_listFlat[$kID]->listNrNUM = implode('.', $listNrOut);
  122. }
  123. $this->_listIds = array_keys($this->_listFlat);
  124. uasort($this->_listFlat, array($this, 'sortListCallback'));
  125. return $this->_listFlat;
  126. }
  127. public function getListIds() {
  128. return $this->_listIds;
  129. }
  130. public function fetchData() {
  131. $sqlIds = $this->_listIds;
  132. if (empty($sqlIds)) {
  133. return $this->_data;
  134. }
  135. $sqlGotoIds = array();
  136. $db = DB::getDB();
  137. $sql = "select t.*
  138. from `{$this->_tbl}` as t
  139. where t.`A_STATUS` in('WAITING','NORMAL')
  140. and t.`ID` in(" . implode(",", $sqlIds) . ")
  141. ";
  142. if(V::get('DBG_TF', '', $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  143. $res = $db->query($sql);
  144. while ($r = $db->fetch($res)) {
  145. if ($r->IF_TRUE_GOTO > 0) {
  146. $sqlGotoIds[] = $r->IF_TRUE_GOTO;
  147. }
  148. $this->_data[$r->ID] = $r;
  149. }
  150. if (!empty($sqlGotoIds)) {
  151. $sql = "select t.*
  152. from `{$this->_tbl}` as t
  153. where t.`A_STATUS` in('WAITING','NORMAL')
  154. and t.`ID` in(" . implode(",", $sqlGotoIds) . ")
  155. ";
  156. if(V::get('DBG_TF', '', $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sqlGoto (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  157. $res = $db->query($sql);
  158. while ($r = $db->fetch($res)) {
  159. $this->_data[$r->ID] = $r;
  160. }
  161. }
  162. if(V::get('DBG_TF', '', $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">data (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->_data);echo'</pre>';}
  163. return $this->_data;
  164. }
  165. public function getData($id) {
  166. return (array_key_exists($id, $this->_data))? $this->_data[$id] : null;
  167. }
  168. public function sortListCallback($a, $b) {
  169. $cntA = count($a->LIST_NRS);
  170. $cntB = count($b->LIST_NRS);
  171. $cnt = min($cntA, $cntB);
  172. for ($i = 0; $i < $cnt; $i++) {
  173. if ($a->LIST_NRS[$i] > $b->LIST_NRS[$i]) {
  174. return 1;
  175. } else if ($a->LIST_NRS[$i] < $b->LIST_NRS[$i]) {
  176. return -1;
  177. }
  178. }
  179. return ($cntA < $cntB)? -1 : 1;
  180. }
  181. }