/** * Compares the incoming field array with the current record and unsets all fields which are the same. * Used for existing records being updated * * @param string $table Record table name * @param int $id Record uid * @param array $fieldArray Array of field=>value pairs intended to be inserted into the database. All keys with values matching exactly the current value will be unset! * @return array Returns $fieldArray. If the returned array is empty, then the record should not be updated! */ public function compareFieldArrayWithCurrentAndUnset($table, $id, $fieldArray) { // Fetch the original record: $res = $this->databaseConnection->exec_SELECTquery('*', $table, 'uid=' . (int) $id); $currentRecord = $this->databaseConnection->sql_fetch_assoc($res); // If the current record exists (which it should...), begin comparison: if (is_array($currentRecord)) { // Read all field types: $c = 0; $cRecTypes = array(); foreach ($currentRecord as $col => $val) { $cRecTypes[$col] = $this->databaseConnection->sql_field_type($res, $c); $c++; } // Free result: $this->databaseConnection->sql_free_result($res); // Unset the fields which are similar: foreach ($fieldArray as $col => $val) { $fieldConfiguration = $GLOBALS['TCA'][$table]['columns'][$col]['config']; $isNullField = !empty($fieldConfiguration['eval']) && GeneralUtility::inList($fieldConfiguration['eval'], 'null'); // Unset fields if stored and submitted values are equal - except the current field holds MM relations. // In general this avoids to store superfluous data which also will be visualized in the editing history. if (!$fieldConfiguration['MM'] && $this->isSubmittedValueEqualToStoredValue($val, $currentRecord[$col], $cRecTypes[$col], $isNullField)) { unset($fieldArray[$col]); } else { if (!isset($this->mmHistoryRecords[$table . ':' . $id]['oldRecord'][$col])) { $this->historyRecords[$table . ':' . $id]['oldRecord'][$col] = $currentRecord[$col]; } elseif ($this->mmHistoryRecords[$table . ':' . $id]['oldRecord'][$col] != $this->mmHistoryRecords[$table . ':' . $id]['newRecord'][$col]) { $this->historyRecords[$table . ':' . $id]['oldRecord'][$col] = $this->mmHistoryRecords[$table . ':' . $id]['oldRecord'][$col]; } if (!isset($this->mmHistoryRecords[$table . ':' . $id]['newRecord'][$col])) { $this->historyRecords[$table . ':' . $id]['newRecord'][$col] = $fieldArray[$col]; } elseif ($this->mmHistoryRecords[$table . ':' . $id]['newRecord'][$col] != $this->mmHistoryRecords[$table . ':' . $id]['oldRecord'][$col]) { $this->historyRecords[$table . ':' . $id]['newRecord'][$col] = $this->mmHistoryRecords[$table . ':' . $id]['newRecord'][$col]; } } } } else { // If the current record does not exist this is an error anyways and we just return an empty array here. $fieldArray = array(); } return $fieldArray; }