[FEATURE] Item filters for TCA types "group" and "inline"
authorIngmar Schlecht <ingmar@typo3.org>
Thu, 3 May 2012 12:34:50 +0000 (14:34 +0200)
committerTolleiv Nietsch <tolleiv.nietsch@typo3.org>
Sat, 5 May 2012 15:55:59 +0000 (17:55 +0200)
Releases: 6.0
Resolves: #36810
Change-Id: Ib8a7c30e19dab9cff72412f7dfe51aa8e94343e4
Reviewed-on: http://review.typo3.org/10953
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
Reviewed-by: Tolleiv Nietsch
Tested-by: Tolleiv Nietsch
t3lib/class.t3lib_tcemain.php
typo3/class.browse_links.php
typo3/class.db_list_extra.inc

index b1da5cb..f252dc1 100644 (file)
@@ -1321,6 +1321,8 @@ class t3lib_TCEmain {
                        // This could be a good spot for parsing the array through a validation-function which checks if the values are alright (except that database references are not in their final form - but that is the point, isn't it?)
                        // NOTE!!! Must check max-items of files before the later check because that check would just leave out filenames if there are too many!!
 
+               $valueArray = $this->applyFiltersToValues($tcaFieldConf, $valueArray);
+
                        // Checking for select / authMode, removing elements from $valueArray if any of them is not allowed!
                if ($tcaFieldConf['type'] == 'select' && $tcaFieldConf['authMode']) {
                        $preCount = count($valueArray);
@@ -1386,6 +1388,36 @@ class t3lib_TCEmain {
        }
 
        /**
+        * Applies the filter methods from a column's TCA configuration to a value array.
+        *
+        * @param array $tcaFieldConfiguration
+        * @param array $values
+        * @return array|mixed
+        * @throws RuntimeException
+        */
+       protected function applyFiltersToValues(array $tcaFieldConfiguration, array $values) {
+               if (!is_array($tcaFieldConfiguration['filter'])) {
+                       return $values;
+               }
+
+               foreach ($tcaFieldConfiguration['filter'] as $filter) {
+                       if (!$filter['userFunc']) {
+                               continue;
+                       }
+
+                       $parameters = $filter['parameters'] ? $filter['parameters'] : array();
+                       $parameters['values'] = $values;
+                       $parameters['tcaFieldConfig'] = $tcaFieldConfiguration;
+                       $values = t3lib_div::callUserFunction($filter['userFunc'], $parameters, $this);
+
+                       if (!is_array($values)) {
+                               throw new RuntimeException('Failed calling filter userFunc.', 1336051942);
+                       }
+               }
+               return $values;
+       }
+
+       /**
         * Handling files for group/select function
         *
         * @param $valueArray array Array of incoming file references. Keys are numeric, values are files (basically, this is the exploded list of incoming files)
@@ -2373,6 +2405,8 @@ class t3lib_TCEmain {
                $newValue = '';
                $foreignTable = $tcaFieldConf['foreign_table'];
 
+               $valueArray = $this->applyFiltersToValues($tcaFieldConf, $valueArray);
+
                /*
                 * Fetch the related child records by using t3lib_loadDBGroup:
                 * @var $dbAnalysis t3lib_loadDBGroup
index e764d4f..e9c0b88 100644 (file)
@@ -53,6 +53,20 @@ require_once (PATH_typo3.'/class.db_list_extra.inc');
 class TBE_browser_recordList extends localRecordList {
        var $thisScript = 'browse_links.php';
 
+       /*
+        * Table name of the field pointing to this element browser
+        *
+        * @var string
+        */
+       protected $relatingTable;
+
+       /*
+        * Field name of the field pointing to this element browser
+        *
+        * @var string
+        */
+       protected $relatingField;
+
        /**
         * Initializes the script path
         *
@@ -130,6 +144,64 @@ class TBE_browser_recordList extends localRecordList {
                                $ATag_e;
        }
 
+
+       /**
+        * Check if all row listing conditions are fulfilled.
+        *
+        * This function serves as a dummy method to be overriden in extending classes.
+        *
+        * @param   string  $table string Table name
+        * @param   array   $row array Record
+        * @return  bool    True, if all conditions are fulfilled.
+        */
+       protected function isRowListingConditionFulfilled($table, $row) {
+
+               $returnValue = TRUE;
+
+               if ($this->relatingField && $this->relatingTable) {
+                       $tcaFieldConfig = $GLOBALS['TCA'][$this->relatingTable]['columns'][$this->relatingField]['config'];
+
+                       if (is_array($tcaFieldConfig['filter'])) {
+                               foreach ($tcaFieldConfig['filter'] as $filter) {
+                                       if (!$filter['userFunc']) {
+                                               continue;
+                                       }
+
+                                       $parameters = $filter['parameters']?$filter['parameters']:array();
+                                       $parameters['values'] = array($table.'_'.$row['uid']);
+                                       $parameters['tcaFieldConfig'] = $tcaFieldConfig;
+                                       $valueArray = t3lib_div::callUserFunction($filter['userFunc'], $parameters, $this);
+
+                                       if (count($valueArray) === 0) {
+                                               $returnValue = FALSE;
+                                       }
+                               }
+                       }
+               }
+               return $returnValue;
+       }
+
+
+       /**
+        * Set which pointint field (in the TCEForm) we are currently rending the element browser for
+        *
+        * @param   string  $tableName Table name
+        * @param   string   $fieldName Field name
+        */
+       public function setRelatingTableAndField($tableName, $fieldName) {
+               global $TCA;
+
+               // Check validity of the input data and load TCA
+               if (isset($TCA[$tableName])) {
+                       $this->relatingTable = $tableName;
+                       t3lib_div::loadTCA($tableName);
+                       if ($fieldName && isset($TCA[$tableName]['columns'][$fieldName])) {
+                               $this->relatingField = $fieldName;
+                       }
+               }
+       }
+
+
        /**
         * Local version that sets allFields to TRUE to support userFieldSelect
         *
@@ -1947,6 +2019,18 @@ class browse_links {
                        $dblist->clickMenuEnabled=0;
                        $dblist->tableList=implode(',',$tablesArr);
 
+
+                       $pArr = explode('|', $this->bparams);
+                       $fieldPointerString = $pArr[0]; // a string like "data[pages][79][storage_pid]"
+                       $fieldPointerParts = explode('[', $fieldPointerString); // parts like: data, pages], 79], storage_pid]
+                       $relatingTableName = substr($fieldPointerParts[1], 0, -1);
+                       $relatingFieldName = substr($fieldPointerParts[3], 0, -1);
+
+                       if($relatingTableName && $relatingFieldName) {
+                               $dblist->setRelatingTableAndField($relatingTableName, $relatingFieldName);
+                       }
+
+
                        $dblist->start($id,t3lib_div::_GP('table'),$pointer,
                                t3lib_div::_GP('search_field'),
                                t3lib_div::_GP('search_levels'),
index fc56385..53f6f43 100644 (file)
@@ -495,6 +495,10 @@ class localRecordList extends recordList {
                                $accRows = array();     // Accumulate rows here
                                while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result))    {
 
+                                       if (!$this->isRowListingConditionFulfilled($table, $row)) {
+                                               continue;
+                                       }
+
                                                // In offline workspace, look for alternative record:
                                        t3lib_BEfunc::workspaceOL($table, $row, $GLOBALS['BE_USER']->workspace, TRUE);
 
@@ -604,6 +608,20 @@ class localRecordList extends recordList {
                return $out;
        }
 
+
+       /**
+        * Check if all row listing conditions are fulfilled.
+        *
+        * This function serves as a dummy method to be overriden in extending classes.
+        *
+        * @param   string  $table Table name
+        * @param   array   $row Record
+        * @return  bool    True, if all conditions are fulfilled.
+        */
+       protected function isRowListingConditionFulfilled($table, $row) {
+               return TRUE;
+       }
+
        /**
         * Rendering a single row for the list
         *