/lib/page/SpotPage_catsjson.php

https://github.com/NZBtje/spotweb-1 · PHP · 161 lines · 108 code · 26 blank · 27 comment · 42 complexity · c566b00076d871ef942b3e102c57ed3a MD5 · raw file

  1. <?php
  2. class SpotPage_catsjson extends SpotPage_Abs {
  3. private $_params;
  4. function __construct(SpotDb $db, SpotSettings $settings, $currentSession, $params) {
  5. parent::__construct($db, $settings, $currentSession);
  6. # stuur een expires header zodat dit niet gecached is, hierin staat
  7. # state van de boom
  8. $this->sendExpireHeaders(true);
  9. $this->sendContentTypeHeader();
  10. $this->_params = $params;
  11. } # ctor
  12. /*
  13. * render a page
  14. */
  15. function render() {
  16. $this->categoriesToJson();
  17. } # render
  18. /*
  19. * Geeft JSON terug interpreteerbaar voor DynaTree om de categorylist als boom
  20. * te kunnen weergeven
  21. */
  22. function categoriesToJson() {
  23. /* First parse the search string so we know which items to select and which not */
  24. $spotUserSystem = new SpotUserSystem($this->_db, $this->_settings);
  25. $spotsOverview = new SpotsOverview($this->_db, $this->_settings);
  26. $parsedSearch = $spotsOverview->filterToQuery($this->_params['search'],
  27. array(),
  28. $this->_currentSession,
  29. $spotUserSystem->getIndexFilter($this->_currentSession['user']['userid']));
  30. if ($this->_params['disallowstrongnot']) {
  31. $parsedSearch['strongNotList'] = '';
  32. } # if
  33. $compressedCatList = ',' . $spotsOverview->compressCategorySelection($parsedSearch['categoryList'], $parsedSearch['strongNotList']);
  34. //error_log($this->_params['search']['tree']);
  35. //var_dump($parsedSearch);
  36. //var_dump($compressedCatList);
  37. //die();
  38. echo "[";
  39. $hcatList = array();
  40. foreach(SpotCategories::$_head_categories as $hcat_key => $hcat_val) {
  41. # The uer can opt to only show a specific category, if so, skip all others
  42. if (($hcat_key != $this->_params['category']) && ($this->_params['category'] != '*')) {
  43. continue;
  44. } # if
  45. # If the user choose to show only one category, we dont want the category item itself
  46. if ($this->_params['category'] == '*') {
  47. $hcatTmp = '{"title": "' . $hcat_val . '", "isFolder": true, "key": "cat' . $hcat_key . '", "children": [' ;
  48. } # if
  49. $typeCatDesc = array();
  50. if (isset(SpotCategories::$_categories[$hcat_key]['z'])) {
  51. foreach(SpotCategories::$_categories[$hcat_key]['z'] as $type_key => $type_value) {
  52. if (($type_key !== 'z') && (($this->_params['subcatz'] == $type_key) || ($this->_params['subcatz'] == '*'))) {
  53. # Now determine wether we need to enable the checkbox
  54. $isSelected = strpos($compressedCatList, ',cat' . $hcat_key . '_z' . $type_key . ',') !== false ? "true" : "false";
  55. # Is this strongnot?
  56. $isStrongNot = strpos($compressedCatList, ',~cat' . $hcat_key . '_z' . $type_key . ',') !== false ? true : false;
  57. if ($isStrongNot) {
  58. $isStrongNot = '"strongnot": true, "addClass": "strongnotnode", ';
  59. $isSelected = 'true';
  60. } else {
  61. $isStrongNot = '';
  62. } # if
  63. # If the user choose to show only one categortype, we dont want the categorytype item itself
  64. if ($this->_params['subcatz'] == '*') {
  65. $typeCatTmp = '{"title": "' . $type_value . '", "isFolder": true, ' . $isStrongNot . ' "select": ' . $isSelected . ', "hideCheckbox": false, "key": "cat' . $hcat_key . '_z' . $type_key . '", "unselectable": false, "children": [';
  66. } # if
  67. } # if
  68. $subcatDesc = array();
  69. foreach(SpotCategories::$_subcat_descriptions[$hcat_key] as $sclist_key => $sclist_desc) {
  70. if (($sclist_key !== 'z') && (($this->_params['subcatz'] == $type_key) || ($this->_params['subcatz'] == '*'))) {
  71. # We inherit the strongnode from our parent
  72. $isStrongNot = strpos($compressedCatList, ',~cat' . $hcat_key . '_z' . $type_key . ',') !== false ? true : false;
  73. if ($isStrongNot) {
  74. $isStrongNot = '"strongnot": true, "addClass": "strongnotnode", ';
  75. $isSelected = 'true';
  76. } else {
  77. $isStrongNot = '';
  78. } # if
  79. $subcatTmp = '{"title": "' . $sclist_desc . '", "isFolder": true, ' . $isStrongNot . ' "hideCheckbox": true, "key": "cat' . $hcat_key . '_z' . $type_key . '_' . $sclist_key . '", "unselectable": false, "children": [';
  80. # echo ".." . $sclist_desc . " <br>";
  81. $catList = array();
  82. foreach(SpotCategories::$_categories[$hcat_key][$sclist_key] as $key => $valTmp) {
  83. if (in_array('z' . $type_key, $valTmp[1])) {
  84. $val = $valTmp[0];
  85. if ((strlen($val) != 0) && (strlen($key) != 0)) {
  86. # Now determine wether we need to enable the checkbox
  87. $isSelected = strpos($compressedCatList, ',cat' . $hcat_key . '_z' . $type_key . '_' . $sclist_key.$key . ',') !== false ? true : false;
  88. $parentSelected = strpos($compressedCatList, ',cat' . $hcat_key . '_z' . $type_key .',') !== false ? true : false;
  89. $isSelected = ($isSelected || $parentSelected) ? 'true' : 'false';
  90. /*
  91. * Is this strongnot?
  92. */
  93. $isStrongNot = strpos($compressedCatList, ',~cat' . $hcat_key . '_z' . $type_key . ',') !== false ? true : false;
  94. if (!$isStrongNot) {
  95. $isStrongNot = strpos($compressedCatList, ',~cat' . $hcat_key . '_z' . $type_key . '_' . $sclist_key.$key . ',') !== false ? true : false;
  96. } # if
  97. if ($isStrongNot) {
  98. $isStrongNot = '"strongnot": true, "addClass": "strongnotnode", ';
  99. $isSelected = 'true';
  100. } else {
  101. $isStrongNot = '';
  102. } # if
  103. $catList[] = '{"title": "' . $val . '", "icon": false, "select": ' . $isSelected . ', ' . $isStrongNot . '"key":"'. 'cat' . $hcat_key . '_z' . $type_key . '_' . $sclist_key.$key .'"}';
  104. } # if
  105. } # if
  106. } # foreach
  107. $subcatTmp .= join(",", $catList);
  108. $subcatDesc[] = $subcatTmp . "]}";
  109. } # if
  110. } # foreach
  111. if ($type_key !== 'z') {
  112. # If the user choose to show only one categortype, we dont want the categorytype item itself
  113. if ($this->_params['subcatz'] == '*') {
  114. $typeCatDesc[] = $typeCatTmp . join(",", $subcatDesc) . "]}";
  115. } else {
  116. if (!empty($subcatDesc)) {
  117. $typeCatDesc[] = join(",", array_filter($subcatDesc));
  118. } # if
  119. } # else
  120. } else {
  121. $typeCatDesc[] = join(",", $subcatDesc);
  122. } # else
  123. } # foreach
  124. } # foreach
  125. # If the user choose to show only one category, we dont want the category item itself
  126. if ($this->_params['category'] == '*') {
  127. $hcatList[] = $hcatTmp . join(",", $typeCatDesc) . "]}";
  128. } else {
  129. $hcatList[] = join(",", $typeCatDesc);
  130. } # if
  131. } # foreach
  132. echo join(",", $hcatList);
  133. echo "]";
  134. } # categoriesToJson
  135. } # class SpotPage_catjson