Skip to content

Commit

Permalink
[BUGFIX] Introduce path-segments for tags, author and category - fixes
Browse files Browse the repository at this point in the history
…#31 (#32)

Fixes #31
  • Loading branch information
benjaminkott committed Feb 12, 2019
1 parent 08d7b82 commit cece9ee
Show file tree
Hide file tree
Showing 12 changed files with 641 additions and 11 deletions.
13 changes: 13 additions & 0 deletions Classes/Domain/Model/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class Author extends AbstractEntity
*/
protected $name = '';

/**
* @var string
*/
protected $slug;

/**
* @var string
*/
Expand Down Expand Up @@ -183,6 +188,14 @@ public function setName(string $name): self
return $this;
}

/**
* @return string
*/
public function getSlug(): ?string
{
return $this->slug;
}

/**
* @return string
*/
Expand Down
13 changes: 13 additions & 0 deletions Classes/Domain/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class Category extends AbstractEntity
*/
protected $title = '';

/**
* @var string
*/
protected $slug;

/**
* @var string
*/
Expand Down Expand Up @@ -113,6 +118,14 @@ public function setTitle(string $title): self
return $this;
}

/**
* @return string
*/
public function getSlug(): ?string
{
return $this->slug;
}

/**
* Gets the description.
*
Expand Down
13 changes: 13 additions & 0 deletions Classes/Domain/Model/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class Tag extends AbstractEntity
*/
protected $title;

/**
* @var string
*/
protected $slug;

/**
* The description of the tag. Used for SEO meta description.
*
Expand Down Expand Up @@ -92,6 +97,14 @@ public function setTitle(string $title): self
return $this;
}

/**
* @return string
*/
public function getSlug(): ?string
{
return $this->slug;
}

/**
* @return string
*/
Expand Down
149 changes: 149 additions & 0 deletions Classes/Updates/AuthorSlugUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?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 TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\DataHandling\SlugHelper;
use TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

/**
* AuthorSlugUpdate
*/
class AuthorSlugUpdate implements UpgradeWizardInterface
{
/**
* @var string
*/
protected $table = 'tx_blog_domain_model_author';

/**
* @var string
*/
protected $slugField = 'slug';

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

/**
* @return string
*/
public function getTitle(): string
{
return '[EXT:blog] Generate Path-Segments for Authors';
}

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

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

/**
* @return bool
*/
public function updateNecessary(): bool
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));

$elementCount = $queryBuilder
->count('uid')
->from($this->table)
->where(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->eq($this->slugField, $queryBuilder->createNamedParameter('')),
$queryBuilder->expr()->isNull($this->slugField)
)
)
->execute()->fetchColumn(0);

return (bool)$elementCount;
}

/**
* @return bool
*/
public function executeUpdate(): bool
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->table);
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));

$statement = $queryBuilder
->select('*')
->from($this->table)
->where(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->eq($this->slugField, $queryBuilder->createNamedParameter('')),
$queryBuilder->expr()->isNull($this->slugField)
)
)
->execute();

$fieldConfig = $GLOBALS['TCA'][$this->table]['columns'][$this->slugField]['config'];
$evalInfo = !empty($fieldConfig['eval']) ? GeneralUtility::trimExplode(',', $fieldConfig['eval'], true) : [];
$hasToBeUniqueInSite = in_array('uniqueInSite', $evalInfo, true);
$hasToBeUniqueInPid = in_array('uniqueInPid', $evalInfo, true);
$slugHelper = GeneralUtility::makeInstance(SlugHelper::class, $this->table, $this->slugField, $fieldConfig);

while ($record = $statement->fetch()) {

$recordId = (int)$record['uid'];
$pid = (int)$record['pid'];

// Build Slug
$slug = $slugHelper->generate($record, $pid);
$state = RecordStateFactory::forName($this->table)->fromArray($record, $pid, $recordId);
if ($hasToBeUniqueInSite && !$slugHelper->isUniqueInSite($slug, $state)) {
$slug = $slugHelper->buildSlugForUniqueInSite($slug, $state);
}
if ($hasToBeUniqueInPid && !$slugHelper->isUniqueInPid($slug, $state)) {
$slug = $slugHelper->buildSlugForUniqueInPid($slug, $state);
}

// Update Record
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder
->update($this->table)
->where(
$queryBuilder->expr()->eq(
'uid',
$queryBuilder->createNamedParameter($record['uid'], \PDO::PARAM_INT)
)
)
->set('slug', $slug);
$queryBuilder->execute();
}

return true;
}
}
165 changes: 165 additions & 0 deletions Classes/Updates/CategorySlugUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?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 TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\DataHandling\SlugHelper;
use TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

/**
* CategorySlugUpdate
*/
class CategorySlugUpdate implements UpgradeWizardInterface
{
/**
* @var string
*/
protected $table = 'sys_category';

/**
* @var string
*/
protected $slugField = 'slug';

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

/**
* @return string
*/
public function getTitle(): string
{
return '[EXT:blog] Generate Path-Segments for Categories';
}

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

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

/**
* @return bool
*/
public function updateNecessary(): bool
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));

$elementCount = $queryBuilder
->count('uid')
->from($this->table)
->where(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->eq($this->slugField, $queryBuilder->createNamedParameter('')),
$queryBuilder->expr()->isNull($this->slugField)
)
)
->execute()->fetchColumn(0);

return (bool)$elementCount;
}

/**
* @return bool
*/
public function executeUpdate(): bool
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->table);
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));

$statement = $queryBuilder
->select('*')
->from($this->table)
->where(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->eq($this->slugField, $queryBuilder->createNamedParameter('')),
$queryBuilder->expr()->isNull($this->slugField)
)
)
->addOrderBy('t3ver_wsid', 'asc')
->addOrderBy('pid', 'asc')
->addOrderBy('sorting', 'asc')
->execute();

$fieldConfig = $GLOBALS['TCA'][$this->table]['columns'][$this->slugField]['config'];
$evalInfo = !empty($fieldConfig['eval']) ? GeneralUtility::trimExplode(',', $fieldConfig['eval'], true) : [];
$hasToBeUniqueInSite = in_array('uniqueInSite', $evalInfo, true);
$hasToBeUniqueInPid = in_array('uniqueInPid', $evalInfo, true);
$slugHelper = GeneralUtility::makeInstance(SlugHelper::class, $this->table, $this->slugField, $fieldConfig);

while ($record = $statement->fetch()) {

$recordId = (int)$record['uid'];
$pid = (int)$record['pid'];

// Respect Workspace
if ($pid === -1) {
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$liveVersion = $queryBuilder
->select('pid')
->from($this->table)
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($record['t3ver_oid'], \PDO::PARAM_INT))
)->execute()->fetch();
$pid = (int)$liveVersion['pid'];
}

// Build Slug
$slug = $slugHelper->generate($record, $pid);
$state = RecordStateFactory::forName($this->table)->fromArray($record, $pid, $recordId);
if ($hasToBeUniqueInSite && !$slugHelper->isUniqueInSite($slug, $state)) {
$slug = $slugHelper->buildSlugForUniqueInSite($slug, $state);
}
if ($hasToBeUniqueInPid && !$slugHelper->isUniqueInPid($slug, $state)) {
$slug = $slugHelper->buildSlugForUniqueInPid($slug, $state);
}

// Update Record
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder
->update($this->table)
->where(
$queryBuilder->expr()->eq(
'uid',
$queryBuilder->createNamedParameter($record['uid'], \PDO::PARAM_INT)
)
)
->set('slug', $slug);
$queryBuilder->execute();
}

return true;
}
}
Loading

0 comments on commit cece9ee

Please sign in to comment.