Commit 70e2cb6d057bd778fd57067d08268dc019f89c57

Authored by nbm
1 parent 7bb96c09

Correct units data after an upgrade - folder_id is stored in units

(not unit_id in folders), and unit_id is stored directly in groups, not
via groups_units_link.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@4722 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/upgrades/UpgradeFunctions.inc.php
... ... @@ -402,9 +402,32 @@ class UpgradeFunctions {
402 402  
403 403 // {{{ fixUnits
404 404 function fixUnits() {
  405 + // First, assign the unit to a group directly on the group
  406 + // table, not via the group_units table, since groups could only
  407 + // belong to a single unit anyway.
405 408 $sGULTable = KTUtil::getTableName("groups_units");
  409 + $sGroupsTable = KTUtil::getTableName('groups');
406 410 $aGroupUnits = DBUtil::getResultArray("SELECT group_id, unit_id FROM $sGULTable");
407   - exit(0);
  411 + foreach ($aGroupUnits as $aRow) {
  412 + // $curunit = DBUtil::getOneResultKey(array("SELECT unit_id FROM $sGroupsTable WHERE id = ?", array($aRow['group_id'])), "unit_id");
  413 + DBUtil::autoUpdate($sGroupsTable, array('unit_id' => $aRow['unit_id']), $aRow['group_id']);
  414 + }
  415 +
  416 + // Now, assign the unit folder id to the unit directly, instead
  417 + // of storing the unit_id on every folder beneath the unit
  418 + // folder.
  419 + $sFoldersTable = KTUtil::getTableName('folders');
  420 + $sUnitsTable = KTUtil::getTableName('units');
  421 + $sQuery = "SELECT id FROM folders WHERE unit_id = ? ORDER BY LENGTH(parent_folder_ids) LIMIT 1";
  422 + $aUnitIds = DBUtil::getResultArrayKey("SELECT id FROM $sUnitsTable", 'id');
  423 + foreach ($aUnitIds as $iUnitId) {
  424 + $aParams = array($iUnitId);
  425 + $iFolderId = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
  426 + if (!empty($iFolderId)) {
  427 + DBUtil::autoUpdate($sUnitsTable, array('folder_id' => $iFolderId), $iUnitId);
  428 + }
  429 + }
  430 + return true;
408 431 }
409 432 // }}}
410 433 }
... ...