<?php

require_once("bootstrap.php");

// Flush cache:
/** @var \OxidEsales\Eshop\Core\Cache\Generic\Cache $oCacheClass */
$oCacheClass = \OxidEsales\Eshop\Core\Registry::get(\OxidEsales\Eshop\Core\Cache\Generic\Cache::class);

// First use of CategoryList with setLoadFull(false) + buildTree with active category '0f4fb00809cec9aa0910aa9c8fe36751' ('Kites')
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$oCacheClass->flush(); // clear cache
/** @var \OxidEsales\Eshop\Application\Model\CategoryList $oCategoryList */
$oCategoryList=oxNew(\OxidEsales\Eshop\Application\Model\CategoryList::class);
$oCategoryList->setLoadFull(true);
$oCategoryList->setLoadLevel(1);
$oCategoryList->buildTree('0f4fb00809cec9aa0910aa9c8fe36751'); // 'Kites'

// Check Content of cache:
$oCachedData = $oCacheClass->get($oCategoryList->getCacheKey());
echo "<div>setLoadFull(true) + buildTree('0f4fb00809cec9aa0910aa9c8fe36751') + setLoadLevel(1)</div>";
echo "<div>Cache-Content: ".md5(json_encode($oCachedData->getData()))."</div>";
dumpPath($oCategoryList);

echo "<hr>";
// Second use of CategoryList this time with setLoadFull(true) + buildTree with active category 'fad569d6659caca39bc93e98d13dd58b' ('Sportswear')
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$oCacheClass->flush(); // clear cache again
$oCategoryList=oxNew(\OxidEsales\Eshop\Application\Model\CategoryList::class);
$oCategoryList->setLoadFull(false);
$oCategoryList->buildTree('fad569d6659caca39bc93e98d13dd58b'); // 'Sportswear'
$oCachedData = $oCacheClass->get($oCategoryList->getCacheKey());
echo "<div>setLoadFull(false) + buildTree('fad569d6659caca39bc93e98d13dd58b')</div>";
echo "<div>Cache-Content: ".md5(json_encode($oCachedData->getData()))."</div>";
dumpPath($oCategoryList);



echo "<hr>";
// First use of CategoryList with setLoadFull(false) + buildTree with active category '0f4fb00809cec9aa0910aa9c8fe36751' ('Kites') now again
// but we do not flush the cache
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//$oCacheClass->flush(); // do not clear cache this time

/** @var \OxidEsales\Eshop\Application\Model\CategoryList $oCategoryList */
$oCategoryList=oxNew(\OxidEsales\Eshop\Application\Model\CategoryList::class);
$oCategoryList->setLoadFull(true);
$oCategoryList->setLoadLevel(1);
$oCategoryList->buildTree('0f4fb00809cec9aa0910aa9c8fe36751'); // 'Kites'
$oCachedData = $oCacheClass->get($oCategoryList->getCacheKey());
echo "<div>setLoadFull(true) + buildTree('0f4fb00809cec9aa0910aa9c8fe36751') + setLoadLevel(1)</div>";
echo "<div>Cache-Content: ".md5(json_encode($oCachedData->getData()))."</div>";
dumpPath($oCategoryList);

function dumpPath($catItems, $lvl=0)
{
    echo "<div style='background-color:yellow;padding:5px;'>";
    foreach ($catItems as $catItem) {
        echo "<div style='font-family:Courier New;font-size:9pt;padding:2px;'>";
        for ($i = 0; $i < $lvl; $i++) {
            echo "&nbsp; ";
        }
        echo $catItem->oxcategories__oxtitle->value;
        echo "</div>";

        $subcats=$catItem->getSubCats();
        dumpPath($subcats,($lvl+1));
    }
    echo "</div>";
}