TreeListView.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. *
  4. * DBG: url arg: '&DBG_TF=1'
  5. */
  6. class TreeListView {
  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
  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. }
  35. 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>';}
  36. }
  37. public function generateListFlat() {
  38. if (!empty($this->_listFlat)) {
  39. return $this->_listFlat;
  40. }
  41. /**
  42. 1 2768 Założenie konta SIP z numerem na platformie AMS [proces urucham
  43. 2 2770 Zaloguj się na platformę AMS Jeżeli posiadasz konto na platformie
  44. 2.1 2771 Dodanie nowego klienta Proces mówiący o tym jak dodać nowego k
  45. 2.1.1 2772 Wprowadź dane klienta Wprowadź wszystkie dane klienta: imię, nazwi
  46. 2.2 2774 Dodanie numeru telefonu dla istniejącego klienta Proces dodawania nume
  47. 2.2.1 2773 Wprowadź dane dotyczące nowego numeru telefonu Wypełnij dane z tabeli:
  48. 2.2.2 2828 Edytowanie konta klienta Dodawanie dostępu do panelu klienckiego (miejsca
  49. *
  50. * 1 [2768] deep:1
  51. * 2 [2770] deep:1
  52. * 3 [2771] deep:2
  53. *
  54. */
  55. $this->_listFlat = array();// array('ID'=>$id_proces, 'LIST_NRS' => array(1));
  56. $lastUserIds = array();
  57. $lastUserIds[] = (object)array('ID'=>$this->_id, 'LIST_NRS' => array(1), 'deep'=>1);
  58. for ($i = 0; $i < 1000; $i++) {
  59. if (empty($lastUserIds)) {
  60. break;
  61. }
  62. $vItem = array_shift($lastUserIds);
  63. $this->_listFlat[$vItem->ID] = $vItem;
  64. // next steps
  65. if (array_key_exists($vItem->ID, $this->_treeFlat)) {
  66. if (count($this->_treeFlat[$vItem->ID]) > 1) {
  67. $subNr = 1;
  68. foreach ($this->_treeFlat[$vItem->ID] as $vSubProcesId) {
  69. $vSubItem = (object)array('ID'=>$vSubProcesId, 'deep'=>$vItem->deep + 1);
  70. $vSubItem->LIST_NRS = $vItem->LIST_NRS;
  71. $vSubItem->LIST_NRS[] = $subNr++;
  72. $vSubItem->LIST_NRS[] = '';
  73. $vSubItem->p_ID = $vItem->ID;
  74. $lastUserIds[] = $vSubItem;
  75. }
  76. }
  77. else {
  78. foreach ($this->_treeFlat[$vItem->ID] as $vSubProcesId) {
  79. $vSubItem = (object)array('ID'=>$vSubProcesId, 'deep'=>$vItem->deep);
  80. $vSubItem->LIST_NRS = $vItem->LIST_NRS;
  81. $subNr = array_pop($vSubItem->LIST_NRS);
  82. $vSubItem->LIST_NRS[] = ++$subNr;
  83. $lastUserIds[] = $vSubItem;
  84. }
  85. }
  86. }
  87. }
  88. 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>';}
  89. foreach ($this->_listFlat as $kID => $vItem) {
  90. $listNrOut = array();
  91. foreach ($vItem->LIST_NRS as $nr) {
  92. if ($nr) {
  93. $listNrOut[] = $nr;
  94. }
  95. }
  96. $this->_listFlat[$kID]->listNr = implode('.', $listNrOut);
  97. }
  98. $this->_listIds = array_keys($this->_listFlat);
  99. uasort($this->_listFlat, array($this, 'sortListCallback'));
  100. return $this->_listFlat;
  101. }
  102. public function getListIds() {
  103. return $this->_listIds;
  104. }
  105. public function fetchData() {
  106. $sqlIds = $this->_listIds;
  107. if (empty($sqlIds)) {
  108. return $this->_data;
  109. }
  110. $sqlGotoIds = array();
  111. $db = DB::getDB();
  112. $sql = "select t.*
  113. from `{$this->_tbl}` as t
  114. where t.`A_STATUS` in('WAITING','NORMAL')
  115. and t.`ID` in(" . implode(",", $sqlIds) . ")
  116. ";
  117. 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>';}
  118. $res = $db->query($sql);
  119. while ($r = $db->fetch($res)) {
  120. if ($r->IF_TRUE_GOTO > 0) {
  121. $sqlGotoIds[] = $r->IF_TRUE_GOTO;
  122. }
  123. $this->_data[$r->ID] = $r;
  124. }
  125. if (!empty($sqlGotoIds)) {
  126. $sql = "select t.*
  127. from `{$this->_tbl}` as t
  128. where t.`A_STATUS` in('WAITING','NORMAL')
  129. and t.`ID` in(" . implode(",", $sqlGotoIds) . ")
  130. ";
  131. 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>';}
  132. $res = $db->query($sql);
  133. while ($r = $db->fetch($res)) {
  134. $this->_data[$r->ID] = $r;
  135. }
  136. }
  137. 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>';}
  138. return $this->_data;
  139. }
  140. public function getData($id) {
  141. return (array_key_exists($id, $this->_data))? $this->_data[$id] : null;
  142. }
  143. public function sortListCallback($a, $b) {
  144. $cntA = count($a->LIST_NRS);
  145. $cntB = count($b->LIST_NRS);
  146. $cnt = min($cntA, $cntB);
  147. for ($i = 0; $i < $cnt; $i++) {
  148. if ($a->LIST_NRS[$i] > $b->LIST_NRS[$i]) {
  149. return 1;
  150. } else if ($a->LIST_NRS[$i] < $b->LIST_NRS[$i]) {
  151. return -1;
  152. }
  153. }
  154. return ($cntA < $cntB)? -1 : 1;
  155. }
  156. }