Skip to content

Commit

Permalink
Add Folder\Container interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati committed Nov 12, 2024
1 parent 1e71cd8 commit 3dfd883
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 8 deletions.
45 changes: 45 additions & 0 deletions concrete/src/Page/Stack/Folder/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Concrete\Core\Page\Stack\Folder;

/**
* An interface implemented by classes that provides folders and stacks
*/
interface Container
{
/**
* @return \Concrete\Core\Page\Page
*/
public function getPage();

/**
* @return \Concrete\Core\Page\Stack\Folder\Container|null
*/
public function getParent();

/**
* Get the child folders.
*
* @return \Concrete\Core\Page\Stack\Folder\Folder[]
*/
public function getFolders();

/**
* Create a new folder.
*
* @param string $name
*
* @return \Concrete\Core\Page\Stack\Folder\Folder
*/
public function createSubfolder($name);

/**
* @return \Concrete\Core\Page\Stack\Stack[]
*/
public function getGlobalAreas();

/**
* @return \Concrete\Core\Page\Stack\Stack[]
*/
public function getStacks();
}
66 changes: 62 additions & 4 deletions concrete/src/Page/Stack/Folder/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Concrete\Core\Permission\AssignableObjectInterface;
use Concrete\Core\Permission\Key\Key;

class Folder implements ExportableInterface, AssignableObjectInterface
class Folder implements Container, ExportableInterface, AssignableObjectInterface
{
/**
* @var \Concrete\Core\Page\Page
Expand All @@ -19,11 +19,17 @@ class Folder implements ExportableInterface, AssignableObjectInterface
* @var \Concrete\Core\Database\Connection\Connection
*/
protected $connection;

public function __construct(Page $page, Connection $connection)

/**
* @var \Concrete\Core\Page\Stack\Folder\FolderService
*/
protected $folderService;

public function __construct(Page $page, Connection $connection, FolderService $folderService)
{
$this->connection = $connection;
$this->page = $page;
$this->folderService = $folderService;
}

public function setChildPermissionsToOverride()
Expand Down Expand Up @@ -61,10 +67,62 @@ public function getExporter()
}

/**
* @return \Concrete\Core\Page\Page
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::getPage()
*/
public function getPage()
{
return $this->page;
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::getParent()
*/
public function getParent()
{
return $this->folderService->getByID($this->getPage()->getCollectionParentID()) ?: $this->folderService;
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::getFolders()
*/
public function getFolders()
{
return $this->folderService->getChildFolders($this);
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::createSubfolder()
*/
public function createSubfolder($name)
{
return $this->folderService->add($name, $this);
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::getGlobalAreas()
*/
public function getGlobalAreas()
{
return [];
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::getGlobalAreas()
*/
public function getStacks()
{
return $this->folderService->getChildStacks($this);
}
}
139 changes: 135 additions & 4 deletions concrete/src/Page/Stack/Folder/FolderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Type\Type;
use Concrete\Core\Page\Stack\StackList;
use Punic\Comparer;

class FolderService
class FolderService implements Container
{
/**
* @var \Concrete\Core\Database\Connection\Connection
Expand Down Expand Up @@ -43,7 +45,7 @@ public function getByPath($path)
{
$c = Page::getByPath(STACKS_PAGE_PATH . '/' . trim($path, '/'));
if ($c->getPageTypeHandle() == STACK_CATEGORY_PAGE_TYPE) {
return $this->application->make(Folder::class, ['page' => $c]);
return $this->makeFolder($c);
}
}

Expand All @@ -56,7 +58,7 @@ public function getByID($cID)
{
$c = Page::getByID($cID);
if ($c->getPageTypeHandle() == STACK_CATEGORY_PAGE_TYPE) {
return $this->application->make(Folder::class, ['page' => $c]);
return $this->makeFolder($c);
}
}

Expand All @@ -72,7 +74,128 @@ public function add($name, Folder $folder = null)
'name' => $name,
]);

return $this->application->make(Folder::class, ['page' => $page]);
return $this->makeFolder($page);
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::getPage()
*/
public function getPage()
{
return $this->getRootPage();
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::getParent()
*/
public function getParent()
{
return null;
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::getFolders()
*/
public function getFolders()
{
return $this->getChildFolders();
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::createSubfolder()
*/
public function createSubfolder($name)
{
return $this->add($name);
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::getGlobalAreas()
*/
public function getGlobalAreas()
{
$stackList = new StackList();
$stackList->filterByGlobalAreas();

return $stackList->getResults();
}

/**
* {@inheritdoc}
*
* @see \Concrete\Core\Page\Stack\Folder\Container::getStacks()
*/
public function getStacks()
{
return $this->getChildStacks(null);
}

/**
* Get the folders contained in a folder.
*
* @param \Concrete\Core\Page\Stack\Folder\Folder|null $folder if NULL you'll have the root folders
*
* @return \Concrete\Core\Page\Stack\Folder\Folder[]
*/
public function getChildFolders(Folder $parentFolder = null)
{
$parentPage = $parentFolder ? $parentFolder->getPage() : $this->getRootPage();
$rs = $this->connection->executeQuery(
'SELECT cID FROM Pages WHERE cParentID = :cParentID AND ptID = :ptID',
[
'cParentID' => $parentPage->getCollectionID(),
'ptID' => $this->getFolderPageType()->getPageTypeID(),
]
);
$result = [];
while (($cID = $rs->fetchOne()) !== false) {
if (($folder = $this->getByID($cID)) !== null) {
$result[] = $folder;
}
}
$comparer = new Comparer();
usort(
$result,
static function (Folder $a, Folder $b) use ($comparer) {
$pageA = $a->getPage();
$pageB = $b->getPage();

return $comparer->compare($pageA->getCollectionName(), $pageB->getCollectionName()) ?: ($pageA->getCollectionID() - $pageA->getCollectionID);
}
);

return $result;
}

/**
* Get the stacks contained in a folder.
*
* @param \Concrete\Core\Page\Stack\Folder\Folder|null $folder if NULL you'll have the stacks in the folder
*
* @return \Concrete\Core\Page\Stack\Stack[]
*/
public function getChildStacks(Folder $parentFolder = null)
{
$stackList = new StackList();
$stackList->excludeGlobalAreas();
$stackList->getQueryObject()->andWhere('p.ptID <> :folderPageType')->setParameter('folderPageType', $this->getFolderPageType()->getPageTypeID());
if ($parentFolder === null) {
$stackList->filterByParentID($this->getRootPage()->getCollectionID());
} else {
$stackList->filterByFolder($parentFolder);
}

return $stackList->getResults();
}

/**
Expand Down Expand Up @@ -101,4 +224,12 @@ private function getFolderPageType()

return $this->folderPageType;
}

/**
* @return \Concrete\Core\Page\Stack\Folder\Folder
*/
private function makeFolder(Page $page)
{
return $this->application->make(Folder::class, ['page' => $page, 'folderService' => $this]);
}
}

0 comments on commit 3dfd883

Please sign in to comment.