[TASK] Show warning when deleting an element which has references 95/50895/8
authorAnja Leichsenring <anja.leichsenring@typo3.com>
Wed, 7 Feb 2018 20:19:30 +0000 (21:19 +0100)
committerSusanne Moog <susanne.moog@typo3.org>
Thu, 8 Feb 2018 13:14:01 +0000 (14:14 +0100)
Show the same warning in the page module as done in the list module,
except without the title and the table:uid combination, as this is
not needed in page module. It will always be tt_content records there.

Resolves: #78912
Releases: master
Change-Id: I4dea94204475e322fabf30d7a0772dc5e30edb60
Reviewed-on: https://review.typo3.org/50895
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Steffen Frese <steffenf14@gmail.com>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/backend/Classes/View/PageLayoutView.php
typo3/sysext/core/Classes/Database/ReferenceIndex.php
typo3/sysext/recordlist/Classes/RecordList/DatabaseRecordList.php

index cce2ddc..f0834ec 100644 (file)
@@ -32,6 +32,7 @@ use TYPO3\CMS\Core\Database\Query\QueryHelper;
 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
+use TYPO3\CMS\Core\Database\ReferenceIndex;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Localization\LanguageService;
@@ -614,6 +615,13 @@ class PageLayoutView implements LoggerAwareInterface
     protected $tableDisplayOrder = [];
 
     /**
+     * Cache the number of references to a record
+     *
+     * @var array
+     */
+    protected $referenceCount = [];
+
+    /**
      * Construct to initialize class variables.
      */
     public function __construct()
@@ -1969,9 +1977,18 @@ class PageLayoutView implements LoggerAwareInterface
                 $disableDelete = (bool)trim($disableDeleteTS['properties']['tt_content'] ?? $disableDeleteTS['value']);
                 if (!$disableDelete) {
                     $params = '&cmd[tt_content][' . $row['uid'] . '][delete]=1';
+                    $refCountMsg = BackendUtility::referenceCount(
+                            'tt_content',
+                            $row['uid'],
+                            ' ' . $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.referencesToRecord'),
+                            $this->getReferenceCount('tt_content', $row['uid'])
+                        ) . BackendUtility::translationCount(
+                            'tt_content',
+                            $row['uid'],
+                            ' ' . $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.translationsOfRecord')
+                        );
                     $confirm = $this->getLanguageService()->getLL('deleteWarning')
-                        . BackendUtility::translationCount('tt_content', $row['uid'], (' '
-                        . $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.translationsOfRecord')));
+                        . $refCountMsg;
                     $out .= '<a class="btn btn-default t3js-modal-trigger" href="' . htmlspecialchars(BackendUtility::getLinkToDataHandlerAction($params)) . '"'
                         . ' data-severity="warning"'
                         . ' data-title="' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_alt_doc.xlf:label.confirm.delete_record.title')) . '"'
@@ -2045,6 +2062,24 @@ class PageLayoutView implements LoggerAwareInterface
     }
 
     /**
+     * Gets the number of records referencing the record with the UID $uid in
+     * the table $tableName.
+     *
+     * @param string $tableName
+     * @param int $uid
+     * @return int The number of references to record $uid in table
+     */
+    protected function getReferenceCount(string $tableName, int $uid): int
+    {
+        if (!isset($this->referenceCount[$tableName][$uid])) {
+            $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
+            $numberOfReferences = $referenceIndex->getNumberOfReferencedRecords($tableName, $uid);
+            $this->referenceCount[$tableName][$uid] = $numberOfReferences;
+        }
+        return $this->referenceCount[$tableName][$uid];
+    }
+
+    /**
      * Determine whether Drag & Drop should be allowed
      *
      * @param array $row
index 834674e..82afec8 100644 (file)
@@ -338,6 +338,34 @@ class ReferenceIndex implements LoggerAwareInterface
     }
 
     /**
+     * Returns the amount of references for the given record
+     *
+     * @param string $tableName
+     * @param int $uid
+     * @return int
+     */
+    public function getNumberOfReferencedRecords(string $tableName, int $uid): int
+    {
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_refindex');
+        return (int)$queryBuilder
+            ->count('*')->from('sys_refindex')
+            ->where(
+                $queryBuilder->expr()->eq(
+                    'ref_table',
+                    $queryBuilder->createNamedParameter($tableName, \PDO::PARAM_STR)
+                ),
+                $queryBuilder->expr()->eq(
+                    'ref_uid',
+                    $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)
+                ),
+                $queryBuilder->expr()->eq(
+                    'deleted',
+                    $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
+                )
+            )->execute()->fetchColumn(0);
+    }
+
+    /**
      * Calculate the relations for a record of a given table
      *
      * @param string $tableName Table being processed
index 63424b3..1b3a3d9 100644 (file)
@@ -30,6 +30,7 @@ use TYPO3\CMS\Core\Database\Query\QueryBuilder;
 use TYPO3\CMS\Core\Database\Query\QueryHelper;
 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
+use TYPO3\CMS\Core\Database\ReferenceIndex;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Localization\LanguageService;
@@ -1517,17 +1518,8 @@ class DatabaseRecordList
     protected function getReferenceCount($tableName, $uid)
     {
         if (!isset($this->referenceCount[$tableName][$uid])) {
-            $numberOfReferences = GeneralUtility::makeInstance(ConnectionPool::class)
-                ->getConnectionForTable('sys_refindex')
-                ->count(
-                    '*',
-                    'sys_refindex',
-                    [
-                        'ref_table' => $tableName,
-                        'ref_uid' => (int)$uid,
-                        'deleted' => 0
-                    ]
-                );
+            $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
+            $numberOfReferences = $referenceIndex->getNumberOfReferencedRecords($tableName, $uid);
             $this->referenceCount[$tableName][$uid] = $numberOfReferences;
         }
         return $this->referenceCount[$tableName][$uid];