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 and extend models
  • Loading branch information
einpraegsam committed Feb 21, 2024
1 parent 420e669 commit b49c34a
Show file tree
Hide file tree
Showing 18 changed files with 335 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Classes/Domain/Model/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Download extends AbstractModel

protected string $href = '';
protected string $domain = '';
protected string $site = '';

protected ?Visitor $visitor = null;
protected ?DateTime $crdate = null;
Expand Down Expand Up @@ -89,4 +90,15 @@ public function setDomainAutomatically(): self
$this->domain = FrontendUtility::getCurrentDomain();
return $this;
}

public function getSite(): string
{
return $this->site;
}

public function setSite(string $site): self
{
$this->site = $site;
return $this;
}
}
12 changes: 12 additions & 0 deletions Classes/Domain/Model/Linkclick.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Linkclick extends AbstractModel
protected ?Linklistener $linklistener = null;
protected ?Page $page = null;
protected ?Visitor $visitor = null;
protected string $site = '';

public function getCrdate(): ?DateTime
{
Expand Down Expand Up @@ -47,6 +48,17 @@ public function setPage(Page $page): self
return $this;
}

public function getSite(): string
{
return $this->site;
}

public function setSite(string $site): self
{
$this->site = $site;
return $this;
}

public function getVisitor(): ?Visitor
{
return $this->visitor;
Expand Down
12 changes: 12 additions & 0 deletions Classes/Domain/Model/Pagevisit.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Pagevisit extends AbstractModel

protected string $referrer = '';
protected string $domain = '';
protected string $site = '';

protected int $language = 0;

Expand Down Expand Up @@ -132,6 +133,17 @@ public function setDomain(string $domain): self
return $this;
}

public function getSite(): string
{
return $this->site;
}

public function setSite(string $site): self
{
$this->site = $site;
return $this;
}

public function setDomainAutomatically(): self
{
$this->domain = FrontendUtility::getCurrentDomain();
Expand Down
12 changes: 12 additions & 0 deletions Classes/Domain/Model/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Search extends AbstractModel
protected string $searchterm = '';

protected ?Visitor $visitor = null;
protected ?Pagevisit $pagevisit = null;
protected ?DateTime $crdate = null;

public function getVisitor(): ?Visitor
Expand All @@ -25,6 +26,17 @@ public function setVisitor(Visitor $visitor): self
return $this;
}

public function getPagevisit(): ?Pagevisit
{
return $this->pagevisit;
}

public function setPagevisit(?Pagevisit $pagevisit): self
{
$this->pagevisit = $pagevisit;
return $this;
}

public function getCrdate(): ?DateTime
{
return $this->crdate;
Expand Down
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\AddSitesForLinkclick;
use In2code\Lux\Update\AddSites\AddSitesForPagevisits;
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
Loading

0 comments on commit b49c34a

Please sign in to comment.