Skip to content

Commit

Permalink
[FEATURE] Introduce blog categories (#142)
Browse files Browse the repository at this point in the history
The behavior of blog categories is now streamlined with categories that can be selected within the page records. Without touching or limiting normal categories. Blog categories only show other blog categories within the parent selector box, that are created in the same directory.
  • Loading branch information
benjaminkott authored Jun 5, 2020
1 parent a3061fe commit 434a50e
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 55 deletions.
31 changes: 31 additions & 0 deletions Classes/Backend/FormDataProvider/CategoryDefaultValueProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types = 1);

/*
* This file is part of the package t3g/blog.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace T3G\AgencyPack\Blog\Backend\FormDataProvider;

use T3G\AgencyPack\Blog\Constants;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;

class CategoryDefaultValueProvider implements FormDataProviderInterface
{
public function addData(array $result): array
{
if ($result['command'] !== 'new' ||
$result['tableName'] !== 'sys_category' ||
$result['parentPageRow']['module'] !== 'blog') {
return $result;
}

$result['databaseRow']['record_type'][0] = (string) Constants::CATEGORY_TYPE_BLOG;
$result['recordTypeValue'] = (string) Constants::CATEGORY_TYPE_BLOG;

return $result;
}
}
8 changes: 7 additions & 1 deletion Classes/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
class Constants
{
/**
* Named constants for "magic numbers" of the field doktype.
* Named constants for "magic numbers" of the field pages.doktype.
*/
public const DOKTYPE_BLOG_POST = 137;
public const DOKTYPE_BLOG_PAGE = 138;

/**
* Named constants for "magic numbers" of the field sys_category.category_type.
*/
public const CATEGORY_TYPE_DEFAULT = 1;
public const CATEGORY_TYPE_BLOG = 100;
}
14 changes: 14 additions & 0 deletions Classes/Domain/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@

namespace T3G\AgencyPack\Blog\Domain\Model;

use T3G\AgencyPack\Blog\Constants;
use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;

class Category extends AbstractEntity
{
/**
* @var int
*/
protected $recordType = Constants::CATEGORY_TYPE_BLOG;

/**
* @var string
* @Extbase\Validate("NotEmpty")
Expand Down Expand Up @@ -78,6 +84,14 @@ public function initializeObject(): void
$this->posts = new ObjectStorage();
}

/**
* @return int
*/
public function getRecordType(): ?int
{
return $this->recordType;
}

/**
* Gets the title.
*
Expand Down
147 changes: 147 additions & 0 deletions Classes/Updates/CategoryTypeUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
declare(strict_types = 1);

/*
* This file is part of the package t3g/blog.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace T3G\AgencyPack\Blog\Updates;

use T3G\AgencyPack\Blog\Constants;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

/**
* CategoryTypeUpdate
*/
class CategoryTypeUpdate implements UpgradeWizardInterface
{
/**
* @return string
*/
public function getIdentifier(): string
{
return self::class;
}

/**
* @return string
*/
public function getTitle(): string
{
return '[EXT:blog] Use Blog-Type for Categories';
}

/**
* @return string
*/
public function getDescription(): string
{
return '';
}

/**
* @return array
*/
public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class
];
}

/**
* @return bool
*/
public function updateNecessary(): bool
{
$queryBuilderPages = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
$queryBuilderPages->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$pagesStatement = $queryBuilderPages
->select('uid')
->from('pages')
->where(
$queryBuilderPages->expr()->andX(
$queryBuilderPages->expr()->eq('doktype', $queryBuilderPages->createNamedParameter(254, \PDO::PARAM_INT)),
$queryBuilderPages->expr()->eq('module', $queryBuilderPages->createNamedParameter('blog'))
)
)
->execute();
$pages = [];
while ($pageRecord = $pagesStatement->fetch()) {
$pages[] = $pageRecord['uid'];
}

$queryBuilderCategories = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_category');
$queryBuilderCategories->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$elementCount = $queryBuilderCategories
->count('uid')
->from('sys_category')
->where(
$queryBuilderCategories->expr()->andX(
$queryBuilderCategories->expr()->eq('record_type', $queryBuilderCategories->createNamedParameter(1, \PDO::PARAM_INT)),
$queryBuilderCategories->expr()->in('pid', $queryBuilderCategories->createNamedParameter($pages, Connection::PARAM_INT_ARRAY))
)
)
->execute()
->fetchColumn(0);

return (bool)$elementCount;
}

/**
* @return bool
*/
public function executeUpdate(): bool
{
$queryBuilderPages = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');
$queryBuilderPages->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$pagesStatement = $queryBuilderPages
->select('uid')
->from('pages')
->where(
$queryBuilderPages->expr()->andX(
$queryBuilderPages->expr()->eq('doktype', $queryBuilderPages->createNamedParameter(254, \PDO::PARAM_INT)),
$queryBuilderPages->expr()->eq('module', $queryBuilderPages->createNamedParameter('blog'))
)
)
->execute();
$pages = [];
while ($pageRecord = $pagesStatement->fetch()) {
$pages[] = $pageRecord['uid'];
}

$queryBuilderCategories = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_category');
$queryBuilderCategories->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$categoryStatement = $queryBuilderCategories
->select('uid', 'record_type')
->from('sys_category')
->where(
$queryBuilderCategories->expr()->andX(
$queryBuilderCategories->expr()->eq('record_type', $queryBuilderCategories->createNamedParameter(Constants::CATEGORY_TYPE_DEFAULT, \PDO::PARAM_INT)),
$queryBuilderCategories->expr()->in('pid', $queryBuilderCategories->createNamedParameter($pages, Connection::PARAM_INT_ARRAY))
)
)
->execute();

while ($categoryRecord = $categoryStatement->fetch()) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_category');
$queryBuilder
->update('sys_category')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter((int) $categoryRecord['uid'], \PDO::PARAM_INT))
)
->set('record_type', Constants::CATEGORY_TYPE_BLOG);
$queryBuilder->execute();
}

return true;
}
}
Loading

0 comments on commit 434a50e

Please sign in to comment.