Wyszukiwanie dzieci
@param array $categories
@param integer $parentCategoryId
@return array
429732
private function _searchChildren(array $categories, $parentCategoryId = null)
    {
        //iteracja po kategoriach
        foreach ($categories as $id => $category) {
            if ($id == $parentCategoryId) {
                return $category['children'];
            }
            if ([] !== $child = $this->_searchChildren($category['children'], $parentCategoryId)) {
                return $child;
            }
        }
        return [];
    }
747492
public function categoryAclAllowed($categoryId)
    {
        //zwrot z ACL kategorii
        return (new \CmsAdmin\Model\CategoryAclModel)->getAcl()->isAllowed(\App\Registry::$auth->getRoles(), $categoryId);
    }
763170
public static function getPathToCategory($categoryId)
    {
        $path = [];

        $category = (new CategoryQuery)->findPk($categoryId);
        if ($category !== null) {
            $path[] = $category;

            if ($category->getParent() !== 0) {
                $path = array_merge(self::getPathToCategory($category->getParent()), $path);
            }
        }

        return $path;
    }
621140
public function getByCategoryID($iCategoryID)
    {
        if (empty($iCategoryID)) {
            return [];
        }

        //Get category data
        $this->addCategoryData($iCategoryID, true);
        $this->arResult = array_reverse($this->arResult);

        return $this->arResult;
    }
163367
protected function initParentCategory()
    {
        if (!array_key_exists('parent_id', $this->arImportData)) {
            return;
        }

        $iParentID = array_get($this->arImportData, 'parent_id');
        array_forget($this->arImportData, 'parent_id');
        if (empty($iParentID)) {
            $this->obParentCategory = false;

            return;
        }

        //Find parent category
        $this->obParentCategory = Category::getByExternalID($iParentID)->first();
    }
1187824
protected function prepareTree(array $categories, array $options)
    {
        $tree = array();
        $parents_tree = array();
        $children_tree = array();
        $categories_tree = array();

        $parent = isset($options['parent_id']) ? (int) $options['parent_id'] : 0;

        foreach ($categories as $category) {
            $children_tree[$category['parent_id']][] = $category['category_id'];
            $parents_tree[$category['category_id']][] = $category['parent_id'];
            $categories_tree[$category['category_id']] = $category;
        }

        $max_depth = isset($options['depth']) ? (int) $options['depth'] : count($children_tree);

        $process_parents = array();
        $process_parents[] = $parent;

        while (count($process_parents)) {
            $parent = array_pop($process_parents);
            $depth = count($process_parents);

            if ($max_depth <= $depth || empty($children_tree[$parent])) {
                continue;
            }

            $has_children = false;

            $child = current($children_tree[$parent]);

            do {
                if (empty($child)) {
                    break;
                }

                $category = $categories_tree[$child];

                $category['depth'] = $depth;
                $category['parents'] = $parents_tree[$category['category_id']];

                $tree[$category['category_id']] = $category;

                if (!empty($children_tree[$category['category_id']])) {
                    $has_children = true;
                    $process_parents[] = $parent;
                    $process_parents[] = $category['category_id'];
                    reset($categories_tree[$category['category_id']]);
                    next($children_tree[$parent]);
                    break;
                }
            } while ($child = next($children_tree[$parent]));

            if (!$has_children) {
                reset($children_tree[$parent]);
            }
        }

        return $tree;
    }
1242622
public function getByID($akCategoryID)
    {
        $r = $this->entityManager->getRepository(Category::class);

        return $r->findOneBy(['akCategoryID' => $akCategoryID]);
    }
694183
public function addCategories(array $categories)
	{
		$this->categories = array();
		foreach ($categories as $category)
		{
			$this->addCategory($category);
		}

		return $this;
	}
575251
private static function findAllAncestry($categoryId, $parentCategoriesArray = [])
    {
        $category = Category::findOne($categoryId);
        if (!empty($category)) {
            $parentCategoryId = $category->parent_id;
        }
        else throw new NotFoundHttpException();

        if (!empty($parentCategoryId)) {
            $parentCategoriesArray[] = $parentCategoryId;
            return self::findAllAncestry($parentCategoryId, $parentCategoriesArray);
        } else return $parentCategoriesArray;
    }
675912
public function destroy(Category $category)
    {
        foreach ($category->children as $child) {
            $child->parent_id = 0;
            $child->update();
        }

        $category->delete();

        return redirect()->route('admin.category.index');
    }
289020
public static function getAllSubcategoriesIds($category)
    {
        $ids = Database::getInstance()->getChildRecords($category, static::$strTable, false, (array) $category, (!BE_USER_LOGGED_IN ? 'published=1' : ''));
        $ids = \array_map('intval', $ids);

        return $ids;
    }
1084590
public static function findByCategoryId( $categoryId ) {

		$optionTable = CoreTables::getTableName( CoreTables::TABLE_OPTION );

		return self::find()->where( "$optionTable.categoryId=:id", [ ':id' => $categoryId ] )->all();
	}
814696
public function getCategoryFlatTree($parentCategoryId = null, $categoryTypeKey = null)
    {
        $flatTree = [];
        //budowanie płaskie drzewo
        $this->_buildFlatTree('', $flatTree, $this->getCategoryTree($parentCategoryId), $categoryTypeKey);
        return $flatTree;
    }
728324
public function descendantsOf(ChildCategory $category)
    {
        return $this
            ->addUsingAlias(ChildCategory::LEFT_COL, $category->getLeftValue(), Criteria::GREATER_THAN)
            ->addUsingAlias(ChildCategory::LEFT_COL, $category->getRightValue(), Criteria::LESS_THAN);
    }
1063361
public static function delete_categories($categories) {
        global $CFG, $DB;
        require_once($CFG->dirroot . "/course/lib.php");

        // Validate parameters.
        $params = self::validate_parameters(self::delete_categories_parameters(), array('categories' => $categories));

        $transaction = $DB->start_delegated_transaction();

        foreach ($params['categories'] as $category) {
            $deletecat = core_course_category::get($category['id'], MUST_EXIST);
            $context = context_coursecat::instance($deletecat->id);
            require_capability('moodle/category:manage', $context);
            self::validate_context($context);
            self::validate_context(get_category_or_system_context($deletecat->parent));

            if ($category['recursive']) {
                // If recursive was specified, then we recursively delete the category's contents.
                if ($deletecat->can_delete_full()) {
                    $deletecat->delete_full(false);
                } else {
                    throw new moodle_exception('youcannotdeletecategory', '', '', $deletecat->get_formatted_name());
                }
            } else {
                // In this situation, we don't delete the category's contents, we either move it to newparent or parent.
                // If the parent is the root, moving is not supported (because a course must always be inside a category).
                // We must move to an existing category.
                if (!empty($category['newparent'])) {
                    $newparentcat = core_course_category::get($category['newparent']);
                } else {
                    $newparentcat = core_course_category::get($deletecat->parent);
                }

                // This operation is not allowed. We must move contents to an existing category.
                if (!$newparentcat->id) {
                    throw new moodle_exception('movecatcontentstoroot');
                }

                self::validate_context(context_coursecat::instance($newparentcat->id));
                if ($deletecat->can_move_content_to($newparentcat->id)) {
                    $deletecat->delete_move($newparentcat->id, false);
                } else {
                    throw new moodle_exception('youcannotdeletecategory', '', '', $deletecat->get_formatted_name());
                }
            }
        }

        $transaction->allow_commit();
    }
841385
public function showCategory(DocumentCategory $category)
    {
        $documents = $category->documents;

        return $this->showDocumentable($category, $documents);
    }
623102
private function _redirectAfterEdit(\Cms\Orm\CmsCategoryRecord $category)
    {
        //referer
        $referer = $this->_getReferrer();
        //jeśli istnieje referer
        if ($referer) {
            $this->getResponse()->redirectToUrl($referer);
        }
        //przekierowanie na stronę frontową kategorii jeśli wskazana
        if ($category) {
            $this->getResponse()->redirectToUrl($category->getUrl());
        }
        $this->getResponse()->redirect('cmsAdmin', 'category', 'tree');
    }