[FEATURE] Substitution of default markers in single function 61/22661/5
authorNicole Cordes <typo3@cordes.co>
Mon, 29 Jul 2013 15:48:25 +0000 (17:48 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Wed, 7 Aug 2013 15:12:32 +0000 (17:12 +0200)
In the function
TYPO3\CMS\Backend\Utility\BackendUtility::exec_foreign_table_where_query
a lot of default markers (like CURRENT_PID, etc.) are replaced.
It would be useful to have this substitution available as single
function, so that it can be called without executing some query.

Resolves: #21892
Releases: 6.2
Change-Id: I4da8279d1b95566690bb0934ac00299e4f4fee3e
Reviewed-on: https://review.typo3.org/22661
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Nicole Cordes
Tested-by: Nicole Cordes
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/backend/Classes/Utility/BackendUtility.php
typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php

index d4e8cfa..58ba1bd 100644 (file)
@@ -3069,28 +3069,7 @@ class BackendUtility {
                $foreign_table = $fieldValue['config'][$prefix . 'foreign_table'];
                $rootLevel = $GLOBALS['TCA'][$foreign_table]['ctrl']['rootLevel'];
                $fTWHERE = $fieldValue['config'][$prefix . 'foreign_table_where'];
-               if (strstr($fTWHERE, '###REC_FIELD_')) {
-                       $fTWHERE_parts = explode('###REC_FIELD_', $fTWHERE);
-                       foreach ($fTWHERE_parts as $kk => $vv) {
-                               if ($kk) {
-                                       $fTWHERE_subpart = explode('###', $vv, 2);
-                                       if (substr($fTWHERE_parts[0], -1) === '\'' && $fTWHERE_subpart[1][0] === '\'') {
-                                               $fTWHERE_parts[$kk] = $GLOBALS['TYPO3_DB']->quoteStr($TSconfig['_THIS_ROW'][$fTWHERE_subpart[0]], $foreign_table) . $fTWHERE_subpart[1];
-                                       } else {
-                                               $fTWHERE_parts[$kk] = $GLOBALS['TYPO3_DB']->fullQuoteStr($TSconfig['_THIS_ROW'][$fTWHERE_subpart[0]], $foreign_table) . $fTWHERE_subpart[1];
-                                       }
-                               }
-                       }
-                       $fTWHERE = implode('', $fTWHERE_parts);
-               }
-               $fTWHERE = str_replace('###CURRENT_PID###', intval($TSconfig['_CURRENT_PID']), $fTWHERE);
-               $fTWHERE = str_replace('###THIS_UID###', intval($TSconfig['_THIS_UID']), $fTWHERE);
-               $fTWHERE = str_replace('###THIS_CID###', intval($TSconfig['_THIS_CID']), $fTWHERE);
-               $fTWHERE = str_replace('###STORAGE_PID###', intval($TSconfig['_STORAGE_PID']), $fTWHERE);
-               $fTWHERE = str_replace('###SITEROOT###', intval($TSconfig['_SITEROOT']), $fTWHERE);
-               $fTWHERE = str_replace('###PAGE_TSCONFIG_ID###', intval($TSconfig[$field]['PAGE_TSCONFIG_ID']), $fTWHERE);
-               $fTWHERE = str_replace('###PAGE_TSCONFIG_IDLIST###', $GLOBALS['TYPO3_DB']->cleanIntList($TSconfig[$field]['PAGE_TSCONFIG_IDLIST']), $fTWHERE);
-               $fTWHERE = str_replace('###PAGE_TSCONFIG_STR###', $GLOBALS['TYPO3_DB']->quoteStr($TSconfig[$field]['PAGE_TSCONFIG_STR'], $foreign_table), $fTWHERE);
+               $fTWHERE = static::replaceMarkersInWhereClause($fTWHERE, $field, $TSconfig, $foreign_table);
                $wgolParts = $GLOBALS['TYPO3_DB']->splitGroupOrderLimit($fTWHERE);
                // rootLevel = -1 means that elements can be on the rootlevel OR on any page (pid!=-1)
                // rootLevel = 0 means that elements are not allowed on root level
@@ -3133,6 +3112,52 @@ class BackendUtility {
        }
 
        /**
+        * Replaces all special markers in a where clause.
+        * Special markers are:
+        * ###REC_FIELD_[field name]###
+        * ###THIS_UID### - is current element uid (zero if new).
+        * ###THIS_CID###
+        * ###CURRENT_PID### - is the current page id (pid of the record).
+        * ###STORAGE_PID###
+        * ###SITEROOT###
+        * ###PAGE_TSCONFIG_ID### - a value you can set from Page TSconfig dynamically.
+        * ###PAGE_TSCONFIG_IDLIST### - a value you can set from Page TSconfig dynamically.
+        * ###PAGE_TSCONFIG_STR### - a value you can set from Page TSconfig dynamically.
+        *
+        * @param string $whereClause Where clause with markers
+        * @param string $table Name of the table of the current record row
+        * @param string $field Field name
+        * @param array $tsConfig TSconfig array from which to get further configuration settings for the field name
+        * @return string
+        */
+       static public function replaceMarkersInWhereClause($whereClause, $table, $field = '', $tsConfig = array()) {
+               if (strstr($whereClause, '###REC_FIELD_')) {
+                       $whereClauseParts = explode('###REC_FIELD_', $whereClause);
+                       foreach ($whereClauseParts as $key => $value) {
+                               if ($key) {
+                                       $whereClauseSubarts = explode('###', $value, 2);
+                                       if (substr($whereClauseParts[0], -1) === '\'' && $whereClauseSubarts[1][0] === '\'') {
+                                               $whereClauseParts[$key] = $GLOBALS['TYPO3_DB']->quoteStr($tsConfig['_THIS_ROW'][$whereClauseSubarts[0]], $table) . $whereClauseSubarts[1];
+                                       } else {
+                                               $whereClauseParts[$key] = $GLOBALS['TYPO3_DB']->fullQuoteStr($tsConfig['_THIS_ROW'][$whereClauseSubarts[0]], $table) . $whereClauseSubarts[1];
+                                       }
+                               }
+                       }
+                       $whereClause = implode('', $whereClauseParts);
+               }
+               $whereClause = str_replace('###CURRENT_PID###', intval($tsConfig['_CURRENT_PID']), $whereClause);
+               $whereClause = str_replace('###THIS_UID###', intval($tsConfig['_THIS_UID']), $whereClause);
+               $whereClause = str_replace('###THIS_CID###', intval($tsConfig['_THIS_CID']), $whereClause);
+               $whereClause = str_replace('###STORAGE_PID###', intval($tsConfig['_STORAGE_PID']), $whereClause);
+               $whereClause = str_replace('###SITEROOT###', intval($tsConfig['_SITEROOT']), $whereClause);
+               $whereClause = str_replace('###PAGE_TSCONFIG_ID###', intval($tsConfig[$field]['PAGE_TSCONFIG_ID']), $whereClause);
+               $whereClause = str_replace('###PAGE_TSCONFIG_IDLIST###', $GLOBALS['TYPO3_DB']->cleanIntList($tsConfig[$field]['PAGE_TSCONFIG_IDLIST']), $whereClause);
+               $whereClause = str_replace('###PAGE_TSCONFIG_STR###', $GLOBALS['TYPO3_DB']->quoteStr($tsConfig[$field]['PAGE_TSCONFIG_STR'], $table), $whereClause);
+
+               return $whereClause;
+       }
+
+       /**
         * Returns TSConfig for the TCEFORM object in Page TSconfig.
         * Used in TCEFORMs
         *
index dfba55a..d4fb7ea 100644 (file)
@@ -684,6 +684,200 @@ class BackendUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $onclickCode = 'var previewWin = window.open(\'' . $alternativeUrl . '\',\'newTYPO3frontendWindow\');';
                $this->assertStringMatchesFormat($onclickCode, Utility\BackendUtility::viewOnClick(NULL, NULL, NULL, NULL, $alternativeUrl, NULL, FALSE));
        }
-}
 
-?>
\ No newline at end of file
+       /**
+        * Tests concerning replaceMarkersInWhereClause
+        */
+
+       /**
+        * @return array
+        */
+       public function replaceMarkersInWhereClauseDataProvider() {
+               return array(
+                       'replaceMarkersInWhereClause replaces record field marker with quoted string' => array(
+                               ' AND dummytable.title=\'###REC_FIELD_dummyfield###\'',
+                               array(
+                                       '_THIS_ROW' => array(
+                                               'dummyfield' => 'Hello World'
+                                       )
+                               ),
+                               ' AND dummytable.title=\'Hello World\''
+                       ),
+                       'replaceMarkersInWhereClause replaces record field marker with fullquoted string' => array(
+                               ' AND dummytable.title=###REC_FIELD_dummyfield###',
+                               array(
+                                       '_THIS_ROW' => array(
+                                               'dummyfield' => 'Hello World'
+                                       )
+                               ),
+                               ' AND dummytable.title=\'Hello World\''
+                       ),
+                       'replaceMarkersInWhereClause replaces multiple record field markers' => array(
+                               ' AND dummytable.title=\'###REC_FIELD_dummyfield###\' AND dummytable.pid=###REC_FIELD_pid###',
+                               array(
+                                       '_THIS_ROW' => array(
+                                               'dummyfield' => 'Hello World',
+                                               'pid' => 42
+                                       )
+                               ),
+                               ' AND dummytable.title=\'Hello World\' AND dummytable.pid=\'42\''
+                       ),
+                       'replaceMarkersInWhereClause replaces current pid with integer' => array(
+                               ' AND dummytable.uid=###CURRENT_PID###',
+                               array(
+                                       '_CURRENT_PID' => 42
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces current pid with string' => array(
+                               ' AND dummytable.uid=###CURRENT_PID###',
+                               array(
+                                       '_CURRENT_PID' => '42string'
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces current record uid with integer' => array(
+                               ' AND dummytable.uid=###THIS_UID###',
+                               array(
+                                       '_THIS_UID' => 42
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces current record uid with string' => array(
+                               ' AND dummytable.uid=###THIS_UID###',
+                               array(
+                                       '_THIS_UID' => '42string'
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces current record cid with integer' => array(
+                               ' AND dummytable.uid=###THIS_CID###',
+                               array(
+                                       '_THIS_CID' => 42
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces current record cid with string' => array(
+                               ' AND dummytable.uid=###THIS_CID###',
+                               array(
+                                       '_THIS_CID' => '42string'
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces storage pid with integer' => array(
+                               ' AND dummytable.uid=###STORAGE_PID###',
+                               array(
+                                       '_STORAGE_PID' => 42
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces storage pid with string' => array(
+                               ' AND dummytable.uid=###STORAGE_PID###',
+                               array(
+                                       '_STORAGE_PID' => '42string'
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces siteroot uid with integer' => array(
+                               ' AND dummytable.uid=###SITEROOT###',
+                               array(
+                                       '_SITEROOT' => 42
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces siteroot uid with string' => array(
+                               ' AND dummytable.uid=###SITEROOT###',
+                               array(
+                                       '_SITEROOT' => '42string'
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces page tsconfig id with integer' => array(
+                               ' AND dummytable.uid=###PAGE_TSCONFIG_ID###',
+                               array(
+                                       'dummyfield' => array(
+                                               'PAGE_TSCONFIG_ID' => 42
+                                       )
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces page tsconfig id with string' => array(
+                               ' AND dummytable.uid=###PAGE_TSCONFIG_ID###',
+                               array(
+                                       'dummyfield' => array(
+                                               'PAGE_TSCONFIG_ID' => '42string'
+                                       )
+                               ),
+                               ' AND dummytable.uid=42'
+                       ),
+                       'replaceMarkersInWhereClause replaces page tsconfig id list' => array(
+                               ' AND dummytable.uid IN (###PAGE_TSCONFIG_IDLIST###)',
+                               array(
+                                       'dummyfield' => array(
+                                               'PAGE_TSCONFIG_IDLIST' => '1,a,2,b,3,c'
+                                       )
+                               ),
+                               ' AND dummytable.uid IN (1,0,2,0,3,0)'
+                       ),
+                       'replaceMarkersInWhereClause replaces page tsconfig id list with empty string' => array(
+                               ' AND dummytable.uid IN (###PAGE_TSCONFIG_IDLIST###)',
+                               array(
+                                       'dummyfield' => array(
+                                               'PAGE_TSCONFIG_IDLIST' => ''
+                                       )
+                               ),
+                               ' AND dummytable.uid IN (0)'
+                       ),
+                       'replaceMarkersInWhereClause replaces page tsconfig string' => array(
+                               ' AND dummytable.title=\'###PAGE_TSCONFIG_STR###\'',
+                               array(
+                                       'dummyfield' => array(
+                                               'PAGE_TSCONFIG_STR' => '42'
+                                       )
+                               ),
+                               ' AND dummytable.title=\'42\''
+                       ),
+                       'replaceMarkersInWhereClause replaces all markers' => array(
+                               ' AND dummytable.title=\'###REC_FIELD_dummyfield###\'' .
+                               ' AND dummytable.uid=###REC_FIELD_uid###' .
+                               ' AND dummytable.pid=###CURRENT_PID###' .
+                               ' AND dummytable.l18n_parent=###THIS_UID###' .
+                               ' AND dummytable.cid=###THIS_CID###' .
+                               ' AND dummytable.storage_pid=###STORAGE_PID###' .
+                               ' AND dummytable.siteroot=###SITEROOT###' .
+                               ' AND dummytable.config_uid=###PAGE_TSCONFIG_ID###' .
+                               ' AND dummytable.idlist IN (###PAGE_TSCONFIG_IDLIST###)' .
+                               ' AND dummytable.string=\'###PAGE_TSCONFIG_STR###\'',
+                               array(
+                                       '_THIS_ROW' => array(
+                                               'dummyfield' => 'Hello World',
+                                               'uid' => 42
+                                       ),
+                                       '_CURRENT_PID' => '1',
+                                       '_THIS_UID' => 2,
+                                       '_THIS_CID' => 3,
+                                       '_STORAGE_PID' => 4,
+                                       '_SITEROOT' => 5,
+                                       'dummyfield' => array(
+                                               'PAGE_TSCONFIG_ID' => 6,
+                                               'PAGE_TSCONFIG_IDLIST' => '1,2,3',
+                                               'PAGE_TSCONFIG_STR' => 'string'
+                                       )
+                               ),
+                               ' AND dummytable.title=\'Hello World\' AND dummytable.uid=\'42\' AND dummytable.pid=1' .
+                               ' AND dummytable.l18n_parent=2 AND dummytable.cid=3 AND dummytable.storage_pid=4' .
+                               ' AND dummytable.siteroot=5 AND dummytable.config_uid=6 AND dummytable.idlist IN (1,2,3)' .
+                               ' AND dummytable.string=\'string\'',
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider replaceMarkersInWhereClauseDataProvider
+        */
+       public function replaceMarkersInWhereClauseReturnsValidWhereClause($whereClause, $tsConfig, $expected) {
+               $this->assertSame($expected, Utility\BackendUtility::replaceMarkersInWhereClause($whereClause, 'dummytable', 'dummyfield', $tsConfig));
+       }
+}
+?>