00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00076 class t3lib_loadDBGroup {
00077
00078 var $fromTC = 1;
00079 var $registerNonTableValues=0;
00080
00081
00082 var $tableArray=Array();
00083 var $itemArray=Array();
00084 var $nonTableArray=array();
00085 var $additionalWhere=array();
00086 var $checkIfDeleted = 1;
00087 var $dbPaths=Array();
00088 var $firstTable = '';
00089 var $secondTable = '';
00090
00091 var $MM_is_foreign = 0;
00092 var $MM_oppositeField = '';
00093 var $MM_oppositeTable = '';
00094 var $MM_oppositeFieldConf = '';
00095 var $MM_isMultiTableRelationship = 0;
00096 var $currentTable;
00097 var $undeleteRecord;
00098
00099
00100 var $MM_match_fields = array();
00101 var $MM_insert_fields = array();
00102 var $MM_table_where = '';
00103
00104
00116 function start($itemlist, $tablelist, $MMtable='', $MMuid=0, $currentTable='', $conf=array()) {
00117
00118 $this->MM_is_foreign = ($conf['MM_opposite_field']?1:0);
00119 $this->MM_oppositeField = $conf['MM_opposite_field'];
00120 $this->MM_table_where = $conf['MM_table_where'];
00121 $this->MM_match_fields = is_array($conf['MM_match_fields']) ? $conf['MM_match_fields'] : array();
00122 $this->MM_insert_fields = is_array($conf['MM_insert_fields']) ? $conf['MM_insert_fields'] : $this->MM_match_fields;
00123
00124 $this->currentTable = $currentTable;
00125 if ($this->MM_is_foreign) {
00126 $tmp = ($conf['type']==='group'?$conf['allowed']:$conf['foreign_table']);
00127
00128 $tmp = t3lib_div::trimExplode(',', $tmp);
00129 $this->MM_oppositeTable = $tmp[0];
00130 unset($tmp);
00131
00132
00133 $this->MM_oppositeFieldConf = $GLOBALS['TCA'][$this->MM_oppositeTable]['columns'][$this->MM_oppositeField]['config'];
00134
00135 if ($this->MM_oppositeFieldConf['allowed']) {
00136 $oppositeFieldConf_allowed = explode(',', $this->MM_oppositeFieldConf['allowed']);
00137 if (count($oppositeFieldConf_allowed) > 1) {
00138 $this->MM_isMultiTableRelationship = $oppositeFieldConf_allowed[0];
00139 }
00140 }
00141 }
00142
00143
00144
00145
00146 if (!strcmp(trim($tablelist),'*')) {
00147 $tablelist = implode(',',array_keys($GLOBALS['TCA']));
00148 }
00149
00150
00151 $tempTableArray = t3lib_div::trimExplode(',',$tablelist,1);
00152 foreach($tempTableArray as $key => $val) {
00153 $tName = trim($val);
00154 $this->tableArray[$tName] = Array();
00155 if ($this->checkIfDeleted && $GLOBALS['TCA'][$tName]['ctrl']['delete']) {
00156 $fieldN = $tName.'.'.$GLOBALS['TCA'][$tName]['ctrl']['delete'];
00157 $this->additionalWhere[$tName].=' AND '.$fieldN.'=0';
00158 }
00159 }
00160
00161 if (is_array($this->tableArray)) {
00162 reset($this->tableArray);
00163 } else {return 'No tables!';}
00164
00165
00166 $this->firstTable = key($this->tableArray);
00167 next($this->tableArray);
00168 $this->secondTable = key($this->tableArray);
00169
00170
00171 if ($MMtable) {
00172 $this->readMM($MMtable,$MMuid);
00173 } elseif ($MMuid && $conf['foreign_field']) {
00174
00175 $this->readForeignField($MMuid, $conf);
00176 } else {
00177
00178 $this->readList($itemlist);
00179
00180 if ($conf['foreign_default_sortby']) {
00181 $this->sortList($conf['foreign_default_sortby']);
00182 }
00183 }
00184 }
00185
00192 function readList($itemlist) {
00193 if ((string)trim($itemlist)!='') {
00194 $tempItemArray = t3lib_div::trimExplode(',', $itemlist);
00195 foreach($tempItemArray as $key => $val) {
00196 $isSet = 0;
00197
00198
00199 $val = strrev($val);
00200 $parts = explode('_',$val,2);
00201 $theID = strrev($parts[0]);
00202
00203
00204 if (t3lib_div::testInt($theID)) {
00205
00206 $theTable = trim($parts[1]) ? strrev(trim($parts[1])) : ($this->secondTable && $theID<0 ? $this->secondTable : $this->firstTable);
00207
00208 if ((string)$theID!='' && $theID && $theTable && isset($this->tableArray[$theTable])) {
00209
00210 $theID = $this->secondTable ? abs(intval($theID)) : intval($theID);
00211
00212 $this->itemArray[$key]['id'] = $theID;
00213 $this->itemArray[$key]['table'] = $theTable;
00214 $this->tableArray[$theTable][] = $theID;
00215
00216 $isSet=1;
00217 }
00218 }
00219
00220
00221 if (!$isSet && $this->registerNonTableValues) {
00222 $this->itemArray[$key]['id'] = $tempItemArray[$key];
00223 $this->itemArray[$key]['table'] = '_NO_TABLE';
00224 $this->nonTableArray[] = $tempItemArray[$key];
00225 }
00226 }
00227 }
00228 }
00229
00238 function sortList($sortby) {
00239
00240 if ($sortby == 'uid') {
00241 usort($this->itemArray, create_function('$a,$b', 'return $a["id"] < $b["id"] ? -1 : 1;'));
00242
00243 } elseif (count($this->tableArray) == 1) {
00244 reset($this->tableArray);
00245 $table = key($this->tableArray);
00246 $uidList = implode(',', current($this->tableArray));
00247
00248 if ($uidList) {
00249 $this->itemArray = array();
00250 $this->tableArray = array();
00251
00252 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', $table, 'uid IN ('.$uidList.')', '', $sortby);
00253 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00254 $this->itemArray[] = array('id' => $row['uid'], 'table' => $table);
00255 $this->tableArray[$table][] = $row['uid'];
00256 }
00257 $GLOBALS['TYPO3_DB']->sql_free_result($res);
00258 }
00259 }
00260 }
00261
00270 function readMM($tableName,$uid) {
00271 $key=0;
00272 $additionalWhere = '';
00273
00274 if ($this->MM_is_foreign) {
00275 $uidLocal_field = 'uid_foreign';
00276 $uidForeign_field = 'uid_local';
00277 $sorting_field = 'sorting_foreign';
00278
00279 if ($this->MM_isMultiTableRelationship) {
00280 $additionalWhere .= ' AND ( tablenames="'.$this->currentTable.'"';
00281 if ($this->currentTable == $this->MM_isMultiTableRelationship) {
00282 $additionalWhere .= ' OR tablenames=""';
00283 }
00284 $additionalWhere .= ' ) ';
00285 }
00286 $theTable = $this->MM_oppositeTable;
00287 } else {
00288 $uidLocal_field = 'uid_local';
00289 $uidForeign_field = 'uid_foreign';
00290 $sorting_field = 'sorting';
00291 }
00292
00293
00294 if ($this->MM_table_where) {
00295 $additionalWhere.= "\n".str_replace('###THIS_UID###', intval($uid), $this->MM_table_where);
00296 }
00297 foreach ($this->MM_match_fields as $field => $value) {
00298 $additionalWhere.= ' AND '.$field.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($value, $tableName);
00299 }
00300
00301
00302 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $tableName, $uidLocal_field.'='.intval($uid).$additionalWhere, '', $sorting_field);
00303 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00304 if (!$this->MM_is_foreign) {
00305 $theTable = $row['tablenames'] ? $row['tablenames'] : $this->firstTable;
00306 }
00307 if (($row[$uidForeign_field] || $theTable=='pages') && $theTable && isset($this->tableArray[$theTable])) {
00308
00309 $this->itemArray[$key]['id'] = $row[$uidForeign_field];
00310 $this->itemArray[$key]['table'] = $theTable;
00311 $this->tableArray[$theTable][]= $row[$uidForeign_field];
00312 } elseif ($this->registerNonTableValues) {
00313 $this->itemArray[$key]['id'] = $row[$uidForeign_field];
00314 $this->itemArray[$key]['table'] = '_NO_TABLE';
00315 $this->nonTableArray[] = $row[$uidForeign_field];
00316 }
00317 $key++;
00318 }
00319 $GLOBALS['TYPO3_DB']->sql_free_result($res);
00320 }
00321
00330 function writeMM($tableName,$uid,$prependTableName=0) {
00331
00332 if ($this->MM_is_foreign) {
00333 $uidLocal_field = 'uid_foreign';
00334 $uidForeign_field = 'uid_local';
00335 $sorting_field = 'sorting_foreign';
00336 } else {
00337 $uidLocal_field = 'uid_local';
00338 $uidForeign_field = 'uid_foreign';
00339 $sorting_field = 'sorting';
00340 }
00341
00342
00343 $tableC = count($this->tableArray);
00344 if ($tableC) {
00345 $prep = ($tableC>1||$prependTableName||$this->MM_isMultiTableRelationship) ? 1 : 0;
00346 $c=0;
00347
00348 $additionalWhere_tablenames = '';
00349 if ($this->MM_is_foreign && $prep) {
00350 $additionalWhere_tablenames = ' AND tablenames="'.$this->currentTable.'"';
00351 }
00352
00353 $additionalWhere = '';
00354
00355 if ($this->MM_table_where) {
00356 $additionalWhere.= "\n".str_replace('###THIS_UID###', intval($uid), $this->MM_table_where);
00357 }
00358
00359 foreach ($this->MM_match_fields as $field => $value) {
00360 $additionalWhere.= ' AND '.$field.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($value, $tableName);
00361 }
00362
00363 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($uidForeign_field.($prep?', tablenames':''), $tableName, $uidLocal_field.'='.$uid.$additionalWhere_tablenames.$additionalWhere);
00364
00365 $oldMMs = array();
00366 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00367 if (!$this->MM_is_foreign && $prep) {
00368 $oldMMs[] = array($row['tablenames'], $row[$uidForeign_field]);
00369 } else {
00370 $oldMMs[] = $row[$uidForeign_field];
00371 }
00372 }
00373
00374
00375 foreach($this->itemArray as $val) {
00376 $c++;
00377
00378 if ($prep || $val['table']=='_NO_TABLE') {
00379 if ($this->MM_is_foreign) {
00380 $tablename = $this->currentTable;
00381 } else {
00382 $tablename = $val['table'];
00383 }
00384 } else {
00385 $tablename = '';
00386 }
00387
00388 if(!$this->MM_is_foreign && $prep) {
00389 $item = array($val['table'], $val['id']);
00390 } else {
00391 $item = $val['id'];
00392 }
00393
00394 if (in_array($item, $oldMMs)) {
00395 unset($oldMMs[array_search($item, $oldMMs)]);
00396
00397 $whereClause = $uidLocal_field.'='.$uid.' AND '.$uidForeign_field.'='.$val['id'];
00398 if ($tablename) {
00399 $whereClause .= ' AND tablenames="'.$tablename.'"';
00400 }
00401 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($tableName, $whereClause.$additionalWhere, array($sorting_field => $c));
00402 } else {
00403
00404 $insertFields = $this->MM_insert_fields;
00405 $insertFields[$uidLocal_field] = $uid;
00406 $insertFields[$uidForeign_field] = $val['id'];
00407 $insertFields[$sorting_field] = $c;
00408 if($tablename) {
00409 $insertFields['tablenames'] = $tablename;
00410 }
00411
00412 $GLOBALS['TYPO3_DB']->exec_INSERTquery($tableName, $insertFields);
00413 }
00414 }
00415
00416
00417 if(is_array($oldMMs) && count($oldMMs) > 0) {
00418 $removeClauses = array();
00419 foreach($oldMMs as $mmItem) {
00420 if(is_array($mmItem)) {
00421 $removeClauses[] = 'tablenames="'.$mmItem[0].'" AND '.$uidForeign_field.'='.$mmItem[1];
00422 } else {
00423 $removeClauses[] = $uidForeign_field.'='.$mmItem;
00424 }
00425 }
00426 $deleteAddWhere = ' AND ('.implode(' OR ', $removeClauses).')';
00427 $GLOBALS['TYPO3_DB']->exec_DELETEquery($tableName, $uidLocal_field.'='.intval($uid).$deleteAddWhere.$additionalWhere_tablenames.$additionalWhere);
00428 }
00429 }
00430 }
00431
00440 function readForeignField($uid, $conf) {
00441 $key = 0;
00442 $uid = intval($uid);
00443 $whereClause = '';
00444 $foreign_table = $conf['foreign_table'];
00445 $foreign_table_field = $conf['foreign_table_field'];
00446 $useDeleteClause = $this->undeleteRecord ? false : true;
00447
00448
00449 if ($conf['symmetric_field']) {
00450 $whereClause = '('.$conf['foreign_field'].'='.$uid.' OR '.$conf['symmetric_field'].'='.$uid.')';
00451 } else {
00452 $whereClause = $conf['foreign_field'].'='.$uid;
00453 }
00454
00455 if ($useDeleteClause) {
00456 $whereClause .= t3lib_BEfunc::deleteClause($foreign_table);
00457 }
00458
00459
00460 if ($foreign_table_field && $this->currentTable) {
00461 $whereClause .= ' AND '.$foreign_table_field.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->currentTable, $foreign_table);
00462 }
00463
00464
00465 if ($conf['foreign_sortby']) {
00466 if ($conf['symmetric_sortby'] && $conf['symmetric_field']) {
00467
00468 $sortby = '
00469 CASE
00470 WHEN '.$conf['foreign_field'].'='.$uid.'
00471 THEN '.$conf['foreign_sortby'].'
00472 ELSE '.$conf['symmetric_sortby'].'
00473 END';
00474 } else {
00475
00476 $sortby = $conf['foreign_sortby'];
00477 }
00478 } elseif ($conf['foreign_default_sortby']) {
00479 $sortby = $conf['foreign_default_sortby'];
00480 } elseif ($GLOBALS['TCA'][$foreign_table]['ctrl']['sortby']) {
00481 $sortby = $GLOBALS['TCA'][$foreign_table]['ctrl']['sortby'];
00482 } elseif ($GLOBALS['TCA'][$foreign_table]['ctrl']['default_sortby']) {
00483 $sortby = $GLOBALS['TCA'][$foreign_table]['ctrl']['default_sortby'];
00484 }
00485
00486
00487 $sortby = $GLOBALS['TYPO3_DB']->stripOrderBy($sortby);
00488
00489 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $foreign_table, $whereClause, '', $sortby);
00490
00491 if (count($rows)) {
00492 foreach ($rows as $row) {
00493 $this->itemArray[$key]['id'] = $row['uid'];
00494 $this->itemArray[$key]['table'] = $foreign_table;
00495 $this->tableArray[$foreign_table][]= $row['uid'];
00496 $key++;
00497 }
00498 }
00499 }
00500
00510 function writeForeignField($conf, $parentUid, $updateToUid=0, $skipSorting=false) {
00511 $c = 0;
00512 $foreign_table = $conf['foreign_table'];
00513 $foreign_field = $conf['foreign_field'];
00514 $symmetric_field = $conf['symmetric_field'];
00515 $foreign_table_field = $conf['foreign_table_field'];
00516
00517
00518 if (t3lib_div::testInt($parentUid) && count($this->tableArray)) {
00519
00520 if (!(t3lib_div::testInt($updateToUid) && $updateToUid > 0)) {
00521 $updateToUid = 0;
00522 }
00523 $fields = 'uid,'.$foreign_field.($symmetric_field ? ','.$symmetric_field : '');
00524
00525
00526 foreach ($this->itemArray as $val) {
00527 $uid = $val['id'];
00528 $table = $val['table'];
00529
00530
00531 if ($conf['symmetric_field']) {
00532 $row = t3lib_BEfunc::getRecord($table,$uid,$fields,'',false);
00533 $isOnSymmetricSide = t3lib_loadDBGroup::isOnSymmetricSide($parentUid, $conf, $row);
00534 }
00535
00536 $updateValues = array();
00537
00538
00539
00540 if (!$updateToUid) {
00541
00542 if ($isOnSymmetricSide) {
00543 $updateValues[$symmetric_field] = $parentUid;
00544 } else {
00545 $updateValues[$foreign_field] = $parentUid;
00546 }
00547
00548
00549 if ($foreign_table_field && $this->currentTable) {
00550 $updateValues[$foreign_table_field] = $this->currentTable;
00551 }
00552
00553
00554 if (!$skipSorting) {
00555
00556 if ($conf['foreign_sortby']) {
00557 $sortby = $conf['foreign_sortby'];
00558 } elseif ($GLOBALS['TCA'][$foreign_table]['ctrl']['sortby']) {
00559 $sortby = $GLOBALS['TCA'][$foreign_table]['ctrl']['sortby'];
00560 }
00561
00562 $sortby = $GLOBALS['TYPO3_DB']->stripOrderBy($sortby);
00563 $symSortby = $conf['symmetric_sortby'];
00564
00565
00566 if ($isOnSymmetricSide && $symSortby) {
00567 $updateValues[$symSortby] = ++$c;
00568 } elseif ($sortby) {
00569 $updateValues[$sortby] = ++$c;
00570 }
00571 }
00572
00573
00574
00575 } else {
00576 if ($isOnSymmetricSide) {
00577 $updateValues[$symmetric_field] = $updateToUid;
00578 } else {
00579 $updateValues[$foreign_field] = $updateToUid;
00580 }
00581 }
00582
00583 if (count($updateValues)) {
00584 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . intval($uid), $updateValues);
00585 $this->updateRefIndex($table, $uid);
00586 }
00587 }
00588 }
00589 }
00590
00597 function getValueArray($prependTableName='') {
00598
00599 $valueArray=Array();
00600 $tableC = count($this->tableArray);
00601
00602
00603 if ($tableC) {
00604
00605 $prep = ($tableC>1||$prependTableName) ? 1 : 0;
00606
00607
00608 foreach($this->itemArray as $val) {
00609 $valueArray[]=(($prep && $val['table']!='_NO_TABLE') ? $val['table'].'_' : '').
00610 $val['id'];
00611 }
00612 }
00613
00614 return $valueArray;
00615 }
00616
00625 function convertPosNeg($valueArray,$fTable,$nfTable) {
00626 if (is_array($valueArray) && $fTable) {
00627 foreach($valueArray as $key => $val) {
00628 $val = strrev($val);
00629 $parts = explode('_',$val,2);
00630 $theID = strrev($parts[0]);
00631 $theTable = strrev($parts[1]);
00632
00633 if ( t3lib_div::testInt($theID) && (!$theTable || !strcmp($theTable,$fTable) || !strcmp($theTable,$nfTable)) ) {
00634 $valueArray[$key]= $theTable && strcmp($theTable,$fTable) ? $theID*-1 : $theID;
00635 }
00636 }
00637 }
00638 return $valueArray;
00639 }
00640
00647 function getFromDB() {
00648
00649 foreach($this->tableArray as $key => $val) {
00650 if (is_array($val)) {
00651 $itemList = implode(',',$val);
00652 if ($itemList) {
00653 $from = '*';
00654 if ($this->fromTC) {
00655 $from = 'uid,pid';
00656 if ($GLOBALS['TCA'][$key]['ctrl']['label']) {
00657 $from.= ','.$GLOBALS['TCA'][$key]['ctrl']['label'];
00658 }
00659 if ($GLOBALS['TCA'][$key]['ctrl']['label_alt']) {
00660 $from.= ','.$GLOBALS['TCA'][$key]['ctrl']['label_alt'];
00661 }
00662 if ($GLOBALS['TCA'][$key]['ctrl']['thumbnail']) {
00663 $from.= ','.$GLOBALS['TCA'][$key]['ctrl']['thumbnail'];
00664 }
00665 }
00666 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($from, $key, 'uid IN ('.$itemList.')'.$this->additionalWhere[$key]);
00667 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
00668 $this->results[$key][$row['uid']]=$row;
00669 }
00670 }
00671 }
00672 }
00673 return $this->results;
00674 }
00675
00682 function readyForInterface() {
00683 global $TCA;
00684
00685 if (!is_array($this->itemArray)) {return false;}
00686
00687 $output=array();
00688 $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
00689 $titleLen=intval($GLOBALS['BE_USER']->uc['titleLen']);
00690
00691 foreach($this->itemArray as $key => $val) {
00692 $theRow = $this->results[$val['table']][$val['id']];
00693 if ($theRow && is_array($TCA[$val['table']])) {
00694 $label = t3lib_div::fixed_lgd_cs(strip_tags(t3lib_BEfunc::getRecordTitle($val['table'], $theRow)),$titleLen);
00695 $label = ($label)?$label:'[...]';
00696 $output[]=str_replace(',','',$val['table'].'_'.$val['id'].'|'.rawurlencode($label));
00697 }
00698 }
00699 return implode(',',$output);
00700 }
00701
00708 function countItems($returnAsArray = true) {
00709 $count = count($this->itemArray);
00710 if ($returnAsArray) $count = array($count);
00711 return $count;
00712 }
00713
00723 function updateRefIndex($table,$id) {
00724 $refIndexObj = t3lib_div::makeInstance('t3lib_refindex');
00725 $result = $refIndexObj->updateRefIndexTable($table,$id);
00726 }
00727
00736 function isOnSymmetricSide($parentUid, $parentConf, $childRec) {
00737 return t3lib_div::testInt($childRec['uid']) && $parentConf['symmetric_field'] && $parentUid == $childRec[$parentConf['symmetric_field']]
00738 ? true
00739 : false;
00740 }
00741 }
00742
00743
00744 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loaddbgroup.php']) {
00745 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_loaddbgroup.php']);
00746 }
00747 ?>