forked from extcode/cart_products
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] add slug/path_segment and update wizard
Resolves: extcode#18
- Loading branch information
Daniel Lorenz
committed
Nov 15, 2018
1 parent
efcc130
commit 3f500c6
Showing
7 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace Extcode\CartProducts\Updates; | ||
|
||
use Symfony\Component\Console\Output\OutputInterface; | ||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
use TYPO3\CMS\Core\DataHandling\SlugHelper; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Install\Updates\ChattyInterface; | ||
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface; | ||
|
||
/** | ||
* Generate slugs for empty path_segments | ||
*/ | ||
class SlugUpdater implements UpgradeWizardInterface, ChattyInterface | ||
{ | ||
const IDENTIFIER = 'cartProductsSlugUpdater'; | ||
const TABLE_NAME = 'tx_cartproducts_domain_model_product_product'; | ||
|
||
/** | ||
* Return the identifier for this wizard | ||
* This should be the same string as used in the ext_localconf class registration | ||
* | ||
* @return string | ||
*/ | ||
public function getIdentifier(): string | ||
{ | ||
return self::IDENTIFIER; | ||
} | ||
|
||
/** | ||
* Get title | ||
* | ||
* @return string | ||
*/ | ||
public function getTitle(): string | ||
{ | ||
return 'Updates slug field "path_segment" of EXT:cart_products records'; | ||
} | ||
|
||
/** | ||
* Return the description for this wizard | ||
* | ||
* @return string | ||
*/ | ||
public function getDescription(): string | ||
{ | ||
return 'TYPO3 includes native URL handling. Every product record has its own speaking URL path called "slug" which can be edited in TYPO3 Backend. However, it is necessary that all products have a URL pre-filled. This is done by evaluating the title.'; | ||
} | ||
|
||
/** | ||
* Checks if an update is needed | ||
* | ||
* @return bool Whether an update is needed (TRUE) or not (FALSE) | ||
*/ | ||
public function updateNecessary(): bool | ||
{ | ||
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(self::TABLE_NAME); | ||
$queryBuilder->getRestrictions()->removeAll(); | ||
$elementCount = $queryBuilder->count('uid') | ||
->from(self::TABLE_NAME) | ||
->where( | ||
$queryBuilder->expr()->orX( | ||
$queryBuilder->expr()->eq('path_segment', $queryBuilder->createNamedParameter('', \PDO::PARAM_STR)), | ||
$queryBuilder->expr()->isNull('path_segment') | ||
) | ||
) | ||
->execute()->fetchColumn(0); | ||
|
||
return (bool)$elementCount; | ||
} | ||
|
||
/** | ||
* Performs the database update | ||
* | ||
* @return bool | ||
*/ | ||
public function executeUpdate(): bool | ||
{ | ||
$slugHelper = GeneralUtility::makeInstance( | ||
SlugHelper::class, | ||
self::TABLE_NAME, | ||
'path_segment', | ||
$GLOBALS['TCA'][self::TABLE_NAME]['columns']['path_segment']['config'] | ||
); | ||
|
||
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable(self::TABLE_NAME); | ||
$queryBuilder = $connection->createQueryBuilder(); | ||
$queryBuilder->getRestrictions()->removeAll(); | ||
$statement = $queryBuilder->select('uid', 'title') | ||
->from(self::TABLE_NAME) | ||
->where( | ||
$queryBuilder->expr()->orX( | ||
$queryBuilder->expr()->eq('path_segment', $queryBuilder->createNamedParameter('', \PDO::PARAM_STR)), | ||
$queryBuilder->expr()->isNull('path_segment') | ||
) | ||
) | ||
->execute(); | ||
while ($record = $statement->fetch()) { | ||
$queryBuilder = $connection->createQueryBuilder(); | ||
$queryBuilder->update(self::TABLE_NAME) | ||
->where( | ||
$queryBuilder->expr()->eq( | ||
'uid', | ||
$queryBuilder->createNamedParameter($record['uid'], \PDO::PARAM_INT) | ||
) | ||
) | ||
->set('path_segment', $slugHelper->sanitize((string)$record['title'])); | ||
$queryBuilder->getSQL(); | ||
$queryBuilder->execute(); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Returns an array of class names of Prerequisite classes | ||
* | ||
* @return string[] | ||
*/ | ||
public function getPrerequisites(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Setter injection for output into upgrade wizards | ||
* | ||
* @param OutputInterface $output | ||
*/ | ||
public function setOutput(OutputInterface $output): void | ||
{ | ||
$this->output = $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters