Skip to content

Commit

Permalink
[FEATURE] add slug/path_segment and update wizard
Browse files Browse the repository at this point in the history
Resolves: extcode#18
  • Loading branch information
Daniel Lorenz committed Nov 15, 2018
1 parent efcc130 commit 3f500c6
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 1 deletion.
135 changes: 135 additions & 0 deletions Classes/Updates/SlugUpdater.php
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;
}
}
16 changes: 16 additions & 0 deletions Configuration/TCA/tx_cartproducts_domain_model_product_product.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'showitem' => '
sys_language_uid;;;;1-1-1, l10n_parent, l10n_diffsource,
product_type, sku, title,
path_segment,
--div--;' . $_LLL . ':tx_cartproducts_domain_model_product_product.div.descriptions,
teaser;;;richtext:rte_transform[mode=ts_links], description;;;richtext:rte_transform[mode=ts_links],
product_content,
Expand Down Expand Up @@ -224,6 +225,21 @@
],
],

'path_segment' => [
'exclude' => true,
'label' => $_LLL . ':tx_cartproducts_domain_model_product_product.path_segment',
'config' => [
'type' => 'slug',
'size' => 50,
'generatorOptions' => [
'fields' => ['title'],
],
'fallbackCharacter' => '-',
'eval' => 'uniqueInSite',
'default' => '',
],
],

'teaser' => [
'exclude' => 1,
'label' => $_LLL . ':tx_cartproducts_domain_model_product_product.teaser',
Expand Down
4 changes: 4 additions & 0 deletions Resources/Private/Language/de.locallang_db.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@
<trans-unit id="tx_cartproducts_domain_model_product_product.title">
<target>Titel</target>
</trans-unit>
<trans-unit id="tx_cartproducts_domain_model_product_product.path_segment">
<source>URL segment</source>
<target>URL Segment</target>
</trans-unit>
<trans-unit id="tx_cartproducts_domain_model_product_product.teaser">
<target>Teaser</target>
</trans-unit>
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Language/locallang_db.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<trans-unit id="tx_cartproducts_domain_model_product_product.sku">
<source>SKU</source>
</trans-unit>
<trans-unit id="tx_cartproducts_domain_model_product_product.path_segment">
<source>URL segment</source>
</trans-unit>
<trans-unit id="tx_cartproducts_domain_model_product_product.is_net_price">
<source>Price (net)</source>
</trans-unit>
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
},
"require": {
"php": ">=7.2.0 <7.3",
"typo3/cms-core": "^9.3",
"ext-pdo": "*",
"typo3/cms-core": "^9.5",
"typo3/cms-extbase": "^9.5",
"typo3/cms-fluid": "^9.5",
"extcode/cart": "dev-6.x.alpha"
},
"require-dev": {
Expand Down
4 changes: 4 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['cartproducts'][]
= 'Extcode\\CartProducts\\ViewHelpers';

// update wizard for slugs
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['cartProductsSlugUpdater'] =
\Extcode\CartProducts\Updates\SlugUpdater::class;

// register listTemplateLayouts
$GLOBALS['TYPO3_CONF_VARS']['EXT'][$_EXTKEY]['templateLayouts']['products'][] = [$_LLL_be . ':flexforms_template.templateLayout.products.table', 'table'];
$GLOBALS['TYPO3_CONF_VARS']['EXT'][$_EXTKEY]['templateLayouts']['products'][] = [$_LLL_be . ':flexforms_template.templateLayout.products.grid', 'grid'];
Expand Down
2 changes: 2 additions & 0 deletions ext_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ CREATE TABLE tx_cartproducts_domain_model_product_product (
teaser text NOT NULL,
description text NOT NULL,

path_segment varchar(2048),

min_number_in_order int(11) unsigned DEFAULT '0' NOT NULL,
max_number_in_order int(11) unsigned DEFAULT '0' NOT NULL,

Expand Down

0 comments on commit 3f500c6

Please sign in to comment.