Exemplo n.º 1
0
 public static function getCategoriesRecursive($categoryId, $checkLanguage = false, $checkAccess = true, $fetchSelfCategory = false, $countCat = false, $countListing = false, $getIdOnly = false)
 {
     if (!$categoryId) {
         $categoryId = 1;
     }
     $storeId = md5(__METHOD__ . "::{$categoryId}::" . (int) $checkLanguage . "::" . (int) $checkAccess . "::" . (int) $fetchSelfCategory . "::" . (int) $countCat . "::" . (int) $countListing . "::" . (int) $getIdOnly);
     if (!isset(self::$cache[$storeId])) {
         $user = JFactory::getUser();
         $levels = $user->getAuthorisedViewLevels();
         $nowDate = JFactory::getDate()->toSql();
         JTable::addIncludePath(JPATH_ADMINISTRATOR . "/components/com_judirectory/tables");
         $categoryTable = JTable::getInstance('Category', 'JUDirectoryTable');
         $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 ($countListing) {
                 $validCategory->total_listings = JUDirectoryFrontHelperCategory::getTotalListingsInCategory($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];
 }
Exemplo n.º 2
0
 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('#__judirectory_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);
         $showTotalChildListings = $params->get('show_total_listings_of_subcat', 0);
         $nestedCategories = null;
         if ($showTotalChildListings || $showTotalSubCats) {
             $nestedCategories = JUDirectoryFrontHelperCategory::getCategoriesRecursive($category->id, true, true, true, false, false, true);
             if ($showTotalChildListings) {
                 $category->total_listings = JUDirectoryFrontHelperCategory::getTotalListingsInCategory($category->id, $nestedCategories);
             }
             if ($showTotalSubCats) {
                 $category->total_nested_categories = JUDirectoryFrontHelperCategory::getTotalSubCategoriesInCategory($category->id, $nestedCategories);
             }
         }
         $registry = new JRegistry();
         $registry->loadString($category->images);
         $category->images = $registry->toObject();
         $category->link = JRoute::_(JUDirectoryHelperRoute::getCategoryRoute($category->id));
         if (!$showEmptyCategory) {
             if (is_null($nestedCategories)) {
                 $nestedCategories = JUDirectoryFrontHelperCategory::getCategoriesRecursive($category->id, true, true, true, false, false, true);
             }
             if (!isset($category->total_nested_categories)) {
                 $category->total_nested_categories = JUDirectoryFrontHelperCategory::getTotalSubCategoriesInCategory($category->id, $nestedCategories);
             }
             if (!isset($category->total_listings)) {
                 $category->total_listings = JUDirectoryFrontHelperCategory::getTotalListingsInCategory($category->id, $nestedCategories);
             }
             if ($category->total_nested_categories > 0 || $category->total_listings > 0) {
                 $subCategoriesAfter[] = $category;
             }
         } else {
             $subCategoriesAfter[] = $category;
         }
     }
     return $subCategoriesAfter;
 }