<?php

namespace Opentalent\OtTemplating\Utilities;

use TYPO3\CMS\Frontend\Page\PageRepository;

/**
 * Class OtPageRepository
 *
 * Provides some useful methods to query typo3 pages
 *
 * @package Opentalent\OtTemplating\Page
 */
class OtPageRepository extends PageRepository
{
    /**
     * Recursively returns all the subpages of the given page
     *
     * @param $pageId
     * @return array
     */
    public function getAllSubpagesForPage($pageId) {
        $subpages = [];

        $stack = $this->getSubpagesForPages(
            [$pageId],
            '*',
            'sorting',
            '',
            false
        );

        foreach ($stack as $page) {
            $subpages[] = $page;
            $children = $this->getAllSubpagesForPage($page['uid']);
            if (!empty($children)) {
                $subpages = array_merge($subpages, $children);
            }
        }
        return $subpages;
    }


}
