Project

General

Profile

Bug #82538 ยป LanguageSortingUpdate.php

Wizard? - Markus Gerdes, 2017-09-21 16:58

 
<?php
namespace TYPO3\CMS\Install\Updates;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Update sys_language records to use the newly sorting column,
* set default sorting from title
* @author MadaXel (atlantis media GmbH)
*/
class LanguageSortingUpdate extends AbstractUpdate
{
/**
* @var string
*/
protected $title = 'Update sys_language records to use sorting field';

/**
* Checks if an update is needed
*
* @param string &$description The description for the update
* @return bool Whether an update is needed (TRUE) or not (FALSE)
*/
public function checkForUpdate(&$description)
{
if ($this->isWizardDone()) {
return false;
}

$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_language');
$numberOfAffectedRows = $queryBuilder->count('uid')
->from('sys_language')
->where(
$queryBuilder->expr()->eq('sorting', 0),
$queryBuilder->expr()->isNotNull('sorting')
)
->execute()
->fetchColumn(0);
if ((bool)$numberOfAffectedRows) {
$description = 'The sys_language records have unsorted rows. '
. ' This upgrade wizard adds values depending on the language title';
}
return (bool)$numberOfAffectedRows;
}

/**
* Performs the database update if the sorting field is 0 or null
*
* @param array &$databaseQueries Queries done in this update
* @param string &$customMessage Custom message
* @return bool
*/
public function performUpdate(array &$databaseQueries, &$customMessage)
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_language');
$statement = $queryBuilder->select('uid', 'language_isocode', 'static_lang_isocode')
->from('sys_language')
->where(
$queryBuilder->expr()->eq('sorting', 0),
$queryBuilder->expr()->isNotNull('sorting')
)
->execute();
$sortcounter = 128;
while ($languageRecord = $statement->fetch()) {
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_language');
$queryBuilder->update('sys_language')
->where(
$queryBuilder->expr()->eq(
'uid',
$queryBuilder->createNamedParameter($languageRecord['uid'], \PDO::PARAM_INT)
)
)
->set('sorting', $sortcounter);
$databaseQueries[] = $queryBuilder->getSQL();
$queryBuilder->execute();
$sortcounter *= 2;
}
$this->markWizardAsDone();
return true;
}
}
    (1-1/1)