public function getSubCategories($parentId, $ordering = 'title', $direction = 'ASC') { $params = $this->getState('params'); $showEmptyCategory = $params->get('show_empty_subcategory', 1); $user = JFactory::getUser(); $levels = $user->getAuthorisedViewLevels(); $levelsStr = implode(',', $levels); $db = JFactory::getDbo(); $nullDate = $db->getNullDate(); $nowDate = JFactory::getDate()->toSql(); $query = $db->getQuery(true); $query->select('*'); $query->from('#__judownload_categories'); $query->where('parent_id=' . $parentId); $query->where('published = 1'); $query->where('(publish_up = ' . $db->quote($nullDate) . ' OR publish_up <= ' . $db->quote($nowDate) . ')'); $query->where('(publish_down = ' . $db->quote($nullDate) . ' OR publish_down >= ' . $db->quote($nowDate) . ')'); $query->where('access IN (' . $levelsStr . ')'); $app = JFactory::getApplication(); $tag = JFactory::getLanguage()->getTag(); if ($app->getLanguageFilter()) { $query->where('language IN (' . $db->quote($tag) . ',' . $db->quote('*') . ',' . $db->quote('') . ')'); } $query->order($ordering . ' ' . $direction); $db->setQuery($query); $subCategoriesBefore = $db->loadObjectList(); $subCategoriesAfter = array(); foreach ($subCategoriesBefore AS $category) { $showTotalSubCats = $params->get('show_total_subcats_of_subcat', 0); $showTotalChildDocs = $params->get('show_total_docs_of_subcat', 0); $nestedCategories = null; if ($showTotalChildDocs || $showTotalSubCats) { $nestedCategories = JUDownloadFrontHelperCategory::getCategoriesRecursive($category->id, true, true, true, false, false, true); if ($showTotalChildDocs) { $category->total_documents = JUDownloadFrontHelperCategory::getTotalDocumentsInCategory($category->id, $nestedCategories); } if ($showTotalSubCats) { $category->total_nested_categories = JUDownloadFrontHelperCategory::getTotalSubCategoriesInCategory($category->id, $nestedCategories); } } $registry = new JRegistry; $registry->loadString($category->images); $category->images = $registry->toObject(); $category->link = JRoute::_(JUDownloadHelperRoute::getCategoryRoute($category->id)); if (!$showEmptyCategory) { if (is_null($nestedCategories)) { $nestedCategories = JUDownloadFrontHelperCategory::getCategoriesRecursive($category->id, true, true, true, false, false, true); } if (!isset($category->total_nested_categories)) { $category->total_nested_categories = JUDownloadFrontHelperCategory::getTotalSubCategoriesInCategory($category->id, $nestedCategories); } if (!isset($category->total_documents)) { $category->total_documents = JUDownloadFrontHelperCategory::getTotalDocumentsInCategory($category->id, $nestedCategories); } if (($category->total_nested_categories > 0) || ($category->total_documents > 0)) { $subCategoriesAfter[] = $category; } } else { $subCategoriesAfter[] = $category; } } return $subCategoriesAfter; }
public static function getCategoriesRecursive($categoryId, $checkLanguage = false, $checkAccess = true, $fetchSelfCategory = false, $countCat = false, $countDoc = false, $getIdOnly = false) { if (!$categoryId) { $categoryId = 1; } $storeId = md5(__METHOD__ . "::$categoryId::" . (int) $checkLanguage . "::" . (int) $checkAccess . "::" . (int) $fetchSelfCategory . "::" . (int) $countCat . "::" . (int) $countDoc . "::" . (int) $getIdOnly); if (!isset(self::$cache[$storeId])) { $user = JFactory::getUser(); $levels = $user->getAuthorisedViewLevels(); $nowDate = JFactory::getDate()->toSql(); JTable::addIncludePath(JPATH_ADMINISTRATOR . "/components/com_judownload/tables"); $categoryTable = JTable::getInstance('Category', 'JUDownloadTable'); $nestedCategories = $categoryTable->customGetTree($categoryId); if (!is_array($nestedCategories) || empty($nestedCategories)) { return array(); } $validCategories = array(); $validCategoryIds = array(); $filterLanguage = false; if ($checkLanguage) { $app = JFactory::getApplication(); if ($app->getLanguageFilter()) { $filterLanguage = true; $languageTag = JFactory::getLanguage()->getTag(); $languageTagArray = array('', '*', $languageTag); } } foreach ($nestedCategories AS $key => $category) { if ($key == 0) { if ($checkAccess) { if (!in_array($category->access, $levels)) { return array(); } } if ($category->published != 1) { return array(); } if ($category->publish_up > $nowDate) { return array(); } if (intval($category->publish_down) != 0 && $category->publish_down < $nowDate) { return array(); } if ($filterLanguage) { if (!in_array($category->language, $languageTagArray)) { return array(); } } if ($fetchSelfCategory) { $validCategories[] = $category; $validCategoryIds[] = $category->id; } else { $validCategoryIds[] = $category->id; } } else { if (!in_array($category->parent_id, $validCategoryIds)) { unset($nestedCategories[$key]); continue; } if ($checkAccess) { if (!in_array($category->access, $levels)) { unset($nestedCategories[$key]); continue; } } if ($category->published != 1) { unset($nestedCategories[$key]); continue; } if ($category->publish_up > $nowDate) { unset($nestedCategories[$key]); continue; } if (intval($category->publish_down) != 0 && $category->publish_down < $nowDate) { unset($nestedCategories[$key]); continue; } if ($filterLanguage) { if (!in_array($category->language, $languageTagArray)) { unset($nestedCategories[$key]); continue; } } $validCategories[] = $category; $validCategoryIds[] = $category->id; } } $path = array(); foreach ($validCategories AS $validCategory) { if (isset($path[$validCategory->parent_id])) { $path[$validCategory->id] = $path[$validCategory->parent_id]; array_unshift($path[$validCategory->id], $validCategory->parent_id); } else { $path[$validCategory->id] = array($validCategory->parent_id); } } foreach ($validCategories AS $validCategory) { if (isset($path[$validCategory->id])) { $validCategory->path = $path[$validCategory->id]; } } $countChild = array(); $countChildNested = array(); $childCategoryId = array(); foreach ($validCategories AS $validCategory) { if (!isset($countChild[$validCategory->parent_id])) { $countChild[$validCategory->parent_id] = 1; } else { $countChild[$validCategory->parent_id] += 1; } foreach ($validCategory->path AS $pathId) { if (isset($countChildNested[$pathId])) { $countChildNested[$pathId] += 1; } else { $countChildNested[$pathId] = 1; } if (isset($childCategoryId[$pathId])) { array_push($childCategoryId[$pathId], $validCategory->id); } else { $childCategoryId[$pathId] = array($validCategory->id); } } } foreach ($validCategories AS $validCategory) { if (isset($countChild[$validCategory->id])) { $validCategory->total_childs = $countChild[$validCategory->id]; } else { $validCategory->total_childs = 0; } if (isset($childCategoryId[$validCategory->id])) { $validCategory->nested_cat_array = $childCategoryId[$validCategory->id]; } if (isset($validCategory->nested_cat_array)) { if (is_array($validCategory->nested_cat_array) && count($validCategory->nested_cat_array) > 0) { $includedCategoryArr = array_merge(array($validCategory->id), $validCategory->nested_cat_array); } else { $includedCategoryArr = array($validCategory->id); } } else { $includedCategoryArr = array($validCategory->id); } if ($countCat) { $validCategory->total_nested_categories = count($includedCategoryArr) - 1; } if ($countDoc) { $validCategory->total_documents = JUDownloadFrontHelperCategory::getTotalDocumentsInCategory($validCategory->id, $includedCategoryArr); } } if ($getIdOnly) { if (!empty($validCategories)) { foreach ($validCategories AS $keyValidCategory => $valueValidCategory) { $validCategories[$keyValidCategory] = $valueValidCategory->id; } } } self::$cache[$storeId] = $validCategories; } return self::$cache[$storeId]; }