revert_mm_complete.diff

Administrator Admin, 2006-03-21 20:13

Download (19.4 KB)

View differences:

TYPO3core/t3lib/class.t3lib_loaddbgroup.php 2006-03-21 19:54:16.000000000 +0100
70 70
 * @subpackage t3lib
71 71
 */
72 72
class t3lib_loadDBGroup	{
73

  
74
	/**
75
	 * Means that only uid and the label-field is returned
76
	 */
77
	var $fromTC = true;
78

  
79
	/**
80
	 * Will exclude deleted records.
81
	 * deleted-column is added to $additionalWhere...
82
	 */
83
	var $checkIfDeleted = true;
84

  
85
	/**
86
	 * If set, values that are not ids in tables are normally discarded.
87
	 * By this options they will be preserved and stored in $itemArray.
88
	 */
89
	var $registerNonTableValues = false;
90

  
91
	/**
92
	 * Usded to collect NON-table elements if registerNonTableValues = true
93
	 */
94
	var $nonTableArray = array();
95

  
96

  
97

  
98
	/**
99
	 * Contains the table names htat are allowed for the relation as keys.
100
	 * The values are the id-values for each table. Should ONLY contain proper table names.
101
	 */
102
	var $tableArray = array();
103

  
104
	/**
105
	 * Contains items in an numeric array (table/id for each). Tablenames here might be "_NO_TABLE"
106
	 */
107
	var $itemArray = array();
108

  
109

  
110

  
111
	var $additionalWhere = array();		// used for additional where clause like "AND table.delete=0"
73
		// External, static:
74
	var $fromTC = 1;					// Means that only uid and the label-field is returned
75
	var $registerNonTableValues=0;		// If set, values that are not ids in tables are normally discarded. By this options they will be preserved.
76

  
77
		// Internal, dynamic:
78
	var $tableArray=Array();			// Contains the table names as keys. The values are the id-values for each table. Should ONLY contain proper table names.
79
	var $itemArray=Array();				// Contains items in an numeric array (table/id for each). Tablenames here might be "_NO_TABLE"
80
	var $nonTableArray=array();			// Array for NON-table elements
81
	var $additionalWhere=array();
82
	var $checkIfDeleted = 1;			// deleted-column is added to additionalWhere... if this is set...
83
	var $dbPaths=Array();
112 84
	var $firstTable = '';				// Will contain the first table name in the $tablelist (for positive ids)
113 85
	var $secondTable = '';				// Will contain the second table name in the $tablelist (for negative ids)
114 86

  
115 87

  
116 88

  
117
	// MM stuff for foreign select
118

  
119
	/**
120
	 * field value pairs that should match while select and to be set while insert
121
	 */
122
	var $MMmatchFields = array();
123

  
124
	/**
125
	 * Extra field value pairs that should be set while insert
126
	 */
127
	var $MMinsertFields = array();
128

  
129
	/**
130
	 * The name of the local table. Needed for foreign select
131
	 */
132
	var $MMlocalTable = '';
133

  
134
	/**
135
	 * The tablenames field should match this list
136
	 */
137
	var $MMmatchTablenames = '';
138

  
139
	/**
140
	 * Extra table where clause
141
	 */
142
	var $MMtableWhere = '';
143

  
144
	/**
145
	 * If true this is a foregin select and uid's needs to be swapped
146
	 */
147
	var $MMswapLocalForeign = false;
148

  
149

  
150

  
151 89

  
152 90
	/**
153 91
	 * Initialization of the class.
154 92
	 *
155
	 * Known parameter from TCA:
156
	 *
157
	 * MM_foreign_select
158
	 * MM_table_where
159
	 * MM_insert_fields
160
	 * prepend_tname
161
	 * allowed
162
	 *
163
	 * @param 	string	 	Name of the local table
164
	 * @param	array		Configuration array coming from TCA.
165
	 * @return	void
166
	 */
167
	function init($thisTable, $MMconf) {
168

  
169
			// the local table name, needed for foreign select
170
		$this->MMlocalTable = $thisTable;
171

  
172
			// If set, uid_local and uid_foreign field names will be swapped
173
		$this->MMswapLocalForeign = $MMconf['MM_foreign_select'];
174

  
175
			// extra MM table where
176
		$this->MMtableWhere = $MMconf['MM_table_where'];
177

  
178
			// array of fields value pairs that should match while SELECT and will be written into MM table
179
		$this->MMmatchFields = is_array($MMconf['MM_match_fields']) ? $MMconf['MM_match_fields'] : array();
180

  
181
			// array of fields and value pairs used for insert in MM table
182
		$this->MMinsertFields = is_array($MMconf['MM_insert_fields']) ? $MMconf['MM_insert_fields'] : $this->MMmatchFields;
183

  
184
	 		// the "tablenames" field will be queried with this table name list if needed.
185
		$this->MMmatchTablenames = '';
186
		if ($MMconf['prepend_tname'] && $MMconf['MM_foreign_select']) {
187
			$this->MMmatchTablenames = $this->MMlocalTable;
188
		}
189
	}
190

  
191

  
192
	/**
193
	 * Start of the MM handling. Reads relations.
194
	 *
195
	 * @param	string		List of group/select items. Possible format: tablename_uid,tablename2_uid
196
	 * @param	string		Comma list of allowed tables, first table takes priority if no table is set for an entry in the item list.
93
	 * @param	string		List of group/select items
94
	 * @param	string		Comma list of tables, first table takes priority if no table is set for an entry in the list.
197 95
	 * @param	string		Name of a MM table.
198 96
	 * @param	integer		Local UID for MM lookup
199 97
	 * @return	void
200 98
	 */
201
	function start($itemlist, $tablelist, $MMtable='', $MMuid=0)	{
202

  
99
	function start($itemlist,$tablelist, $MMtable='',$MMuid=0)	{
203 100
			// If the table list is "*" then all tables are used in the list:
204 101
		if (!strcmp(trim($tablelist),'*'))	{
205 102
			$tablelist = implode(',',array_keys($GLOBALS['TCA']));
206
		} else {
207
				// add MMmatchTablenames which might be the local table for foreign access
208
			$tablelist = $tablelist.','.$this->MMmatchTablenames;
209
			$tablelist = implode(',', array_unique(t3lib_div::trimExplode(',',$tablelist,1)));
210 103
		}
211 104

  
212 105
			// The tables are traversed and internal arrays are initialized:
213 106
		$tempTableArray = t3lib_div::trimExplode(',',$tablelist,1);
214 107
		foreach($tempTableArray as $key => $val)	{
215 108
			$tName = trim($val);
216
			$this->tableArray[$tName] = array();
109
			$this->tableArray[$tName] = Array();
217 110
			if ($this->checkIfDeleted && $GLOBALS['TCA'][$tName]['ctrl']['delete'])	{
218 111
				$fieldN = $tName.'.'.$GLOBALS['TCA'][$tName]['ctrl']['delete'];
219
				$this->additionalWhere[$tName].=' AND NOT '.$fieldN;
112
				$this->additionalWhere[$tName].=' AND '.$fieldN.'=0';
220 113
			}
221 114
		}
222 115

  
......
229 122
		next($this->tableArray);
230 123
		$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...
231 124

  
232
		$this->itemArray = array();
233

  
234 125
			// Now, populate the internal itemArray and tableArray arrays:
235 126
		if ($MMtable)	{	// If MM, then call this function to do that:
236

  
237
				// used here to get defVals when set - [tablename]_[id]
238
			if($itemlist) {
239
				$this->readList($itemlist);
240

  
241
					// remap submitted defVals from form to local table if in foreign mode
242
				if ($this->MMswapLocalForeign && $this->MMlocalTable) {
243
					foreach ($this->itemArray as $key => $val) {
244
						$this->itemArray[$key]['table_relation'] = $this->MMlocalTable; // is the current table
245
					}
246
				}
247
			}
248

  
249
			$this->readMM($MMtable, $MMuid, $this->MMmatchTablenames);
250

  
127
			$this->readMM($MMtable,$MMuid);
251 128
		} else {
252 129
				// If not MM, then explode the itemlist by "," and traverse the list:
253 130
			$this->readList($itemlist);
......
280 157
							// Get ID as the right value:
281 158
						$theID = $this->secondTable ? abs(intval($theID)) : intval($theID);
282 159
							// Register ID/table name in internal arrays:
283
						$itemKey = $theTable.'_'.$theID;
284
						$this->itemArray[$itemKey]['id'] = $theID;
285
						$this->itemArray[$itemKey]['table'] = $theTable;
286
						$this->tableArray[$theTable][$theID] = $theID;
160
						$this->itemArray[$key]['id'] = $theID;
161
						$this->itemArray[$key]['table'] = $theTable;
162
						$this->tableArray[$theTable][] = $theID;
287 163
							// Set update-flag:
288 164
						$isSet=1;
289 165
					}
......
305 181
	 *
306 182
	 * @param	string		MM Tablename
307 183
	 * @param	integer		Local UID
308
	 * @param	string		The "tablenames" field will be queried with this table name list
309 184
	 * @return	void
310 185
	 */
311
	function readMM($tableName, $uid, $tableList='')	{
312
		$key=count($this->itemArray);
313

  
314
		if ($this->MMswapLocalForeign) {
315
			$uid_foreign = 'uid_local';
316
			$uid_local = 'uid_foreign';
317
		} else {
318
			$uid_local = 'uid_local';
319
			$uid_foreign = 'uid_foreign';
320
		}
321

  
322
		$where = $uid_local.'='.intval($uid);
323
		if ($this->MMtableWhere) {
324
			$where.= "\n".str_replace('###THIS_UID###', intval($uid), $this->MMtableWhere);
325
		}
326
		foreach ($this->MMmatchFields as $field => $value) {
327
			$where.= ' AND '.$field.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($value, $tableName);
328
		}
329
		if ($tableList) {
330
			$tableArr = t3lib_div::trimExplode(',', $tableList, 1);
331
			$whereArr = array();
332
			foreach($tableArr as $foreignTable) {
333
				$whereArr[] = 'tablenames='.$GLOBALS['TYPO3_DB']->fullQuoteStr($foreignTable, $tableName);
334
			}
335
			$where.= ' AND ( '.implode(' OR ', $whereArr).' ) ';
336
		}
186
	function readMM($tableName,$uid)	{
187
		$key=0;
337 188

  
338 189
			// Select all MM relations:
339
		$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $tableName, $where, '', 'sorting');
190
		$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $tableName, 'uid_local='.intval($uid), '', 'sorting');
340 191
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))	{
341

  
342
				// in foreign_select mode firstTable is the right table - tablenames should then be the current table
343
			$theTable = ($row['tablenames'] AND !$this->MMswapLocalForeign) ? $row['tablenames'] : $this->firstTable;
344
			$itemKey = $key;
345
			if (($row[$uid_foreign] || $theTable=='pages') && $theTable && isset($this->tableArray[$theTable]))	{
346
				$itemKey = $theTable.'_'.$row[$uid_foreign];
347
				$this->itemArray[$itemKey]['id'] = $row[$uid_foreign];
348
				$this->itemArray[$itemKey]['table'] = $theTable;
349
				$this->itemArray[$itemKey]['table_relation'] = $row['tablenames'];
350
				$this->tableArray[$theTable][$row[$uid_foreign]]= $row[$uid_foreign];
192
			$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...
193
			if (($row['uid_foreign'] || $theTable=='pages') && $theTable && isset($this->tableArray[$theTable]))	{
194
				$this->itemArray[$key]['id'] = $row['uid_foreign'];
195
				$this->itemArray[$key]['table'] = $theTable;
196
				$this->tableArray[$theTable][]= $row['uid_foreign'];
351 197
			} elseif ($this->registerNonTableValues)	{
352
				$this->itemArray[$itemKey]['id'] = $row[$uid_foreign];
353
				$this->itemArray[$itemKey]['table'] = '_NO_TABLE';
354
				$this->itemArray[$itemKey]['table_relation'] = '_NO_TABLE';
355
				$this->nonTableArray[] = $row[$uid_foreign];
198
				$this->itemArray[$key]['id'] = $row['uid_foreign'];
199
				$this->itemArray[$key]['table'] = '_NO_TABLE';
200
				$this->nonTableArray[] = $row['uid_foreign'];
356 201
			}
357
			$this->itemArray[$itemKey]['foreign_select'] = $this->MMswapLocalForeign;
358 202
			$key++;
359 203
		}
360

  
361 204
		$GLOBALS['TYPO3_DB']->sql_free_result($res);
362 205
	}
363 206

  
......
369 212
	 * @param	boolean		If set, then table names will always be written.
370 213
	 * @return	void
371 214
	 */
372
	function writeMM($tableName, $uid, $prependTableName=0)	{
215
	function writeMM($tableName,$uid,$prependTableName=0)	{
373 216

  
374
		if ($this->MMswapLocalForeign) {
375
			$uid_foreign = 'uid_local';
376
			$uid_local = 'uid_foreign';
377
		} else {
378
			$uid_local = 'uid_local';
379
			$uid_foreign = 'uid_foreign';
380
		}
217
			// Delete all relations:
218
		$GLOBALS['TYPO3_DB']->exec_DELETEquery($tableName, 'uid_local='.intval($uid));
381 219

  
382 220
			// If there are tables...
383 221
		$tableC = count($this->tableArray);
384
		$prep = ($tableC>1||$prependTableName) ? 1 : 0;
385

  
386
			// delete all relations with local uid
387
		$where = $uid_local.'='.intval($uid);
388

  
389
			// add WHERE clause if configured
390
		if ($this->MMtableWhere) {
391
			$where.= "\n".str_replace('###THIS_UID###', intval($uid), $this->MMtableWhere);
392
		}
393
			// delete those relations that match the configured fields
394
		foreach ($this->MMmatchFields as $field => $value) {
395
			$where.= ' AND '.$field.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($value, $tableName);
396
		}
397

  
398
			// if prepend table name is configured the 'tablenames' have to match those
399
		if ($prep) {
400
			$whereArr = array();
401
			foreach($this->tableArray as $matchTable => $dummy) {
402
				$whereArr[] = 'tablenames='.$GLOBALS['TYPO3_DB']->fullQuoteStr($matchTable, $tableName);
403
			}
404
			$where.= ' AND ( '.implode(' OR ', $whereArr).' ) ';
405
		}
406

  
407

  
408
			// Delete all relations:
409
		$GLOBALS['TYPO3_DB']->exec_DELETEquery($tableName, $where);
410

  
411

  
412

  
413 222
		if ($tableC)	{
414
				// For each item, insert it:
223
			$prep = ($tableC>1||$prependTableName) ? 1 : 0;
415 224
			$c=0;
225
			$tName=array();
226

  
227
				// For each item, insert it:
416 228
			foreach($this->itemArray as $val)	{
417 229
				$c++;
418 230

  
419
				$insertFields = $this->MMinsertFields;
420
				$insertFields[$uid_local] = $uid;
421
				$insertFields[$uid_foreign] = $val['id'];
422
				$insertFields['sorting'] = $c;
423

  
231
				$insertFields = array(
232
					'uid_local' => $uid,
233
					'uid_foreign' => $val['id'],
234
					'sorting' => $c
235
				);
424 236
				if ($prep || $val['table']=='_NO_TABLE')	{
425
					$insertFields['tablenames'] = $val['table_relation'] ? $val['table_relation'] : $val['table'];
237
					$insertFields['tablenames'] = $val['table'];
426 238
				}
427 239

  
428 240
				$GLOBALS['TYPO3_DB']->exec_INSERTquery($tableName, $insertFields);
......
436 248
	 * @param	boolean		If set, then table names will ALWAYS be prepended (unless its a _NO_TABLE value)
437 249
	 * @return	array		A numeric array.
438 250
	 */
439
	function getValueArray($prependTableName=false)	{
251
	function getValueArray($prependTableName='')	{
440 252
			// INIT:
441
		$valueArray=array();
253
		$valueArray=Array();
442 254
		$tableC = count($this->tableArray);
443 255

  
444 256
			// If there are tables in the table array:
......
488 300
	 */
489 301
	function getFromDB()	{
490 302
			// Traverses the tables listed:
491
		foreach($this->tableArray as $table => $val)	{
303
		foreach($this->tableArray as $key => $val)	{
492 304
			if (is_array($val))	{
493 305
				$itemList = implode(',',$val);
494 306
				if ($itemList)	{
495 307
					$from = '*';
496 308
					if ($this->fromTC)	{
497 309
						$from = 'uid,pid';
498
						if ($GLOBALS['TCA'][$table]['ctrl']['label'])	{
499
							$from.= ','.$GLOBALS['TCA'][$table]['ctrl']['label'];	// Title
310
						if ($GLOBALS['TCA'][$key]['ctrl']['label'])	{
311
							$from.= ','.$GLOBALS['TCA'][$key]['ctrl']['label'];	// Titel
500 312
						}
501
						if ($GLOBALS['TCA'][$table]['ctrl']['thumbnail'])	{
502
							$from.= ','.$GLOBALS['TCA'][$table]['ctrl']['thumbnail'];	// Thumbnail
313
						if ($GLOBALS['TCA'][$key]['ctrl']['thumbnail'])	{
314
							$from.= ','.$GLOBALS['TCA'][$key]['ctrl']['thumbnail'];	// Thumbnail
503 315
						}
504 316
					}
505
					$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($from, $table, 'uid IN ('.$itemList.')'.$this->additionalWhere[$table]);
317
					$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($from, $key, 'uid IN ('.$itemList.')'.$this->additionalWhere[$key]);
506 318
					while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))	{
507
						$this->results[$table][$row['uid']]=$row;
319
						$this->results[$key][$row['uid']]=$row;
508 320
					}
509 321
				}
510 322
			}
TYPO3core/t3lib/class.t3lib_tcemain.php 2006-03-21 19:48:23.000000000 +0100
1315 1315
					);
1316 1316
				break;
1317 1317
				case 'db':
1318
					$valueArray = $this->checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,'group',$PP);
1318
					$valueArray = $this->checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,'group');
1319 1319
				break;
1320 1320
			}
1321 1321
		}
1322 1322
			// For select types which has a foreign table attached:
1323 1323
		if ($tcaFieldConf['type']=='select' && $tcaFieldConf['foreign_table'])	{
1324
			$valueArray = $this->checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,'select',$PP);
1324
			$valueArray = $this->checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,'select');
1325 1325
		}
1326 1326

  
1327 1327
// 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...
......
1832 1832
	 * @param	integer		Record id, used for look-up of MM relations (local_uid)
1833 1833
	 * @param	string		Status string ('update' or 'new')
1834 1834
	 * @param	string		The type, either 'select' or 'group'
1835
	 * @param	array		Additional parameters in a numeric array: $table,$id,$curValue,$status,$realPid,$recFID
1836 1835
	 * @return	array		Modified value array
1837 1836
	 */
1838
	function checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,$type,$PP)	{
1839

  
1840
		list($table,$id,$curValue,$status,$realPid,$recFID) = $PP;
1841

  
1837
	function checkValue_group_select_processDBdata($valueArray,$tcaFieldConf,$id,$status,$type)	{
1842 1838
		$tables = $type=='group'?$tcaFieldConf['allowed']:$tcaFieldConf['foreign_table'].','.$tcaFieldConf['neg_foreign_table'];
1843 1839
		$prep = $type=='group'?$tcaFieldConf['prepend_tname']:$tcaFieldConf['neg_foreign_table'];
1844 1840

  
1845 1841
		$dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
1846 1842
		$dbAnalysis->registerNonTableValues=$tcaFieldConf['allowNonIdValues'] ? 1 : 0;
1843
		$dbAnalysis->start(implode(',',$valueArray),$tables);
1847 1844

  
1848 1845
		if ($tcaFieldConf['MM'])	{
1849

  
1850
			$dbAnalysis->init($table, $tcaFieldConf);
1851
			$dbAnalysis->start(implode(',',$valueArray),$tables,$tcaFieldConf['MM']);
1852

  
1853 1846
			if ($status=='update')	{
1854 1847
				$dbAnalysis->writeMM($tcaFieldConf['MM'],$id,$prep);
1855 1848
			} else {
1856 1849
				$this->dbAnalysisStore[] = array($dbAnalysis,$tcaFieldConf['MM'],$id,$prep);	// This will be traversed later to execute the actions
1857 1850
			}
1858

  
1859 1851
			$cc=count($dbAnalysis->itemArray);
1860 1852
			$valueArray = array($cc);
1861

  
1862 1853
		} else {
1863
			$dbAnalysis->start(implode(',',$valueArray),$tables);
1864 1854
			$valueArray = $dbAnalysis->getValueArray($prep);
1865 1855
			if ($type=='select' && $prep)	{
1866 1856
				$valueArray = $dbAnalysis->convertPosNeg($valueArray,$tcaFieldConf['foreign_table'],$tcaFieldConf['neg_foreign_table']);
......
2588 2578
			$prependName = $conf['type']=='group' ? $conf['prepend_tname'] : $conf['neg_foreign_table'];
2589 2579
			if ($conf['MM'])	{
2590 2580
				$dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
2591
				$dbAnalysis->init($table, $conf);
2592 2581
				$dbAnalysis->start('',$allowedTables,$conf['MM'],$uid);
2593 2582
				$value = implode(',',$dbAnalysis->getValueArray($prependName));
2594 2583
			}
......
4947 4935
	 */
4948 4936
	function removeCacheFiles()	{
4949 4937
		return t3lib_extMgm::removeCacheFiles();
4950
			}
4938
	}
4951 4939

  
4952 4940
	/**
4953 4941
	 * 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
382 382
			break;
383 383
			case 'db':
384 384
				$loadDB = t3lib_div::makeInstance('t3lib_loadDBGroup');
385
				$loadDB->init($table, $fieldConfig['config']);
386 385
				$loadDB->start($data, $fieldConfig['config']['allowed'], $fieldConfig['config']['MM'], $row['uid']);
387

  
388 386
				$loadDB->getFromDB();
389 387
				$data = $loadDB->readyForInterface();
390 388
			break;