/inc/PMF_Helper/Category.php

https://github.com/charles-marion/itk-migrationfaq · PHP · 211 lines · 117 code · 25 blank · 69 comment · 37 complexity · 271003d4ef707819774c6cfe3fa80a9e MD5 · raw file

  1. <?php
  2. /**
  3. * Helper class for phpMyFAQ categories
  4. *
  5. * @package phpMyFAQ
  6. * @subpackage PMF_Helper
  7. * @license MPL
  8. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  9. * @since 2009-09-07
  10. * @version SVN: $Id$
  11. * @copyright 2009 phpMyFAQ Team
  12. *
  13. * The contents of this file are subject to the Mozilla Public License
  14. * Version 1.1 (the "License"); you may not use this file except in
  15. * compliance with the License. You may obtain a copy of the License at
  16. * http://www.mozilla.org/MPL/
  17. *
  18. * Software distributed under the License is distributed on an "AS IS"
  19. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  20. * License for the specific language governing rights and limitations
  21. * under the License.
  22. */
  23. if (!defined('IS_VALID_PHPMYFAQ')) {
  24. exit();
  25. }
  26. /**
  27. * PMF_Helper
  28. *
  29. * @package phpMyFAQ
  30. * @subpackage PMF_Helper
  31. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  32. * @since 2009-09-07
  33. * @copyright 2009 phpMyFAQ Team
  34. */
  35. class PMF_Helper_Category extends PMF_Helper
  36. {
  37. /**
  38. * Instance
  39. *
  40. * @var PMF_Helper_Search
  41. */
  42. private static $instance = null;
  43. /**
  44. * Language
  45. *
  46. * @var string
  47. */
  48. private $language = null;
  49. /**
  50. * Constructor
  51. *
  52. * @return
  53. */
  54. private function __construct()
  55. {
  56. }
  57. /**
  58. * Returns the single instance
  59. *
  60. * @access static
  61. * @return PMF_Helper_Category
  62. */
  63. public static function getInstance()
  64. {
  65. if (null == self::$instance) {
  66. $className = __CLASS__;
  67. self::$instance = new $className();
  68. }
  69. return self::$instance;
  70. }
  71. /**
  72. * __clone() Magic method to prevent cloning
  73. *
  74. * @return void
  75. */
  76. private function __clone()
  77. {
  78. }
  79. /**
  80. * Renders the main navigation
  81. *
  82. * @param integer $activeCategory Selected category
  83. * @return string
  84. */
  85. public function renderCategoryNavigation($activeCategory = 0)
  86. {
  87. global $sids, $PMF_LANG;
  88. $open = 0;
  89. $output = '';
  90. $numCategories = $this->Category->height();
  91. if ($numCategories > 0) {
  92. for ($y = 0 ;$y < $numCategories; $y = $this->Category->getNextLineTree($y)) {
  93. list($symbol, $name, $categoryId, $description) = $this->Category->getLineDisplay($y);
  94. if ($activeCategory == $categoryId) {
  95. $isActive = true;
  96. } else {
  97. $isActive = false;
  98. }
  99. $level = $this->Category->treeTab[$y]['level'];
  100. $leveldiff = $open - $level;
  101. if ($leveldiff > 1) {
  102. $output .= '</li>';
  103. for ($i = $leveldiff; $i > 1; $i--) {
  104. $output .= sprintf("\n%s</ul>\n%s</li>\n",
  105. str_repeat("\t", $level + $i + 1),
  106. str_repeat("\t", $level + $i));
  107. }
  108. }
  109. if ($level < $open) {
  110. if (($level - $open) == -1) {
  111. $output .= '</li>';
  112. }
  113. $output .= "\n".str_repeat("\t", $level + 2)."</ul>\n".str_repeat("\t", $level + 1)."</li>\n";
  114. } elseif ($level == $open && $y != 0) {
  115. $output .= "</li>\n";
  116. }
  117. if ($level > $open) {
  118. $output .= sprintf("\n%s<ul class=\"subcat\">\n%s<li>",
  119. str_repeat("\t", $level + 1),
  120. str_repeat("\t", $level + 1));
  121. } else {
  122. $output .= str_repeat("\t", $level + 1)."<li>";
  123. }
  124. if (isset($this->Category->treeTab[$y]['symbol']) && $this->Category->treeTab[$y]['symbol'] == 'plus') {
  125. $output .= $this->Category->addCategoryLink($sids, $categoryId, $name, $description, true, $isActive);
  126. } else {
  127. if ($this->Category->treeTab[$y]['symbol'] == 'minus') {
  128. $name = ($this->Category->treeTab[$y]['parent_id'] == 0)
  129. ?
  130. $name
  131. :
  132. $this->Category->categoryName[$this->Category->treeTab[$y]['id']]['name'];
  133. $output .= $this->Category->addCategoryLink($sids, $this->Category->treeTab[$y]['parent_id'], $name, $description, false, $isActive);
  134. } else {
  135. $output .= $this->Category->addCategoryLink($sids, $categoryId, $name, $description, false, $isActive);
  136. }
  137. }
  138. $open = $level;
  139. }
  140. if ($open > 0) {
  141. $output .= str_repeat("</li>\n\t</ul>\n\t", $open);
  142. }
  143. $output .= "</li>";
  144. return $output;
  145. } else {
  146. $output = '<li><a href="#">'.$PMF_LANG['no_cats'].'</a></li>';
  147. }
  148. return $output;
  149. }
  150. /**
  151. * Get all categories in <option> tags
  152. *
  153. * @param mixed $categoryId Category id or array of category ids
  154. *
  155. * @return string
  156. */
  157. public function renderCategoryOptions($categoryId = '')
  158. {
  159. $categories = '';
  160. if (!is_array($categoryId)) {
  161. $categoryId = array(array('category_id' => $categoryId,
  162. 'category_lang' => ''));
  163. }
  164. $i = 0;
  165. foreach ($this->Category->catTree as $cat) {
  166. $indent = '';
  167. for ($j = 0; $j < $cat['indent']; $j++) {
  168. $indent .= '....';
  169. }
  170. $categories .= "\t<option value=\"".$cat['id']."\"";
  171. if (0 == $i && count($categoryId) == 0) {
  172. $categories .= ' selected="selected"';
  173. } else {
  174. foreach ($categoryId as $categoryid) {
  175. if ($cat['id'] == $categoryid['category_id']) {
  176. $categories .= ' selected="selected"';
  177. }
  178. }
  179. }
  180. $categories .= ">";
  181. $categories .= $indent.$cat['name'] . "</option>\n";
  182. $i++;
  183. }
  184. return $categories;
  185. }
  186. }