Skip to content

Commit

Permalink
[FEATURE] Add new site fields to relevant tables, add upgradewizards …
Browse files Browse the repository at this point in the history
…and tca
  • Loading branch information
einpraegsam committed Feb 21, 2024
1 parent 420e669 commit febe641
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 3 deletions.
38 changes: 38 additions & 0 deletions Classes/Update/AddSites/AbstractSiteUpgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace In2code\Lux\Update\AddSites;

use In2code\Lux\Domain\Service\SiteService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

abstract class AbstractSiteUpgrade
{
/**
* [
* 123 => 'site 1',
* 124 => 'site 2',
* ]
*
* @var array
*/
protected array $mapping = [];
protected SiteService $siteService;

public function __construct()
{
$this->siteService = GeneralUtility::makeInstance(SiteService::class);
}

protected function getSiteIdentifierFromPage(int $pageIdentifier): string
{
if (array_key_exists($pageIdentifier, $this->mapping)) {
return $this->mapping[$pageIdentifier];
}
$site = $this->siteService->getSiteFromPageIdentifier($pageIdentifier);
$siteIdentifier = $site->getIdentifier();
$this->mapping[$pageIdentifier] = $siteIdentifier;
return $siteIdentifier;
}
}
25 changes: 25 additions & 0 deletions Classes/Update/AddSites/AddSitesForDownloads.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace In2code\Lux\Update\AddSites;

use In2code\Lux\Domain\Model\Download;
use In2code\Lux\Utility\DatabaseUtility;

class AddSitesForDownloads extends AbstractSiteUpgrade
{
public function run(): void
{
$connection = DatabaseUtility::getConnectionForTable(Download::TABLE_NAME);
$records = $connection
->executeQuery('select * from ' . Download::TABLE_NAME . ' where deleted=0')
->fetchAllAssociative();
foreach ($records as $record) {
$siteIdentifier = $this->getSiteIdentifierFromPage($record['page']);
$connection->executeQuery(
'update ' . Download::TABLE_NAME . ' set site = "' . $siteIdentifier . '" where uid=' . $record['uid']
);
}
}
}
25 changes: 25 additions & 0 deletions Classes/Update/AddSites/AddSitesForLinkclick.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace In2code\Lux\Update\AddSites;

use In2code\Lux\Domain\Model\Linkclick;
use In2code\Lux\Utility\DatabaseUtility;

class AddSitesForLinkclick extends AbstractSiteUpgrade
{
public function run(): void
{
$connection = DatabaseUtility::getConnectionForTable(Linkclick::TABLE_NAME);
$records = $connection
->executeQuery('select * from ' . Linkclick::TABLE_NAME . ' where deleted=0')
->fetchAllAssociative();
foreach ($records as $record) {
$siteIdentifier = $this->getSiteIdentifierFromPage($record['page']);
$connection->executeQuery(
'update ' . Linkclick::TABLE_NAME . ' set site = "' . $siteIdentifier . '" where uid=' . $record['uid']
);
}
}
}
25 changes: 25 additions & 0 deletions Classes/Update/AddSites/AddSitesForPagevisits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace In2code\Lux\Update\AddSites;

use In2code\Lux\Domain\Model\Pagevisit;
use In2code\Lux\Utility\DatabaseUtility;

class AddSitesForPagevisits extends AbstractSiteUpgrade
{
public function run(): void
{
$connection = DatabaseUtility::getConnectionForTable(Pagevisit::TABLE_NAME);
$records = $connection
->executeQuery('select * from ' . Pagevisit::TABLE_NAME . ' where deleted=0')
->fetchAllAssociative();
foreach ($records as $record) {
$siteIdentifier = $this->getSiteIdentifierFromPage($record['page']);
$connection->executeQuery(
'update ' . Pagevisit::TABLE_NAME . ' set site = "' . $siteIdentifier . '" where uid=' . $record['uid']
);
}
}
}
59 changes: 59 additions & 0 deletions Classes/Update/AddSitesUpgradeWizard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);
namespace In2code\Lux\Update;

use In2code\Lux\Domain\Model\Pagevisit;
use In2code\Lux\Update\AddSites\AddSitesForDownloads;
use In2code\Lux\Update\AddSites\AddSitesForPagevisits;
use In2code\Lux\Update\AddSites\AddSitesForLinkclick;
use In2code\Lux\Utility\DatabaseUtility;
use Throwable;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Attribute\UpgradeWizard;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;

#[UpgradeWizard('addSitesUpgradeWizard')]
class AddSitesUpgradeWizard implements UpgradeWizardInterface
{
public function getIdentifier(): string
{
return 'addSitesUpgradeWizard';
}

public function getTitle(): string
{
return 'LUX: Add sites to existing pagevisit records for multiclient functionality';
}

public function getDescription(): string
{
return 'Fill some database fields with information about sites';
}

public function executeUpdate(): bool
{
try {
// GeneralUtility::makeInstance(AddSitesForPagevisits::class)->run();
// GeneralUtility::makeInstance(AddSitesForDownloads::class)->run();
GeneralUtility::makeInstance(AddSitesForLinkclick::class)->run();
} catch (Throwable $exception) {
return false;
}
return true;
}

public function updateNecessary(): bool
{
return DatabaseUtility::isFieldExistingInTable('site', Pagevisit::TABLE_NAME)
&& DatabaseUtility::isAnyFieldFilledInTable('site', Pagevisit::TABLE_NAME) === false;
}

public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class,
];
}
}
18 changes: 18 additions & 0 deletions Classes/Utility/DatabaseUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,22 @@ public static function isFieldInTableFilled(string $fieldName, string $tableName
->executeQuery()
->fetchOne() !== '';
}

/**
* @param string $fieldName
* @param string $tableName
* @return bool
* @throws ExceptionDbal
*/
public static function isAnyFieldFilledInTable(string $fieldName, string $tableName): bool
{
$queryBuilder = self::getQueryBuilderForTable($tableName);
return $queryBuilder
->select($fieldName)
->from($tableName)
->where($fieldName . ' != \'\'')
->setMaxResults(1)
->executeQuery()
->fetchOne() !== false;
}
}
11 changes: 10 additions & 1 deletion Configuration/TCA/tx_lux_domain_model_download.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'hideTable' => true,
],
'types' => [
'1' => ['showitem' => 'crdate,href,page,file,properties,domain,visitor'],
'1' => ['showitem' => 'crdate,href,page,file,properties,domain,site,visitor'],
],
'columns' => [
'sys_language_uid' => [
Expand Down Expand Up @@ -104,6 +104,15 @@
'readOnly' => true,
],
],
'site' => [
'exclude' => true,
'label' => 'LLL:EXT:lux/Resources/Private/Language/locallang_db.xlf:' . Download::TABLE_NAME . '.site',
'config' => [
'type' => 'input',
'size' => 30,
'readOnly' => true,
],
],
'visitor' => [
'exclude' => true,
'label' => 'LLL:EXT:lux/Resources/Private/Language/locallang_db.xlf:' . Download::TABLE_NAME . '.visitor',
Expand Down
11 changes: 10 additions & 1 deletion Configuration/TCA/tx_lux_domain_model_linkclick.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'hideTable' => true,
],
'types' => [
'1' => ['showitem' => 'crdate,linklistener,page,visitor'],
'1' => ['showitem' => 'crdate,linklistener,page,site,visitor'],
],
'columns' => [
'sys_language_uid' => [
Expand Down Expand Up @@ -86,6 +86,15 @@
'readOnly' => true,
],
],
'site' => [
'exclude' => true,
'label' => 'LLL:EXT:lux/Resources/Private/Language/locallang_db.xlf:' . Linkclick::TABLE_NAME . '.site',
'config' => [
'type' => 'input',
'size' => 30,
'readOnly' => true,
],
],
'visitor' => [
'exclude' => true,
'label' => 'LLL:EXT:lux/Resources/Private/Language/locallang_db.xlf:' . Linkclick::TABLE_NAME . '.visitor',
Expand Down
11 changes: 10 additions & 1 deletion Configuration/TCA/tx_lux_domain_model_pagevisit.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'hideTable' => true,
],
'types' => [
'1' => ['showitem' => 'page,language,crdate,referrer,domain,visitor'],
'1' => ['showitem' => 'page,language,crdate,referrer,domain,site,visitor'],
],
'columns' => [
'sys_language_uid' => [
Expand Down Expand Up @@ -134,6 +134,15 @@
'readOnly' => true,
],
],
'site' => [
'exclude' => true,
'label' => 'LLL:EXT:lux/Resources/Private/Language/locallang_db.xlf:' . Pagevisit::TABLE_NAME . '.site',
'config' => [
'type' => 'input',
'size' => 30,
'readOnly' => true,
],
],
'visitor' => [
'exclude' => true,
'label' => 'LLL:EXT:lux/Resources/Private/Language/locallang_db.xlf:' . Pagevisit::TABLE_NAME . '.visitor',
Expand Down
16 changes: 16 additions & 0 deletions Resources/Private/Language/de.locallang_db.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@
<source>Current domain</source>
<target state="translated">Aktuelle Domain</target>
</trans-unit>
<trans-unit id="tx_lux_domain_model_pagevisit.site">
<source>Site</source>
<target state="translated">Site</target>
</trans-unit>
<trans-unit id="tx_lux_domain_model_pagevisit.visitor">
<source>Lead</source>
<target state="translated">Lead</target>
Expand Down Expand Up @@ -244,6 +248,14 @@
<source>File</source>
<target state="translated">Datei</target>
</trans-unit>
<trans-unit id="tx_lux_domain_model_download.domain">
<source>Domain</source>
<target state="translated">Domain</target>
</trans-unit>
<trans-unit id="tx_lux_domain_model_download.site">
<source>Site</source>
<target state="translated">Site</target>
</trans-unit>
<trans-unit id="tx_lux_domain_model_download.visitor">
<source>Lead</source>
<target state="translated">Lead</target>
Expand Down Expand Up @@ -452,6 +464,10 @@
<source>Page</source>
<target state="translated">Seite</target>
</trans-unit>
<trans-unit id="tx_lux_domain_model_linkclick.site">
<source>Site</source>
<target state="translated">Site</target>
</trans-unit>
<trans-unit id="tx_lux_domain_model_linkclick.visitor">
<source>Lead</source>
<target state="translated">Lead</target>
Expand Down
11 changes: 11 additions & 0 deletions Resources/Private/Language/locallang_db.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
<trans-unit id="tx_lux_domain_model_pagevisit.domain">
<source>Current domain</source>
</trans-unit>
<trans-unit id="tx_lux_domain_model_pagevisit.site">
<source>Site</source>
<trans-unit id="tx_lux_domain_model_pagevisit.visitor">
<source>Lead</source>
</trans-unit>
Expand Down Expand Up @@ -185,6 +187,12 @@
<trans-unit id="tx_lux_domain_model_download.file">
<source>File</source>
</trans-unit>
<trans-unit id="tx_lux_domain_model_download.domain">
<source>Domain</source>
</trans-unit>
<trans-unit id="tx_lux_domain_model_download.site">
<source>Site</source>
</trans-unit>
<trans-unit id="tx_lux_domain_model_download.visitor">
<source>Lead</source>
</trans-unit>
Expand Down Expand Up @@ -344,6 +352,9 @@
<trans-unit id="tx_lux_domain_model_linkclick.page">
<source>Page</source>
</trans-unit>
<trans-unit id="tx_lux_domain_model_linkclick.site">
<source>Site</source>
</trans-unit>
<trans-unit id="tx_lux_domain_model_linkclick.visitor">
<source>Lead</source>
</trans-unit>
Expand Down
9 changes: 9 additions & 0 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,14 @@ function () {
* CacheHash: Add LUX paramters to excluded variables
*/
\In2code\Lux\Utility\CacheHashUtility::addLuxArgumentsToExcludedVariables();

/**
* Upgrade Wizards
* Todo: Can be removed when TYPO3 11 support is dropped
*/
if (\In2code\Lux\Utility\ConfigurationUtility::isTypo3Version11()) {
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/install']['update']['addSitesUpgradeWizard']
= \In2code\Lux\Update\AddSitesUpgradeWizard::class;
}
}
);
Loading

0 comments on commit febe641

Please sign in to comment.