|
<?php
|
|
class tx_elementshelpers_tcemain {
|
|
static $oppositeFields = array();
|
|
protected $parentObj;
|
|
|
|
function processDatamap_preProcessFieldArray(&$fieldArray, $table, $id, &$pObj){
|
|
}
|
|
|
|
function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray, &$pObj){
|
|
$this->parentObj = $pObj;
|
|
if (count($fieldArray)) {
|
|
$tca = $GLOBALS['TCA'][$table]['columns'];
|
|
if (is_array($tca) && count($tca)) {
|
|
foreach ($fieldArray as $field => $value) {
|
|
$fieldConfig = $tca[$field]['config'];
|
|
if ($fieldConfig['MM'] && $fieldConfig['foreign_table'] && $fieldConfig['MM_opposite_field']) {
|
|
$this->oppositeFields[$table][$field] = $fieldConfig;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray, &$pObj) {
|
|
$this->parentObj = $pObj;
|
|
$id = $this->getCleanRecordUid($id);
|
|
// update MM_opposite_fields
|
|
if (is_array($this->oppositeFields[$table]) && count($this->oppositeFields[$table])) {
|
|
foreach ($this->oppositeFields[$table] as $field => $config) {
|
|
if (isset($fieldArray[$field])) {
|
|
$value = $fieldArray[$field];
|
|
$this->updateMMOppositeField($config, $value, $table, $field, $id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function updateMMOppositeField($fieldConfig, $foreignValue, $foreignTable, $foreignField, $foreignId) {
|
|
$oldRelations = explode(',',trim($this->parentObj->mmHistoryRecords[$foreignTable.':'.$foreignId]['oldRecord'][$foreignField]));
|
|
$currentRelations = explode(',',trim($this->parentObj->mmHistoryRecords[$foreignTable.':'.$foreignId]['newRecord'][$foreignField]));
|
|
|
|
$results = $this->findRelatedOppositeIds($fieldConfig, $foreignTable, $foreignField, $foreignId);
|
|
|
|
// if there are relations
|
|
if (is_array($results) && count($results)) {
|
|
foreach($results as $result) {
|
|
// use updateDB instead of a direkt exec_UPDATEquery in order to update the REF-index accordingly etc.
|
|
$this->parentObj->updateDB($fieldConfig['foreign_table'],$result['uid'],array($fieldConfig['MM_opposite_field'] => $result['count']));
|
|
}
|
|
}
|
|
if (count($oldRelations) > 0) {
|
|
// if we had previous relations and now no longer, we still have to update the opposite side
|
|
foreach($oldRelations as $key => $oldRecordId) {
|
|
if ($oldRecordId && !in_array($oldRecordId,$currentRelations)) {
|
|
$queryParts = array(
|
|
'where' => 'uid_local = '.$oldRecordId,
|
|
);
|
|
$data = $this->execRelatedRecordQuery($queryParts, $fieldConfig, $foreignTable, $foreignField, $foreignId);
|
|
if (is_array($data)) {
|
|
$result = current($data);
|
|
$this->parentObj->updateDB($fieldConfig['foreign_table'],$oldRecordId,array($fieldConfig['MM_opposite_field'] => intval($result['count'])));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function findRelatedOppositeIds($fieldConfig, $foreignTable, $foreignField, $foreignId) {
|
|
$MMtable = $fieldConfig['MM'];
|
|
$uid = $this->getCleanRecordUid($foreignId);
|
|
|
|
$idList = $this->parentObj->mmHistoryRecords[$foreignTable.':'.$uid]['newRecord'][$foreignField];
|
|
if (!strlen($idList)) { return NULL; }
|
|
|
|
$queryParts = array(
|
|
'where' => 'uid_local IN ('.$idList.')',
|
|
);
|
|
return $this->execRelatedRecordQuery($queryParts, $fieldConfig, $foreignTable, $foreignField, $foreignId);
|
|
}
|
|
|
|
|
|
function execRelatedRecordQuery($queryParts, $foreignFieldConfig, $foreignTable, $foreignField, $foreignId) {
|
|
$table = $foreignFieldConfig['foreign_table'];
|
|
$field = $foreignFieldConfig['MM_opposite_field'];
|
|
|
|
$fieldConfig = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
|
|
|
|
if (!is_array($fieldConfig) || $fieldConfig['MM'] != $foreignFieldConfig['MM'] || $fieldConfig['foreign_table'] != $foreignTable) { return NULL; }
|
|
|
|
$matchFields = $fieldConfig['MM_match_fields'];
|
|
$MMtable = $fieldConfig['MM'];
|
|
|
|
$where = array();
|
|
if ($queryParts['where']) {
|
|
$where[] = $queryParts['where'];
|
|
}
|
|
|
|
if (is_array($matchFields)) {
|
|
foreach ($matchFields as $matchField => $matchValue) {
|
|
$where[] = $matchField.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($matchValue,$MMtable);
|
|
}
|
|
}
|
|
return $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($MMtable.'.uid_local as uid, COUNT('.$MMtable.'.uid_foreign) as count',$MMtable.$queryParts['additionalTables'],implode(' AND ',$where),$MMtable.'.uid_local');
|
|
}
|
|
|
|
|
|
function getCleanRecordUid($uid) {
|
|
if (strpos($uid, 'NEW') !== false) {
|
|
$uid = $this->parentObj->substNEWwithIDs[$uid];
|
|
}
|
|
return $uid;
|
|
}
|
|
}
|
|
?>
|