PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/zabbix-2.0.1/frontends/php/include/classes/tree/CTree.php

#
PHP | 240 lines | 178 code | 33 blank | 29 comment | 19 complexity | 52769afa9d9e2a925b39359d94adaf04 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. <?php
  2. /*
  3. ** Zabbix
  4. ** Copyright (C) 2000-2011 Zabbix SIA
  5. **
  6. ** This program is free software; you can redistribute it and/or modify
  7. ** it under the terms of the GNU General Public License as published by
  8. ** the Free Software Foundation; either version 2 of the License, or
  9. ** (at your option) any later version.
  10. **
  11. ** This program is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ** GNU General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU General Public License
  17. ** along with this program; if not, write to the Free Software
  18. ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. **/
  20. ?>
  21. <?php
  22. /**
  23. * A class for rendering HTML trees.
  24. */
  25. class CTree {
  26. public $tree;
  27. public $fields;
  28. public $treename;
  29. private $size;
  30. private $maxlevel;
  31. public function __construct($treename, $value = array(), $fields = array()) {
  32. $this->maxlevel = 0;
  33. $this->tree = $value;
  34. $this->fields = $fields;
  35. $this->treename = $treename;
  36. $this->size = count($value);
  37. unset($value, $fields);
  38. if (!$this->checkTree()) {
  39. $this->destroy();
  40. return false;
  41. }
  42. else {
  43. $this->countDepth();
  44. }
  45. }
  46. public function getTree() {
  47. return $this->tree;
  48. }
  49. public function getHTML() {
  50. $html[] = $this->createJS();
  51. $html[] = $this->simpleHTML();
  52. return $html;
  53. }
  54. private function makeHeaders() {
  55. $c = 0;
  56. $tr = new CRow($this->fields['caption'], 'header');
  57. unset($this->fields['caption']);
  58. foreach ($this->fields as $id => $caption) {
  59. $tr->addItem($caption);
  60. $fields[$c] = $id;
  61. $c++;
  62. }
  63. $this->fields = $fields;
  64. return $tr;
  65. }
  66. private function simpleHTML() {
  67. $table = new CTableInfo();
  68. $table->addRow($this->makeHeaders());
  69. foreach ($this->tree as $id => $rows) {
  70. $table->addRow($this->makeRow($id));
  71. }
  72. return $table;
  73. }
  74. private function makeRow($id) {
  75. $table = new CTable();
  76. $tr = $this->makeSImgStr($id);
  77. $tr->addItem($this->tree[$id]['caption']);
  78. $table->addRow($tr);
  79. $tr = new CRow();
  80. $tr->addItem($table);
  81. $tr->setAttribute('id', 'id_'.$id);
  82. $tr->setAttribute('style', $this->tree[$id]['parentid'] != '0' ? 'display: none;' : '');
  83. foreach ($this->fields as $value) {
  84. $tr->addItem($this->makeCol($id, $value));
  85. }
  86. return $tr;
  87. }
  88. /**
  89. * Returns a column object for the given row and field.
  90. *
  91. * @param $rowId
  92. * @param $colName
  93. *
  94. * @return CCol
  95. */
  96. protected function makeCol($rowId, $colName) {
  97. return new CCol($this->tree[$rowId][$colName]);
  98. }
  99. private function makeSImgStr($id) {
  100. $tr = new CRow();
  101. $count = isset($this->tree[$id]['nodeimg']) ? zbx_strlen($this->tree[$id]['nodeimg']) : 0;
  102. for ($i = 0; $i < $count; $i++) {
  103. $td = new CCol();
  104. switch ($this->tree[$id]['nodeimg'][$i]) {
  105. case 'O':
  106. $td->setAttribute('style', 'width: 22px');
  107. $img = new CImg('images/general/tree/zero.gif', 'o', '22', '14');
  108. break;
  109. case 'I':
  110. $td->setAttribute('style', 'width:22px; background-image:url(images/general/tree/pointc.gif);');
  111. $img = new CImg('images/general/tree/zero.gif', 'i', '22', '14');
  112. break;
  113. case 'L':
  114. $td->setAttribute('valign', 'top');
  115. $div = new CTag('div', 'yes');
  116. $div->setAttribute('style', 'height: 10px; width:22px; margin-left: -1px; background-image:url(images/general/tree/pointc.gif);');
  117. if ($this->tree[$id]['nodetype'] == 2) {
  118. $img = new CImg('images/general/tree/plus.gif', 'y', '22', '14');
  119. $img->setAttribute('onclick', $this->treename.'.closeSNodeX("'.$id.'",this);');
  120. $img->setAttribute('id', 'idi_'.$id);
  121. $img->setAttribute('class', 'pointer');
  122. }
  123. else {
  124. $img = new CImg('images/general/tree/pointl.gif', 'y', '22', '14');
  125. }
  126. $div->addItem($img);
  127. $img = $div;
  128. break;
  129. case 'T':
  130. $td->setAttribute('valign', 'top');
  131. if ($this->tree[$id]['nodetype'] == 2) {
  132. $td->setAttribute('style', 'width:22px; background-image:url(images/general/tree/pointc.gif);');
  133. $img = new CImg('images/general/tree/plus.gif', 't', '22', '14');
  134. $img->setAttribute('onclick', $this->treename.'.closeSNodeX("'.$id.'",this);');
  135. $img->setAttribute('id', 'idi_'.$id);
  136. $img->setAttribute('class', 'pointer');
  137. }
  138. else {
  139. $td->setAttribute('style', 'width:22px; background-image:url(images/general/tree/pointc.gif);');
  140. $img = new CImg('images/general/tree/pointl.gif', 't', '22', '14');
  141. }
  142. break;
  143. }
  144. $td->addItem($img);
  145. $tr->addItem($td);
  146. }
  147. return $tr;
  148. }
  149. private function countDepth() {
  150. foreach ($this->tree as $id => $rows) {
  151. if ($rows['id'] == '0') {
  152. continue;
  153. }
  154. $parentid = $this->tree[$id]['parentid'];
  155. $this->tree[$id]['nodeimg'] = $this->getImg($id, isset($this->tree[$parentid]['nodeimg']) ? $this->tree[$parentid]['nodeimg'] : '');
  156. $this->tree[$parentid]['nodetype'] = 2;
  157. $this->tree[$id]['Level'] = isset($this->tree[$parentid]['Level']) ? $this->tree[$parentid]['Level'] + 1 : 1;
  158. $this->maxlevel>$this->tree[$id]['Level'] ? '' : $this->maxlevel = $this->tree[$id]['Level'];
  159. }
  160. }
  161. public function createJS() {
  162. $js = '<script src="js/class.ctree.js" type="text/javascript"></script>'."\n".
  163. '<script type="text/javascript"> var '.$this->treename.'_tree = [];';
  164. foreach ($this->tree as $id => $rows) {
  165. $parentid = $rows['parentid'];
  166. $this->tree[$parentid]['nodelist'] .= $id.',';
  167. }
  168. foreach ($this->tree as $id => $rows) {
  169. if ($rows['nodetype'] == '2') {
  170. $js .= $this->treename.'_tree[\''.$id.'\'] = { status: \'close\', nodelist : \''.$rows['nodelist'].'\', parentid : \''.$rows['parentid'].'\'};';
  171. $js .= "\n";
  172. }
  173. }
  174. $js.= 'var '.$this->treename.' = null';
  175. $js.= '</script>'."\n";
  176. zbx_add_post_js($this->treename.' = new CTree("tree_'.CWebUser::$data['alias'].'_'.$this->treename.'", '.$this->treename.'_tree);');
  177. return new CJSscript($js);
  178. }
  179. private function getImg($id, $img) {
  180. $img = str_replace('T', 'I', $img);
  181. $img = str_replace('L', 'O', $img);
  182. $ch = 'L';
  183. $childs = $this->tree[$this->tree[$id]['parentid']]['childnodes'];
  184. $childs_last = count($this->tree[$this->tree[$id]['parentid']]['childnodes']) - 1;
  185. if (isset($childs[$childs_last]) && $childs[$childs_last] != $id) {
  186. $ch = 'T';
  187. }
  188. $img .= $ch;
  189. return $img;
  190. }
  191. private function checkTree() {
  192. if (!is_array($this->tree)) {
  193. return false;
  194. }
  195. foreach ($this->tree as $id => $cell) {
  196. $this->tree[$id]['nodetype'] = 0;
  197. $parentid = $cell['parentid'];
  198. $this->tree[$parentid]['childnodes'][] = $id;
  199. $this->tree[$id]['nodelist'] = '';
  200. }
  201. return true;
  202. }
  203. private function destroy() {
  204. unset($this->tree);
  205. }
  206. }
  207. ?>