Project

General

Profile

Bug #15861 ยป revert_mm_complete.diff

Administrator Admin, 2006-03-21 20:13

View differences:

TYPO3core/t3lib/class.t3lib_loaddbgroup.php 2006-03-21 19:54:16.000000000 +0100
* @subpackage t3lib
*/
class t3lib_loadDBGroup {
/**
* Means that only uid and the label-field is returned
*/
var $fromTC = true;
/**
* Will exclude deleted records.
* deleted-column is added to $additionalWhere...
*/
var $checkIfDeleted = true;
/**
* If set, values that are not ids in tables are normally discarded.
* By this options they will be preserved and stored in $itemArray.
*/
var $registerNonTableValues = false;
/**
* Usded to collect NON-table elements if registerNonTableValues = true
*/
var $nonTableArray = array();
/**
* Contains the table names htat are allowed for the relation as keys.
* The values are the id-values for each table. Should ONLY contain proper table names.
*/
var $tableArray = array();
/**
* Contains items in an numeric array (table/id for each). Tablenames here might be "_NO_TABLE"
*/
var $itemArray = array();
var $additionalWhere = array(); // used for additional where clause like "AND table.delete=0"
// External, static:
var $fromTC = 1; // Means that only uid and the label-field is returned
var $registerNonTableValues=0; // If set, values that are not ids in tables are normally discarded. By this options they will be preserved.
// Internal, dynamic:
var $tableArray=Array(); // Contains the table names as keys. The values are the id-values for each table. Should ONLY contain proper table names.
var $itemArray=Array(); // Contains items in an numeric array (table/id for each). Tablenames here might be "_NO_TABLE"
var $nonTableArray=array(); // Array for NON-table elements
var $additionalWhere=array();
var $checkIfDeleted = 1; // deleted-column is added to additionalWhere... if this is set...
var $dbPaths=Array();
var $firstTable = ''; // Will contain the first table name in the $tablelist (for positive ids)
var $secondTable = ''; // Will contain the second table name in the $tablelist (for negative ids)
// MM stuff for foreign select
/**
* field value pairs that should match while select and to be set while insert
*/
var $MMmatchFields = array();
/**
* Extra field value pairs that should be set while insert
*/
var $MMinsertFields = array();
/**
* The name of the local table. Needed for foreign select
*/
var $MMlocalTable = '';
/**
* The tablenames field should match this list
*/
var $MMmatchTablenames = '';
/**
* Extra table where clause
*/
var $MMtableWhere = '';
/**
* If true this is a foregin select and uid's needs to be swapped
*/
var $MMswapLocalForeign = false;
/**
* Initialization of the class.
*
* Known parameter from TCA:
*
* MM_foreign_select
* MM_table_where
* MM_insert_fields
* prepend_tname
* allowed
*
* @param string Name of the local table
* @param array Configuration array coming from TCA.
* @return void
*/
function init($thisTable, $MMconf) {
// the local table name, needed for foreign select
$this->MMlocalTable = $thisTable;
// If set, uid_local and uid_foreign field names will be swapped
$this->MMswapLocalForeign = $MMconf['MM_foreign_select'];
// extra MM table where
$this->MMtableWhere = $MMconf['MM_table_where'];
// array of fields value pairs that should match while SELECT and will be written into MM table
$this->MMmatchFields = is_array($MMconf['MM_match_fields']) ? $MMconf['MM_match_fields'] : array();
// array of fields and value pairs used for insert in MM table
$this->MMinsertFields = is_array($MMconf['MM_insert_fields']) ? $MMconf['MM_insert_fields'] : $this->MMmatchFields;
// the "tablenames" field will be queried with this table name list if needed.
$this->MMmatchTablenames = '';
if ($MMconf['prepend_tname'] && $MMconf['MM_foreign_select']) {
$this->MMmatchTablenames = $this->MMlocalTable;
}
}
/**
* Start of the MM handling. Reads relations.
*
* @param string List of group/select items. Possible format: tablename_uid,tablename2_uid
* @param string Comma list of allowed tables, first table takes priority if no table is set for an entry in the item list.
* @param string List of group/select items
* @param string Comma list of tables, first table takes priority if no table is set for an entry in the list.
* @param string Name of a MM table.
* @param integer Local UID for MM lookup
* @return void
*/
function start($itemlist, $tablelist, $MMtable='', $MMuid=0) {
function start($itemlist,$tablelist, $MMtable='',$MMuid=0) {
// If the table list is "*" then all tables are used in the list:
if (!strcmp(trim($tablelist),'*')) {
$tablelist = implode(',',array_keys($GLOBALS['TCA']));
} else {
// add MMmatchTablenames which might be the local table for foreign access
$tablelist = $tablelist.','.$this->MMmatchTablenames;
$tablelist = implode(',', array_unique(t3lib_div::trimExplode(',',$tablelist,1)));
}
// The tables are traversed and internal arrays are initialized:
$tempTableArray = t3lib_div::trimExplode(',',$tablelist,1);
foreach($tempTableArray as $key => $val) {
$tName = trim($val);
$this->tableArray[$tName] = array();
$this->tableArray[$tName] = Array();
if ($this->checkIfDeleted && $GLOBALS['TCA'][$tName]['ctrl']['delete']) {
$fieldN = $tName.'.'.$GLOBALS['TCA'][$tName]['ctrl']['delete'];
$this->additionalWhere[$tName].=' AND NOT '.$fieldN;
$this->additionalWhere[$tName].=' AND '.$fieldN.'=0';
}
}
......
next($this->tableArray);
$this->secondTable = key($this->tableArray); // If the second table is set and the ID number is less than zero (later) then the record is regarded to come from the second table...
$this->itemArray = array();
// Now, populate the internal itemArray and tableArray arrays:
if ($MMtable) { // If MM, then call this function to do that:
// used here to get defVals when set - [tablename]_[id]
if($itemlist) {
$this->readList($itemlist);
// remap submitted defVals from form to local table if in foreign mode
if ($this->MMswapLocalForeign && $this->MMlocalTable) {
foreach ($this->itemArray as $key => $val) {
$this->itemArray[$key]['table_relation'] = $this->MMlocalTable; // is the current table
}
}
}
$this->readMM($MMtable, $MMuid, $this->MMmatchTablenames);
$this->readMM($MMtable,$MMuid);
} else {
// If not MM, then explode the itemlist by "," and traverse the list:
$this->readList($itemlist);
......
// Get ID as the right value:
$theID = $this->secondTable ? abs(intval($theID)) : intval($theID);
// Register ID/table name in internal arrays:
$itemKey = $theTable.'_'.$theID;
$this->itemArray[$itemKey]['id'] = $theID;
$this->itemArray[$itemKey]['table'] = $theTable;
$this->tableArray[$theTable][$theID] = $theID;
$this->itemArray[$key]['id'] = $theID;
$this->itemArray[$key]['table'] = $theTable;
$this->tableArray[$theTable][] = $theID;
// Set update-flag:
$isSet=1;
}
......
*
* @param string MM Tablename
* @param integer Local UID
* @param string The "tablenames" field will be queried with this table name list
* @return void
*/
function readMM($tableName, $uid, $tableList='') {
$key=count($this->itemArray);
if ($this->MMswapLocalForeign) {
$uid_foreign = 'uid_local';
$uid_local = 'uid_foreign';
} else {
$uid_local = 'uid_local';
$uid_foreign = 'uid_foreign';
}
$where = $uid_local.'='.intval($uid);
if ($this->MMtableWhere) {
$where.= "\n".str_replace('###THIS_UID###', intval($uid), $this->MMtableWhere);
}
foreach ($this->MMmatchFields as $field => $value) {
$where.= ' AND '.$field.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($value, $tableName);
}
if ($tableList) {
$tableArr = t3lib_div::trimExplode(',', $tableList, 1);
$whereArr = array();
foreach($tableArr as $foreignTable) {
$whereArr[] = 'tablenames='.$GLOBALS['TYPO3_DB']->fullQuoteStr($foreignTable, $tableName);
}
$where.= ' AND ( '.implode(' OR ', $whereArr).' ) ';
}
function readMM($tableName,$uid) {
$key=0;
// Select all MM relations:
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $tableName, $where, '', 'sorting');
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $tableName, 'uid_local='.intval($uid), '', 'sorting');
while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
// in foreign_select mode firstTable is the right table - tablenames should then be the current table
$theTable = ($row['tablenames'] AND !$this->MMswapLocalForeign) ? $row['tablenames'] : $this->firstTable;
$itemKey = $key;
if (($row[$uid_foreign] || $theTable=='pages') && $theTable && isset($this->tableArray[$theTable])) {
$itemKey = $theTable.'_'.$row[$uid_foreign];
$this->itemArray[$itemKey]['id'] = $row[$uid_foreign];
$this->itemArray[$itemKey]['table'] = $theTable;
$this->itemArray[$itemKey]['table_relation'] = $row['tablenames'];
$this->tableArray[$theTable][$row[$uid_foreign]]= $row[$uid_foreign];
$theTable = $row['tablenames'] ? $row['tablenames'] : $this->firstTable; // If tablesnames columns exists and contain a name, then this value is the table, else it's the firstTable...
if (($row['uid_foreign'] || $theTable=='pages') && $theTable && isset($this->tableArray[$theTable])) {
$this->itemArray[$key]['id'] = $row['uid_foreign'];
$this->itemArray[$key]['table'] = $theTable;
$this->tableArray[$theTable][]= $row['uid_foreign'];
} elseif ($this->registerNonTableValues) {
$this->itemArray[$itemKey]['id'] = $row[$uid_foreign];
$this->itemArray[$itemKey]['table'] = '_NO_TABLE';
$this->itemArray[$itemKey]['table_relation'] = '_NO_TABLE';
$this->nonTableArray[] = $row[$uid_foreign];
$this->itemArray[$key]['id'] = $row['uid_foreign'];
$this->itemArray[$key]['table'] = '_NO_TABLE';
$this->nonTableArray[] = $row['uid_foreign'];
}
$this->itemArray[$itemKey]['foreign_select'] = $this->MMswapLocalForeign;
$key++;
}
$GLOBALS['TYPO3_DB']->sql_free_result($res);
}
......
* @param boolean If set, then table names will always be written.
* @return void
*/
function writeMM($tableName, $uid, $prependTableName=0) {
function writeMM($tableName,$uid,$prependTableName=0) {
if ($this->MMswapLocalForeign) {
$uid_foreign = 'uid_local';
$uid_local = 'uid_foreign';
} else {
$uid_local = 'uid_local';
$uid_foreign = 'uid_foreign';
}
// Delete all relations:
$GLOBALS['TYPO3_DB']->exec_DELETEquery($tableName, 'uid_local='.intval($uid));
// If there are tables...
$tableC = count($this->tableArray);
$prep = ($tableC>1||$prependTableName) ? 1 : 0;
// delete all relations with local uid
$where = $uid_local.'='.intval($uid);
// add WHERE clause if configured
if ($this->MMtableWhere) {
$where.= "\n".str_replace('###THIS_UID###', intval($uid), $this->MMtableWhere);
}
// delete those relations that match the configured fields
foreach ($this->MMmatchFields as $field => $value) {
$where.= ' AND '.$field.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($value, $tableName);
}
// if prepend table name is configured the 'tablenames' have to match those
if ($prep) {
$whereArr = array();
foreach($this->tableArray as $matchTable => $dummy) {
$whereArr[] = 'tablenames='.$GLOBALS['TYPO3_DB']->fullQuoteStr($matchTable, $tableName);
}
$where.= ' AND ( '.implode(' OR ', $whereArr).' ) ';
}
// Delete all relations:
$GLOBALS['TYPO3_DB']->exec_DELETEquery($tableName, $where);
if ($tableC) {
// For each item, insert it:
$prep = ($tableC>1||$prependTableName) ? 1 : 0;
$c=0;
$tName=array();
// For each item, insert it:
foreach($this->itemArray as $val) {
$c++;
$insertFields = $this->MMinsertFields;
$insertFields[$uid_local] = $uid;
$insertFields[$uid_foreign] = $val['id'];
$insertFields['sorting'] = $c;
$insertFields = array(
'uid_local' => $uid,
'uid_foreign' => $val['id'],
'sorting' => $c
);
if ($prep || $val['table']=='_NO_TABLE') {
$insertFields['tablenames'] = $val['table_relation'] ? $val['table_relation'] : $val['table'];
$insertFields['tablenames'] = $val['table'];
}
$GLOBALS['TYPO3_DB']->exec_INSERTquery($tableName, $insertFields);
......
* @param boolean If set, then table names will ALWAYS be prepended (unless its a _NO_TABLE value)
* @return array A numeric array.
*/
function getValueArray($prependTableName=false) {
function getValueArray($prependTableName='') {
// INIT:
$valueArray=array();
$valueArray=Array();
$tableC = count($this->tableArray);
// If there are tables in the table array:
......
*/
function getFromDB() {
// Traverses the tables listed:
foreach($this->tableArray as $table => $val) {
foreach($this->tableArray as $key => $val) {
if (is_array($val)) {
$itemList = implode(',',$val);
if ($itemList) {
$from = '*';
if ($this->fromTC) {
$from = 'uid,pid';
if ($GLOBALS['TCA'][$table]['ctrl']['label']) {
$from.= ','.$GLOBALS['TCA'][$table]['ctrl']['label']; // Title
if ($GLOBALS['TCA'][$key]['ctrl']['label']) {
$from.= ','.$GLOBALS['TCA'][$key]['ctrl']['label']; // Titel
}
if ($GLOBALS['TCA'][$table]['ctrl']['thumbnail']) {
$from.= ','.$GLOBALS['TCA'][$table]['ctrl']['thumbnail']; // Thumbnail
if ($GLOBALS['TCA'][$key]['ctrl']['thumbnail']) {
$from.= ','.$GLOBALS['TCA'][$key]['ctrl']['thumbnail']; // Thumbnail
}
}
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($from, $table, 'uid IN ('.$itemList.')'.$this->additionalWhere[$table]);
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($from, $key, 'uid IN ('.$itemList.')'.$this->additionalWhere[$key]);
while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
$this->results[$table][$row['uid']]=$row;
$this->results[$key][$row['uid']]=$row;
}
}
}
TYPO3core/t3lib/class.t3lib_tcemain.php 2006-03-21 19:48:23.000000000 +0100
);
break;
case 'db':
$valueArray = $this->checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,'group',$PP);
$valueArray = $this->checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,'group');
break;
}
}
// For select types which has a foreign table attached:
if ($tcaFieldConf['type']=='select' && $tcaFieldConf['foreign_table']) {
$valueArray = $this->checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,'select',$PP);
$valueArray = $this->checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,'select');
}
// BTW, checking for min and max items here does NOT make any sense when MM is used because the above function calls will just return an array with a single item (the count) if MM is used... Why didn't I perform the check before? Probably because we could not evaluate the validity of record uids etc... Hmm...
......
* @param integer Record id, used for look-up of MM relations (local_uid)
* @param string Status string ('update' or 'new')
* @param string The type, either 'select' or 'group'
* @param array Additional parameters in a numeric array: $table,$id,$curValue,$status,$realPid,$recFID
* @return array Modified value array
*/
function checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,$type,$PP) {
list($table,$id,$curValue,$status,$realPid,$recFID) = $PP;
function checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,$type) {
$tables = $type=='group'?$tcaFieldConf['allowed']:$tcaFieldConf['foreign_table'].','.$tcaFieldConf['neg_foreign_table'];
$prep = $type=='group'?$tcaFieldConf['prepend_tname']:$tcaFieldConf['neg_foreign_table'];
$dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
$dbAnalysis->registerNonTableValues=$tcaFieldConf['allowNonIdValues'] ? 1 : 0;
$dbAnalysis->start(implode(',',$valueArray),$tables);
if ($tcaFieldConf['MM']) {
$dbAnalysis->init($table, $tcaFieldConf);
$dbAnalysis->start(implode(',',$valueArray),$tables,$tcaFieldConf['MM']);
if ($status=='update') {
$dbAnalysis->writeMM($tcaFieldConf['MM'],$id,$prep);
} else {
$this->dbAnalysisStore[] = array($dbAnalysis,$tcaFieldConf['MM'],$id,$prep); // This will be traversed later to execute the actions
}
$cc=count($dbAnalysis->itemArray);
$valueArray = array($cc);
} else {
$dbAnalysis->start(implode(',',$valueArray),$tables);
$valueArray = $dbAnalysis->getValueArray($prep);
if ($type=='select' && $prep) {
$valueArray = $dbAnalysis->convertPosNeg($valueArray,$tcaFieldConf['foreign_table'],$tcaFieldConf['neg_foreign_table']);
......
$prependName = $conf['type']=='group' ? $conf['prepend_tname'] : $conf['neg_foreign_table'];
if ($conf['MM']) {
$dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
$dbAnalysis->init($table, $conf);
$dbAnalysis->start('',$allowedTables,$conf['MM'],$uid);
$value = implode(',',$dbAnalysis->getValueArray($prependName));
}
......
*/
function removeCacheFiles() {
return t3lib_extMgm::removeCacheFiles();
}
}
/**
* Returns array, $CPtable, of pages under the $pid going down to $counter levels.
TYPO3core/t3lib/class.t3lib_transferdata.php 2006-03-21 19:44:07.000000000 +0100
break;
case 'db':
$loadDB = t3lib_div::makeInstance('t3lib_loadDBGroup');
$loadDB->init($table, $fieldConfig['config']);
$loadDB->start($data, $fieldConfig['config']['allowed'], $fieldConfig['config']['MM'], $row['uid']);
$loadDB->getFromDB();
$data = $loadDB->readyForInterface();
break;
    (1-1/1)